3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
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 * Is it possible to create separate activities with this form?
125 * When TRUE, the form will ask whether the user wants to create separate
126 * activities (if the user has specified multiple contacts in the "with"
129 * When FALSE, the form will create one activity with all contacts together
130 * and won't ask the user anything.
132 * Note: This is a class property so that child classes can turn off this
133 * behavior (e.g. in CRM_Case_Form_Activity)
137 protected $supportsActivitySeparation = TRUE;
140 * Explicitly declare the entity api name.
144 public function getDefaultEntity() {
149 * The _fields var can be used by sub class to set/unset/edit the
150 * form fields based on their requirement
152 public function setFields() {
153 // Remove print document activity type
154 $unwanted = CRM_Core_OptionGroup
::values('activity_type', FALSE, FALSE, FALSE, "AND v.name = 'Print PDF Letter'");
155 $activityTypes = array_diff_key(CRM_Core_PseudoConstant
::ActivityType(FALSE), $unwanted);
157 $this->_fields
= array(
160 'label' => ts('Subject'),
161 'attributes' => CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity',
167 'label' => ts('Duration'),
168 'attributes' => array('size' => 4, 'maxlength' => 8),
173 'label' => ts('Location'),
174 'attributes' => CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity', 'location'),
179 'label' => ts('Details'),
180 'attributes' => array('class' => 'huge'),
183 'status_id' => array(
187 'priority_id' => array(
191 'source_contact_id' => array(
192 'type' => 'entityRef',
193 'label' => ts('Added By'),
196 'target_contact_id' => array(
197 'type' => 'entityRef',
198 'label' => ts('With Contact'),
199 'attributes' => array('multiple' => TRUE, 'create' => TRUE),
201 'assignee_contact_id' => array(
202 'type' => 'entityRef',
203 'label' => ts('Assigned to'),
204 'attributes' => array(
207 'api' => array('params' => array('is_deceased' => 0)),
210 'followup_assignee_contact_id' => array(
211 'type' => 'entityRef',
212 'label' => ts('Assigned to'),
213 'attributes' => array(
216 'api' => array('params' => array('is_deceased' => 0)),
219 'followup_activity_type_id' => array(
221 'label' => ts('Followup Activity'),
222 'attributes' => array('' => '- ' . ts('select activity') . ' -') +
$activityTypes,
223 'extra' => array('class' => 'crm-select2'),
225 // Add optional 'Subject' field for the Follow-up Activiity, CRM-4491
226 'followup_activity_subject' => array(
228 'label' => ts('Subject'),
229 'attributes' => CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity',
237 * Build the form object.
239 public function preProcess() {
240 CRM_Core_Form_RecurringEntity
::preProcess('civicrm_activity');
241 $this->_atypefile
= CRM_Utils_Array
::value('atypefile', $_GET);
242 $this->assign('atypefile', FALSE);
243 if ($this->_atypefile
) {
244 $this->assign('atypefile', TRUE);
247 $session = CRM_Core_Session
::singleton();
248 $this->_currentUserId
= CRM_Core_Session
::getLoggedInContactID();
250 $this->_currentlyViewedContactId
= $this->get('contactId');
251 if (!$this->_currentlyViewedContactId
) {
252 $this->_currentlyViewedContactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
254 $this->assign('contactId', $this->_currentlyViewedContactId
);
257 if (!isset($this->_context
)) {
258 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this);
259 if (CRM_Contact_Form_Search
::isSearchContext($this->_context
)) {
260 $this->_context
= 'search';
262 elseif (!in_array($this->_context
, array('dashlet', 'case', 'dashletFullscreen'))
263 && $this->_currentlyViewedContactId
265 $this->_context
= 'activity';
267 $this->_compContext
= CRM_Utils_Request
::retrieve('compContext', 'String', $this);
270 $this->assign('context', $this->_context
);
272 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this);
274 if ($this->_action
& CRM_Core_Action
::DELETE
) {
275 if (!CRM_Core_Permission
::check('delete activities')) {
276 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
281 // When we come from contact search, activity id never comes.
282 // So don't try to get from object, it might gives you wrong one.
284 // if we're not adding new one, there must be an id to
285 // an activity we're trying to work on.
286 if ($this->_action
!= CRM_Core_Action
::ADD
&&
287 get_class($this->controller
) != 'CRM_Contact_Controller_Search'
289 $this->_activityId
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
292 $this->_activityTypeId
= CRM_Utils_Request
::retrieve('atype', 'Positive', $this);
293 $this->assign('atype', $this->_activityTypeId
);
295 $this->assign('activityId', $this->_activityId
);
297 // Check for required permissions, CRM-6264.
298 if ($this->_activityId
&&
299 in_array($this->_action
, array(
300 CRM_Core_Action
::UPDATE
,
301 CRM_Core_Action
::VIEW
,
303 !CRM_Activity_BAO_Activity
::checkPermission($this->_activityId
, $this->_action
)
305 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
307 if (($this->_action
& CRM_Core_Action
::VIEW
) &&
308 CRM_Activity_BAO_Activity
::checkPermission($this->_activityId
, CRM_Core_Action
::UPDATE
)
310 $this->assign('permission', 'edit');
313 if (!$this->_activityTypeId
&& $this->_activityId
) {
314 $this->_activityTypeId
= CRM_Core_DAO
::getFieldValue('CRM_Activity_DAO_Activity',
320 // Assigning Activity type name.
321 if ($this->_activityTypeId
) {
322 $activityTName = CRM_Core_OptionGroup
::values('activity_type', FALSE, FALSE, FALSE, 'AND v.value = ' . $this->_activityTypeId
, 'label');
323 if ($activityTName[$this->_activityTypeId
]) {
324 $this->_activityTypeName
= $activityTName[$this->_activityTypeId
];
325 $this->assign('activityTName', $activityTName[$this->_activityTypeId
]);
330 if (isset($activityTName)) {
331 $activityName = CRM_Utils_Array
::value($this->_activityTypeId
, $activityTName);
332 $this->assign('pageTitle', ts('%1 Activity', array(1 => $activityName)));
334 if ($this->_currentlyViewedContactId
) {
335 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_currentlyViewedContactId
);
336 // Check if this is default domain contact CRM-10482.
337 if (CRM_Contact_BAO_Contact
::checkDomainContact($this->_currentlyViewedContactId
)) {
338 $displayName .= ' (' . ts('default organization') . ')';
340 CRM_Utils_System
::setTitle($displayName . ' - ' . $activityName);
343 CRM_Utils_System
::setTitle(ts('%1 Activity', array(1 => $activityName)));
347 // Check the mode when this form is called either single or as
348 // search task action.
349 if ($this->_activityTypeId ||
350 $this->_context
== 'standalone' ||
351 $this->_currentlyViewedContactId
353 $this->_single
= TRUE;
354 $this->assign('urlPath', 'civicrm/activity');
357 // Set the appropriate action.
358 $url = CRM_Utils_System
::currentPath();
359 $urlArray = explode('/', $url);
360 $searchPath = array_pop($urlArray);
361 $searchType = 'basic';
362 $this->_action
= CRM_Core_Action
::BASIC
;
363 switch ($searchPath) {
365 $searchType = $searchPath;
366 $this->_action
= CRM_Core_Action
::BASIC
;
370 $searchType = $searchPath;
371 $this->_action
= CRM_Core_Action
::ADVANCED
;
375 $searchType = $searchPath;
376 $this->_action
= CRM_Core_Action
::PROFILE
;
380 $this->_action
= CRM_Core_Action
::COPY
;
381 $searchType = $searchPath;
385 parent
::preProcess();
386 $this->_single
= FALSE;
388 $this->assign('urlPath', "civicrm/contact/search/$searchType");
389 $this->assign('urlPathVar', "_qf_Activity_display=true&qfKey={$this->controller->_key}");
392 $this->assign('single', $this->_single
);
393 $this->assign('action', $this->_action
);
395 if ($this->_action
& CRM_Core_Action
::VIEW
) {
396 // Get the tree of custom fields.
397 $this->_groupTree
= CRM_Core_BAO_CustomGroup
::getTree('Activity', NULL,
398 $this->_activityId
, 0, $this->_activityTypeId
402 if ($this->_activityTypeId
) {
403 // Set activity type name and description to template.
404 list($this->_activityTypeName
, $activityTypeDescription) = CRM_Core_BAO_OptionValue
::getActivityTypeDetails($this->_activityTypeId
);
405 $this->assign('activityTypeName', $this->_activityTypeName
);
406 $this->assign('activityTypeDescription', $activityTypeDescription);
410 $urlParams = $urlString = NULL;
411 $qfKey = CRM_Utils_Request
::retrieve('key', 'String', $this);
413 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $this);
416 // Validate the qfKey.
417 if (!CRM_Utils_Rule
::qfKey($qfKey)) {
421 if ($this->_context
== 'fulltext') {
423 $urlParams = 'force=1';
424 $urlString = 'civicrm/contact/search/custom';
425 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
427 $urlParams .= '&context=fulltext&action=view';
428 $urlString = 'civicrm/contact/view/activity';
431 $urlParams .= "$keyName=$qfKey";
433 $this->assign('searchKey', $qfKey);
435 elseif (in_array($this->_context
, array(
442 $urlParams = 'reset=1';
443 $urlString = 'civicrm/dashboard';
445 elseif ($this->_context
== 'search') {
446 $urlParams = 'force=1';
448 $urlParams .= "&qfKey=$qfKey";
450 $path = CRM_Utils_System
::currentPath();
451 if ($this->_compContext
== 'advanced') {
452 $urlString = 'civicrm/contact/search/advanced';
454 elseif ($path == 'civicrm/group/search'
455 ||
$path == 'civicrm/contact/search'
456 ||
$path == 'civicrm/contact/search/advanced'
457 ||
$path == 'civicrm/contact/search/custom'
458 ||
$path == 'civicrm/group/search'
463 $urlString = 'civicrm/activity/search';
465 $this->assign('searchKey', $qfKey);
467 elseif ($this->_context
!= 'caseActivity') {
468 $urlParams = "action=browse&reset=1&cid={$this->_currentlyViewedContactId}&selectedChild=activity";
469 $urlString = 'civicrm/contact/view';
473 $session->pushUserContext(CRM_Utils_System
::url($urlString, $urlParams));
476 // hack to retrieve activity type id from post variables
477 if (!$this->_activityTypeId
) {
478 $this->_activityTypeId
= CRM_Utils_Array
::value('activity_type_id', $_POST);
481 // when custom data is included in this page
482 if (!empty($_POST['hidden_custom'])) {
483 // We need to set it in the session for the code below to work.
485 // Need to assign custom data subtype to the template.
486 $this->set('type', 'Activity');
487 $this->set('subType', $this->_activityTypeId
);
488 $this->set('entityId', $this->_activityId
);
489 CRM_Custom_Form_CustomData
::preProcess($this, NULL, $this->_activityTypeId
, 1, 'Activity', $this->_activityId
);
490 CRM_Custom_Form_CustomData
::buildQuickForm($this);
491 CRM_Custom_Form_CustomData
::setDefaultValues($this);
494 // add attachments part
495 CRM_Core_BAO_File
::buildAttachment($this, 'civicrm_activity', $this->_activityId
, NULL, TRUE);
497 // figure out the file name for activity type, if any
498 if ($this->_activityTypeId
&&
499 $this->_activityTypeFile
= CRM_Activity_BAO_Activity
::getFileForActivityTypeId($this->_activityTypeId
, $this->_crmDir
)
501 $this->assign('activityTypeFile', $this->_activityTypeFile
);
502 $this->assign('crmDir', $this->_crmDir
);
507 if ($this->_activityTypeFile
) {
508 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
509 $className::preProcess($this);
512 $this->_values
= $this->get('values');
513 if (!is_array($this->_values
)) {
514 $this->_values
= array();
515 if (isset($this->_activityId
) && $this->_activityId
) {
516 $params = array('id' => $this->_activityId
);
517 CRM_Activity_BAO_Activity
::retrieve($params, $this->_values
);
519 $this->set('values', $this->_values
);
522 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
523 CRM_Core_Form_RecurringEntity
::preProcess('civicrm_activity');
526 if ($this->_action
& CRM_Core_Action
::VIEW
) {
527 $url = CRM_Utils_System
::url(implode("/", $this->urlPath
), "reset=1&id={$this->_activityId}&action=view&cid={$this->_values['source_contact_id']}");
528 CRM_Utils_Recent
::add($this->_values
['subject'],
530 $this->_values
['id'],
532 $this->_values
['source_contact_id'],
533 $this->_values
['source_contact']
539 * Set default values for the form.
541 * For edit/view mode the default values are retrieved from the database.
545 public function setDefaultValues() {
547 $defaults = $this->_values + CRM_Core_Form_RecurringEntity
::setDefaultValues();
548 // if we're editing...
549 if (isset($this->_activityId
)) {
550 if (empty($defaults['activity_date_time'])) {
551 list($defaults['activity_date_time'], $defaults['activity_date_time_time']) = CRM_Utils_Date
::setDateDefaults(NULL, 'activityDateTime');
553 elseif ($this->_action
& CRM_Core_Action
::UPDATE
) {
554 $this->assign('current_activity_date_time', $defaults['activity_date_time']);
555 list($defaults['activity_date_time'],
556 $defaults['activity_date_time_time']
557 ) = CRM_Utils_Date
::setDateDefaults($defaults['activity_date_time'], 'activityDateTime');
558 list($defaults['repetition_start_date'], $defaults['repetition_start_date_time']) = CRM_Utils_Date
::setDateDefaults($defaults['activity_date_time'], 'activityDateTime');
561 if ($this->_context
!= 'standalone') {
562 $this->assign('target_contact_value',
563 CRM_Utils_Array
::value('target_contact_value', $defaults)
565 $this->assign('assignee_contact_value',
566 CRM_Utils_Array
::value('assignee_contact_value', $defaults)
570 // Fixme: why are we getting the wrong keys from upstream?
571 $defaults['target_contact_id'] = CRM_Utils_Array
::value('target_contact', $defaults);
572 $defaults['assignee_contact_id'] = CRM_Utils_Array
::value('assignee_contact', $defaults);
574 // set default tags if exists
575 $defaults['tag'] = implode(',', CRM_Core_BAO_EntityTag
::getTag($this->_activityId
, 'civicrm_activity'));
578 // if it's a new activity, we need to set default values for associated contact fields
579 $this->_sourceContactId
= $this->_currentUserId
;
580 $this->_targetContactId
= $this->_currentlyViewedContactId
;
582 $defaults['source_contact_id'] = $this->_sourceContactId
;
583 $defaults['target_contact_id'] = $this->_targetContactId
;
585 list($defaults['activity_date_time'], $defaults['activity_date_time_time'])
586 = CRM_Utils_Date
::setDateDefaults(NULL, 'activityDateTime');
589 if ($this->_activityTypeId
) {
590 $defaults['activity_type_id'] = $this->_activityTypeId
;
593 if (!$this->_single
&& !empty($this->_contactIds
)) {
594 $defaults['target_contact_id'] = $this->_contactIds
;
597 // CRM-15472 - 50 is around the practical limit of how many items a select2 entityRef can handle
598 if ($this->_action
== 2 && !empty($defaults['target_contact_id'])) {
599 $count = count(is_array($defaults['target_contact_id']) ?
$defaults['target_contact_id'] : explode(',', $defaults['target_contact_id']));
601 $this->freeze(array('target_contact_id'));
605 if ($this->_action
& (CRM_Core_Action
::DELETE | CRM_Core_Action
::RENEW
)) {
606 $this->assign('delName', CRM_Utils_Array
::value('subject', $defaults));
609 if ($this->_activityTypeFile
) {
610 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
611 $defaults +
= $className::setDefaultValues($this);
613 if (empty($defaults['priority_id'])) {
614 $priority = CRM_Core_PseudoConstant
::get('CRM_Activity_DAO_Activity', 'priority_id');
615 $defaults['priority_id'] = array_search('Normal', $priority);
617 if (empty($defaults['status_id'])) {
618 $defaults['status_id'] = CRM_Core_OptionGroup
::getDefaultValue('activity_status');
623 public function buildQuickForm() {
624 if ($this->_action
& (CRM_Core_Action
::DELETE | CRM_Core_Action
::RENEW
)) {
625 //enable form element (ActivityLinks sets this true)
626 $this->assign('suppressForm', FALSE);
628 $button = ts('Delete');
629 if ($this->_action
& CRM_Core_Action
::RENEW
) {
630 $button = ts('Restore');
632 $this->addButtons(array(
636 'spacing' => ' ',
641 'name' => ts('Cancel'),
647 // Build other activity links.
648 CRM_Activity_Form_ActivityLinks
::commonBuildQuickForm($this);
650 // Enable form element (ActivityLinks sets this true).
651 $this->assign('suppressForm', FALSE);
653 $element = &$this->add('select', 'activity_type_id', ts('Activity Type'),
654 array('' => '- ' . ts('select') . ' -') +
$this->_fields
['followup_activity_type_id']['attributes'],
656 'onchange' => "CRM.buildCustomData( 'Activity', this.value );",
657 'class' => 'crm-select2 required',
661 // Freeze for update mode.
662 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
666 // Call to RecurringEntity buildQuickForm for add/update mode.
667 if ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD
)) {
668 CRM_Core_Form_RecurringEntity
::buildQuickForm($this);
671 foreach ($this->_fields
as $field => $values) {
672 if (!empty($this->_fields
[$field])) {
673 $attribute = CRM_Utils_Array
::value('attributes', $values);
674 $required = !empty($values['required']);
676 if ($values['type'] == 'select' && empty($attribute)) {
677 $this->addSelect($field, array('entity' => 'activity'), $required);
679 elseif ($values['type'] == 'entityRef') {
680 $this->addEntityRef($field, $values['label'], $attribute, $required);
683 $this->add($values['type'], $field, $values['label'], $attribute, $required, CRM_Utils_Array
::value('extra', $values));
688 // CRM-7362 --add campaigns.
689 CRM_Campaign_BAO_Campaign
::addCampaign($this, CRM_Utils_Array
::value('campaign_id', $this->_values
));
691 // Add engagement level CRM-7775
692 $buildEngagementLevel = FALSE;
693 if (CRM_Campaign_BAO_Campaign
::isCampaignEnable() &&
694 CRM_Campaign_BAO_Campaign
::accessCampaign()
696 $buildEngagementLevel = TRUE;
697 $this->addSelect('engagement_level', array('entity' => 'activity'));
698 $this->addRule('engagement_level',
699 ts('Please enter the engagement index as a number (integers only).'),
703 $this->assign('buildEngagementLevel', $buildEngagementLevel);
705 // check for survey activity
706 $this->_isSurveyActivity
= FALSE;
708 if ($this->_activityId
&& CRM_Campaign_BAO_Campaign
::isCampaignEnable() &&
709 CRM_Campaign_BAO_Campaign
::accessCampaign()
712 $this->_isSurveyActivity
= CRM_Campaign_BAO_Survey
::isSurveyActivity($this->_activityId
);
713 if ($this->_isSurveyActivity
) {
714 $surveyId = CRM_Core_DAO
::getFieldValue('CRM_Activity_DAO_Activity',
718 $responseOptions = CRM_Campaign_BAO_Survey
::getResponsesOptions($surveyId);
719 if ($responseOptions) {
720 $this->add('select', 'result', ts('Result'),
721 array('' => ts('- select -')) +
array_combine($responseOptions, $responseOptions)
726 $surveyTitle = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'title');
728 $this->assign('surveyTitle', $surveyTitle);
731 $this->assign('surveyActivity', $this->_isSurveyActivity
);
733 // Add the "Activity Separation" field
734 $actionIsAdd = $this->_action
!= CRM_Core_Action
::UPDATE
;
735 $separationIsPossible = $this->supportsActivitySeparation
;
736 if ($actionIsAdd && $separationIsPossible) {
739 ts('Activity Separation'),
741 'separate' => ts('Create separate activities for each contact'),
742 'combined' => ts('Create one activity with all contacts together'),
747 $this->addRule('duration',
748 ts('Please enter the duration as number of minutes (integers only).'), 'positiveInteger'
750 $this->addDateTime('activity_date_time', ts('Date'), TRUE, array('formatType' => 'activityDateTime'));
752 // Add followup date.
753 $this->addDateTime('followup_date', ts('in'), FALSE, array('formatType' => 'activityDateTime'));
755 // Only admins and case-workers can change the activity source
756 if (!CRM_Core_Permission
::check('administer CiviCRM') && $this->_context
!= 'caseActivity') {
757 $this->getElement('source_contact_id')->freeze();
760 //need to assign custom data type and subtype to the template
761 $this->assign('customDataType', 'Activity');
762 $this->assign('customDataSubType', $this->_activityTypeId
);
763 $this->assign('entityID', $this->_activityId
);
765 $tags = CRM_Core_BAO_Tag
::getColorTags('civicrm_activity');
768 $this->add('select2', 'tag', ts('Tags'), $tags, FALSE, array('class' => 'huge', 'placeholder' => ts('- select -'), 'multiple' => TRUE));
771 // we need to hide activity tagset for special activities
772 $specialActivities = array('Open Case');
774 if (!in_array($this->_activityTypeName
, $specialActivities)) {
776 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_activity');
777 CRM_Core_Form_Tag
::buildQuickForm($this, $parentNames, 'civicrm_activity', $this->_activityId
);
780 // if we're viewing, we're assigning different buttons than for adding/editing
781 if ($this->_action
& CRM_Core_Action
::VIEW
) {
782 if (isset($this->_groupTree
)) {
783 CRM_Core_BAO_CustomGroup
::buildCustomDataView($this, $this->_groupTree
, FALSE, NULL, NULL, NULL, $this->_activityId
);
785 // form should be frozen for view mode
791 'name' => ts('Done'),
793 $this->addButtons($buttons);
797 '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.'),
798 '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.'),
800 $js = array('onclick' => "return activityStatus(" . json_encode($message) . ");");
801 $this->addButtons(array(
804 'name' => ts('Save'),
810 'name' => ts('Cancel'),
815 if ($this->_activityTypeFile
) {
816 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
818 $className::buildQuickForm($this);
819 $this->addFormRule(array($className, 'formRule'), $this);
822 $this->addFormRule(array('CRM_Activity_Form_Activity', 'formRule'), $this);
824 $doNotNotifyAssigneeFor = (array) Civi
::settings()->get('do_not_notify_assignees_for');
825 if (($this->_activityTypeId
&& in_array($this->_activityTypeId
, $doNotNotifyAssigneeFor)) ||
!Civi
::settings()->get('activity_assignee_notification')) {
826 $this->assign('activityAssigneeNotification', FALSE);
829 $this->assign('activityAssigneeNotification', TRUE);
831 $this->assign('doNotNotifyAssigneeFor', $doNotNotifyAssigneeFor);
837 * @param array $fields
838 * The input form values.
839 * @param array $files
840 * The uploaded files if any.
844 * true if no errors, else array of errors
846 public static function formRule($fields, $files, $self) {
847 // skip form rule if deleting
848 if (CRM_Utils_Array
::value('_qf_Activity_next_', $fields) == 'Delete') {
852 if ((array_key_exists('activity_type_id', $fields) ||
!$self->_single
) && empty($fields['activity_type_id'])) {
853 $errors['activity_type_id'] = ts('Activity Type is a required field');
856 if (CRM_Utils_Array
::value('activity_type_id', $fields) == 3 &&
857 CRM_Utils_Array
::value('status_id', $fields) == 1
859 $errors['status_id'] = ts('You cannot record scheduled email activity.');
861 elseif (CRM_Utils_Array
::value('activity_type_id', $fields) == 4 &&
862 CRM_Utils_Array
::value('status_id', $fields) == 1
864 $errors['status_id'] = ts('You cannot record scheduled SMS activity.');
867 if (!empty($fields['followup_activity_type_id']) && empty($fields['followup_date'])) {
868 $errors['followup_date_time'] = ts('Followup date is a required field.');
870 // Activity type is mandatory if subject or follow-up date is specified for an Follow-up activity, CRM-4515.
871 if ((!empty($fields['followup_activity_subject']) ||
!empty($fields['followup_date'])) && empty($fields['followup_activity_type_id'])) {
872 $errors['followup_activity_subject'] = ts('Follow-up Activity type is a required field.');
875 // Check that a value has been set for the "activity separation" field if needed
876 $separationIsPossible = $self->supportsActivitySeparation
;
877 $actionIsAdd = $self->_action
== CRM_Core_Action
::ADD
;
878 $hasMultipleTargetContacts = !empty($fields['target_contact_id']) && strpos($fields['target_contact_id'], ',') !== FALSE;
879 $separationFieldIsEmpty = empty($fields['separation']);
880 if ($separationIsPossible && $actionIsAdd && $hasMultipleTargetContacts && $separationFieldIsEmpty) {
881 $errors['separation'] = ts('Activity Separation is a required field.');
888 * Process the form submission.
891 * @param array $params
894 public function postProcess($params = NULL) {
895 if ($this->_action
& CRM_Core_Action
::DELETE
) {
896 $deleteParams = array('id' => $this->_activityId
);
897 $moveToTrash = CRM_Case_BAO_Case
::isCaseActivity($this->_activityId
);
898 CRM_Activity_BAO_Activity
::deleteActivity($deleteParams, $moveToTrash);
900 // delete tags for the entity
902 'entity_table' => 'civicrm_activity',
903 'entity_id' => $this->_activityId
,
906 CRM_Core_BAO_EntityTag
::del($tagParams);
908 CRM_Core_Session
::setStatus(ts("Selected Activity has been deleted successfully."), ts('Record Deleted'), 'success');
912 // store the submitted values in an array
914 $params = $this->controller
->exportValues($this->_name
);
917 // Set activity type id.
918 if (empty($params['activity_type_id'])) {
919 $params['activity_type_id'] = $this->_activityTypeId
;
922 if (!empty($params['hidden_custom']) &&
923 !isset($params['custom'])
925 $customFields = CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
926 $this->_activityTypeId
928 $customFields = CRM_Utils_Array
::crmArrayMerge($customFields,
929 CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
933 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
939 // store the date with proper format
940 $params['activity_date_time'] = CRM_Utils_Date
::processDate($params['activity_date_time'], $params['activity_date_time_time']);
942 // format params as arrays
943 foreach (array('target', 'assignee', 'followup_assignee') as $name) {
944 if (!empty($params["{$name}_contact_id"])) {
945 $params["{$name}_contact_id"] = explode(',', $params["{$name}_contact_id"]);
948 $params["{$name}_contact_id"] = array();
952 // get ids for associated contacts
953 if (!$params['source_contact_id']) {
954 $params['source_contact_id'] = $this->_currentUserId
;
957 if (isset($this->_activityId
)) {
958 $params['id'] = $this->_activityId
;
961 // add attachments as needed
962 CRM_Core_BAO_File
::formatAttachment($params,
968 $params['is_multi_activity'] = CRM_Utils_Array
::value('separation', $params) == 'separate';
971 if (!empty($params['is_multi_activity']) &&
972 !CRM_Utils_Array
::crmIsEmptyArray($params['target_contact_id'])
974 $targetContacts = $params['target_contact_id'];
975 foreach ($targetContacts as $targetContactId) {
976 $params['target_contact_id'] = array($targetContactId);
978 $activity[] = $this->processActivity($params);
983 $activity = $this->processActivity($params);
986 $activityIds = empty($this->_activityIds
) ?
array($this->_activityId
) : $this->_activityIds
;
987 foreach ($activityIds as $activityId) {
988 // set params for repeat configuration in create mode
989 $params['entity_id'] = $activityId;
990 $params['entity_table'] = 'civicrm_activity';
991 if (!empty($params['entity_id']) && !empty($params['entity_table'])) {
992 $checkParentExistsForThisId = CRM_Core_BAO_RecurringEntity
::getParentFor($params['entity_id'], $params['entity_table']);
993 if ($checkParentExistsForThisId) {
994 $params['parent_entity_id'] = $checkParentExistsForThisId;
995 $scheduleReminderDetails = CRM_Core_BAO_RecurringEntity
::getReminderDetailsByEntityId($checkParentExistsForThisId, $params['entity_table']);
998 $params['parent_entity_id'] = $params['entity_id'];
999 $scheduleReminderDetails = CRM_Core_BAO_RecurringEntity
::getReminderDetailsByEntityId($params['entity_id'], $params['entity_table']);
1001 if (property_exists($scheduleReminderDetails, 'id')) {
1002 $params['schedule_reminder_id'] = $scheduleReminderDetails->id
;
1005 $params['dateColumns'] = array('activity_date_time');
1007 // Set default repetition start if it was not provided.
1008 if (empty($params['repetition_start_date'])) {
1009 $params['repetition_start_date'] = $params['activity_date_time'];
1012 // unset activity id
1013 unset($params['id']);
1014 $linkedEntities = array(
1016 'table' => 'civicrm_activity_contact',
1017 'findCriteria' => array(
1018 'activity_id' => $activityId,
1020 'linkedColumns' => array('activity_id'),
1021 'isRecurringEntityRecord' => FALSE,
1024 CRM_Core_Form_RecurringEntity
::postProcess($params, 'civicrm_activity', $linkedEntities);
1027 return array('activity' => $activity);
1031 * Process activity creation.
1033 * @param array $params
1034 * Associated array of submitted values.
1036 * @return self|null|object
1038 protected function processActivity(&$params) {
1039 $activityAssigned = array();
1040 $activityContacts = CRM_Activity_BAO_ActivityContact
::buildOptions('record_type_id', 'validate');
1041 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
1042 // format assignee params
1043 if (!CRM_Utils_Array
::crmIsEmptyArray($params['assignee_contact_id'])) {
1044 //skip those assignee contacts which are already assigned
1045 //while sending a copy.CRM-4509.
1046 $activityAssigned = array_flip($params['assignee_contact_id']);
1047 if ($this->_activityId
) {
1048 $assigneeContacts = CRM_Activity_BAO_ActivityContact
::getNames($this->_activityId
, $assigneeID);
1049 $activityAssigned = array_diff_key($activityAssigned, $assigneeContacts);
1053 // call begin post process. Idea is to let injecting file do
1054 // any processing before the activity is added/updated.
1055 $this->beginPostProcess($params);
1057 $activity = CRM_Activity_BAO_Activity
::create($params);
1059 // add tags if exists
1060 $tagParams = array();
1061 if (!empty($params['tag'])) {
1062 if (!is_array($params['tag'])) {
1063 $params['tag'] = explode(',', $params['tag']);
1065 foreach ($params['tag'] as $tag) {
1066 $tagParams[$tag] = 1;
1070 // Save static tags.
1071 CRM_Core_BAO_EntityTag
::create($tagParams, 'civicrm_activity', $activity->id
);
1074 if (isset($params['activity_taglist']) && !empty($params['activity_taglist'])) {
1075 CRM_Core_Form_Tag
::postProcess($params['activity_taglist'], $activity->id
, 'civicrm_activity', $this);
1078 // call end post process. Idea is to let injecting file do any
1079 // processing needed, after the activity has been added/updated.
1080 $this->endPostProcess($params, $activity);
1083 if (!empty($params['is_multi_activity'])) {
1084 $this->_activityIds
[] = $activity->id
;
1087 $this->_activityId
= $activity->id
;
1090 // create follow up activity if needed
1091 $followupStatus = '';
1092 $followupActivity = NULL;
1093 if (!empty($params['followup_activity_type_id'])) {
1094 $followupActivity = CRM_Activity_BAO_Activity
::createFollowupActivity($activity->id
, $params);
1095 $followupStatus = ts('A followup activity has been scheduled.');
1098 // send copy to assignee contacts.CRM-4509
1101 if (Civi
::settings()->get('activity_assignee_notification')
1102 && !in_array($activity->activity_type_id
, Civi
::settings()->get('do_not_notify_assignees_for'))) {
1103 $activityIDs = array($activity->id
);
1104 if ($followupActivity) {
1105 $activityIDs = array_merge($activityIDs, array($followupActivity->id
));
1107 $assigneeContacts = CRM_Activity_BAO_ActivityAssignment
::getAssigneeNames($activityIDs, TRUE, FALSE);
1109 if (!CRM_Utils_Array
::crmIsEmptyArray($params['assignee_contact_id'])) {
1110 $mailToContacts = array();
1112 // Build an associative array with unique email addresses.
1113 foreach ($activityAssigned as $id => $dnc) {
1114 if (isset($id) && array_key_exists($id, $assigneeContacts)) {
1115 $mailToContacts[$assigneeContacts[$id]['email']] = $assigneeContacts[$id];
1119 $sent = CRM_Activity_BAO_Activity
::sendToAssignee($activity, $mailToContacts);
1121 $mailStatus .= ts("A copy of the activity has also been sent to assignee contacts(s).");
1125 // Also send email to follow-up activity assignees if set
1126 if ($followupActivity) {
1127 $mailToFollowupContacts = array();
1128 foreach ($assigneeContacts as $values) {
1129 if ($values['activity_id'] == $followupActivity->id
) {
1130 $mailToFollowupContacts[$values['email']] = $values;
1134 $sentFollowup = CRM_Activity_BAO_Activity
::sendToAssignee($followupActivity, $mailToFollowupContacts);
1135 if ($sentFollowup) {
1136 $mailStatus .= '<br />' . ts("A copy of the follow-up activity has also been sent to follow-up assignee contacts(s).");
1141 // set status message
1143 if (!empty($params['subject'])) {
1144 $subject = "'" . $params['subject'] . "'";
1147 CRM_Core_Session
::setStatus(ts('Activity %1 has been saved. %2 %3',
1150 2 => $followupStatus,
1153 ), ts('Saved'), 'success');
1159 * Shorthand for getting id by display name (makes code more readable)
1160 * @param $displayName
1161 * @return null|string
1163 protected function _getIdByDisplayName($displayName) {
1164 return CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
1172 * Shorthand for getting display name by id (makes code more readable)
1174 * @return null|string
1176 protected function _getDisplayNameById($id) {
1177 return CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
1185 * Let injecting activity type file do any processing.
1186 * needed, before the activity is added/updated
1188 * @param array $params
1190 public function beginPostProcess(&$params) {
1191 if ($this->_activityTypeFile
) {
1192 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
1193 $className::beginPostProcess($this, $params);
1198 * Let injecting activity type file do any processing
1199 * needed, after the activity has been added/updated
1201 * @param array $params
1204 public function endPostProcess(&$params, &$activity) {
1205 if ($this->_activityTypeFile
) {
1206 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
1207 $className::endPostProcess($this, $params, $activity);