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