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