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