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