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