Merge pull request #4630 from atif-shaikh/CRM-15546
[civicrm-core.git] / CRM / Activity / Form / Activity.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components for Activity
38 *
39 */
40 class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
41
42 /**
43 * The id of the object being edited / created
44 *
45 * @var int
46 */
47 public $_activityId;
48
49 /**
50 * Store activity ids when multiple activities are created
51 *
52 * @var int
53 */
54 public $_activityIds = array();
55
56 /**
57 * The id of activity type
58 *
59 * @var int
60 */
61 public $_activityTypeId;
62
63 /**
64 * The name of activity type
65 *
66 * @var string
67 */
68 public $_activityTypeName;
69
70 /**
71 * The id of currently viewed contact
72 *
73 * @var int
74 */
75 public $_currentlyViewedContactId;
76
77 /**
78 * The id of source contact and target contact
79 *
80 * @var int
81 */
82 protected $_sourceContactId;
83 protected $_targetContactId;
84 protected $_asigneeContactId;
85
86 protected $_single;
87
88 public $_context;
89 public $_compContext;
90 public $_action;
91 public $_activityTypeFile;
92
93 /**
94 * The id of the logged in user, used when add / edit
95 *
96 * @var int
97 */
98 public $_currentUserId;
99
100 /**
101 * The array of form field attributes
102 *
103 * @var array
104 */
105 public $_fields;
106
107 /**
108 * The the directory inside CRM, to include activity type file from
109 *
110 * @var string
111 */
112 protected $_crmDir = 'Activity';
113
114 /**
115 * Survey activity
116 *
117 * @var boolean
118 */
119 protected $_isSurveyActivity;
120
121 protected $_values = array();
122
123 protected $unsavedWarn = TRUE;
124
125 /**
126 * The _fields var can be used by sub class to set/unset/edit the
127 * form fields based on their requirement
128 *
129 */
130 function setFields() {
131 $this->_fields = array(
132 'subject' => array(
133 'type' => 'text',
134 'label' => ts('Subject'),
135 'attributes' => CRM_Core_DAO::getAttribute('CRM_Activity_DAO_Activity',
136 'subject'
137 ),
138 ),
139 'duration' => array(
140 'type' => 'text',
141 'label' => ts('Duration'),
142 'attributes' => array('size' => 4, 'maxlength' => 8),
143 'required' => FALSE,
144 ),
145 'location' => array(
146 'type' => 'text',
147 'label' => ts('Location'),
148 'attributes' =>
149 CRM_Core_DAO::getAttribute('CRM_Activity_DAO_Activity',
150 'location'
151 ),
152 'required' => FALSE
153 ),
154 'details' => array(
155 'type' => 'wysiwyg',
156 'label' => ts('Details'),
157 // forces a smaller edit window
158 'attributes' => array('rows' => 4, 'cols' => 60),
159 'required' => FALSE
160 ),
161 'status_id' => array(
162 'type' => 'select',
163 'required' => TRUE,
164 ),
165 'priority_id' => array(
166 'type' => 'select',
167 'required' => TRUE,
168 ),
169 'source_contact_id' => array(
170 'type' => 'entityRef',
171 'label' => ts('Added By'),
172 'required' => FALSE
173 ),
174 'target_contact_id' => array(
175 'type' => 'entityRef',
176 'label' => ts('With Contact'),
177 'attributes' => array('multiple' => TRUE, 'create' => TRUE)
178 ),
179 'assignee_contact_id' => array(
180 'type' => 'entityRef',
181 'label' => ts('Assigned to'),
182 'attributes' => array('multiple' => TRUE, 'create' => TRUE, 'api' => array('params' => array('is_deceased' => 0))),
183 ),
184 'followup_assignee_contact_id' => array(
185 'type' => 'entityRef',
186 'label' => ts('Assigned to'),
187 'attributes' => array('multiple' => TRUE, 'create' => TRUE, 'api' => array('params' => array('is_deceased' => 0))),
188 ),
189 'followup_activity_type_id' => array(
190 'type' => 'select',
191 'label' => ts('Followup Activity'),
192 'attributes' => array('' => '- ' . ts('select activity') . ' -') + CRM_Core_PseudoConstant::ActivityType(FALSE),
193 'extra' => array('class' => 'crm-select2'),
194 ),
195 // Add optional 'Subject' field for the Follow-up Activiity, CRM-4491
196 'followup_activity_subject' => array(
197 'type' => 'text',
198 'label' => ts('Subject'),
199 'attributes' => CRM_Core_DAO::getAttribute('CRM_Activity_DAO_Activity',
200 'subject'
201 )
202 )
203 );
204
205 if (($this->_context == 'standalone') &&
206 ($printPDF = CRM_Utils_Array::key('Print PDF Letter', $this->_fields['followup_activity_type_id']['attributes']))
207 ) {
208 unset($this->_fields['followup_activity_type_id']['attributes'][$printPDF]);
209 }
210 }
211
212 /**
213 * Build the form object
214 *
215 * @return void
216 * @access public
217 */
218 function preProcess() {
219 $this->_cdType = CRM_Utils_Array::value('type', $_GET);
220 $this->assign('cdType', FALSE);
221 if ($this->_cdType) {
222 $this->assign('cdType', TRUE);
223 return CRM_Custom_Form_CustomData::preProcess($this);
224 }
225 CRM_Core_Form_RecurringEntity::preProcess('civicrm_activity');
226 $this->_atypefile = CRM_Utils_Array::value('atypefile', $_GET);
227 $this->assign('atypefile', FALSE);
228 if ($this->_atypefile) {
229 $this->assign('atypefile', TRUE);
230 }
231
232 $session = CRM_Core_Session::singleton();
233 $this->_currentUserId = $session->get('userID');
234
235 $this->_currentlyViewedContactId = $this->get('contactId');
236 if (!$this->_currentlyViewedContactId) {
237 $this->_currentlyViewedContactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
238 }
239 $this->assign('contactId', $this->_currentlyViewedContactId);
240
241 //give the context.
242 if (!isset($this->_context)) {
243 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this);
244 if (CRM_Contact_Form_Search::isSearchContext($this->_context)) {
245 $this->_context = 'search';
246 }
247 elseif (!in_array($this->_context, array('dashlet', 'dashletFullscreen'))
248 && $this->_currentlyViewedContactId
249 ) {
250 $this->_context = 'activity';
251 }
252 $this->_compContext = CRM_Utils_Request::retrieve('compContext', 'String', $this);
253 }
254
255 $this->assign('context', $this->_context);
256
257 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this);
258
259 if ($this->_action & CRM_Core_Action::DELETE) {
260 if (!CRM_Core_Permission::check('delete activities')) {
261 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
262 }
263 }
264
265 //CRM-6957
266 //when we come from contact search, activity id never comes.
267 //so don't try to get from object, it might gives you wrong one.
268
269 // if we're not adding new one, there must be an id to
270 // an activity we're trying to work on.
271 if ($this->_action != CRM_Core_Action::ADD &&
272 get_class($this->controller) != 'CRM_Contact_Controller_Search'
273 ) {
274 $this->_activityId = CRM_Utils_Request::retrieve('id', 'Positive', $this);
275 }
276
277 $this->_activityTypeId = CRM_Utils_Request::retrieve('atype', 'Positive', $this);
278 $this->assign('atype', $this->_activityTypeId);
279
280 $this->assign('activityId', $this->_activityId);
281
282 //check for required permissions, CRM-6264
283 if ($this->_activityId &&
284 in_array($this->_action, array(
285 CRM_Core_Action::UPDATE,
286 CRM_Core_Action::VIEW
287 )) &&
288 !CRM_Activity_BAO_Activity::checkPermission($this->_activityId, $this->_action)
289 ) {
290 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
291 }
292 if (($this->_action & CRM_Core_Action::VIEW) &&
293 CRM_Activity_BAO_Activity::checkPermission($this->_activityId, CRM_Core_Action::UPDATE)
294 ) {
295 $this->assign('permission', 'edit');
296 }
297
298 if (!$this->_activityTypeId && $this->_activityId) {
299 $this->_activityTypeId = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity',
300 $this->_activityId,
301 'activity_type_id'
302 );
303 }
304
305 //Assigning Activity type name
306 if ($this->_activityTypeId) {
307 $activityTName = CRM_Core_OptionGroup::values('activity_type', FALSE, FALSE, FALSE, 'AND v.value = ' . $this->_activityTypeId, 'name');
308 if ($activityTName[$this->_activityTypeId]) {
309 $this->_activityTypeName = $activityTName[$this->_activityTypeId];
310 $this->assign('activityTName', $activityTName[$this->_activityTypeId]);
311 }
312 }
313
314 // Set title
315 if (isset($activityTName)) {
316 $activityName = CRM_Utils_Array::value($this->_activityTypeId, $activityTName);
317 $this->assign('pageTitle', ts('%1 Activity', array( 1 => $activityName)));
318
319 if ($this->_currentlyViewedContactId) {
320 $displayName = CRM_Contact_BAO_Contact::displayName($this->_currentlyViewedContactId);
321 // Check if this is default domain contact CRM-10482
322 if (CRM_Contact_BAO_Contact::checkDomainContact($this->_currentlyViewedContactId)) {
323 $displayName .= ' (' . ts('default organization') . ')';
324 }
325 CRM_Utils_System::setTitle($displayName . ' - ' . $activityName);
326 }
327 else {
328 CRM_Utils_System::setTitle(ts('%1 Activity', array( 1 => $activityName)));
329 }
330 }
331
332 //check the mode when this form is called either single or as
333 //search task action
334 if ($this->_activityTypeId ||
335 $this->_context == 'standalone' ||
336 $this->_currentlyViewedContactId
337 ) {
338 $this->_single = TRUE;
339 $this->assign('urlPath', 'civicrm/activity');
340 }
341 else {
342 //set the appropriate action
343 $url = CRM_Utils_System::currentPath();
344 $urlArray = explode('/', $url);
345 $searchPath = array_pop($urlArray);
346 $searchType = 'basic';
347 $this->_action = CRM_Core_Action::BASIC;
348 switch ($searchPath) {
349 case 'basic':
350 $searchType = $searchPath;
351 $this->_action = CRM_Core_Action::BASIC;
352 break;
353
354 case 'advanced':
355 $searchType = $searchPath;
356 $this->_action = CRM_Core_Action::ADVANCED;
357 break;
358
359 case 'builder':
360 $searchType = $searchPath;
361 $this->_action = CRM_Core_Action::PROFILE;
362 break;
363
364 case 'custom':
365 $this->_action = CRM_Core_Action::COPY;
366 $searchType = $searchPath;
367 break;
368 }
369
370 parent::preProcess();
371 $this->_single = FALSE;
372
373 $this->assign('urlPath', "civicrm/contact/search/$searchType");
374 $this->assign('urlPathVar', "_qf_Activity_display=true&qfKey={$this->controller->_key}");
375 }
376
377 $this->assign('single', $this->_single);
378 $this->assign('action', $this->_action);
379
380 if ($this->_action & CRM_Core_Action::VIEW) {
381 // get the tree of custom fields
382 $this->_groupTree = &CRM_Core_BAO_CustomGroup::getTree('Activity', $this,
383 $this->_activityId, 0, $this->_activityTypeId
384 );
385 }
386
387 if ($this->_activityTypeId) {
388 //set activity type name and description to template
389 list($this->_activityTypeName, $activityTypeDescription) = CRM_Core_BAO_OptionValue::getActivityTypeDetails($this->_activityTypeId);
390 $this->assign('activityTypeName', $this->_activityTypeName);
391 $this->assign('activityTypeDescription', $activityTypeDescription);
392 }
393
394 // set user context
395 $urlParams = $urlString = NULL;
396 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this);
397 if (!$qfKey) {
398 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
399 }
400
401 //validate the qfKey
402 if (!CRM_Utils_Rule::qfKey($qfKey)) {
403 $qfKey = NULL;
404 }
405
406 if ($this->_context == 'fulltext') {
407 $keyName = '&qfKey';
408 $urlParams = 'force=1';
409 $urlString = 'civicrm/contact/search/custom';
410 if ($this->_action == CRM_Core_Action::UPDATE) {
411 $keyName = '&key';
412 $urlParams .= '&context=fulltext&action=view';
413 $urlString = 'civicrm/contact/view/activity';
414 }
415 if ($qfKey) {
416 $urlParams .= "$keyName=$qfKey";
417 }
418 $this->assign('searchKey', $qfKey);
419 }
420 elseif (in_array($this->_context, array(
421 'standalone',
422 'home',
423 'dashlet',
424 'dashletFullscreen'
425 ))
426 ) {
427 $urlParams = 'reset=1';
428 $urlString = 'civicrm/dashboard';
429 }
430 elseif ($this->_context == 'search') {
431 $urlParams = 'force=1';
432 if ($qfKey) {
433 $urlParams .= "&qfKey=$qfKey";
434 }
435 $path = CRM_Utils_System::currentPath();
436 if ($this->_compContext == 'advanced' ) {
437 $urlString = 'civicrm/contact/search/advanced';
438 }
439 elseif ($path == 'civicrm/group/search'
440 || $path == 'civicrm/contact/search'
441 || $path == 'civicrm/contact/search/advanced'
442 || $path == 'civicrm/contact/search/custom'
443 || $path == 'civicrm/group/search') {
444 $urlString = $path;
445 }
446 else {
447 $urlString = 'civicrm/activity/search';
448 }
449 $this->assign('searchKey', $qfKey);
450 }
451 elseif ($this->_context != 'caseActivity') {
452 $urlParams = "action=browse&reset=1&cid={$this->_currentlyViewedContactId}&selectedChild=activity";
453 $urlString = 'civicrm/contact/view';
454 }
455
456 if ($urlString) {
457 $session->pushUserContext(CRM_Utils_System::url($urlString, $urlParams));
458 }
459
460 // hack to retrieve activity type id from post variables
461 if (!$this->_activityTypeId) {
462 $this->_activityTypeId = CRM_Utils_Array::value('activity_type_id', $_POST);
463 }
464
465 // when custom data is included in this page
466 if (!empty($_POST['hidden_custom'])) {
467 // we need to set it in the session for the below code to work
468 // CRM-3014
469 //need to assign custom data subtype to the template
470 $this->set('type', 'Activity');
471 $this->set('subType', $this->_activityTypeId);
472 $this->set('entityId', $this->_activityId);
473 CRM_Custom_Form_CustomData::preProcess($this);
474 CRM_Custom_Form_CustomData::buildQuickForm($this);
475 CRM_Custom_Form_CustomData::setDefaultValues($this);
476 }
477
478 // add attachments part
479 CRM_Core_BAO_File::buildAttachment($this, 'civicrm_activity', $this->_activityId, NULL, TRUE);
480
481 // figure out the file name for activity type, if any
482 if ($this->_activityTypeId &&
483 $this->_activityTypeFile =
484 CRM_Activity_BAO_Activity::getFileForActivityTypeId($this->_activityTypeId, $this->_crmDir)
485 ) {
486 $this->assign('activityTypeFile', $this->_activityTypeFile);
487 $this->assign('crmDir', $this->_crmDir);
488 }
489
490 $this->setFields();
491
492 if ($this->_activityTypeFile) {
493 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
494 $className::preProcess($this);
495 }
496
497 $this->_values = $this->get('values');
498 if (!is_array($this->_values)) {
499 $this->_values = array();
500 if (isset($this->_activityId) && $this->_activityId) {
501 $params = array('id' => $this->_activityId);
502 CRM_Activity_BAO_Activity::retrieve($params, $this->_values);
503 }
504 $this->set('values', $this->_values);
505 }
506
507 if ($this->_action & CRM_Core_Action::UPDATE) {
508 CRM_Core_Form_RecurringEntity::preProcess('civicrm_activity');
509 }
510 }
511
512 /**
513 * Set default values for the form. For edit/view mode
514 * the default values are retrieved from the database
515 *
516 * @access public
517 *
518 * @return void
519 */
520 function setDefaultValues() {
521 if ($this->_cdType) {
522 return CRM_Custom_Form_CustomData::setDefaultValues($this);
523 }
524
525 $defaults = $this->_values;
526 // if we're editing...
527 if (isset($this->_activityId)) {
528 if (empty($defaults['activity_date_time'])) {
529 list($defaults['activity_date_time'], $defaults['activity_date_time_time']) = CRM_Utils_Date::setDateDefaults(NULL, 'activityDateTime');
530 }
531 elseif ($this->_action & CRM_Core_Action::UPDATE) {
532 $this->assign('current_activity_date_time', $defaults['activity_date_time']);
533 list($defaults['activity_date_time'],
534 $defaults['activity_date_time_time']
535 ) = CRM_Utils_Date::setDateDefaults($defaults['activity_date_time'], 'activityDateTime');
536 list($defaults['repetition_start_date'], $defaults['repetition_start_date_time']) = CRM_Utils_Date::setDateDefaults($defaults['activity_date_time'], 'activityDateTime');
537 $recurringEntityDefaults = array();
538 $recurringEntityDefaults = CRM_Core_Form_RecurringEntity::setDefaultValues();
539 $defaults = array_merge($defaults, $recurringEntityDefaults);
540 }
541
542 if ($this->_context != 'standalone') {
543 $this->assign('target_contact_value',
544 CRM_Utils_Array::value('target_contact_value', $defaults)
545 );
546 $this->assign('assignee_contact_value',
547 CRM_Utils_Array::value('assignee_contact_value', $defaults)
548 );
549 }
550
551 // Fixme: why are we getting the wrong keys from upstream?
552 $defaults['target_contact_id'] = CRM_Utils_Array::value('target_contact', $defaults);
553 $defaults['assignee_contact_id'] = CRM_Utils_Array::value('assignee_contact', $defaults);
554
555 // set default tags if exists
556 $defaults['tag'] = CRM_Core_BAO_EntityTag::getTag($this->_activityId, 'civicrm_activity');
557 }
558 else {
559 // if it's a new activity, we need to set default values for associated contact fields
560 $this->_sourceContactId = $this->_currentUserId;
561 $this->_targetContactId = $this->_currentlyViewedContactId;
562
563 $defaults['source_contact_id'] = $this->_sourceContactId;
564 $defaults['target_contact_id'] = $this->_targetContactId;
565
566 list($defaults['activity_date_time'], $defaults['activity_date_time_time']) =
567 CRM_Utils_Date::setDateDefaults(NULL, 'activityDateTime');
568 }
569
570 if ($this->_activityTypeId) {
571 $defaults['activity_type_id'] = $this->_activityTypeId;
572 }
573
574 if (!$this->_single && !empty($this->_contactIds)) {
575 $defaults['target_contact_id'] = $this->_contactIds;
576 }
577
578 // CRM-15472 - 50 is around the practial limit of how many items a select2 entityRef can handle
579 if (!empty($defaults['target_contact_id'])) {
580 $count = count(is_array($defaults['target_contact_id']) ? $defaults['target_contact_id'] : explode(',', $defaults['target_contact_id']));
581 if ($count > 50) {
582 $this->freeze(array('target_contact_id'));
583 }
584 }
585
586 if ($this->_action & (CRM_Core_Action::DELETE | CRM_Core_Action::RENEW)) {
587 $this->assign('delName', CRM_Utils_Array::value('subject', $defaults));
588 }
589
590 if ($this->_activityTypeFile) {
591 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
592 $defaults += $className::setDefaultValues($this);
593 }
594 if (empty($defaults['priority_id'])) {
595 $priority = CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'priority_id');
596 $defaults['priority_id'] = array_search('Normal', $priority);
597 }
598 if (empty($defaults['status_id'])) {
599 $defaults['status_id'] = CRM_Core_OptionGroup::getDefaultValue('activity_status');
600 }
601 return $defaults;
602 }
603
604 public function buildQuickForm() {
605 if ($this->_action & (CRM_Core_Action::DELETE | CRM_Core_Action::RENEW)) {
606 //enable form element (ActivityLinks sets this true)
607 $this->assign('suppressForm', FALSE);
608
609 $button = ts('Delete');
610 if ($this->_action & CRM_Core_Action::RENEW) {
611 $button = ts('Restore');
612 }
613 $this->addButtons(array(
614 array(
615 'type' => 'next',
616 'name' => $button,
617 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
618 'isDefault' => TRUE
619 ),
620 array(
621 'type' => 'cancel',
622 'name' => ts('Cancel')
623 )
624 ));
625 return;
626 }
627
628 if ($this->_cdType) {
629 return CRM_Custom_Form_CustomData::buildQuickForm($this);
630 }
631
632 //build other activity links
633 CRM_Activity_Form_ActivityLinks::commonBuildQuickForm($this);
634
635 //enable form element (ActivityLinks sets this true)
636 $this->assign('suppressForm', FALSE);
637
638 $element = & $this->add('select', 'activity_type_id', ts('Activity Type'),
639 $this->_fields['followup_activity_type_id']['attributes'],
640 FALSE, array(
641 'onchange' => "CRM.buildCustomData( 'Activity', this.value );",
642 'class' => 'crm-select2',
643 )
644 );
645
646 //freeze for update mode.
647 if ($this->_action & CRM_Core_Action::UPDATE) {
648 $element->freeze();
649 }
650
651 //Call to RecurringEntity buildQuickForm for add/update mode
652 if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) {
653 CRM_Core_Form_RecurringEntity::buildQuickForm($this);
654 }
655
656 foreach ($this->_fields as $field => $values) {
657 if (!empty($this->_fields[$field])) {
658 $attribute = CRM_Utils_Array::value('attributes', $values);
659 $required = !empty($values['required']);
660
661 if ($values['type'] == 'wysiwyg') {
662 $this->addWysiwyg($field, $values['label'], $attribute, $required);
663 }
664 elseif ($values['type'] == 'select' && empty($attribute)) {
665 $this->addSelect($field, array('entity' => 'activity'), $required);
666 }
667 elseif ($values['type'] == 'entityRef') {
668 $this->addEntityRef($field, $values['label'], $attribute, $required);
669 }
670 else {
671 $this->add($values['type'], $field, $values['label'], $attribute, $required, CRM_Utils_Array::value('extra', $values));
672 }
673 }
674 }
675
676 //CRM-7362 --add campaigns.
677 CRM_Campaign_BAO_Campaign::addCampaign($this, CRM_Utils_Array::value('campaign_id', $this->_values));
678
679 //add engagement level CRM-7775
680 $buildEngagementLevel = FALSE;
681 if (CRM_Campaign_BAO_Campaign::isCampaignEnable() &&
682 CRM_Campaign_BAO_Campaign::accessCampaign()
683 ) {
684 $buildEngagementLevel = TRUE;
685 $this->addSelect('engagement_level',array('entity' => 'activity'));
686 $this->addRule('engagement_level',
687 ts('Please enter the engagement index as a number (integers only).'),
688 'positiveInteger'
689 );
690 }
691 $this->assign('buildEngagementLevel', $buildEngagementLevel);
692
693 // check for survey activity
694 $this->_isSurveyActivity = FALSE;
695
696 if ($this->_activityId && CRM_Campaign_BAO_Campaign::isCampaignEnable() &&
697 CRM_Campaign_BAO_Campaign::accessCampaign()
698 ) {
699
700 $this->_isSurveyActivity = CRM_Campaign_BAO_Survey::isSurveyActivity($this->_activityId);
701 if ($this->_isSurveyActivity) {
702 $surveyId = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity',
703 $this->_activityId,
704 'source_record_id'
705 );
706 $responseOptions = CRM_Campaign_BAO_Survey::getResponsesOptions($surveyId);
707 if ($responseOptions) {
708 $this->add('select', 'result', ts('Result'),
709 array('' => ts('- select -')) + array_combine($responseOptions, $responseOptions)
710 );
711 }
712 $surveyTitle = NULL;
713 if ($surveyId) {
714 $surveyTitle = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'title');
715 }
716 $this->assign('surveyTitle', $surveyTitle);
717 }
718 }
719 $this->assign('surveyActivity', $this->_isSurveyActivity);
720
721 // this option should be available only during add mode
722 if ($this->_action != CRM_Core_Action::UPDATE) {
723 $this->add('advcheckbox', 'is_multi_activity', ts('Create a separate activity for each contact.'));
724 }
725
726 $this->addRule('duration',
727 ts('Please enter the duration as number of minutes (integers only).'), 'positiveInteger'
728 );
729 $this->addDateTime('activity_date_time', ts('Date'), TRUE, array('formatType' => 'activityDateTime'));
730
731 //add followup date
732 $this->addDateTime('followup_date', ts('in'), FALSE, array('formatType' => 'activityDateTime'));
733
734 // Only admins and case-workers can change the activity source
735 if (!CRM_Core_Permission::check('administer CiviCRM') && $this->_context != 'caseActivity') {
736 $this->getElement('source_contact_id')->freeze();
737 }
738
739 //need to assign custom data type and subtype to the template
740 $this->assign('customDataType', 'Activity');
741 $this->assign('customDataSubType', $this->_activityTypeId);
742 $this->assign('entityID', $this->_activityId);
743
744 $tags = CRM_Core_BAO_Tag::getTags('civicrm_activity');
745
746 if (!empty($tags)) {
747 $this->add('select', 'tag', ts('Tags'), $tags, FALSE,
748 array('id' => 'tags', 'multiple' => 'multiple', 'class' => 'crm-select2 huge')
749 );
750 }
751
752 // we need to hide activity tagset for special activities
753 $specialActivities = array('Open Case');
754
755 if (!in_array($this->_activityTypeName, $specialActivities)) {
756 // build tag widget
757 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_activity');
758 CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_activity', $this->_activityId);
759 }
760
761 // if we're viewing, we're assigning different buttons than for adding/editing
762 if ($this->_action & CRM_Core_Action::VIEW) {
763 if (isset($this->_groupTree)) {
764 CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $this->_groupTree);
765 }
766 // form should be frozen for view mode
767 $this->freeze();
768
769 $buttons = array();
770 $buttons[] = array(
771 'type' => 'cancel',
772 'name' => ts('Done')
773 );
774 $this->addButtons($buttons);
775 }
776 else {
777 $message = array(
778 'completed' => ts('Are you sure? This is a COMPLETED activity with the DATE in the FUTURE. Click Cancel to change the date / status. Otherwise, click OK to save.'),
779 'scheduled' => ts('Are you sure? This is a SCHEDULED activity with the DATE in the PAST. Click Cancel to change the date / status. Otherwise, click OK to save.'),
780 );
781 $js = array('onclick' => "return activityStatus(" . json_encode($message) . ");");
782 $this->addButtons(array(
783 array(
784 'type' => 'upload',
785 'name' => ts('Save'),
786 'js' => $js,
787 'isDefault' => TRUE
788 ),
789 array(
790 'type' => 'cancel',
791 'name' => ts('Cancel')
792 )
793 )
794 );
795 }
796
797 if ($this->_activityTypeFile) {
798 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
799
800 $className::buildQuickForm($this);
801 $this->addFormRule(array($className, 'formRule'), $this);
802 }
803
804 $this->addFormRule(array('CRM_Activity_Form_Activity', 'formRule'), $this);
805
806 if (CRM_Core_BAO_Setting::getItem(
807 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
808 'activity_assignee_notification'
809 )
810 ) {
811 $this->assign('activityAssigneeNotification', TRUE);
812 }
813 else {
814 $this->assign('activityAssigneeNotification', FALSE);
815 }
816 }
817
818 /**
819 * Global form rule
820 *
821 * @param array $fields the input form values
822 * @param array $files the uploaded files if any
823 * @param $self
824 *
825 * @return true if no errors, else array of errors
826 * @access public
827 * @static
828 */
829 static function formRule($fields, $files, $self) {
830 // skip form rule if deleting
831 if (CRM_Utils_Array::value('_qf_Activity_next_', $fields) == 'Delete') {
832 return TRUE;
833 }
834 $errors = array();
835 if (!$self->_single && !$fields['activity_type_id']) {
836 $errors['activity_type_id'] = ts('Activity Type is a required field');
837 }
838
839 //Activity type is mandatory if creating new activity, CRM-4515
840 if (array_key_exists('activity_type_id', $fields) && empty($fields['activity_type_id'])) {
841 $errors['activity_type_id'] = ts('Activity Type is required field.');
842 }
843
844 if (CRM_Utils_Array::value('activity_type_id', $fields) == 3 &&
845 CRM_Utils_Array::value('status_id', $fields) == 1
846 ) {
847 $errors['status_id'] = ts('You cannot record scheduled email activity.');
848 }
849 elseif (CRM_Utils_Array::value('activity_type_id', $fields) == 4 &&
850 CRM_Utils_Array::value('status_id', $fields) == 1
851 ) {
852 $errors['status_id'] = ts('You cannot record scheduled SMS activity.');
853 }
854
855 if (!empty($fields['followup_activity_type_id']) && empty($fields['followup_date'])) {
856 $errors['followup_date_time'] = ts('Followup date is a required field.');
857 }
858 //Activity type is mandatory if subject or follow-up date is specified for an Follow-up activity, CRM-4515
859 if ((!empty($fields['followup_activity_subject']) || !empty($fields['followup_date'])) && empty($fields['followup_activity_type_id'])) {
860 $errors['followup_activity_subject'] = ts('Follow-up Activity type is a required field.');
861 }
862 return $errors;
863 }
864
865 /**
866 * Process the form submission
867 *
868 * @access public
869 *
870 * @param array $params
871 * @return array|null
872 */
873 public function postProcess($params = NULL) {
874 if ($this->_action & CRM_Core_Action::DELETE) {
875 $deleteParams = array('id' => $this->_activityId);
876 $moveToTrash = CRM_Case_BAO_Case::isCaseActivity($this->_activityId);
877 CRM_Activity_BAO_Activity::deleteActivity($deleteParams, $moveToTrash);
878
879 // delete tags for the entity
880 $tagParams = array(
881 'entity_table' => 'civicrm_activity',
882 'entity_id' => $this->_activityId
883 );
884
885 CRM_Core_BAO_EntityTag::del($tagParams);
886
887 CRM_Core_Session::setStatus(ts("Selected Activity has been deleted successfully."), ts('Record Deleted'), 'success');
888 return;
889 }
890
891 // store the submitted values in an array
892 if (!$params) {
893 $params = $this->controller->exportValues($this->_name);
894 }
895
896 //set activity type id
897 if (empty($params['activity_type_id'])) {
898 $params['activity_type_id'] = $this->_activityTypeId;
899 }
900
901 if (!empty($params['hidden_custom']) &&
902 !isset($params['custom'])
903 ) {
904 $customFields = CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE,
905 $this->_activityTypeId
906 );
907 $customFields = CRM_Utils_Array::crmArrayMerge($customFields,
908 CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE,
909 NULL, NULL, TRUE
910 )
911 );
912 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
913 $customFields,
914 $this->_activityId,
915 'Activity'
916 );
917 }
918
919 // store the date with proper format
920 $params['activity_date_time'] = CRM_Utils_Date::processDate($params['activity_date_time'], $params['activity_date_time_time']);
921
922 // format params as arrays
923 foreach (array('target', 'assignee', 'followup_assignee') as $name) {
924 if (!empty($params["{$name}_contact_id"])) {
925 $params["{$name}_contact_id"] = explode(',', $params["{$name}_contact_id"]);
926 }
927 else {
928 $params["{$name}_contact_id"] = array();
929 }
930 }
931
932 // get ids for associated contacts
933 if (!$params['source_contact_id']) {
934 $params['source_contact_id'] = $this->_currentUserId;
935 }
936
937 if (isset($this->_activityId)) {
938 $params['id'] = $this->_activityId;
939 }
940
941 // add attachments as needed
942 CRM_Core_BAO_File::formatAttachment($params,
943 $params,
944 'civicrm_activity',
945 $this->_activityId
946 );
947
948 $activity = array();
949 if (!empty($params['is_multi_activity']) &&
950 !CRM_Utils_Array::crmIsEmptyArray($params['target_contact_id'])
951 ) {
952 $targetContacts = $params['target_contact_id'];
953 foreach ($targetContacts as $targetContactId) {
954 $params['target_contact_id'] = array($targetContactId);
955 // save activity
956 $activity[] = $this->processActivity($params);
957 }
958 }
959 else {
960 // save activity
961 $activity = $this->processActivity($params);
962 }
963
964 //Set for repeat configuration in create mode
965 $params['entity_id'] = $this->_activityId;
966 $params['entity_table'] = 'civicrm_activity';
967 $scheduleReminderDetails = array();
968 if (!empty($params['entity_id']) && !empty($params['entity_table'])) {
969 $checkParentExistsForThisId = CRM_Core_BAO_RecurringEntity::getParentFor($params['entity_id'], $params['entity_table']);
970 if ($checkParentExistsForThisId) {
971 $params['parent_entity_id'] = $checkParentExistsForThisId;
972 $scheduleReminderDetails = CRM_Core_BAO_RecurringEntity::getReminderDetailsByEntityId($checkParentExistsForThisId, $params['entity_table']);
973 }
974 else {
975 $params['parent_entity_id'] = $params['entity_id'];
976 $scheduleReminderDetails = CRM_Core_BAO_RecurringEntity::getReminderDetailsByEntityId($params['entity_id'], $params['entity_table']);
977 }
978 if (property_exists($scheduleReminderDetails, 'id')) {
979 $params['schedule_reminder_id'] = $scheduleReminderDetails->id;
980 }
981 }
982 $params['dateColumns'] = array('activity_date_time');
983
984 //Unset activity id
985 unset($params['id']);
986 $linkedEntities = array(
987 array(
988 'table' => 'civicrm_activity_contact',
989 'findCriteria' => array(
990 'activity_id' => $this->_activityId,
991 ),
992 'linkedColumns' => array('activity_id'),
993 'isRecurringEntityRecord' => FALSE,
994 )
995 );
996 CRM_Core_Form_RecurringEntity::postProcess($params, 'civicrm_activity', $linkedEntities);
997
998 return array('activity' => $activity);
999 }
1000
1001 /**
1002 * Process activity creation
1003 *
1004 * @param array $params associated array of submitted values
1005 *
1006 * @return $this|null|object
1007 * @access protected
1008 */
1009 protected function processActivity(&$params) {
1010 $activityAssigned = array();
1011 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
1012 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
1013 // format assignee params
1014 if (!CRM_Utils_Array::crmIsEmptyArray($params['assignee_contact_id'])) {
1015 //skip those assignee contacts which are already assigned
1016 //while sending a copy.CRM-4509.
1017 $activityAssigned = array_flip($params['assignee_contact_id']);
1018 if ($this->_activityId) {
1019 $assigneeContacts = CRM_Activity_BAO_ActivityContact::getNames($this->_activityId, $assigneeID);
1020 $activityAssigned = array_diff_key($activityAssigned, $assigneeContacts);
1021 }
1022 }
1023
1024 // call begin post process. Idea is to let injecting file do
1025 // any processing before the activity is added/updated.
1026 $this->beginPostProcess($params);
1027
1028 $activity = CRM_Activity_BAO_Activity::create($params);
1029
1030 // add tags if exists
1031 $tagParams = array();
1032 if (!empty($params['tag'])) {
1033 foreach ($params['tag'] as $tag) {
1034 $tagParams[$tag] = 1;
1035 }
1036 }
1037
1038 //save static tags
1039 CRM_Core_BAO_EntityTag::create($tagParams, 'civicrm_activity', $activity->id);
1040
1041 //save free tags
1042 if (isset($params['activity_taglist']) && !empty($params['activity_taglist'])) {
1043 CRM_Core_Form_Tag::postProcess($params['activity_taglist'], $activity->id, 'civicrm_activity', $this);
1044 }
1045
1046 // call end post process. Idea is to let injecting file do any
1047 // processing needed, after the activity has been added/updated.
1048 $this->endPostProcess($params, $activity);
1049
1050 // CRM-9590
1051 if (!empty($params['is_multi_activity'])) {
1052 $this->_activityIds[] = $activity->id;
1053 }
1054 else {
1055 $this->_activityId = $activity->id;
1056 }
1057
1058 // create follow up activity if needed
1059 $followupStatus = '';
1060 $followupActivity = NULL;
1061 if (!empty($params['followup_activity_type_id'])) {
1062 $followupActivity = CRM_Activity_BAO_Activity::createFollowupActivity($activity->id, $params);
1063 $followupStatus = ts('A followup activity has been scheduled.');
1064 }
1065
1066 // send copy to assignee contacts.CRM-4509
1067 $mailStatus = '';
1068
1069 if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
1070 'activity_assignee_notification')) {
1071 $activityIDs = array($activity->id);
1072 if ($followupActivity) {
1073 $activityIDs = array_merge($activityIDs, array($followupActivity->id));
1074 }
1075 $assigneeContacts = CRM_Activity_BAO_ActivityAssignment::getAssigneeNames($activityIDs, TRUE, FALSE);
1076
1077 if (!CRM_Utils_Array::crmIsEmptyArray($params['assignee_contact_id'])) {
1078 $mailToContacts = array();
1079
1080 //build an associative array with unique email addresses.
1081 foreach ($activityAssigned as $id => $dnc) {
1082 if (isset($id) && array_key_exists($id, $assigneeContacts)) {
1083 $mailToContacts[$assigneeContacts[$id]['email']] = $assigneeContacts[$id];
1084 }
1085 }
1086
1087 if (!CRM_Utils_array::crmIsEmptyArray($mailToContacts)) {
1088 //include attachments while sending a copy of activity.
1089 $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_activity', $activity->id);
1090
1091 $ics = new CRM_Activity_BAO_ICalendar($activity);
1092 $ics->addAttachment($attachments, $mailToContacts);
1093
1094 // CRM-8400 add param with _currentlyViewedContactId for URL link in mail
1095 CRM_Case_BAO_Case::sendActivityCopy(NULL, $activity->id, $mailToContacts, $attachments, NULL);
1096
1097 $ics->cleanup();
1098
1099 $mailStatus .= ts("A copy of the activity has also been sent to assignee contacts(s).");
1100 }
1101 }
1102
1103 // Also send email to follow-up activity assignees if set
1104 if ($followupActivity) {
1105 $mailToFollowupContacts = array();
1106 foreach ($assigneeContacts as $values) {
1107 if ($values['activity_id'] == $followupActivity->id) {
1108 $mailToFollowupContacts[$values['email']] = $values;
1109 }
1110 }
1111
1112 if (!CRM_Utils_array::crmIsEmptyArray($mailToFollowupContacts)) {
1113 $ics = new CRM_Activity_BAO_ICalendar($followupActivity);
1114 $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_activity', $followupActivity->id);
1115 $ics->addAttachment($attachments, $mailToFollowupContacts);
1116
1117 CRM_Case_BAO_Case::sendActivityCopy(NULL, $followupActivity->id, $mailToFollowupContacts, $attachments, NULL);
1118
1119 $ics->cleanup();
1120
1121 $mailStatus .= '<br />' . ts("A copy of the follow-up activity has also been sent to follow-up assignee contacts(s).");
1122 }
1123 }
1124 }
1125
1126 // set status message
1127 $subject = '';
1128 if (!empty($params['subject'])) {
1129 $subject = "'" . $params['subject'] . "'";
1130 }
1131
1132 CRM_Core_Session::setStatus(ts('Activity %1 has been saved. %2 %3',
1133 array(
1134 1 => $subject,
1135 2 => $followupStatus,
1136 3 => $mailStatus
1137 )
1138 ), ts('Saved'), 'success');
1139
1140 return $activity;
1141 }
1142
1143 /**
1144 * Shorthand for getting id by display name (makes code more readable)
1145 *
1146 * @access protected
1147 */
1148 protected function _getIdByDisplayName($displayName) {
1149 return CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
1150 $displayName,
1151 'id',
1152 'sort_name'
1153 );
1154 }
1155
1156 /**
1157 * Shorthand for getting display name by id (makes code more readable)
1158 *
1159 * @access protected
1160 */
1161 protected function _getDisplayNameById($id) {
1162 return CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
1163 $id,
1164 'sort_name',
1165 'id'
1166 );
1167 }
1168
1169 /**
1170 * Let injecting activity type file do any processing
1171 * needed, before the activity is added/updated
1172 *
1173 * @param array $params
1174 */
1175 function beginPostProcess(&$params) {
1176 if ($this->_activityTypeFile) {
1177 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
1178 $className::beginPostProcess($this, $params);
1179 }
1180 }
1181
1182 /**
1183 * Let injecting activity type file do any processing
1184 * needed, after the activity has been added/updated
1185 *
1186 * @param array $params
1187 * @param $activity
1188 */
1189 function endPostProcess(&$params, &$activity) {
1190 if ($this->_activityTypeFile) {
1191 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
1192 $className::endPostProcess($this, $params, $activity);
1193 }
1194 }
1195 }
1196