3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class generates form components for Activity
40 class CRM_Activity_Form_Activity
extends CRM_Contact_Form_Task
{
43 * The id of the object being edited / created
50 * Store activity ids when multiple activities are created
54 public $_activityIds = array();
57 * The id of activity type
61 public $_activityTypeId;
64 * The name of activity type
68 public $_activityTypeName;
71 * The id of currently viewed contact
75 public $_currentlyViewedContactId;
78 * The id of source contact and target contact
82 protected $_sourceContactId;
83 protected $_targetContactId;
84 protected $_asigneeContactId;
91 public $_activityTypeFile;
94 * The id of the logged in user, used when add / edit
98 public $_currentUserId;
101 * The array of form field attributes
108 * The the directory inside CRM, to include activity type file from
112 protected $_crmDir = 'Activity';
119 protected $_isSurveyActivity;
121 protected $_values = array();
123 protected $unsavedWarn = TRUE;
126 * The _fields var can be used by sub class to set/unset/edit the
127 * form fields based on their requirement
130 function setFields() {
131 $this->_fields
= array(
134 'label' => ts('Subject'),
135 'attributes' => CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity',
141 'label' => ts('Duration'),
142 'attributes' => array('size' => 4, 'maxlength' => 8),
147 'label' => ts('Location'),
148 'attributes' => CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity', 'location'),
153 'label' => ts('Details'),
154 // forces a smaller edit window
155 'attributes' => array('rows' => 4, 'cols' => 60),
158 'status_id' => array(
162 'priority_id' => array(
166 'source_contact_id' => array(
167 'type' => 'entityRef',
168 'label' => ts('Added By'),
171 'target_contact_id' => array(
172 'type' => 'entityRef',
173 'label' => ts('With Contact'),
174 'attributes' => array('multiple' => TRUE, 'create' => TRUE)
176 'assignee_contact_id' => array(
177 'type' => 'entityRef',
178 'label' => ts('Assigned to'),
179 'attributes' => array('multiple' => TRUE, 'create' => TRUE, 'api' => array('params' => array('is_deceased' => 0))),
181 'followup_assignee_contact_id' => array(
182 'type' => 'entityRef',
183 'label' => ts('Assigned to'),
184 'attributes' => array('multiple' => TRUE, 'create' => TRUE, 'api' => array('params' => array('is_deceased' => 0))),
186 'followup_activity_type_id' => array(
188 'label' => ts('Followup Activity'),
189 'attributes' => array('' => '- ' . ts('select activity') . ' -') + CRM_Core_PseudoConstant
::ActivityType(FALSE),
190 'extra' => array('class' => 'crm-select2'),
192 // Add optional 'Subject' field for the Follow-up Activiity, CRM-4491
193 'followup_activity_subject' => array(
195 'label' => ts('Subject'),
196 'attributes' => CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity',
202 if (($this->_context
== 'standalone') &&
203 ($printPDF = CRM_Utils_Array
::key('Print PDF Letter', $this->_fields
['followup_activity_type_id']['attributes']))
205 unset($this->_fields
['followup_activity_type_id']['attributes'][$printPDF]);
210 * Build the form object
215 function preProcess() {
216 $this->_cdType
= CRM_Utils_Array
::value('type', $_GET);
217 $this->assign('cdType', FALSE);
218 if ($this->_cdType
) {
219 $this->assign('cdType', TRUE);
220 return CRM_Custom_Form_CustomData
::preProcess($this);
222 CRM_Core_Form_RecurringEntity
::preProcess('civicrm_activity');
223 $this->_atypefile
= CRM_Utils_Array
::value('atypefile', $_GET);
224 $this->assign('atypefile', FALSE);
225 if ($this->_atypefile
) {
226 $this->assign('atypefile', TRUE);
229 $session = CRM_Core_Session
::singleton();
230 $this->_currentUserId
= $session->get('userID');
232 $this->_currentlyViewedContactId
= $this->get('contactId');
233 if (!$this->_currentlyViewedContactId
) {
234 $this->_currentlyViewedContactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
236 $this->assign('contactId', $this->_currentlyViewedContactId
);
239 if (!isset($this->_context
)) {
240 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this);
241 if (CRM_Contact_Form_Search
::isSearchContext($this->_context
)) {
242 $this->_context
= 'search';
244 elseif (!in_array($this->_context
, array('dashlet', 'dashletFullscreen'))
245 && $this->_currentlyViewedContactId
247 $this->_context
= 'activity';
249 $this->_compContext
= CRM_Utils_Request
::retrieve('compContext', 'String', $this);
252 $this->assign('context', $this->_context
);
254 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this);
256 if ($this->_action
& CRM_Core_Action
::DELETE
) {
257 if (!CRM_Core_Permission
::check('delete activities')) {
258 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
263 //when we come from contact search, activity id never comes.
264 //so don't try to get from object, it might gives you wrong one.
266 // if we're not adding new one, there must be an id to
267 // an activity we're trying to work on.
268 if ($this->_action
!= CRM_Core_Action
::ADD
&&
269 get_class($this->controller
) != 'CRM_Contact_Controller_Search'
271 $this->_activityId
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
274 $this->_activityTypeId
= CRM_Utils_Request
::retrieve('atype', 'Positive', $this);
275 $this->assign('atype', $this->_activityTypeId
);
277 $this->assign('activityId', $this->_activityId
);
279 //check for required permissions, CRM-6264
280 if ($this->_activityId
&&
281 in_array($this->_action
, array(
282 CRM_Core_Action
::UPDATE
,
283 CRM_Core_Action
::VIEW
285 !CRM_Activity_BAO_Activity
::checkPermission($this->_activityId
, $this->_action
)
287 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
289 if (($this->_action
& CRM_Core_Action
::VIEW
) &&
290 CRM_Activity_BAO_Activity
::checkPermission($this->_activityId
, CRM_Core_Action
::UPDATE
)
292 $this->assign('permission', 'edit');
295 if (!$this->_activityTypeId
&& $this->_activityId
) {
296 $this->_activityTypeId
= CRM_Core_DAO
::getFieldValue('CRM_Activity_DAO_Activity',
302 //Assigning Activity type name
303 if ($this->_activityTypeId
) {
304 $activityTName = CRM_Core_OptionGroup
::values('activity_type', FALSE, FALSE, FALSE, 'AND v.value = ' . $this->_activityTypeId
, 'name');
305 if ($activityTName[$this->_activityTypeId
]) {
306 $this->_activityTypeName
= $activityTName[$this->_activityTypeId
];
307 $this->assign('activityTName', $activityTName[$this->_activityTypeId
]);
312 if (isset($activityTName)) {
313 $activityName = CRM_Utils_Array
::value($this->_activityTypeId
, $activityTName);
314 $this->assign('pageTitle', ts('%1 Activity', array( 1 => $activityName)));
316 if ($this->_currentlyViewedContactId
) {
317 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_currentlyViewedContactId
);
318 // Check if this is default domain contact CRM-10482
319 if (CRM_Contact_BAO_Contact
::checkDomainContact($this->_currentlyViewedContactId
)) {
320 $displayName .= ' (' . ts('default organization') . ')';
322 CRM_Utils_System
::setTitle($displayName . ' - ' . $activityName);
325 CRM_Utils_System
::setTitle(ts('%1 Activity', array( 1 => $activityName)));
329 //check the mode when this form is called either single or as
331 if ($this->_activityTypeId ||
332 $this->_context
== 'standalone' ||
333 $this->_currentlyViewedContactId
335 $this->_single
= TRUE;
336 $this->assign('urlPath', 'civicrm/activity');
339 //set the appropriate action
340 $url = CRM_Utils_System
::currentPath();
341 $urlArray = explode('/', $url);
342 $searchPath = array_pop($urlArray);
343 $searchType = 'basic';
344 $this->_action
= CRM_Core_Action
::BASIC
;
345 switch ($searchPath) {
347 $searchType = $searchPath;
348 $this->_action
= CRM_Core_Action
::BASIC
;
352 $searchType = $searchPath;
353 $this->_action
= CRM_Core_Action
::ADVANCED
;
357 $searchType = $searchPath;
358 $this->_action
= CRM_Core_Action
::PROFILE
;
362 $this->_action
= CRM_Core_Action
::COPY
;
363 $searchType = $searchPath;
367 parent
::preProcess();
368 $this->_single
= FALSE;
370 $this->assign('urlPath', "civicrm/contact/search/$searchType");
371 $this->assign('urlPathVar', "_qf_Activity_display=true&qfKey={$this->controller->_key}");
374 $this->assign('single', $this->_single
);
375 $this->assign('action', $this->_action
);
377 if ($this->_action
& CRM_Core_Action
::VIEW
) {
378 // get the tree of custom fields
379 $this->_groupTree
= &CRM_Core_BAO_CustomGroup
::getTree('Activity', $this,
380 $this->_activityId
, 0, $this->_activityTypeId
384 if ($this->_activityTypeId
) {
385 //set activity type name and description to template
386 list($this->_activityTypeName
, $activityTypeDescription) = CRM_Core_BAO_OptionValue
::getActivityTypeDetails($this->_activityTypeId
);
387 $this->assign('activityTypeName', $this->_activityTypeName
);
388 $this->assign('activityTypeDescription', $activityTypeDescription);
392 $urlParams = $urlString = NULL;
393 $qfKey = CRM_Utils_Request
::retrieve('key', 'String', $this);
395 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $this);
399 if (!CRM_Utils_Rule
::qfKey($qfKey)) {
403 if ($this->_context
== 'fulltext') {
405 $urlParams = 'force=1';
406 $urlString = 'civicrm/contact/search/custom';
407 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
409 $urlParams .= '&context=fulltext&action=view';
410 $urlString = 'civicrm/contact/view/activity';
413 $urlParams .= "$keyName=$qfKey";
415 $this->assign('searchKey', $qfKey);
417 elseif (in_array($this->_context
, array(
424 $urlParams = 'reset=1';
425 $urlString = 'civicrm/dashboard';
427 elseif ($this->_context
== 'search') {
428 $urlParams = 'force=1';
430 $urlParams .= "&qfKey=$qfKey";
432 $path = CRM_Utils_System
::currentPath();
433 if ($this->_compContext
== 'advanced' ) {
434 $urlString = 'civicrm/contact/search/advanced';
436 elseif ($path == 'civicrm/group/search'
437 ||
$path == 'civicrm/contact/search'
438 ||
$path == 'civicrm/contact/search/advanced'
439 ||
$path == 'civicrm/contact/search/custom'
440 ||
$path == 'civicrm/group/search') {
444 $urlString = 'civicrm/activity/search';
446 $this->assign('searchKey', $qfKey);
448 elseif ($this->_context
!= 'caseActivity') {
449 $urlParams = "action=browse&reset=1&cid={$this->_currentlyViewedContactId}&selectedChild=activity";
450 $urlString = 'civicrm/contact/view';
454 $session->pushUserContext(CRM_Utils_System
::url($urlString, $urlParams));
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);
462 // when custom data is included in this page
463 if (!empty($_POST['hidden_custom'])) {
464 // we need to set it in the session for the below code to work
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);
475 // add attachments part
476 CRM_Core_BAO_File
::buildAttachment($this, 'civicrm_activity', $this->_activityId
, NULL, TRUE);
478 // figure out the file name for activity type, if any
479 if ($this->_activityTypeId
&&
480 $this->_activityTypeFile
=
481 CRM_Activity_BAO_Activity
::getFileForActivityTypeId($this->_activityTypeId
, $this->_crmDir
)
483 $this->assign('activityTypeFile', $this->_activityTypeFile
);
484 $this->assign('crmDir', $this->_crmDir
);
489 if ($this->_activityTypeFile
) {
490 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
491 $className::preProcess($this);
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
);
501 $this->set('values', $this->_values
);
504 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
505 CRM_Core_Form_RecurringEntity
::preProcess('civicrm_activity');
510 * Set default values for the form. For edit/view mode
511 * the default values are retrieved from the database
517 function setDefaultValues() {
518 if ($this->_cdType
) {
519 return CRM_Custom_Form_CustomData
::setDefaultValues($this);
522 $defaults = $this->_values
;
523 // if we're editing...
524 if (isset($this->_activityId
)) {
525 if (empty($defaults['activity_date_time'])) {
526 list($defaults['activity_date_time'], $defaults['activity_date_time_time']) = CRM_Utils_Date
::setDateDefaults(NULL, 'activityDateTime');
528 elseif ($this->_action
& CRM_Core_Action
::UPDATE
) {
529 $this->assign('current_activity_date_time', $defaults['activity_date_time']);
530 list($defaults['activity_date_time'],
531 $defaults['activity_date_time_time']
532 ) = CRM_Utils_Date
::setDateDefaults($defaults['activity_date_time'], 'activityDateTime');
533 list($defaults['repetition_start_date'], $defaults['repetition_start_date_time']) = CRM_Utils_Date
::setDateDefaults($defaults['activity_date_time'], 'activityDateTime');
534 $recurringEntityDefaults = array();
535 $recurringEntityDefaults = CRM_Core_Form_RecurringEntity
::setDefaultValues();
536 $defaults = array_merge($defaults, $recurringEntityDefaults);
539 if ($this->_context
!= 'standalone') {
540 $this->assign('target_contact_value',
541 CRM_Utils_Array
::value('target_contact_value', $defaults)
543 $this->assign('assignee_contact_value',
544 CRM_Utils_Array
::value('assignee_contact_value', $defaults)
548 // Fixme: why are we getting the wrong keys from upstream?
549 $defaults['target_contact_id'] = CRM_Utils_Array
::value('target_contact', $defaults);
550 $defaults['assignee_contact_id'] = CRM_Utils_Array
::value('assignee_contact', $defaults);
552 // set default tags if exists
553 $defaults['tag'] = CRM_Core_BAO_EntityTag
::getTag($this->_activityId
, 'civicrm_activity');
556 // if it's a new activity, we need to set default values for associated contact fields
557 $this->_sourceContactId
= $this->_currentUserId
;
558 $this->_targetContactId
= $this->_currentlyViewedContactId
;
560 $defaults['source_contact_id'] = $this->_sourceContactId
;
561 $defaults['target_contact_id'] = $this->_targetContactId
;
563 list($defaults['activity_date_time'], $defaults['activity_date_time_time']) =
564 CRM_Utils_Date
::setDateDefaults(NULL, 'activityDateTime');
567 if ($this->_activityTypeId
) {
568 $defaults['activity_type_id'] = $this->_activityTypeId
;
571 if (!$this->_single
&& !empty($this->_contactIds
)) {
572 $defaults['target_contact_id'] = $this->_contactIds
;
575 // CRM-15472 - 50 is around the practial limit of how many items a select2 entityRef can handle
576 if (!empty($defaults['target_contact_id'])) {
577 $count = count(is_array($defaults['target_contact_id']) ?
$defaults['target_contact_id'] : explode(',', $defaults['target_contact_id']));
579 $this->freeze(array('target_contact_id'));
583 if ($this->_action
& (CRM_Core_Action
::DELETE | CRM_Core_Action
::RENEW
)) {
584 $this->assign('delName', CRM_Utils_Array
::value('subject', $defaults));
587 if ($this->_activityTypeFile
) {
588 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
589 $defaults +
= $className::setDefaultValues($this);
591 if (empty($defaults['priority_id'])) {
592 $priority = CRM_Core_PseudoConstant
::get('CRM_Activity_DAO_Activity', 'priority_id');
593 $defaults['priority_id'] = array_search('Normal', $priority);
595 if (empty($defaults['status_id'])) {
596 $defaults['status_id'] = CRM_Core_OptionGroup
::getDefaultValue('activity_status');
601 public function buildQuickForm() {
602 if ($this->_action
& (CRM_Core_Action
::DELETE | CRM_Core_Action
::RENEW
)) {
603 //enable form element (ActivityLinks sets this true)
604 $this->assign('suppressForm', FALSE);
606 $button = ts('Delete');
607 if ($this->_action
& CRM_Core_Action
::RENEW
) {
608 $button = ts('Restore');
610 $this->addButtons(array(
614 'spacing' => ' ',
619 'name' => ts('Cancel')
625 if ($this->_cdType
) {
626 return CRM_Custom_Form_CustomData
::buildQuickForm($this);
629 //build other activity links
630 CRM_Activity_Form_ActivityLinks
::commonBuildQuickForm($this);
632 //enable form element (ActivityLinks sets this true)
633 $this->assign('suppressForm', FALSE);
635 $element = & $this->add('select', 'activity_type_id', ts('Activity Type'),
636 $this->_fields
['followup_activity_type_id']['attributes'],
638 'onchange' => "CRM.buildCustomData( 'Activity', this.value );",
639 'class' => 'crm-select2',
643 //freeze for update mode.
644 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
648 //Call to RecurringEntity buildQuickForm for add/update mode
649 if ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD
)) {
650 CRM_Core_Form_RecurringEntity
::buildQuickForm($this);
653 foreach ($this->_fields
as $field => $values) {
654 if (!empty($this->_fields
[$field])) {
655 $attribute = CRM_Utils_Array
::value('attributes', $values);
656 $required = !empty($values['required']);
658 if ($values['type'] == 'wysiwyg') {
659 $this->addWysiwyg($field, $values['label'], $attribute, $required);
661 elseif ($values['type'] == 'select' && empty($attribute)) {
662 $this->addSelect($field, array('entity' => 'activity'), $required);
664 elseif ($values['type'] == 'entityRef') {
665 $this->addEntityRef($field, $values['label'], $attribute, $required);
668 $this->add($values['type'], $field, $values['label'], $attribute, $required, CRM_Utils_Array
::value('extra', $values));
673 //CRM-7362 --add campaigns.
674 CRM_Campaign_BAO_Campaign
::addCampaign($this, CRM_Utils_Array
::value('campaign_id', $this->_values
));
676 //add engagement level CRM-7775
677 $buildEngagementLevel = FALSE;
678 if (CRM_Campaign_BAO_Campaign
::isCampaignEnable() &&
679 CRM_Campaign_BAO_Campaign
::accessCampaign()
681 $buildEngagementLevel = TRUE;
682 $this->addSelect('engagement_level',array('entity' => 'activity'));
683 $this->addRule('engagement_level',
684 ts('Please enter the engagement index as a number (integers only).'),
688 $this->assign('buildEngagementLevel', $buildEngagementLevel);
690 // check for survey activity
691 $this->_isSurveyActivity
= FALSE;
693 if ($this->_activityId
&& CRM_Campaign_BAO_Campaign
::isCampaignEnable() &&
694 CRM_Campaign_BAO_Campaign
::accessCampaign()
697 $this->_isSurveyActivity
= CRM_Campaign_BAO_Survey
::isSurveyActivity($this->_activityId
);
698 if ($this->_isSurveyActivity
) {
699 $surveyId = CRM_Core_DAO
::getFieldValue('CRM_Activity_DAO_Activity',
703 $responseOptions = CRM_Campaign_BAO_Survey
::getResponsesOptions($surveyId);
704 if ($responseOptions) {
705 $this->add('select', 'result', ts('Result'),
706 array('' => ts('- select -')) +
array_combine($responseOptions, $responseOptions)
711 $surveyTitle = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'title');
713 $this->assign('surveyTitle', $surveyTitle);
716 $this->assign('surveyActivity', $this->_isSurveyActivity
);
718 // this option should be available only during add mode
719 if ($this->_action
!= CRM_Core_Action
::UPDATE
) {
720 $this->add('advcheckbox', 'is_multi_activity', ts('Create a separate activity for each contact.'));
723 $this->addRule('duration',
724 ts('Please enter the duration as number of minutes (integers only).'), 'positiveInteger'
726 $this->addDateTime('activity_date_time', ts('Date'), TRUE, array('formatType' => 'activityDateTime'));
729 $this->addDateTime('followup_date', ts('in'), FALSE, array('formatType' => 'activityDateTime'));
731 // Only admins and case-workers can change the activity source
732 if (!CRM_Core_Permission
::check('administer CiviCRM') && $this->_context
!= 'caseActivity') {
733 $this->getElement('source_contact_id')->freeze();
736 //need to assign custom data type and subtype to the template
737 $this->assign('customDataType', 'Activity');
738 $this->assign('customDataSubType', $this->_activityTypeId
);
739 $this->assign('entityID', $this->_activityId
);
741 $tags = CRM_Core_BAO_Tag
::getTags('civicrm_activity');
744 $this->add('select', 'tag', ts('Tags'), $tags, FALSE,
745 array('id' => 'tags', 'multiple' => 'multiple', 'class' => 'crm-select2 huge')
749 // we need to hide activity tagset for special activities
750 $specialActivities = array('Open Case');
752 if (!in_array($this->_activityTypeName
, $specialActivities)) {
754 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_activity');
755 CRM_Core_Form_Tag
::buildQuickForm($this, $parentNames, 'civicrm_activity', $this->_activityId
);
758 // if we're viewing, we're assigning different buttons than for adding/editing
759 if ($this->_action
& CRM_Core_Action
::VIEW
) {
760 if (isset($this->_groupTree
)) {
761 CRM_Core_BAO_CustomGroup
::buildCustomDataView($this, $this->_groupTree
);
763 // form should be frozen for view mode
771 $this->addButtons($buttons);
775 '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.'),
776 '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.'),
778 $js = array('onclick' => "return activityStatus(" . json_encode($message) . ");");
779 $this->addButtons(array(
782 'name' => ts('Save'),
788 'name' => ts('Cancel')
794 if ($this->_activityTypeFile
) {
795 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
797 $className::buildQuickForm($this);
798 $this->addFormRule(array($className, 'formRule'), $this);
801 $this->addFormRule(array('CRM_Activity_Form_Activity', 'formRule'), $this);
803 if (CRM_Core_BAO_Setting
::getItem(
804 CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
805 'activity_assignee_notification'
808 $this->assign('activityAssigneeNotification', TRUE);
811 $this->assign('activityAssigneeNotification', FALSE);
818 * @param array $fields the input form values
819 * @param array $files the uploaded files if any
822 * @return true if no errors, else array of errors
826 static function formRule($fields, $files, $self) {
827 // skip form rule if deleting
828 if (CRM_Utils_Array
::value('_qf_Activity_next_', $fields) == 'Delete') {
832 if (!$self->_single
&& !$fields['activity_type_id']) {
833 $errors['activity_type_id'] = ts('Activity Type is a required field');
836 //Activity type is mandatory if creating new activity, CRM-4515
837 if (array_key_exists('activity_type_id', $fields) && empty($fields['activity_type_id'])) {
838 $errors['activity_type_id'] = ts('Activity Type is required field.');
841 if (CRM_Utils_Array
::value('activity_type_id', $fields) == 3 &&
842 CRM_Utils_Array
::value('status_id', $fields) == 1
844 $errors['status_id'] = ts('You cannot record scheduled email activity.');
846 elseif (CRM_Utils_Array
::value('activity_type_id', $fields) == 4 &&
847 CRM_Utils_Array
::value('status_id', $fields) == 1
849 $errors['status_id'] = ts('You cannot record scheduled SMS activity.');
852 if (!empty($fields['followup_activity_type_id']) && empty($fields['followup_date'])) {
853 $errors['followup_date_time'] = ts('Followup date is a required field.');
855 //Activity type is mandatory if subject or follow-up date is specified for an Follow-up activity, CRM-4515
856 if ((!empty($fields['followup_activity_subject']) ||
!empty($fields['followup_date'])) && empty($fields['followup_activity_type_id'])) {
857 $errors['followup_activity_subject'] = ts('Follow-up Activity type is a required field.');
863 * Process the form submission
867 * @param array $params
870 public function postProcess($params = NULL) {
871 if ($this->_action
& CRM_Core_Action
::DELETE
) {
872 $deleteParams = array('id' => $this->_activityId
);
873 $moveToTrash = CRM_Case_BAO_Case
::isCaseActivity($this->_activityId
);
874 CRM_Activity_BAO_Activity
::deleteActivity($deleteParams, $moveToTrash);
876 // delete tags for the entity
878 'entity_table' => 'civicrm_activity',
879 'entity_id' => $this->_activityId
882 CRM_Core_BAO_EntityTag
::del($tagParams);
884 CRM_Core_Session
::setStatus(ts("Selected Activity has been deleted successfully."), ts('Record Deleted'), 'success');
888 // store the submitted values in an array
890 $params = $this->controller
->exportValues($this->_name
);
893 //set activity type id
894 if (empty($params['activity_type_id'])) {
895 $params['activity_type_id'] = $this->_activityTypeId
;
898 if (!empty($params['hidden_custom']) &&
899 !isset($params['custom'])
901 $customFields = CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
902 $this->_activityTypeId
904 $customFields = CRM_Utils_Array
::crmArrayMerge($customFields,
905 CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
909 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
916 // store the date with proper format
917 $params['activity_date_time'] = CRM_Utils_Date
::processDate($params['activity_date_time'], $params['activity_date_time_time']);
919 // format params as arrays
920 foreach (array('target', 'assignee', 'followup_assignee') as $name) {
921 if (!empty($params["{$name}_contact_id"])) {
922 $params["{$name}_contact_id"] = explode(',', $params["{$name}_contact_id"]);
925 $params["{$name}_contact_id"] = array();
929 // get ids for associated contacts
930 if (!$params['source_contact_id']) {
931 $params['source_contact_id'] = $this->_currentUserId
;
934 if (isset($this->_activityId
)) {
935 $params['id'] = $this->_activityId
;
938 // add attachments as needed
939 CRM_Core_BAO_File
::formatAttachment($params,
946 if (!empty($params['is_multi_activity']) &&
947 !CRM_Utils_Array
::crmIsEmptyArray($params['target_contact_id'])
949 $targetContacts = $params['target_contact_id'];
950 foreach ($targetContacts as $targetContactId) {
951 $params['target_contact_id'] = array($targetContactId);
953 $activity[] = $this->processActivity($params);
958 $activity = $this->processActivity($params);
961 //Set for repeat configuration in create mode
962 $params['entity_id'] = $this->_activityId
;
963 $params['entity_table'] = 'civicrm_activity';
964 $scheduleReminderDetails = array();
965 if (!empty($params['entity_id']) && !empty($params['entity_table'])) {
966 $checkParentExistsForThisId = CRM_Core_BAO_RecurringEntity
::getParentFor($params['entity_id'], $params['entity_table']);
967 if ($checkParentExistsForThisId) {
968 $params['parent_entity_id'] = $checkParentExistsForThisId;
969 $scheduleReminderDetails = CRM_Core_BAO_RecurringEntity
::getReminderDetailsByEntityId($checkParentExistsForThisId, $params['entity_table']);
972 $params['parent_entity_id'] = $params['entity_id'];
973 $scheduleReminderDetails = CRM_Core_BAO_RecurringEntity
::getReminderDetailsByEntityId($params['entity_id'], $params['entity_table']);
975 if (property_exists($scheduleReminderDetails, 'id')) {
976 $params['schedule_reminder_id'] = $scheduleReminderDetails->id
;
979 $params['dateColumns'] = array('activity_date_time');
982 unset($params['id']);
983 $linkedEntities = array(
985 'table' => 'civicrm_activity_contact',
986 'findCriteria' => array(
987 'activity_id' => $this->_activityId
,
989 'linkedColumns' => array('activity_id'),
990 'isRecurringEntityRecord' => FALSE,
993 CRM_Core_Form_RecurringEntity
::postProcess($params, 'civicrm_activity', $linkedEntities);
995 return array('activity' => $activity);
999 * Process activity creation
1001 * @param array $params associated array of submitted values
1003 * @return $this|null|object
1006 protected function processActivity(&$params) {
1007 $activityAssigned = array();
1008 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
1009 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
1010 // format assignee params
1011 if (!CRM_Utils_Array
::crmIsEmptyArray($params['assignee_contact_id'])) {
1012 //skip those assignee contacts which are already assigned
1013 //while sending a copy.CRM-4509.
1014 $activityAssigned = array_flip($params['assignee_contact_id']);
1015 if ($this->_activityId
) {
1016 $assigneeContacts = CRM_Activity_BAO_ActivityContact
::getNames($this->_activityId
, $assigneeID);
1017 $activityAssigned = array_diff_key($activityAssigned, $assigneeContacts);
1021 // call begin post process. Idea is to let injecting file do
1022 // any processing before the activity is added/updated.
1023 $this->beginPostProcess($params);
1025 $activity = CRM_Activity_BAO_Activity
::create($params);
1027 // add tags if exists
1028 $tagParams = array();
1029 if (!empty($params['tag'])) {
1030 foreach ($params['tag'] as $tag) {
1031 $tagParams[$tag] = 1;
1036 CRM_Core_BAO_EntityTag
::create($tagParams, 'civicrm_activity', $activity->id
);
1039 if (isset($params['activity_taglist']) && !empty($params['activity_taglist'])) {
1040 CRM_Core_Form_Tag
::postProcess($params['activity_taglist'], $activity->id
, 'civicrm_activity', $this);
1043 // call end post process. Idea is to let injecting file do any
1044 // processing needed, after the activity has been added/updated.
1045 $this->endPostProcess($params, $activity);
1048 if (!empty($params['is_multi_activity'])) {
1049 $this->_activityIds
[] = $activity->id
;
1052 $this->_activityId
= $activity->id
;
1055 // create follow up activity if needed
1056 $followupStatus = '';
1057 $followupActivity = NULL;
1058 if (!empty($params['followup_activity_type_id'])) {
1059 $followupActivity = CRM_Activity_BAO_Activity
::createFollowupActivity($activity->id
, $params);
1060 $followupStatus = ts('A followup activity has been scheduled.');
1063 // send copy to assignee contacts.CRM-4509
1066 if (CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
1067 'activity_assignee_notification')) {
1068 $activityIDs = array($activity->id
);
1069 if ($followupActivity) {
1070 $activityIDs = array_merge($activityIDs, array($followupActivity->id
));
1072 $assigneeContacts = CRM_Activity_BAO_ActivityAssignment
::getAssigneeNames($activityIDs, TRUE, FALSE);
1074 if (!CRM_Utils_Array
::crmIsEmptyArray($params['assignee_contact_id'])) {
1075 $mailToContacts = array();
1077 //build an associative array with unique email addresses.
1078 foreach ($activityAssigned as $id => $dnc) {
1079 if (isset($id) && array_key_exists($id, $assigneeContacts)) {
1080 $mailToContacts[$assigneeContacts[$id]['email']] = $assigneeContacts[$id];
1084 if (!CRM_Utils_array
::crmIsEmptyArray($mailToContacts)) {
1085 //include attachments while sending a copy of activity.
1086 $attachments = CRM_Core_BAO_File
::getEntityFile('civicrm_activity', $activity->id
);
1088 $ics = new CRM_Activity_BAO_ICalendar($activity);
1089 $ics->addAttachment($attachments, $mailToContacts);
1091 // CRM-8400 add param with _currentlyViewedContactId for URL link in mail
1092 CRM_Case_BAO_Case
::sendActivityCopy(NULL, $activity->id
, $mailToContacts, $attachments, NULL);
1096 $mailStatus .= ts("A copy of the activity has also been sent to assignee contacts(s).");
1100 // Also send email to follow-up activity assignees if set
1101 if ($followupActivity) {
1102 $mailToFollowupContacts = array();
1103 foreach ($assigneeContacts as $values) {
1104 if ($values['activity_id'] == $followupActivity->id
) {
1105 $mailToFollowupContacts[$values['email']] = $values;
1109 if (!CRM_Utils_array
::crmIsEmptyArray($mailToFollowupContacts)) {
1110 $ics = new CRM_Activity_BAO_ICalendar($followupActivity);
1111 $attachments = CRM_Core_BAO_File
::getEntityFile('civicrm_activity', $followupActivity->id
);
1112 $ics->addAttachment($attachments, $mailToFollowupContacts);
1114 CRM_Case_BAO_Case
::sendActivityCopy(NULL, $followupActivity->id
, $mailToFollowupContacts, $attachments, NULL);
1118 $mailStatus .= '<br />' . ts("A copy of the follow-up activity has also been sent to follow-up assignee contacts(s).");
1123 // set status message
1125 if (!empty($params['subject'])) {
1126 $subject = "'" . $params['subject'] . "'";
1129 CRM_Core_Session
::setStatus(ts('Activity %1 has been saved. %2 %3',
1132 2 => $followupStatus,
1135 ), ts('Saved'), 'success');
1141 * Shorthand for getting id by display name (makes code more readable)
1145 protected function _getIdByDisplayName($displayName) {
1146 return CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
1154 * Shorthand for getting display name by id (makes code more readable)
1158 protected function _getDisplayNameById($id) {
1159 return CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
1167 * Let injecting activity type file do any processing
1168 * needed, before the activity is added/updated
1170 * @param array $params
1172 function beginPostProcess(&$params) {
1173 if ($this->_activityTypeFile
) {
1174 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
1175 $className::beginPostProcess($this, $params);
1180 * Let injecting activity type file do any processing
1181 * needed, after the activity has been added/updated
1183 * @param array $params
1186 function endPostProcess(&$params, &$activity) {
1187 if ($this->_activityTypeFile
) {
1188 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
1189 $className::endPostProcess($this, $params, $activity);