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