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