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