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