Merge pull request #7656 from totten/master-civi-ns
[civicrm-core.git] / CRM / Activity / Form / Activity.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 $this->_fields = array(
137 'subject' => array(
138 'type' => 'text',
139 'label' => ts('Subject'),
140 'attributes' => CRM_Core_DAO::getAttribute('CRM_Activity_DAO_Activity',
141 'subject'
142 ),
143 ),
144 'duration' => array(
145 'type' => 'text',
146 'label' => ts('Duration'),
147 'attributes' => array('size' => 4, 'maxlength' => 8),
148 'required' => FALSE,
149 ),
150 'location' => array(
151 'type' => 'text',
152 'label' => ts('Location'),
153 'attributes' => CRM_Core_DAO::getAttribute('CRM_Activity_DAO_Activity', 'location'),
154 'required' => FALSE,
155 ),
156 'details' => array(
157 'type' => 'wysiwyg',
158 'label' => ts('Details'),
159 'attributes' => array('class' => 'huge'),
160 'required' => FALSE,
161 ),
162 'status_id' => array(
163 'type' => 'select',
164 'required' => TRUE,
165 ),
166 'priority_id' => array(
167 'type' => 'select',
168 'required' => TRUE,
169 ),
170 'source_contact_id' => array(
171 'type' => 'entityRef',
172 'label' => ts('Added By'),
173 'required' => FALSE,
174 ),
175 'target_contact_id' => array(
176 'type' => 'entityRef',
177 'label' => ts('With Contact'),
178 'attributes' => array('multiple' => TRUE, 'create' => TRUE),
179 ),
180 'assignee_contact_id' => array(
181 'type' => 'entityRef',
182 'label' => ts('Assigned to'),
183 'attributes' => array(
184 'multiple' => TRUE,
185 'create' => TRUE,
186 'api' => array('params' => array('is_deceased' => 0)),
187 ),
188 ),
189 'followup_assignee_contact_id' => array(
190 'type' => 'entityRef',
191 'label' => ts('Assigned to'),
192 'attributes' => array(
193 'multiple' => TRUE,
194 'create' => TRUE,
195 'api' => array('params' => array('is_deceased' => 0)),
196 ),
197 ),
198 'followup_activity_type_id' => array(
199 'type' => 'select',
200 'label' => ts('Followup Activity'),
201 'attributes' => array('' => '- ' . ts('select activity') . ' -') + CRM_Core_PseudoConstant::ActivityType(FALSE),
202 'extra' => array('class' => 'crm-select2'),
203 ),
204 // Add optional 'Subject' field for the Follow-up Activiity, CRM-4491
205 'followup_activity_subject' => array(
206 'type' => 'text',
207 'label' => ts('Subject'),
208 'attributes' => CRM_Core_DAO::getAttribute('CRM_Activity_DAO_Activity',
209 'subject'
210 ),
211 ),
212 );
213
214 if (($this->_context == 'standalone') &&
215 ($printPDF = CRM_Utils_Array::key('Print PDF Letter', $this->_fields['followup_activity_type_id']['attributes']))
216 ) {
217 unset($this->_fields['followup_activity_type_id']['attributes'][$printPDF]);
218 }
219 }
220
221 /**
222 * Build the form object.
223 */
224 public function preProcess() {
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, 'label');
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 ) {
445 $urlString = $path;
446 }
447 else {
448 $urlString = 'civicrm/activity/search';
449 }
450 $this->assign('searchKey', $qfKey);
451 }
452 elseif ($this->_context != 'caseActivity') {
453 $urlParams = "action=browse&reset=1&cid={$this->_currentlyViewedContactId}&selectedChild=activity";
454 $urlString = 'civicrm/contact/view';
455 }
456
457 if ($urlString) {
458 $session->pushUserContext(CRM_Utils_System::url($urlString, $urlParams));
459 }
460
461 // hack to retrieve activity type id from post variables
462 if (!$this->_activityTypeId) {
463 $this->_activityTypeId = CRM_Utils_Array::value('activity_type_id', $_POST);
464 }
465
466 // when custom data is included in this page
467 if (!empty($_POST['hidden_custom'])) {
468 // We need to set it in the session for the code below to work.
469 // CRM-3014
470 // Need to assign custom data subtype to the template.
471 $this->set('type', 'Activity');
472 $this->set('subType', $this->_activityTypeId);
473 $this->set('entityId', $this->_activityId);
474 CRM_Custom_Form_CustomData::preProcess($this, NULL, $this->_activityTypeId, 1, 'Activity', $this->_activityId);
475 CRM_Custom_Form_CustomData::buildQuickForm($this);
476 CRM_Custom_Form_CustomData::setDefaultValues($this);
477 }
478
479 // add attachments part
480 CRM_Core_BAO_File::buildAttachment($this, 'civicrm_activity', $this->_activityId, NULL, TRUE);
481
482 // figure out the file name for activity type, if any
483 if ($this->_activityTypeId &&
484 $this->_activityTypeFile = 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.
514 *
515 * For edit/view mode the default values are retrieved from the database.
516 *
517 * @return array
518 */
519 public function setDefaultValues() {
520
521 $defaults = $this->_values + CRM_Core_Form_RecurringEntity::setDefaultValues();
522 // if we're editing...
523 if (isset($this->_activityId)) {
524 if (empty($defaults['activity_date_time'])) {
525 list($defaults['activity_date_time'], $defaults['activity_date_time_time']) = CRM_Utils_Date::setDateDefaults(NULL, 'activityDateTime');
526 }
527 elseif ($this->_action & CRM_Core_Action::UPDATE) {
528 $this->assign('current_activity_date_time', $defaults['activity_date_time']);
529 list($defaults['activity_date_time'],
530 $defaults['activity_date_time_time']
531 ) = CRM_Utils_Date::setDateDefaults($defaults['activity_date_time'], 'activityDateTime');
532 list($defaults['repetition_start_date'], $defaults['repetition_start_date_time']) = CRM_Utils_Date::setDateDefaults($defaults['activity_date_time'], 'activityDateTime');
533 }
534
535 if ($this->_context != 'standalone') {
536 $this->assign('target_contact_value',
537 CRM_Utils_Array::value('target_contact_value', $defaults)
538 );
539 $this->assign('assignee_contact_value',
540 CRM_Utils_Array::value('assignee_contact_value', $defaults)
541 );
542 }
543
544 // Fixme: why are we getting the wrong keys from upstream?
545 $defaults['target_contact_id'] = CRM_Utils_Array::value('target_contact', $defaults);
546 $defaults['assignee_contact_id'] = CRM_Utils_Array::value('assignee_contact', $defaults);
547
548 // set default tags if exists
549 $defaults['tag'] = CRM_Core_BAO_EntityTag::getTag($this->_activityId, 'civicrm_activity');
550 }
551 else {
552 // if it's a new activity, we need to set default values for associated contact fields
553 $this->_sourceContactId = $this->_currentUserId;
554 $this->_targetContactId = $this->_currentlyViewedContactId;
555
556 $defaults['source_contact_id'] = $this->_sourceContactId;
557 $defaults['target_contact_id'] = $this->_targetContactId;
558
559 list($defaults['activity_date_time'], $defaults['activity_date_time_time'])
560 = CRM_Utils_Date::setDateDefaults(NULL, 'activityDateTime');
561 }
562
563 if ($this->_activityTypeId) {
564 $defaults['activity_type_id'] = $this->_activityTypeId;
565 }
566
567 if (!$this->_single && !empty($this->_contactIds)) {
568 $defaults['target_contact_id'] = $this->_contactIds;
569 }
570
571 // CRM-15472 - 50 is around the practical limit of how many items a select2 entityRef can handle
572 if (!empty($defaults['target_contact_id'])) {
573 $count = count(is_array($defaults['target_contact_id']) ? $defaults['target_contact_id'] : explode(',', $defaults['target_contact_id']));
574 if ($count > 50) {
575 $this->freeze(array('target_contact_id'));
576 }
577 }
578
579 if ($this->_action & (CRM_Core_Action::DELETE | CRM_Core_Action::RENEW)) {
580 $this->assign('delName', CRM_Utils_Array::value('subject', $defaults));
581 }
582
583 if ($this->_activityTypeFile) {
584 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
585 $defaults += $className::setDefaultValues($this);
586 }
587 if (empty($defaults['priority_id'])) {
588 $priority = CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'priority_id');
589 $defaults['priority_id'] = array_search('Normal', $priority);
590 }
591 if (empty($defaults['status_id'])) {
592 $defaults['status_id'] = CRM_Core_OptionGroup::getDefaultValue('activity_status');
593 }
594 return $defaults;
595 }
596
597 public function buildQuickForm() {
598 if ($this->_action & (CRM_Core_Action::DELETE | CRM_Core_Action::RENEW)) {
599 //enable form element (ActivityLinks sets this true)
600 $this->assign('suppressForm', FALSE);
601
602 $button = ts('Delete');
603 if ($this->_action & CRM_Core_Action::RENEW) {
604 $button = ts('Restore');
605 }
606 $this->addButtons(array(
607 array(
608 'type' => 'next',
609 'name' => $button,
610 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
611 'isDefault' => TRUE,
612 ),
613 array(
614 'type' => 'cancel',
615 'name' => ts('Cancel'),
616 ),
617 ));
618 return;
619 }
620
621 // Build other activity links.
622 CRM_Activity_Form_ActivityLinks::commonBuildQuickForm($this);
623
624 // Enable form element (ActivityLinks sets this true).
625 $this->assign('suppressForm', FALSE);
626
627 $element = &$this->add('select', 'activity_type_id', ts('Activity Type'),
628 array('' => '- ' . ts('select') . ' -') + $this->_fields['followup_activity_type_id']['attributes'],
629 FALSE, array(
630 'onchange' => "CRM.buildCustomData( 'Activity', this.value );",
631 'class' => 'crm-select2 required',
632 )
633 );
634
635 // Freeze for update mode.
636 if ($this->_action & CRM_Core_Action::UPDATE) {
637 $element->freeze();
638 }
639
640 // Call to RecurringEntity buildQuickForm for add/update mode.
641 if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) {
642 CRM_Core_Form_RecurringEntity::buildQuickForm($this);
643 }
644
645 foreach ($this->_fields as $field => $values) {
646 if (!empty($this->_fields[$field])) {
647 $attribute = CRM_Utils_Array::value('attributes', $values);
648 $required = !empty($values['required']);
649
650 if ($values['type'] == 'select' && empty($attribute)) {
651 $this->addSelect($field, array('entity' => 'activity'), $required);
652 }
653 elseif ($values['type'] == 'entityRef') {
654 $this->addEntityRef($field, $values['label'], $attribute, $required);
655 }
656 else {
657 $this->add($values['type'], $field, $values['label'], $attribute, $required, CRM_Utils_Array::value('extra', $values));
658 }
659 }
660 }
661
662 // CRM-7362 --add campaigns.
663 CRM_Campaign_BAO_Campaign::addCampaign($this, CRM_Utils_Array::value('campaign_id', $this->_values));
664
665 // Add engagement level CRM-7775
666 $buildEngagementLevel = FALSE;
667 if (CRM_Campaign_BAO_Campaign::isCampaignEnable() &&
668 CRM_Campaign_BAO_Campaign::accessCampaign()
669 ) {
670 $buildEngagementLevel = TRUE;
671 $this->addSelect('engagement_level', array('entity' => 'activity'));
672 $this->addRule('engagement_level',
673 ts('Please enter the engagement index as a number (integers only).'),
674 'positiveInteger'
675 );
676 }
677 $this->assign('buildEngagementLevel', $buildEngagementLevel);
678
679 // check for survey activity
680 $this->_isSurveyActivity = FALSE;
681
682 if ($this->_activityId && CRM_Campaign_BAO_Campaign::isCampaignEnable() &&
683 CRM_Campaign_BAO_Campaign::accessCampaign()
684 ) {
685
686 $this->_isSurveyActivity = CRM_Campaign_BAO_Survey::isSurveyActivity($this->_activityId);
687 if ($this->_isSurveyActivity) {
688 $surveyId = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity',
689 $this->_activityId,
690 'source_record_id'
691 );
692 $responseOptions = CRM_Campaign_BAO_Survey::getResponsesOptions($surveyId);
693 if ($responseOptions) {
694 $this->add('select', 'result', ts('Result'),
695 array('' => ts('- select -')) + array_combine($responseOptions, $responseOptions)
696 );
697 }
698 $surveyTitle = NULL;
699 if ($surveyId) {
700 $surveyTitle = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'title');
701 }
702 $this->assign('surveyTitle', $surveyTitle);
703 }
704 }
705 $this->assign('surveyActivity', $this->_isSurveyActivity);
706
707 // this option should be available only during add mode
708 if ($this->_action != CRM_Core_Action::UPDATE) {
709 $this->add('advcheckbox', 'is_multi_activity', ts('Create a separate activity for each contact.'));
710 }
711
712 $this->addRule('duration',
713 ts('Please enter the duration as number of minutes (integers only).'), 'positiveInteger'
714 );
715 $this->addDateTime('activity_date_time', ts('Date'), TRUE, array('formatType' => 'activityDateTime'));
716
717 // Add followup date.
718 $this->addDateTime('followup_date', ts('in'), FALSE, array('formatType' => 'activityDateTime'));
719
720 // Only admins and case-workers can change the activity source
721 if (!CRM_Core_Permission::check('administer CiviCRM') && $this->_context != 'caseActivity') {
722 $this->getElement('source_contact_id')->freeze();
723 }
724
725 //need to assign custom data type and subtype to the template
726 $this->assign('customDataType', 'Activity');
727 $this->assign('customDataSubType', $this->_activityTypeId);
728 $this->assign('entityID', $this->_activityId);
729
730 CRM_Core_BAO_Tag::getTags('civicrm_activity', $tags, NULL,
731 '&nbsp;&nbsp;', TRUE);
732
733 if (!empty($tags)) {
734 $this->add('select', 'tag', ts('Tags'), $tags, FALSE,
735 array('id' => 'tags', 'multiple' => 'multiple', 'class' => 'crm-select2 huge')
736 );
737 }
738
739 // we need to hide activity tagset for special activities
740 $specialActivities = array('Open Case');
741
742 if (!in_array($this->_activityTypeName, $specialActivities)) {
743 // build tag widget
744 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_activity');
745 CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_activity', $this->_activityId);
746 }
747
748 // if we're viewing, we're assigning different buttons than for adding/editing
749 if ($this->_action & CRM_Core_Action::VIEW) {
750 if (isset($this->_groupTree)) {
751 CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $this->_groupTree, FALSE, NULL, NULL, NULL, $this->_activityId);
752 }
753 // form should be frozen for view mode
754 $this->freeze();
755
756 $buttons = array();
757 $buttons[] = array(
758 'type' => 'cancel',
759 'name' => ts('Done'),
760 );
761 $this->addButtons($buttons);
762 }
763 else {
764 $message = array(
765 '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.'),
766 '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.'),
767 );
768 $js = array('onclick' => "return activityStatus(" . json_encode($message) . ");");
769 $this->addButtons(array(
770 array(
771 'type' => 'upload',
772 'name' => ts('Save'),
773 'js' => $js,
774 'isDefault' => TRUE,
775 ),
776 array(
777 'type' => 'cancel',
778 'name' => ts('Cancel'),
779 ),
780 )
781 );
782 }
783
784 if ($this->_activityTypeFile) {
785 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
786
787 $className::buildQuickForm($this);
788 $this->addFormRule(array($className, 'formRule'), $this);
789 }
790
791 $this->addFormRule(array('CRM_Activity_Form_Activity', 'formRule'), $this);
792
793 if (Civi::settings()->get('activity_assignee_notification')) {
794 $this->assign('activityAssigneeNotification', TRUE);
795 }
796 else {
797 $this->assign('activityAssigneeNotification', FALSE);
798 }
799 }
800
801 /**
802 * Global form rule.
803 *
804 * @param array $fields
805 * The input form values.
806 * @param array $files
807 * The uploaded files if any.
808 * @param $self
809 *
810 * @return bool|array
811 * true if no errors, else array of errors
812 */
813 public static function formRule($fields, $files, $self) {
814 // skip form rule if deleting
815 if (CRM_Utils_Array::value('_qf_Activity_next_', $fields) == 'Delete') {
816 return TRUE;
817 }
818 $errors = array();
819 if ((array_key_exists('activity_type_id', $fields) || !$self->_single) && empty($fields['activity_type_id'])) {
820 $errors['activity_type_id'] = ts('Activity Type is a required field');
821 }
822
823 if (CRM_Utils_Array::value('activity_type_id', $fields) == 3 &&
824 CRM_Utils_Array::value('status_id', $fields) == 1
825 ) {
826 $errors['status_id'] = ts('You cannot record scheduled email activity.');
827 }
828 elseif (CRM_Utils_Array::value('activity_type_id', $fields) == 4 &&
829 CRM_Utils_Array::value('status_id', $fields) == 1
830 ) {
831 $errors['status_id'] = ts('You cannot record scheduled SMS activity.');
832 }
833
834 if (!empty($fields['followup_activity_type_id']) && empty($fields['followup_date'])) {
835 $errors['followup_date_time'] = ts('Followup date is a required field.');
836 }
837 // Activity type is mandatory if subject or follow-up date is specified for an Follow-up activity, CRM-4515.
838 if ((!empty($fields['followup_activity_subject']) || !empty($fields['followup_date'])) && empty($fields['followup_activity_type_id'])) {
839 $errors['followup_activity_subject'] = ts('Follow-up Activity type is a required field.');
840 }
841 return $errors;
842 }
843
844 /**
845 * Process the form submission.
846 *
847 *
848 * @param array $params
849 * @return array|null
850 */
851 public function postProcess($params = NULL) {
852 if ($this->_action & CRM_Core_Action::DELETE) {
853 $deleteParams = array('id' => $this->_activityId);
854 $moveToTrash = CRM_Case_BAO_Case::isCaseActivity($this->_activityId);
855 CRM_Activity_BAO_Activity::deleteActivity($deleteParams, $moveToTrash);
856
857 // delete tags for the entity
858 $tagParams = array(
859 'entity_table' => 'civicrm_activity',
860 'entity_id' => $this->_activityId,
861 );
862
863 CRM_Core_BAO_EntityTag::del($tagParams);
864
865 CRM_Core_Session::setStatus(ts("Selected Activity has been deleted successfully."), ts('Record Deleted'), 'success');
866 return NULL;
867 }
868
869 // store the submitted values in an array
870 if (!$params) {
871 $params = $this->controller->exportValues($this->_name);
872 }
873
874 // Set activity type id.
875 if (empty($params['activity_type_id'])) {
876 $params['activity_type_id'] = $this->_activityTypeId;
877 }
878
879 if (!empty($params['hidden_custom']) &&
880 !isset($params['custom'])
881 ) {
882 $customFields = CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE,
883 $this->_activityTypeId
884 );
885 $customFields = CRM_Utils_Array::crmArrayMerge($customFields,
886 CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE,
887 NULL, NULL, TRUE
888 )
889 );
890 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
891 $this->_activityId,
892 'Activity'
893 );
894 }
895
896 // store the date with proper format
897 $params['activity_date_time'] = CRM_Utils_Date::processDate($params['activity_date_time'], $params['activity_date_time_time']);
898
899 // format params as arrays
900 foreach (array('target', 'assignee', 'followup_assignee') as $name) {
901 if (!empty($params["{$name}_contact_id"])) {
902 $params["{$name}_contact_id"] = explode(',', $params["{$name}_contact_id"]);
903 }
904 else {
905 $params["{$name}_contact_id"] = array();
906 }
907 }
908
909 // get ids for associated contacts
910 if (!$params['source_contact_id']) {
911 $params['source_contact_id'] = $this->_currentUserId;
912 }
913
914 if (isset($this->_activityId)) {
915 $params['id'] = $this->_activityId;
916 }
917
918 // add attachments as needed
919 CRM_Core_BAO_File::formatAttachment($params,
920 $params,
921 'civicrm_activity',
922 $this->_activityId
923 );
924
925 $activity = array();
926 if (!empty($params['is_multi_activity']) &&
927 !CRM_Utils_Array::crmIsEmptyArray($params['target_contact_id'])
928 ) {
929 $targetContacts = $params['target_contact_id'];
930 foreach ($targetContacts as $targetContactId) {
931 $params['target_contact_id'] = array($targetContactId);
932 // save activity
933 $activity[] = $this->processActivity($params);
934 }
935 }
936 else {
937 // save activity
938 $activity = $this->processActivity($params);
939 }
940
941 $activityIds = empty($this->_activityIds) ? array($this->_activityId) : $this->_activityIds;
942 foreach ($activityIds as $activityId) {
943 // set params for repeat configuration in create mode
944 $params['entity_id'] = $activityId;
945 $params['entity_table'] = 'civicrm_activity';
946 if (!empty($params['entity_id']) && !empty($params['entity_table'])) {
947 $checkParentExistsForThisId = CRM_Core_BAO_RecurringEntity::getParentFor($params['entity_id'], $params['entity_table']);
948 if ($checkParentExistsForThisId) {
949 $params['parent_entity_id'] = $checkParentExistsForThisId;
950 $scheduleReminderDetails = CRM_Core_BAO_RecurringEntity::getReminderDetailsByEntityId($checkParentExistsForThisId, $params['entity_table']);
951 }
952 else {
953 $params['parent_entity_id'] = $params['entity_id'];
954 $scheduleReminderDetails = CRM_Core_BAO_RecurringEntity::getReminderDetailsByEntityId($params['entity_id'], $params['entity_table']);
955 }
956 if (property_exists($scheduleReminderDetails, 'id')) {
957 $params['schedule_reminder_id'] = $scheduleReminderDetails->id;
958 }
959 }
960 $params['dateColumns'] = array('activity_date_time');
961
962 // Set default repetition start if it was not provided.
963 if (empty($params['repetition_start_date'])) {
964 $params['repetition_start_date'] = $params['activity_date_time'];
965 }
966
967 // unset activity id
968 unset($params['id']);
969 $linkedEntities = array(
970 array(
971 'table' => 'civicrm_activity_contact',
972 'findCriteria' => array(
973 'activity_id' => $activityId,
974 ),
975 'linkedColumns' => array('activity_id'),
976 'isRecurringEntityRecord' => FALSE,
977 ),
978 );
979 CRM_Core_Form_RecurringEntity::postProcess($params, 'civicrm_activity', $linkedEntities);
980 }
981
982 return array('activity' => $activity);
983 }
984
985 /**
986 * Process activity creation.
987 *
988 * @param array $params
989 * Associated array of submitted values.
990 *
991 * @return self|null|object
992 */
993 protected function processActivity(&$params) {
994 $activityAssigned = array();
995 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
996 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
997 // format assignee params
998 if (!CRM_Utils_Array::crmIsEmptyArray($params['assignee_contact_id'])) {
999 //skip those assignee contacts which are already assigned
1000 //while sending a copy.CRM-4509.
1001 $activityAssigned = array_flip($params['assignee_contact_id']);
1002 if ($this->_activityId) {
1003 $assigneeContacts = CRM_Activity_BAO_ActivityContact::getNames($this->_activityId, $assigneeID);
1004 $activityAssigned = array_diff_key($activityAssigned, $assigneeContacts);
1005 }
1006 }
1007
1008 // call begin post process. Idea is to let injecting file do
1009 // any processing before the activity is added/updated.
1010 $this->beginPostProcess($params);
1011
1012 $activity = CRM_Activity_BAO_Activity::create($params);
1013
1014 // add tags if exists
1015 $tagParams = array();
1016 if (!empty($params['tag'])) {
1017 foreach ($params['tag'] as $tag) {
1018 $tagParams[$tag] = 1;
1019 }
1020 }
1021
1022 // Save static tags.
1023 CRM_Core_BAO_EntityTag::create($tagParams, 'civicrm_activity', $activity->id);
1024
1025 // Save free tags.
1026 if (isset($params['activity_taglist']) && !empty($params['activity_taglist'])) {
1027 CRM_Core_Form_Tag::postProcess($params['activity_taglist'], $activity->id, 'civicrm_activity', $this);
1028 }
1029
1030 // call end post process. Idea is to let injecting file do any
1031 // processing needed, after the activity has been added/updated.
1032 $this->endPostProcess($params, $activity);
1033
1034 // CRM-9590
1035 if (!empty($params['is_multi_activity'])) {
1036 $this->_activityIds[] = $activity->id;
1037 }
1038 else {
1039 $this->_activityId = $activity->id;
1040 }
1041
1042 // create follow up activity if needed
1043 $followupStatus = '';
1044 $followupActivity = NULL;
1045 if (!empty($params['followup_activity_type_id'])) {
1046 $followupActivity = CRM_Activity_BAO_Activity::createFollowupActivity($activity->id, $params);
1047 $followupStatus = ts('A followup activity has been scheduled.');
1048 }
1049
1050 // send copy to assignee contacts.CRM-4509
1051 $mailStatus = '';
1052
1053 if (Civi::settings()->get('activity_assignee_notification')) {
1054 $activityIDs = array($activity->id);
1055 if ($followupActivity) {
1056 $activityIDs = array_merge($activityIDs, array($followupActivity->id));
1057 }
1058 $assigneeContacts = CRM_Activity_BAO_ActivityAssignment::getAssigneeNames($activityIDs, TRUE, FALSE);
1059
1060 if (!CRM_Utils_Array::crmIsEmptyArray($params['assignee_contact_id'])) {
1061 $mailToContacts = array();
1062
1063 // Build an associative array with unique email addresses.
1064 foreach ($activityAssigned as $id => $dnc) {
1065 if (isset($id) && array_key_exists($id, $assigneeContacts)) {
1066 $mailToContacts[$assigneeContacts[$id]['email']] = $assigneeContacts[$id];
1067 }
1068 }
1069
1070 $sent = CRM_Activity_BAO_Activity::sendToAssignee($activity, $mailToContacts);
1071 if ($sent) {
1072 $mailStatus .= ts("A copy of the activity has also been sent to assignee contacts(s).");
1073 }
1074 }
1075
1076 // Also send email to follow-up activity assignees if set
1077 if ($followupActivity) {
1078 $mailToFollowupContacts = array();
1079 foreach ($assigneeContacts as $values) {
1080 if ($values['activity_id'] == $followupActivity->id) {
1081 $mailToFollowupContacts[$values['email']] = $values;
1082 }
1083 }
1084
1085 $sentFollowup = CRM_Activity_BAO_Activity::sendToAssignee($followupActivity, $mailToFollowupContacts);
1086 if ($sentFollowup) {
1087 $mailStatus .= '<br />' . ts("A copy of the follow-up activity has also been sent to follow-up assignee contacts(s).");
1088 }
1089 }
1090 }
1091
1092 // set status message
1093 $subject = '';
1094 if (!empty($params['subject'])) {
1095 $subject = "'" . $params['subject'] . "'";
1096 }
1097
1098 CRM_Core_Session::setStatus(ts('Activity %1 has been saved. %2 %3',
1099 array(
1100 1 => $subject,
1101 2 => $followupStatus,
1102 3 => $mailStatus,
1103 )
1104 ), ts('Saved'), 'success');
1105
1106 return $activity;
1107 }
1108
1109 /**
1110 * Shorthand for getting id by display name (makes code more readable)
1111 * @param $displayName
1112 * @return null|string
1113 */
1114 protected function _getIdByDisplayName($displayName) {
1115 return CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
1116 $displayName,
1117 'id',
1118 'sort_name'
1119 );
1120 }
1121
1122 /**
1123 * Shorthand for getting display name by id (makes code more readable)
1124 * @param $id
1125 * @return null|string
1126 */
1127 protected function _getDisplayNameById($id) {
1128 return CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
1129 $id,
1130 'sort_name',
1131 'id'
1132 );
1133 }
1134
1135 /**
1136 * Let injecting activity type file do any processing.
1137 * needed, before the activity is added/updated
1138 *
1139 * @param array $params
1140 */
1141 public function beginPostProcess(&$params) {
1142 if ($this->_activityTypeFile) {
1143 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
1144 $className::beginPostProcess($this, $params);
1145 }
1146 }
1147
1148 /**
1149 * Let injecting activity type file do any processing
1150 * needed, after the activity has been added/updated
1151 *
1152 * @param array $params
1153 * @param $activity
1154 */
1155 public function endPostProcess(&$params, &$activity) {
1156 if ($this->_activityTypeFile) {
1157 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
1158 $className::endPostProcess($this, $params, $activity);
1159 }
1160 }
1161
1162 }