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