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