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