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