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