Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | */ |
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)) { | |
751 | CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $this->_groupTree); | |
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( | |
770 | array( | |
771 | 'type' => 'upload', | |
772 | 'name' => ts('Save'), | |
773 | 'js' => $js, | |
21dfd5f5 | 774 | 'isDefault' => TRUE, |
6a488035 TO |
775 | ), |
776 | array( | |
777 | 'type' => 'cancel', | |
21dfd5f5 TO |
778 | 'name' => ts('Cancel'), |
779 | ), | |
6a488035 TO |
780 | ) |
781 | ); | |
782 | } | |
783 | ||
784 | if ($this->_activityTypeFile) { | |
0e6e8724 | 785 | $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}"; |
6a488035 | 786 | |
0e6e8724 DL |
787 | $className::buildQuickForm($this); |
788 | $this->addFormRule(array($className, 'formRule'), $this); | |
6a488035 TO |
789 | } |
790 | ||
791 | $this->addFormRule(array('CRM_Activity_Form_Activity', 'formRule'), $this); | |
6db7e202 | 792 | |
0086c33d | 793 | if (CRM_Core_BAO_Setting::getItem( |
19fc6ae4 | 794 | CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, |
795 | 'activity_assignee_notification' | |
796 | ) | |
6db7e202 | 797 | ) { |
0086c33d DL |
798 | $this->assign('activityAssigneeNotification', TRUE); |
799 | } | |
800 | else { | |
801 | $this->assign('activityAssigneeNotification', FALSE); | |
6db7e202 | 802 | } |
6a488035 TO |
803 | } |
804 | ||
805 | /** | |
fe482240 | 806 | * Global form rule. |
6a488035 | 807 | * |
041ab3d1 TO |
808 | * @param array $fields |
809 | * The input form values. | |
810 | * @param array $files | |
811 | * The uploaded files if any. | |
2a6da8d7 EM |
812 | * @param $self |
813 | * | |
72b3a70c CW |
814 | * @return bool|array |
815 | * true if no errors, else array of errors | |
6a488035 | 816 | */ |
00be9182 | 817 | public static function formRule($fields, $files, $self) { |
6a488035 TO |
818 | // skip form rule if deleting |
819 | if (CRM_Utils_Array::value('_qf_Activity_next_', $fields) == 'Delete') { | |
820 | return TRUE; | |
821 | } | |
822 | $errors = array(); | |
ba6e6f51 | 823 | if ((array_key_exists('activity_type_id', $fields) || !$self->_single) && empty($fields['activity_type_id'])) { |
6a488035 TO |
824 | $errors['activity_type_id'] = ts('Activity Type is a required field'); |
825 | } | |
826 | ||
6a488035 TO |
827 | if (CRM_Utils_Array::value('activity_type_id', $fields) == 3 && |
828 | CRM_Utils_Array::value('status_id', $fields) == 1 | |
829 | ) { | |
830 | $errors['status_id'] = ts('You cannot record scheduled email activity.'); | |
831 | } | |
832 | elseif (CRM_Utils_Array::value('activity_type_id', $fields) == 4 && | |
833 | CRM_Utils_Array::value('status_id', $fields) == 1 | |
834 | ) { | |
835 | $errors['status_id'] = ts('You cannot record scheduled SMS activity.'); | |
836 | } | |
837 | ||
8cc574cf | 838 | if (!empty($fields['followup_activity_type_id']) && empty($fields['followup_date'])) { |
6a488035 TO |
839 | $errors['followup_date_time'] = ts('Followup date is a required field.'); |
840 | } | |
7808aae6 | 841 | // Activity type is mandatory if subject or follow-up date is specified for an Follow-up activity, CRM-4515. |
8cc574cf | 842 | if ((!empty($fields['followup_activity_subject']) || !empty($fields['followup_date'])) && empty($fields['followup_activity_type_id'])) { |
6a488035 TO |
843 | $errors['followup_activity_subject'] = ts('Follow-up Activity type is a required field.'); |
844 | } | |
845 | return $errors; | |
846 | } | |
847 | ||
848 | /** | |
fe482240 | 849 | * Process the form submission. |
6a488035 | 850 | * |
6a488035 | 851 | * |
100fef9d CW |
852 | * @param array $params |
853 | * @return array|null | |
6a488035 TO |
854 | */ |
855 | public function postProcess($params = NULL) { | |
856 | if ($this->_action & CRM_Core_Action::DELETE) { | |
857 | $deleteParams = array('id' => $this->_activityId); | |
858 | $moveToTrash = CRM_Case_BAO_Case::isCaseActivity($this->_activityId); | |
859 | CRM_Activity_BAO_Activity::deleteActivity($deleteParams, $moveToTrash); | |
860 | ||
861 | // delete tags for the entity | |
862 | $tagParams = array( | |
863 | 'entity_table' => 'civicrm_activity', | |
21dfd5f5 | 864 | 'entity_id' => $this->_activityId, |
6a488035 TO |
865 | ); |
866 | ||
867 | CRM_Core_BAO_EntityTag::del($tagParams); | |
868 | ||
869 | CRM_Core_Session::setStatus(ts("Selected Activity has been deleted successfully."), ts('Record Deleted'), 'success'); | |
a1a2a83d | 870 | return NULL; |
6a488035 TO |
871 | } |
872 | ||
873 | // store the submitted values in an array | |
874 | if (!$params) { | |
875 | $params = $this->controller->exportValues($this->_name); | |
876 | } | |
877 | ||
7808aae6 | 878 | // Set activity type id. |
a7488080 | 879 | if (empty($params['activity_type_id'])) { |
6a488035 TO |
880 | $params['activity_type_id'] = $this->_activityTypeId; |
881 | } | |
882 | ||
a7488080 | 883 | if (!empty($params['hidden_custom']) && |
6a488035 TO |
884 | !isset($params['custom']) |
885 | ) { | |
886 | $customFields = CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE, | |
887 | $this->_activityTypeId | |
888 | ); | |
889 | $customFields = CRM_Utils_Array::crmArrayMerge($customFields, | |
890 | CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE, | |
891 | NULL, NULL, TRUE | |
892 | ) | |
893 | ); | |
894 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, | |
6a488035 TO |
895 | $this->_activityId, |
896 | 'Activity' | |
897 | ); | |
898 | } | |
899 | ||
900 | // store the date with proper format | |
901 | $params['activity_date_time'] = CRM_Utils_Date::processDate($params['activity_date_time'], $params['activity_date_time_time']); | |
902 | ||
c27ebe4e CW |
903 | // format params as arrays |
904 | foreach (array('target', 'assignee', 'followup_assignee') as $name) { | |
905 | if (!empty($params["{$name}_contact_id"])) { | |
906 | $params["{$name}_contact_id"] = explode(',', $params["{$name}_contact_id"]); | |
907 | } | |
908 | else { | |
909 | $params["{$name}_contact_id"] = array(); | |
910 | } | |
6a488035 | 911 | } |
6a488035 TO |
912 | |
913 | // get ids for associated contacts | |
914 | if (!$params['source_contact_id']) { | |
915 | $params['source_contact_id'] = $this->_currentUserId; | |
916 | } | |
6a488035 TO |
917 | |
918 | if (isset($this->_activityId)) { | |
919 | $params['id'] = $this->_activityId; | |
920 | } | |
921 | ||
922 | // add attachments as needed | |
923 | CRM_Core_BAO_File::formatAttachment($params, | |
924 | $params, | |
925 | 'civicrm_activity', | |
926 | $this->_activityId | |
927 | ); | |
928 | ||
52c7b3a2 | 929 | $activity = array(); |
a7488080 | 930 | if (!empty($params['is_multi_activity']) && |
52c7b3a2 KJ |
931 | !CRM_Utils_Array::crmIsEmptyArray($params['target_contact_id']) |
932 | ) { | |
933 | $targetContacts = $params['target_contact_id']; | |
19fc6ae4 | 934 | foreach ($targetContacts as $targetContactId) { |
52c7b3a2 KJ |
935 | $params['target_contact_id'] = array($targetContactId); |
936 | // save activity | |
937 | $activity[] = $this->processActivity($params); | |
938 | } | |
939 | } | |
940 | else { | |
941 | // save activity | |
942 | $activity = $this->processActivity($params); | |
943 | } | |
944 | ||
7a4bb524 | 945 | $activityIds = empty($this->_activityIds) ? array($this->_activityId) : $this->_activityIds; |
946 | foreach ($activityIds as $activityId) { | |
947 | // set params for repeat configuration in create mode | |
948 | $params['entity_id'] = $activityId; | |
949 | $params['entity_table'] = 'civicrm_activity'; | |
7a4bb524 | 950 | if (!empty($params['entity_id']) && !empty($params['entity_table'])) { |
951 | $checkParentExistsForThisId = CRM_Core_BAO_RecurringEntity::getParentFor($params['entity_id'], $params['entity_table']); | |
952 | if ($checkParentExistsForThisId) { | |
953 | $params['parent_entity_id'] = $checkParentExistsForThisId; | |
954 | $scheduleReminderDetails = CRM_Core_BAO_RecurringEntity::getReminderDetailsByEntityId($checkParentExistsForThisId, $params['entity_table']); | |
955 | } | |
956 | else { | |
957 | $params['parent_entity_id'] = $params['entity_id']; | |
958 | $scheduleReminderDetails = CRM_Core_BAO_RecurringEntity::getReminderDetailsByEntityId($params['entity_id'], $params['entity_table']); | |
959 | } | |
960 | if (property_exists($scheduleReminderDetails, 'id')) { | |
961 | $params['schedule_reminder_id'] = $scheduleReminderDetails->id; | |
962 | } | |
fe87e754 | 963 | } |
7a4bb524 | 964 | $params['dateColumns'] = array('activity_date_time'); |
b334d9ad | 965 | |
83f3c8a3 CW |
966 | // Set default repetition start if it was not provided. |
967 | if (empty($params['repetition_start_date'])) { | |
968 | $params['repetition_start_date'] = $params['activity_date_time']; | |
969 | } | |
970 | ||
7a4bb524 | 971 | // unset activity id |
972 | unset($params['id']); | |
973 | $linkedEntities = array( | |
974 | array( | |
975 | 'table' => 'civicrm_activity_contact', | |
976 | 'findCriteria' => array( | |
977 | 'activity_id' => $activityId, | |
978 | ), | |
979 | 'linkedColumns' => array('activity_id'), | |
980 | 'isRecurringEntityRecord' => FALSE, | |
b334d9ad | 981 | ), |
7a4bb524 | 982 | ); |
983 | CRM_Core_Form_RecurringEntity::postProcess($params, 'civicrm_activity', $linkedEntities); | |
984 | } | |
b334d9ad | 985 | |
52c7b3a2 KJ |
986 | return array('activity' => $activity); |
987 | } | |
988 | ||
989 | /** | |
fe482240 | 990 | * Process activity creation. |
52c7b3a2 | 991 | * |
041ab3d1 TO |
992 | * @param array $params |
993 | * Associated array of submitted values. | |
77b97be7 | 994 | * |
6c552737 | 995 | * @return self|null|object |
52c7b3a2 KJ |
996 | */ |
997 | protected function processActivity(&$params) { | |
6a488035 | 998 | $activityAssigned = array(); |
e7e657f0 | 999 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
034500d4 | 1000 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); |
6a488035 TO |
1001 | // format assignee params |
1002 | if (!CRM_Utils_Array::crmIsEmptyArray($params['assignee_contact_id'])) { | |
1003 | //skip those assignee contacts which are already assigned | |
1004 | //while sending a copy.CRM-4509. | |
1005 | $activityAssigned = array_flip($params['assignee_contact_id']); | |
1006 | if ($this->_activityId) { | |
034500d4 | 1007 | $assigneeContacts = CRM_Activity_BAO_ActivityContact::getNames($this->_activityId, $assigneeID); |
6a488035 TO |
1008 | $activityAssigned = array_diff_key($activityAssigned, $assigneeContacts); |
1009 | } | |
1010 | } | |
1011 | ||
1012 | // call begin post process. Idea is to let injecting file do | |
1013 | // any processing before the activity is added/updated. | |
1014 | $this->beginPostProcess($params); | |
1015 | ||
1016 | $activity = CRM_Activity_BAO_Activity::create($params); | |
1017 | ||
1018 | // add tags if exists | |
1019 | $tagParams = array(); | |
1020 | if (!empty($params['tag'])) { | |
1021 | foreach ($params['tag'] as $tag) { | |
1022 | $tagParams[$tag] = 1; | |
1023 | } | |
1024 | } | |
1025 | ||
7808aae6 | 1026 | // Save static tags. |
6a488035 TO |
1027 | CRM_Core_BAO_EntityTag::create($tagParams, 'civicrm_activity', $activity->id); |
1028 | ||
7808aae6 | 1029 | // Save free tags. |
6a488035 TO |
1030 | if (isset($params['activity_taglist']) && !empty($params['activity_taglist'])) { |
1031 | CRM_Core_Form_Tag::postProcess($params['activity_taglist'], $activity->id, 'civicrm_activity', $this); | |
1032 | } | |
1033 | ||
1034 | // call end post process. Idea is to let injecting file do any | |
1035 | // processing needed, after the activity has been added/updated. | |
1036 | $this->endPostProcess($params, $activity); | |
1037 | ||
1038 | // CRM-9590 | |
a7488080 | 1039 | if (!empty($params['is_multi_activity'])) { |
52c7b3a2 KJ |
1040 | $this->_activityIds[] = $activity->id; |
1041 | } | |
1042 | else { | |
1043 | $this->_activityId = $activity->id; | |
1044 | } | |
6a488035 TO |
1045 | |
1046 | // create follow up activity if needed | |
1047 | $followupStatus = ''; | |
90b05581 | 1048 | $followupActivity = NULL; |
a7488080 | 1049 | if (!empty($params['followup_activity_type_id'])) { |
90b05581 | 1050 | $followupActivity = CRM_Activity_BAO_Activity::createFollowupActivity($activity->id, $params); |
6a488035 TO |
1051 | $followupStatus = ts('A followup activity has been scheduled.'); |
1052 | } | |
1053 | ||
1054 | // send copy to assignee contacts.CRM-4509 | |
1055 | $mailStatus = ''; | |
1056 | ||
90b05581 | 1057 | if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, |
353ffa53 TO |
1058 | 'activity_assignee_notification') |
1059 | ) { | |
90b05581 DG |
1060 | $activityIDs = array($activity->id); |
1061 | if ($followupActivity) { | |
1062 | $activityIDs = array_merge($activityIDs, array($followupActivity->id)); | |
1063 | } | |
1064 | $assigneeContacts = CRM_Activity_BAO_ActivityAssignment::getAssigneeNames($activityIDs, TRUE, FALSE); | |
6a488035 | 1065 | |
90b05581 DG |
1066 | if (!CRM_Utils_Array::crmIsEmptyArray($params['assignee_contact_id'])) { |
1067 | $mailToContacts = array(); | |
1068 | ||
7808aae6 | 1069 | // Build an associative array with unique email addresses. |
90b05581 DG |
1070 | foreach ($activityAssigned as $id => $dnc) { |
1071 | if (isset($id) && array_key_exists($id, $assigneeContacts)) { | |
1072 | $mailToContacts[$assigneeContacts[$id]['email']] = $assigneeContacts[$id]; | |
1073 | } | |
6a488035 | 1074 | } |
6a488035 | 1075 | |
bc883279 | 1076 | $sent = CRM_Activity_BAO_Activity::sendToAssignee($activity, $mailToContacts); |
1077 | if ($sent) { | |
1078 | $mailStatus .= ts("A copy of the activity has also been sent to assignee contacts(s)."); | |
1079 | } | |
1080 | } | |
77b97be7 | 1081 | |
90b05581 DG |
1082 | // Also send email to follow-up activity assignees if set |
1083 | if ($followupActivity) { | |
1084 | $mailToFollowupContacts = array(); | |
1085 | foreach ($assigneeContacts as $values) { | |
1086 | if ($values['activity_id'] == $followupActivity->id) { | |
1087 | $mailToFollowupContacts[$values['email']] = $values; | |
1088 | } | |
1089 | } | |
1090 | ||
2bbb4a91 | 1091 | $sentFollowup = CRM_Activity_BAO_Activity::sendToAssignee($followupActivity, $mailToFollowupContacts); |
bc883279 | 1092 | if ($sentFollowup) { |
f3c5a172 | 1093 | $mailStatus .= '<br />' . ts("A copy of the follow-up activity has also been sent to follow-up assignee contacts(s)."); |
bc883279 | 1094 | } |
6a488035 TO |
1095 | } |
1096 | } | |
1097 | ||
1098 | // set status message | |
ef89f226 | 1099 | $subject = ''; |
a7488080 | 1100 | if (!empty($params['subject'])) { |
ef89f226 | 1101 | $subject = "'" . $params['subject'] . "'"; |
6a488035 TO |
1102 | } |
1103 | ||
12e2d503 | 1104 | CRM_Core_Session::setStatus(ts('Activity %1 has been saved. %2 %3', |
52c7b3a2 | 1105 | array( |
ef89f226 | 1106 | 1 => $subject, |
52c7b3a2 | 1107 | 2 => $followupStatus, |
21dfd5f5 | 1108 | 3 => $mailStatus, |
52c7b3a2 KJ |
1109 | ) |
1110 | ), ts('Saved'), 'success'); | |
6a488035 | 1111 | |
52c7b3a2 | 1112 | return $activity; |
6a488035 TO |
1113 | } |
1114 | ||
1115 | /** | |
1116 | * Shorthand for getting id by display name (makes code more readable) | |
645ee340 EM |
1117 | * @param $displayName |
1118 | * @return null|string | |
6a488035 TO |
1119 | */ |
1120 | protected function _getIdByDisplayName($displayName) { | |
1121 | return CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
1122 | $displayName, | |
1123 | 'id', | |
1124 | 'sort_name' | |
1125 | ); | |
1126 | } | |
1127 | ||
1128 | /** | |
1129 | * Shorthand for getting display name by id (makes code more readable) | |
645ee340 EM |
1130 | * @param $id |
1131 | * @return null|string | |
6a488035 TO |
1132 | */ |
1133 | protected function _getDisplayNameById($id) { | |
1134 | return CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
1135 | $id, | |
1136 | 'sort_name', | |
1137 | 'id' | |
1138 | ); | |
1139 | } | |
1140 | ||
1141 | /** | |
fe482240 | 1142 | * Let injecting activity type file do any processing. |
6a488035 TO |
1143 | * needed, before the activity is added/updated |
1144 | * | |
100fef9d | 1145 | * @param array $params |
6a488035 | 1146 | */ |
00be9182 | 1147 | public function beginPostProcess(&$params) { |
6a488035 | 1148 | if ($this->_activityTypeFile) { |
0e6e8724 DL |
1149 | $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}"; |
1150 | $className::beginPostProcess($this, $params); | |
6a488035 TO |
1151 | } |
1152 | } | |
1153 | ||
1154 | /** | |
100fef9d | 1155 | * Let injecting activity type file do any processing |
6a488035 TO |
1156 | * needed, after the activity has been added/updated |
1157 | * | |
100fef9d CW |
1158 | * @param array $params |
1159 | * @param $activity | |
6a488035 | 1160 | */ |
00be9182 | 1161 | public function endPostProcess(&$params, &$activity) { |
6a488035 | 1162 | if ($this->_activityTypeFile) { |
0e6e8724 | 1163 | $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}"; |
100fef9d | 1164 | $className::endPostProcess($this, $params, $activity); |
6a488035 TO |
1165 | } |
1166 | } | |
96025800 | 1167 | |
6a488035 | 1168 | } |