3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
35 * This class generates form components for Activity.
37 class CRM_Activity_Form_Activity
extends CRM_Contact_Form_Task
{
40 * The id of the object being edited / created
47 * Store activity ids when multiple activities are created.
51 public $_activityIds = array();
54 * The id of activity type.
58 public $_activityTypeId;
61 * The name of activity type.
65 public $_activityTypeName;
68 * The id of currently viewed contact.
72 public $_currentlyViewedContactId;
75 * The id of source contact and target contact.
79 protected $_sourceContactId;
80 protected $_targetContactId;
81 protected $_asigneeContactId;
88 public $_activityTypeFile;
91 * The id of the logged in user, used when add / edit
95 public $_currentUserId;
98 * The array of form field attributes.
105 * The the directory inside CRM, to include activity type file from
109 protected $_crmDir = 'Activity';
116 protected $_isSurveyActivity;
118 protected $_values = array();
120 protected $unsavedWarn = TRUE;
123 * Explicitly declare the entity api name.
125 public function getDefaultEntity() {
130 * The _fields var can be used by sub class to set/unset/edit the
131 * form fields based on their requirement
133 public function setFields() {
134 $this->_fields
= array(
137 'label' => ts('Subject'),
138 'attributes' => CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity',
144 'label' => ts('Duration'),
145 'attributes' => array('size' => 4, 'maxlength' => 8),
150 'label' => ts('Location'),
151 'attributes' => CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity', 'location'),
156 'label' => ts('Details'),
157 'attributes' => array('class' => 'huge'),
160 'status_id' => array(
164 'priority_id' => array(
168 'source_contact_id' => array(
169 'type' => 'entityRef',
170 'label' => ts('Added By'),
173 'target_contact_id' => array(
174 'type' => 'entityRef',
175 'label' => ts('With Contact'),
176 'attributes' => array('multiple' => TRUE, 'create' => TRUE),
178 'assignee_contact_id' => array(
179 'type' => 'entityRef',
180 'label' => ts('Assigned to'),
181 'attributes' => array(
184 'api' => array('params' => array('is_deceased' => 0)),
187 'followup_assignee_contact_id' => array(
188 'type' => 'entityRef',
189 'label' => ts('Assigned to'),
190 'attributes' => array(
193 'api' => array('params' => array('is_deceased' => 0)),
196 'followup_activity_type_id' => array(
198 'label' => ts('Followup Activity'),
199 'attributes' => array('' => '- ' . ts('select activity') . ' -') + CRM_Core_PseudoConstant
::ActivityType(FALSE),
200 'extra' => array('class' => 'crm-select2'),
202 // Add optional 'Subject' field for the Follow-up Activiity, CRM-4491
203 'followup_activity_subject' => array(
205 'label' => ts('Subject'),
206 'attributes' => CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity',
212 if (($this->_context
== 'standalone') &&
213 ($printPDF = CRM_Utils_Array
::key('Print PDF Letter', $this->_fields
['followup_activity_type_id']['attributes']))
215 unset($this->_fields
['followup_activity_type_id']['attributes'][$printPDF]);
220 * Build the form object.
222 public function preProcess() {
223 CRM_Core_Form_RecurringEntity
::preProcess('civicrm_activity');
224 $this->_atypefile
= CRM_Utils_Array
::value('atypefile', $_GET);
225 $this->assign('atypefile', FALSE);
226 if ($this->_atypefile
) {
227 $this->assign('atypefile', TRUE);
230 $session = CRM_Core_Session
::singleton();
231 $this->_currentUserId
= $session->get('userID');
233 $this->_currentlyViewedContactId
= $this->get('contactId');
234 if (!$this->_currentlyViewedContactId
) {
235 $this->_currentlyViewedContactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
237 $this->assign('contactId', $this->_currentlyViewedContactId
);
240 if (!isset($this->_context
)) {
241 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this);
242 if (CRM_Contact_Form_Search
::isSearchContext($this->_context
)) {
243 $this->_context
= 'search';
245 elseif (!in_array($this->_context
, array('dashlet', 'dashletFullscreen'))
246 && $this->_currentlyViewedContactId
248 $this->_context
= 'activity';
250 $this->_compContext
= CRM_Utils_Request
::retrieve('compContext', 'String', $this);
253 $this->assign('context', $this->_context
);
255 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this);
257 if ($this->_action
& CRM_Core_Action
::DELETE
) {
258 if (!CRM_Core_Permission
::check('delete activities')) {
259 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
264 //when we come from contact search, activity id never comes.
265 //so don't try to get from object, it might gives you wrong one.
267 // if we're not adding new one, there must be an id to
268 // an activity we're trying to work on.
269 if ($this->_action
!= CRM_Core_Action
::ADD
&&
270 get_class($this->controller
) != 'CRM_Contact_Controller_Search'
272 $this->_activityId
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
275 $this->_activityTypeId
= CRM_Utils_Request
::retrieve('atype', 'Positive', $this);
276 $this->assign('atype', $this->_activityTypeId
);
278 $this->assign('activityId', $this->_activityId
);
280 //check for required permissions, CRM-6264
281 if ($this->_activityId
&&
282 in_array($this->_action
, array(
283 CRM_Core_Action
::UPDATE
,
284 CRM_Core_Action
::VIEW
,
286 !CRM_Activity_BAO_Activity
::checkPermission($this->_activityId
, $this->_action
)
288 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
290 if (($this->_action
& CRM_Core_Action
::VIEW
) &&
291 CRM_Activity_BAO_Activity
::checkPermission($this->_activityId
, CRM_Core_Action
::UPDATE
)
293 $this->assign('permission', 'edit');
296 if (!$this->_activityTypeId
&& $this->_activityId
) {
297 $this->_activityTypeId
= CRM_Core_DAO
::getFieldValue('CRM_Activity_DAO_Activity',
303 //Assigning Activity type name
304 if ($this->_activityTypeId
) {
305 $activityTName = CRM_Core_OptionGroup
::values('activity_type', FALSE, FALSE, FALSE, 'AND v.value = ' . $this->_activityTypeId
, 'label');
306 if ($activityTName[$this->_activityTypeId
]) {
307 $this->_activityTypeName
= $activityTName[$this->_activityTypeId
];
308 $this->assign('activityTName', $activityTName[$this->_activityTypeId
]);
313 if (isset($activityTName)) {
314 $activityName = CRM_Utils_Array
::value($this->_activityTypeId
, $activityTName);
315 $this->assign('pageTitle', ts('%1 Activity', array(1 => $activityName)));
317 if ($this->_currentlyViewedContactId
) {
318 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_currentlyViewedContactId
);
319 // Check if this is default domain contact CRM-10482
320 if (CRM_Contact_BAO_Contact
::checkDomainContact($this->_currentlyViewedContactId
)) {
321 $displayName .= ' (' . ts('default organization') . ')';
323 CRM_Utils_System
::setTitle($displayName . ' - ' . $activityName);
326 CRM_Utils_System
::setTitle(ts('%1 Activity', array(1 => $activityName)));
330 //check the mode when this form is called either single or as
332 if ($this->_activityTypeId ||
333 $this->_context
== 'standalone' ||
334 $this->_currentlyViewedContactId
336 $this->_single
= TRUE;
337 $this->assign('urlPath', 'civicrm/activity');
340 //set the appropriate action
341 $url = CRM_Utils_System
::currentPath();
342 $urlArray = explode('/', $url);
343 $searchPath = array_pop($urlArray);
344 $searchType = 'basic';
345 $this->_action
= CRM_Core_Action
::BASIC
;
346 switch ($searchPath) {
348 $searchType = $searchPath;
349 $this->_action
= CRM_Core_Action
::BASIC
;
353 $searchType = $searchPath;
354 $this->_action
= CRM_Core_Action
::ADVANCED
;
358 $searchType = $searchPath;
359 $this->_action
= CRM_Core_Action
::PROFILE
;
363 $this->_action
= CRM_Core_Action
::COPY
;
364 $searchType = $searchPath;
368 parent
::preProcess();
369 $this->_single
= FALSE;
371 $this->assign('urlPath', "civicrm/contact/search/$searchType");
372 $this->assign('urlPathVar', "_qf_Activity_display=true&qfKey={$this->controller->_key}");
375 $this->assign('single', $this->_single
);
376 $this->assign('action', $this->_action
);
378 if ($this->_action
& CRM_Core_Action
::VIEW
) {
379 // get the tree of custom fields
380 $this->_groupTree
= &CRM_Core_BAO_CustomGroup
::getTree('Activity', $this,
381 $this->_activityId
, 0, $this->_activityTypeId
385 if ($this->_activityTypeId
) {
386 //set activity type name and description to template
387 list($this->_activityTypeName
, $activityTypeDescription) = CRM_Core_BAO_OptionValue
::getActivityTypeDetails($this->_activityTypeId
);
388 $this->assign('activityTypeName', $this->_activityTypeName
);
389 $this->assign('activityTypeDescription', $activityTypeDescription);
393 $urlParams = $urlString = NULL;
394 $qfKey = CRM_Utils_Request
::retrieve('key', 'String', $this);
396 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $this);
400 if (!CRM_Utils_Rule
::qfKey($qfKey)) {
404 if ($this->_context
== 'fulltext') {
406 $urlParams = 'force=1';
407 $urlString = 'civicrm/contact/search/custom';
408 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
410 $urlParams .= '&context=fulltext&action=view';
411 $urlString = 'civicrm/contact/view/activity';
414 $urlParams .= "$keyName=$qfKey";
416 $this->assign('searchKey', $qfKey);
418 elseif (in_array($this->_context
, array(
425 $urlParams = 'reset=1';
426 $urlString = 'civicrm/dashboard';
428 elseif ($this->_context
== 'search') {
429 $urlParams = 'force=1';
431 $urlParams .= "&qfKey=$qfKey";
433 $path = CRM_Utils_System
::currentPath();
434 if ($this->_compContext
== 'advanced') {
435 $urlString = 'civicrm/contact/search/advanced';
437 elseif ($path == 'civicrm/group/search'
438 ||
$path == 'civicrm/contact/search'
439 ||
$path == 'civicrm/contact/search/advanced'
440 ||
$path == 'civicrm/contact/search/custom'
441 ||
$path == 'civicrm/group/search'
446 $urlString = 'civicrm/activity/search';
448 $this->assign('searchKey', $qfKey);
450 elseif ($this->_context
!= 'caseActivity') {
451 $urlParams = "action=browse&reset=1&cid={$this->_currentlyViewedContactId}&selectedChild=activity";
452 $urlString = 'civicrm/contact/view';
456 $session->pushUserContext(CRM_Utils_System
::url($urlString, $urlParams));
459 // hack to retrieve activity type id from post variables
460 if (!$this->_activityTypeId
) {
461 $this->_activityTypeId
= CRM_Utils_Array
::value('activity_type_id', $_POST);
464 // when custom data is included in this page
465 if (!empty($_POST['hidden_custom'])) {
466 // we need to set it in the session for the below code to work
468 //need to assign custom data subtype to the template
469 $this->set('type', 'Activity');
470 $this->set('subType', $this->_activityTypeId
);
471 $this->set('entityId', $this->_activityId
);
472 CRM_Custom_Form_CustomData
::preProcess($this);
473 CRM_Custom_Form_CustomData
::buildQuickForm($this);
474 CRM_Custom_Form_CustomData
::setDefaultValues($this);
477 // add attachments part
478 CRM_Core_BAO_File
::buildAttachment($this, 'civicrm_activity', $this->_activityId
, NULL, TRUE);
480 // figure out the file name for activity type, if any
481 if ($this->_activityTypeId
&&
482 $this->_activityTypeFile
= CRM_Activity_BAO_Activity
::getFileForActivityTypeId($this->_activityTypeId
, $this->_crmDir
)
484 $this->assign('activityTypeFile', $this->_activityTypeFile
);
485 $this->assign('crmDir', $this->_crmDir
);
490 if ($this->_activityTypeFile
) {
491 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
492 $className::preProcess($this);
495 $this->_values
= $this->get('values');
496 if (!is_array($this->_values
)) {
497 $this->_values
= array();
498 if (isset($this->_activityId
) && $this->_activityId
) {
499 $params = array('id' => $this->_activityId
);
500 CRM_Activity_BAO_Activity
::retrieve($params, $this->_values
);
502 $this->set('values', $this->_values
);
505 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
506 CRM_Core_Form_RecurringEntity
::preProcess('civicrm_activity');
511 * Set default values for the form.
513 * For edit/view mode the default values are retrieved from the database.
515 public function setDefaultValues() {
517 $defaults = $this->_values + CRM_Core_Form_RecurringEntity
::setDefaultValues();
518 // if we're editing...
519 if (isset($this->_activityId
)) {
520 if (empty($defaults['activity_date_time'])) {
521 list($defaults['activity_date_time'], $defaults['activity_date_time_time']) = CRM_Utils_Date
::setDateDefaults(NULL, 'activityDateTime');
523 elseif ($this->_action
& CRM_Core_Action
::UPDATE
) {
524 $this->assign('current_activity_date_time', $defaults['activity_date_time']);
525 list($defaults['activity_date_time'],
526 $defaults['activity_date_time_time']
527 ) = CRM_Utils_Date
::setDateDefaults($defaults['activity_date_time'], 'activityDateTime');
528 list($defaults['repetition_start_date'], $defaults['repetition_start_date_time']) = CRM_Utils_Date
::setDateDefaults($defaults['activity_date_time'], 'activityDateTime');
531 if ($this->_context
!= 'standalone') {
532 $this->assign('target_contact_value',
533 CRM_Utils_Array
::value('target_contact_value', $defaults)
535 $this->assign('assignee_contact_value',
536 CRM_Utils_Array
::value('assignee_contact_value', $defaults)
540 // Fixme: why are we getting the wrong keys from upstream?
541 $defaults['target_contact_id'] = CRM_Utils_Array
::value('target_contact', $defaults);
542 $defaults['assignee_contact_id'] = CRM_Utils_Array
::value('assignee_contact', $defaults);
544 // set default tags if exists
545 $defaults['tag'] = CRM_Core_BAO_EntityTag
::getTag($this->_activityId
, 'civicrm_activity');
548 // if it's a new activity, we need to set default values for associated contact fields
549 $this->_sourceContactId
= $this->_currentUserId
;
550 $this->_targetContactId
= $this->_currentlyViewedContactId
;
552 $defaults['source_contact_id'] = $this->_sourceContactId
;
553 $defaults['target_contact_id'] = $this->_targetContactId
;
555 list($defaults['activity_date_time'], $defaults['activity_date_time_time'])
556 = CRM_Utils_Date
::setDateDefaults(NULL, 'activityDateTime');
559 if ($this->_activityTypeId
) {
560 $defaults['activity_type_id'] = $this->_activityTypeId
;
563 if (!$this->_single
&& !empty($this->_contactIds
)) {
564 $defaults['target_contact_id'] = $this->_contactIds
;
567 // CRM-15472 - 50 is around the practical limit of how many items a select2 entityRef can handle
568 if (!empty($defaults['target_contact_id'])) {
569 $count = count(is_array($defaults['target_contact_id']) ?
$defaults['target_contact_id'] : explode(',', $defaults['target_contact_id']));
571 $this->freeze(array('target_contact_id'));
575 if ($this->_action
& (CRM_Core_Action
::DELETE | CRM_Core_Action
::RENEW
)) {
576 $this->assign('delName', CRM_Utils_Array
::value('subject', $defaults));
579 if ($this->_activityTypeFile
) {
580 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
581 $defaults +
= $className::setDefaultValues($this);
583 if (empty($defaults['priority_id'])) {
584 $priority = CRM_Core_PseudoConstant
::get('CRM_Activity_DAO_Activity', 'priority_id');
585 $defaults['priority_id'] = array_search('Normal', $priority);
587 if (empty($defaults['status_id'])) {
588 $defaults['status_id'] = CRM_Core_OptionGroup
::getDefaultValue('activity_status');
593 public function buildQuickForm() {
594 if ($this->_action
& (CRM_Core_Action
::DELETE | CRM_Core_Action
::RENEW
)) {
595 //enable form element (ActivityLinks sets this true)
596 $this->assign('suppressForm', FALSE);
598 $button = ts('Delete');
599 if ($this->_action
& CRM_Core_Action
::RENEW
) {
600 $button = ts('Restore');
602 $this->addButtons(array(
606 'spacing' => ' ',
611 'name' => ts('Cancel'),
617 //build other activity links
618 CRM_Activity_Form_ActivityLinks
::commonBuildQuickForm($this);
620 //enable form element (ActivityLinks sets this true)
621 $this->assign('suppressForm', FALSE);
623 $element = &$this->add('select', 'activity_type_id', ts('Activity Type'),
624 array('' => '- ' . ts('select') . ' -') +
$this->_fields
['followup_activity_type_id']['attributes'],
626 'onchange' => "CRM.buildCustomData( 'Activity', this.value );",
627 'class' => 'crm-select2 required',
631 //freeze for update mode.
632 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
636 //Call to RecurringEntity buildQuickForm for add/update mode
637 if ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD
)) {
638 CRM_Core_Form_RecurringEntity
::buildQuickForm($this);
641 foreach ($this->_fields
as $field => $values) {
642 if (!empty($this->_fields
[$field])) {
643 $attribute = CRM_Utils_Array
::value('attributes', $values);
644 $required = !empty($values['required']);
646 if ($values['type'] == 'select' && empty($attribute)) {
647 $this->addSelect($field, array('entity' => 'activity'), $required);
649 elseif ($values['type'] == 'entityRef') {
650 $this->addEntityRef($field, $values['label'], $attribute, $required);
653 $this->add($values['type'], $field, $values['label'], $attribute, $required, CRM_Utils_Array
::value('extra', $values));
658 //CRM-7362 --add campaigns.
659 CRM_Campaign_BAO_Campaign
::addCampaign($this, CRM_Utils_Array
::value('campaign_id', $this->_values
));
661 //add engagement level CRM-7775
662 $buildEngagementLevel = FALSE;
663 if (CRM_Campaign_BAO_Campaign
::isCampaignEnable() &&
664 CRM_Campaign_BAO_Campaign
::accessCampaign()
666 $buildEngagementLevel = TRUE;
667 $this->addSelect('engagement_level', array('entity' => 'activity'));
668 $this->addRule('engagement_level',
669 ts('Please enter the engagement index as a number (integers only).'),
673 $this->assign('buildEngagementLevel', $buildEngagementLevel);
675 // check for survey activity
676 $this->_isSurveyActivity
= FALSE;
678 if ($this->_activityId
&& CRM_Campaign_BAO_Campaign
::isCampaignEnable() &&
679 CRM_Campaign_BAO_Campaign
::accessCampaign()
682 $this->_isSurveyActivity
= CRM_Campaign_BAO_Survey
::isSurveyActivity($this->_activityId
);
683 if ($this->_isSurveyActivity
) {
684 $surveyId = CRM_Core_DAO
::getFieldValue('CRM_Activity_DAO_Activity',
688 $responseOptions = CRM_Campaign_BAO_Survey
::getResponsesOptions($surveyId);
689 if ($responseOptions) {
690 $this->add('select', 'result', ts('Result'),
691 array('' => ts('- select -')) +
array_combine($responseOptions, $responseOptions)
696 $surveyTitle = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'title');
698 $this->assign('surveyTitle', $surveyTitle);
701 $this->assign('surveyActivity', $this->_isSurveyActivity
);
703 // this option should be available only during add mode
704 if ($this->_action
!= CRM_Core_Action
::UPDATE
) {
705 $this->add('advcheckbox', 'is_multi_activity', ts('Create a separate activity for each contact.'));
708 $this->addRule('duration',
709 ts('Please enter the duration as number of minutes (integers only).'), 'positiveInteger'
711 $this->addDateTime('activity_date_time', ts('Date'), TRUE, array('formatType' => 'activityDateTime'));
714 $this->addDateTime('followup_date', ts('in'), FALSE, array('formatType' => 'activityDateTime'));
716 // Only admins and case-workers can change the activity source
717 if (!CRM_Core_Permission
::check('administer CiviCRM') && $this->_context
!= 'caseActivity') {
718 $this->getElement('source_contact_id')->freeze();
721 //need to assign custom data type and subtype to the template
722 $this->assign('customDataType', 'Activity');
723 $this->assign('customDataSubType', $this->_activityTypeId
);
724 $this->assign('entityID', $this->_activityId
);
726 CRM_Core_BAO_Tag
::getTags('civicrm_activity', $tags, NULL,
727 ' ', TRUE);
730 $this->add('select', 'tag', ts('Tags'), $tags, FALSE,
731 array('id' => 'tags', 'multiple' => 'multiple', 'class' => 'crm-select2 huge')
735 // we need to hide activity tagset for special activities
736 $specialActivities = array('Open Case');
738 if (!in_array($this->_activityTypeName
, $specialActivities)) {
740 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_activity');
741 CRM_Core_Form_Tag
::buildQuickForm($this, $parentNames, 'civicrm_activity', $this->_activityId
);
744 // if we're viewing, we're assigning different buttons than for adding/editing
745 if ($this->_action
& CRM_Core_Action
::VIEW
) {
746 if (isset($this->_groupTree
)) {
747 CRM_Core_BAO_CustomGroup
::buildCustomDataView($this, $this->_groupTree
);
749 // form should be frozen for view mode
755 'name' => ts('Done'),
757 $this->addButtons($buttons);
761 '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.'),
762 '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.'),
764 $js = array('onclick' => "return activityStatus(" . json_encode($message) . ");");
765 $this->addButtons(array(
768 'name' => ts('Save'),
774 'name' => ts('Cancel'),
780 if ($this->_activityTypeFile
) {
781 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
783 $className::buildQuickForm($this);
784 $this->addFormRule(array($className, 'formRule'), $this);
787 $this->addFormRule(array('CRM_Activity_Form_Activity', 'formRule'), $this);
789 if (CRM_Core_BAO_Setting
::getItem(
790 CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
791 'activity_assignee_notification'
794 $this->assign('activityAssigneeNotification', TRUE);
797 $this->assign('activityAssigneeNotification', FALSE);
804 * @param array $fields
805 * The input form values.
806 * @param array $files
807 * The uploaded files if any.
811 * true if no errors, else array of errors
813 public static function formRule($fields, $files, $self) {
814 // skip form rule if deleting
815 if (CRM_Utils_Array
::value('_qf_Activity_next_', $fields) == 'Delete') {
819 if ((array_key_exists('activity_type_id', $fields) ||
!$self->_single
) && empty($fields['activity_type_id'])) {
820 $errors['activity_type_id'] = ts('Activity Type is a required field');
823 if (CRM_Utils_Array
::value('activity_type_id', $fields) == 3 &&
824 CRM_Utils_Array
::value('status_id', $fields) == 1
826 $errors['status_id'] = ts('You cannot record scheduled email activity.');
828 elseif (CRM_Utils_Array
::value('activity_type_id', $fields) == 4 &&
829 CRM_Utils_Array
::value('status_id', $fields) == 1
831 $errors['status_id'] = ts('You cannot record scheduled SMS activity.');
834 if (!empty($fields['followup_activity_type_id']) && empty($fields['followup_date'])) {
835 $errors['followup_date_time'] = ts('Followup date is a required field.');
837 //Activity type is mandatory if subject or follow-up date is specified for an Follow-up activity, CRM-4515
838 if ((!empty($fields['followup_activity_subject']) ||
!empty($fields['followup_date'])) && empty($fields['followup_activity_type_id'])) {
839 $errors['followup_activity_subject'] = ts('Follow-up Activity type is a required field.');
845 * Process the form submission.
848 * @param array $params
851 public function postProcess($params = NULL) {
852 if ($this->_action
& CRM_Core_Action
::DELETE
) {
853 $deleteParams = array('id' => $this->_activityId
);
854 $moveToTrash = CRM_Case_BAO_Case
::isCaseActivity($this->_activityId
);
855 CRM_Activity_BAO_Activity
::deleteActivity($deleteParams, $moveToTrash);
857 // delete tags for the entity
859 'entity_table' => 'civicrm_activity',
860 'entity_id' => $this->_activityId
,
863 CRM_Core_BAO_EntityTag
::del($tagParams);
865 CRM_Core_Session
::setStatus(ts("Selected Activity has been deleted successfully."), ts('Record Deleted'), 'success');
869 // store the submitted values in an array
871 $params = $this->controller
->exportValues($this->_name
);
874 //set activity type id
875 if (empty($params['activity_type_id'])) {
876 $params['activity_type_id'] = $this->_activityTypeId
;
879 if (!empty($params['hidden_custom']) &&
880 !isset($params['custom'])
882 $customFields = CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
883 $this->_activityTypeId
885 $customFields = CRM_Utils_Array
::crmArrayMerge($customFields,
886 CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
890 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
896 // store the date with proper format
897 $params['activity_date_time'] = CRM_Utils_Date
::processDate($params['activity_date_time'], $params['activity_date_time_time']);
899 // format params as arrays
900 foreach (array('target', 'assignee', 'followup_assignee') as $name) {
901 if (!empty($params["{$name}_contact_id"])) {
902 $params["{$name}_contact_id"] = explode(',', $params["{$name}_contact_id"]);
905 $params["{$name}_contact_id"] = array();
909 // get ids for associated contacts
910 if (!$params['source_contact_id']) {
911 $params['source_contact_id'] = $this->_currentUserId
;
914 if (isset($this->_activityId
)) {
915 $params['id'] = $this->_activityId
;
918 // add attachments as needed
919 CRM_Core_BAO_File
::formatAttachment($params,
926 if (!empty($params['is_multi_activity']) &&
927 !CRM_Utils_Array
::crmIsEmptyArray($params['target_contact_id'])
929 $targetContacts = $params['target_contact_id'];
930 foreach ($targetContacts as $targetContactId) {
931 $params['target_contact_id'] = array($targetContactId);
933 $activity[] = $this->processActivity($params);
938 $activity = $this->processActivity($params);
941 $activityIds = empty($this->_activityIds
) ?
array($this->_activityId
) : $this->_activityIds
;
942 foreach ($activityIds as $activityId) {
943 // set params for repeat configuration in create mode
944 $params['entity_id'] = $activityId;
945 $params['entity_table'] = 'civicrm_activity';
946 if (!empty($params['entity_id']) && !empty($params['entity_table'])) {
947 $checkParentExistsForThisId = CRM_Core_BAO_RecurringEntity
::getParentFor($params['entity_id'], $params['entity_table']);
948 if ($checkParentExistsForThisId) {
949 $params['parent_entity_id'] = $checkParentExistsForThisId;
950 $scheduleReminderDetails = CRM_Core_BAO_RecurringEntity
::getReminderDetailsByEntityId($checkParentExistsForThisId, $params['entity_table']);
953 $params['parent_entity_id'] = $params['entity_id'];
954 $scheduleReminderDetails = CRM_Core_BAO_RecurringEntity
::getReminderDetailsByEntityId($params['entity_id'], $params['entity_table']);
956 if (property_exists($scheduleReminderDetails, 'id')) {
957 $params['schedule_reminder_id'] = $scheduleReminderDetails->id
;
960 $params['dateColumns'] = array('activity_date_time');
962 // Set default repetition start if it was not provided.
963 if (empty($params['repetition_start_date'])) {
964 $params['repetition_start_date'] = $params['activity_date_time'];
968 unset($params['id']);
969 $linkedEntities = array(
971 'table' => 'civicrm_activity_contact',
972 'findCriteria' => array(
973 'activity_id' => $activityId,
975 'linkedColumns' => array('activity_id'),
976 'isRecurringEntityRecord' => FALSE,
979 CRM_Core_Form_RecurringEntity
::postProcess($params, 'civicrm_activity', $linkedEntities);
982 return array('activity' => $activity);
986 * Process activity creation.
988 * @param array $params
989 * Associated array of submitted values.
991 * @return self|null|object
993 protected function processActivity(&$params) {
994 $activityAssigned = array();
995 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
996 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
997 // format assignee params
998 if (!CRM_Utils_Array
::crmIsEmptyArray($params['assignee_contact_id'])) {
999 //skip those assignee contacts which are already assigned
1000 //while sending a copy.CRM-4509.
1001 $activityAssigned = array_flip($params['assignee_contact_id']);
1002 if ($this->_activityId
) {
1003 $assigneeContacts = CRM_Activity_BAO_ActivityContact
::getNames($this->_activityId
, $assigneeID);
1004 $activityAssigned = array_diff_key($activityAssigned, $assigneeContacts);
1008 // call begin post process. Idea is to let injecting file do
1009 // any processing before the activity is added/updated.
1010 $this->beginPostProcess($params);
1012 $activity = CRM_Activity_BAO_Activity
::create($params);
1014 // add tags if exists
1015 $tagParams = array();
1016 if (!empty($params['tag'])) {
1017 foreach ($params['tag'] as $tag) {
1018 $tagParams[$tag] = 1;
1023 CRM_Core_BAO_EntityTag
::create($tagParams, 'civicrm_activity', $activity->id
);
1026 if (isset($params['activity_taglist']) && !empty($params['activity_taglist'])) {
1027 CRM_Core_Form_Tag
::postProcess($params['activity_taglist'], $activity->id
, 'civicrm_activity', $this);
1030 // call end post process. Idea is to let injecting file do any
1031 // processing needed, after the activity has been added/updated.
1032 $this->endPostProcess($params, $activity);
1035 if (!empty($params['is_multi_activity'])) {
1036 $this->_activityIds
[] = $activity->id
;
1039 $this->_activityId
= $activity->id
;
1042 // create follow up activity if needed
1043 $followupStatus = '';
1044 $followupActivity = NULL;
1045 if (!empty($params['followup_activity_type_id'])) {
1046 $followupActivity = CRM_Activity_BAO_Activity
::createFollowupActivity($activity->id
, $params);
1047 $followupStatus = ts('A followup activity has been scheduled.');
1050 // send copy to assignee contacts.CRM-4509
1053 if (CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
1054 'activity_assignee_notification')
1056 $activityIDs = array($activity->id
);
1057 if ($followupActivity) {
1058 $activityIDs = array_merge($activityIDs, array($followupActivity->id
));
1060 $assigneeContacts = CRM_Activity_BAO_ActivityAssignment
::getAssigneeNames($activityIDs, TRUE, FALSE);
1062 if (!CRM_Utils_Array
::crmIsEmptyArray($params['assignee_contact_id'])) {
1063 $mailToContacts = array();
1065 //build an associative array with unique email addresses.
1066 foreach ($activityAssigned as $id => $dnc) {
1067 if (isset($id) && array_key_exists($id, $assigneeContacts)) {
1068 $mailToContacts[$assigneeContacts[$id]['email']] = $assigneeContacts[$id];
1072 $sent = CRM_Activity_BAO_Activity
::sendToAssignee($activity, $mailToContacts);
1074 $mailStatus .= ts("A copy of the activity has also been sent to assignee contacts(s).");
1078 // Also send email to follow-up activity assignees if set
1079 if ($followupActivity) {
1080 $mailToFollowupContacts = array();
1081 foreach ($assigneeContacts as $values) {
1082 if ($values['activity_id'] == $followupActivity->id
) {
1083 $mailToFollowupContacts[$values['email']] = $values;
1087 $sentFollowup = CRM_Activity_BAO_Activity
::sendToAssignee($followupActivity, $mailToFollowupContacts);
1088 if ($sentFollowup) {
1089 $mailStatus .= '<br />' . ts("A copy of the follow-up activity has also been sent to follow-up assignee contacts(s).");
1094 // set status message
1096 if (!empty($params['subject'])) {
1097 $subject = "'" . $params['subject'] . "'";
1100 CRM_Core_Session
::setStatus(ts('Activity %1 has been saved. %2 %3',
1103 2 => $followupStatus,
1106 ), ts('Saved'), 'success');
1112 * Shorthand for getting id by display name (makes code more readable)
1113 * @param $displayName
1114 * @return null|string
1116 protected function _getIdByDisplayName($displayName) {
1117 return CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
1125 * Shorthand for getting display name by id (makes code more readable)
1127 * @return null|string
1129 protected function _getDisplayNameById($id) {
1130 return CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
1138 * Let injecting activity type file do any processing.
1139 * needed, before the activity is added/updated
1141 * @param array $params
1143 public function beginPostProcess(&$params) {
1144 if ($this->_activityTypeFile
) {
1145 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
1146 $className::beginPostProcess($this, $params);
1151 * Let injecting activity type file do any processing
1152 * needed, after the activity has been added/updated
1154 * @param array $params
1157 public function endPostProcess(&$params, &$activity) {
1158 if ($this->_activityTypeFile
) {
1159 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
1160 $className::endPostProcess($this, $params, $activity);