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