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';
120 protected $_isSurveyActivity;
122 protected $_values = array();
124 protected $unsavedWarn = TRUE;
127 * The _fields var can be used by sub class to set/unset/edit the
128 * form fields based on their requirement
131 function setFields() {
132 $this->_fields
= array(
135 'label' => ts('Subject'),
136 'attributes' => CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity',
142 'label' => ts('Duration'),
143 'attributes' => array('size' => 4, 'maxlength' => 8),
148 'label' => ts('Location'),
150 CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity',
157 'label' => ts('Details'),
158 // forces a smaller edit window
159 'attributes' => array('rows' => 4, 'cols' => 60),
162 'status_id' => array(
166 'priority_id' => array(
170 'source_contact_id' => array(
171 'type' => 'entityRef',
172 'label' => ts('Added By'),
175 'target_contact_id' => array(
176 'type' => 'entityRef',
177 'label' => ts('With Contact'),
178 'attributes' => array('multiple' => TRUE, 'create' => TRUE)
180 'assignee_contact_id' => array(
181 'type' => 'entityRef',
182 'label' => ts('Assigned To'),
183 'attributes' => array('multiple' => TRUE, 'create' => TRUE, 'api' => array('params' => array('is_deceased' => 0))),
185 'followup_assignee_contact_id' => array(
186 'type' => 'entityRef',
187 'label' => ts('Assigned To'),
188 'attributes' => array('multiple' => TRUE, 'create' => TRUE, 'api' => array('params' => array('is_deceased' => 0))),
190 'followup_activity_type_id' => array(
192 'label' => ts('Followup Activity'),
193 'attributes' => array('' => '- ' . ts('select activity') . ' -') + CRM_Core_PseudoConstant
::ActivityType(FALSE),
194 'extra' => array('class' => 'crm-select2'),
196 // Add optional 'Subject' field for the Follow-up Activiity, CRM-4491
197 'followup_activity_subject' => array(
199 'label' => ts('Subject'),
200 'attributes' => CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity',
206 if (($this->_context
== 'standalone') &&
207 ($printPDF = CRM_Utils_Array
::key('Print PDF Letter', $this->_fields
['followup_activity_type_id']['attributes']))
209 unset($this->_fields
['followup_activity_type_id']['attributes'][$printPDF]);
214 * Function to build the form
219 function preProcess() {
220 $this->_cdType
= CRM_Utils_Array
::value('type', $_GET);
221 $this->assign('cdType', FALSE);
222 if ($this->_cdType
) {
223 $this->assign('cdType', TRUE);
224 return CRM_Custom_Form_CustomData
::preProcess($this);
227 $this->_atypefile
= CRM_Utils_Array
::value('atypefile', $_GET);
228 $this->assign('atypefile', FALSE);
229 if ($this->_atypefile
) {
230 $this->assign('atypefile', TRUE);
233 $session = CRM_Core_Session
::singleton();
234 $this->_currentUserId
= $session->get('userID');
236 $this->_currentlyViewedContactId
= $this->get('contactId');
237 if (!$this->_currentlyViewedContactId
) {
238 $this->_currentlyViewedContactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
240 $this->assign('contactId', $this->_currentlyViewedContactId
);
243 if (!isset($this->_context
)) {
244 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this);
245 if (CRM_Contact_Form_Search
::isSearchContext($this->_context
)) {
246 $this->_context
= 'search';
248 elseif (!in_array($this->_context
, array('dashlet', 'dashletFullscreen'))
249 && $this->_currentlyViewedContactId
251 $this->_context
= 'activity';
253 $this->_compContext
= CRM_Utils_Request
::retrieve('compContext', 'String', $this);
256 $this->assign('context', $this->_context
);
258 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this);
260 if ($this->_action
& CRM_Core_Action
::DELETE
) {
261 if (!CRM_Core_Permission
::check('delete activities')) {
262 CRM_Core_Error
::fatal(ts('You do not have permission to access this page'));
267 //when we come from contact search, activity id never comes.
268 //so don't try to get from object, it might gives you wrong one.
270 // if we're not adding new one, there must be an id to
271 // an activity we're trying to work on.
272 if ($this->_action
!= CRM_Core_Action
::ADD
&&
273 get_class($this->controller
) != 'CRM_Contact_Controller_Search'
275 $this->_activityId
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
278 $this->_activityTypeId
= CRM_Utils_Request
::retrieve('atype', 'Positive', $this);
279 $this->assign('atype', $this->_activityTypeId
);
281 $this->assign('activityId', $this->_activityId
);
283 //check for required permissions, CRM-6264
284 if ($this->_activityId
&&
285 in_array($this->_action
, array(
286 CRM_Core_Action
::UPDATE
,
287 CRM_Core_Action
::VIEW
289 !CRM_Activity_BAO_Activity
::checkPermission($this->_activityId
, $this->_action
)
291 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
293 if (($this->_action
& CRM_Core_Action
::VIEW
) &&
294 CRM_Activity_BAO_Activity
::checkPermission($this->_activityId
, CRM_Core_Action
::UPDATE
)
296 $this->assign('permission', 'edit');
299 if (!$this->_activityTypeId
&& $this->_activityId
) {
300 $this->_activityTypeId
= CRM_Core_DAO
::getFieldValue('CRM_Activity_DAO_Activity',
306 //Assigning Activity type name
307 if ($this->_activityTypeId
) {
308 $activityTName = CRM_Core_OptionGroup
::values('activity_type', FALSE, FALSE, FALSE, 'AND v.value = ' . $this->_activityTypeId
, 'name');
309 if ($activityTName[$this->_activityTypeId
]) {
310 $this->_activityTypeName
= $activityTName[$this->_activityTypeId
];
311 $this->assign('activityTName', $activityTName[$this->_activityTypeId
]);
316 if (isset($activityTName)) {
317 $activityName = CRM_Utils_Array
::value($this->_activityTypeId
, $activityTName);
318 $this->assign('pageTitle', ts('%1 Activity', array( 1 => $activityName)));
320 if ($this->_currentlyViewedContactId
) {
321 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_currentlyViewedContactId
);
322 // Check if this is default domain contact CRM-10482
323 if (CRM_Contact_BAO_Contact
::checkDomainContact($this->_currentlyViewedContactId
)) {
324 $displayName .= ' (' . ts('default organization') . ')';
326 CRM_Utils_System
::setTitle($displayName . ' - ' . $activityName);
329 CRM_Utils_System
::setTitle(ts('%1 Activity', array( 1 => $activityName)));
333 //check the mode when this form is called either single or as
335 if ($this->_activityTypeId ||
336 $this->_context
== 'standalone' ||
337 $this->_currentlyViewedContactId
339 $this->_single
= TRUE;
340 $this->assign('urlPath', 'civicrm/activity');
343 //set the appropriate action
344 $url = CRM_Utils_System
::currentPath();
345 $urlArray = explode('/', $url);
346 $searchPath = array_pop($urlArray);
347 $searchType = 'basic';
348 $this->_action
= CRM_Core_Action
::BASIC
;
349 switch ($searchPath) {
351 $searchType = $searchPath;
352 $this->_action
= CRM_Core_Action
::BASIC
;
356 $searchType = $searchPath;
357 $this->_action
= CRM_Core_Action
::ADVANCED
;
361 $searchType = $searchPath;
362 $this->_action
= CRM_Core_Action
::PROFILE
;
366 $this->_action
= CRM_Core_Action
::COPY
;
367 $searchType = $searchPath;
371 parent
::preProcess();
372 $this->_single
= FALSE;
374 $this->assign('urlPath', "civicrm/contact/search/$searchType");
375 $this->assign('urlPathVar', "_qf_Activity_display=true&qfKey={$this->controller->_key}");
378 $this->assign('single', $this->_single
);
379 $this->assign('action', $this->_action
);
381 if ($this->_action
& CRM_Core_Action
::VIEW
) {
382 // get the tree of custom fields
383 $this->_groupTree
= &CRM_Core_BAO_CustomGroup
::getTree('Activity', $this,
384 $this->_activityId
, 0, $this->_activityTypeId
388 if ($this->_activityTypeId
) {
389 //set activity type name and description to template
390 list($this->_activityTypeName
, $activityTypeDescription) = CRM_Core_BAO_OptionValue
::getActivityTypeDetails($this->_activityTypeId
);
391 $this->assign('activityTypeName', $this->_activityTypeName
);
392 $this->assign('activityTypeDescription', $activityTypeDescription);
396 $urlParams = $urlString = NULL;
397 $qfKey = CRM_Utils_Request
::retrieve('key', 'String', $this);
399 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $this);
403 if (!CRM_Utils_Rule
::qfKey($qfKey)) {
407 if ($this->_context
== 'fulltext') {
409 $urlParams = 'force=1';
410 $urlString = 'civicrm/contact/search/custom';
411 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
413 $urlParams .= '&context=fulltext&action=view';
414 $urlString = 'civicrm/contact/view/activity';
417 $urlParams .= "$keyName=$qfKey";
419 $this->assign('searchKey', $qfKey);
421 elseif (in_array($this->_context
, array(
428 $urlParams = 'reset=1';
429 $urlString = 'civicrm/dashboard';
431 elseif ($this->_context
== 'search') {
432 $urlParams = 'force=1';
434 $urlParams .= "&qfKey=$qfKey";
436 $path = CRM_Utils_System
::currentPath();
437 if ($this->_compContext
== 'advanced' ) {
438 $urlString = 'civicrm/contact/search/advanced';
440 elseif ($path == 'civicrm/contact/search'
441 ||
$path == 'civicrm/contact/search/advanced'
442 ||
$path == 'civicrm/contact/search/custom'
443 ||
$path == 'civicrm/group/search') {
447 $urlString = 'civicrm/activity/search';
449 $this->assign('searchKey', $qfKey);
451 elseif ($this->_context
!= 'caseActivity') {
452 $urlParams = "action=browse&reset=1&cid={$this->_currentlyViewedContactId}&selectedChild=activity";
453 $urlString = 'civicrm/contact/view';
457 $session->pushUserContext(CRM_Utils_System
::url($urlString, $urlParams));
460 // hack to retrieve activity type id from post variables
461 if (!$this->_activityTypeId
) {
462 $this->_activityTypeId
= CRM_Utils_Array
::value('activity_type_id', $_POST);
465 // when custom data is included in this page
466 if (!empty($_POST['hidden_custom'])) {
467 // we need to set it in the session for the below code to work
469 //need to assign custom data subtype to the template
470 $this->set('type', 'Activity');
471 $this->set('subType', $this->_activityTypeId
);
472 $this->set('entityId', $this->_activityId
);
473 CRM_Custom_Form_CustomData
::preProcess($this);
474 CRM_Custom_Form_CustomData
::buildQuickForm($this);
475 CRM_Custom_Form_CustomData
::setDefaultValues($this);
478 // add attachments part
479 CRM_Core_BAO_File
::buildAttachment($this, 'civicrm_activity', $this->_activityId
, NULL, TRUE);
481 // figure out the file name for activity type, if any
482 if ($this->_activityTypeId
&&
483 $this->_activityTypeFile
=
484 CRM_Activity_BAO_Activity
::getFileForActivityTypeId($this->_activityTypeId
, $this->_crmDir
)
486 $this->assign('activityTypeFile', $this->_activityTypeFile
);
487 $this->assign('crmDir', $this->_crmDir
);
492 if ($this->_activityTypeFile
) {
493 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
494 $className::preProcess($this);
497 $this->_values
= $this->get('values');
498 if (!is_array($this->_values
)) {
499 $this->_values
= array();
500 if (isset($this->_activityId
) && $this->_activityId
) {
501 $params = array('id' => $this->_activityId
);
502 CRM_Activity_BAO_Activity
::retrieve($params, $this->_values
);
504 $this->set('values', $this->_values
);
509 * This function sets the default values for the form. For edit/view mode
510 * the default values are retrieved from the database
516 function setDefaultValues() {
517 if ($this->_cdType
) {
518 return CRM_Custom_Form_CustomData
::setDefaultValues($this);
521 $defaults = $this->_values
;
522 // if we're editing...
523 if (isset($this->_activityId
)) {
524 if (empty($defaults['activity_date_time'])) {
525 list($defaults['activity_date_time'], $defaults['activity_date_time_time']) = CRM_Utils_Date
::setDateDefaults(NULL, 'activityDateTime');
527 elseif ($this->_action
& CRM_Core_Action
::UPDATE
) {
528 $this->assign('current_activity_date_time', $defaults['activity_date_time']);
529 list($defaults['activity_date_time'],
530 $defaults['activity_date_time_time']
531 ) = CRM_Utils_Date
::setDateDefaults($defaults['activity_date_time'], 'activityDateTime');
534 if ($this->_context
!= 'standalone') {
535 $this->assign('target_contact_value',
536 CRM_Utils_Array
::value('target_contact_value', $defaults)
538 $this->assign('assignee_contact_value',
539 CRM_Utils_Array
::value('assignee_contact_value', $defaults)
543 // Fixme: why are we getting the wrong keys from upstream?
544 $defaults['target_contact_id'] = CRM_Utils_Array
::value('target_contact', $defaults);
545 $defaults['assignee_contact_id'] = CRM_Utils_Array
::value('assignee_contact', $defaults);
547 // set default tags if exists
548 $defaults['tag'] = CRM_Core_BAO_EntityTag
::getTag($this->_activityId
, 'civicrm_activity');
551 // if it's a new activity, we need to set default values for associated contact fields
552 $this->_sourceContactId
= $this->_currentUserId
;
553 $this->_targetContactId
= $this->_currentlyViewedContactId
;
555 $defaults['source_contact_id'] = $this->_sourceContactId
;
556 $defaults['target_contact_id'] = $this->_targetContactId
;
558 list($defaults['activity_date_time'], $defaults['activity_date_time_time']) =
559 CRM_Utils_Date
::setDateDefaults(NULL, 'activityDateTime');
562 if ($this->_activityTypeId
) {
563 $defaults['activity_type_id'] = $this->_activityTypeId
;
566 if (!$this->_single
&& !empty($this->_contactIds
)) {
567 $defaults['target_contact_id'] = $this->_contactIds
;
570 // CRM-15472 - 50 is around the practial limit of how many items a select2 entityRef can handle
571 if (!empty($defaults['target_contact_id'])) {
572 $count = count(is_array($defaults['target_contact_id']) ?
$defaults['target_contact_id'] : explode(',', $defaults['target_contact_id']));
574 $this->freeze(array('target_contact_id'));
578 if ($this->_action
& (CRM_Core_Action
::DELETE | CRM_Core_Action
::RENEW
)) {
579 $this->assign('delName', CRM_Utils_Array
::value('subject', $defaults));
582 if ($this->_activityTypeFile
) {
583 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
584 $defaults +
= $className::setDefaultValues($this);
586 if (empty($defaults['priority_id'])) {
587 $priority = CRM_Core_PseudoConstant
::get('CRM_Activity_DAO_Activity', 'priority_id');
588 $defaults['priority_id'] = array_search('Normal', $priority);
590 if (empty($defaults['status_id'])) {
591 $defaults['status_id'] = CRM_Core_OptionGroup
::getDefaultValue('activity_status');
596 public function buildQuickForm() {
597 if ($this->_action
& (CRM_Core_Action
::DELETE | CRM_Core_Action
::RENEW
)) {
598 //enable form element (ActivityLinks sets this true)
599 $this->assign('suppressForm', FALSE);
601 $button = ts('Delete');
602 if ($this->_action
& CRM_Core_Action
::RENEW
) {
603 $button = ts('Restore');
605 $this->addButtons(array(
609 'spacing' => ' ',
614 'name' => ts('Cancel')
620 if ($this->_cdType
) {
621 return CRM_Custom_Form_CustomData
::buildQuickForm($this);
624 //build other activity links
625 CRM_Activity_Form_ActivityLinks
::commonBuildQuickForm($this);
627 //enable form element (ActivityLinks sets this true)
628 $this->assign('suppressForm', FALSE);
630 $element = & $this->add('select', 'activity_type_id', ts('Activity Type'),
631 $this->_fields
['followup_activity_type_id']['attributes'],
633 'onchange' => "CRM.buildCustomData( 'Activity', this.value );",
634 'class' => 'crm-select2',
638 //freeze for update mode.
639 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
643 foreach ($this->_fields
as $field => $values) {
644 if (!empty($this->_fields
[$field])) {
645 $attribute = CRM_Utils_Array
::value('attributes', $values);
646 $required = !empty($values['required']);
648 if ($values['type'] == 'wysiwyg') {
649 $this->addWysiwyg($field, $values['label'], $attribute, $required);
651 elseif ($values['type'] == 'select' && empty($attribute)) {
652 $this->addSelect($field, array('entity' => 'activity'), $required);
654 elseif ($values['type'] == 'entityRef') {
655 $this->addEntityRef($field, $values['label'], $attribute, $required);
658 $this->add($values['type'], $field, $values['label'], $attribute, $required, CRM_Utils_Array
::value('extra', $values));
663 //CRM-7362 --add campaigns.
664 CRM_Campaign_BAO_Campaign
::addCampaign($this, CRM_Utils_Array
::value('campaign_id', $this->_values
));
666 //add engagement level CRM-7775
667 $buildEngagementLevel = FALSE;
668 if (CRM_Campaign_BAO_Campaign
::isCampaignEnable() &&
669 CRM_Campaign_BAO_Campaign
::accessCampaign()
671 $buildEngagementLevel = TRUE;
672 $this->addSelect('engagement_level',array('entity' => 'activity'));
673 $this->addRule('engagement_level',
674 ts('Please enter the engagement index as a number (integers only).'),
678 $this->assign('buildEngagementLevel', $buildEngagementLevel);
680 // check for survey activity
681 $this->_isSurveyActivity
= FALSE;
683 if ($this->_activityId
&& CRM_Campaign_BAO_Campaign
::isCampaignEnable() &&
684 CRM_Campaign_BAO_Campaign
::accessCampaign()
687 $this->_isSurveyActivity
= CRM_Campaign_BAO_Survey
::isSurveyActivity($this->_activityId
);
688 if ($this->_isSurveyActivity
) {
689 $surveyId = CRM_Core_DAO
::getFieldValue('CRM_Activity_DAO_Activity',
693 $responseOptions = CRM_Campaign_BAO_Survey
::getResponsesOptions($surveyId);
694 if ($responseOptions) {
695 $this->add('select', 'result', ts('Result'),
696 array('' => ts('- select -')) +
array_combine($responseOptions, $responseOptions)
701 $surveyTitle = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'title');
703 $this->assign('surveyTitle', $surveyTitle);
706 $this->assign('surveyActivity', $this->_isSurveyActivity
);
708 // this option should be available only during add mode
709 if ($this->_action
!= CRM_Core_Action
::UPDATE
) {
710 $this->add('advcheckbox', 'is_multi_activity', ts('Create a separate activity for each contact.'));
713 $this->addRule('duration',
714 ts('Please enter the duration as number of minutes (integers only).'), 'positiveInteger'
716 $this->addDateTime('activity_date_time', ts('Date'), TRUE, array('formatType' => 'activityDateTime'));
719 $this->addDateTime('followup_date', ts('in'), FALSE, array('formatType' => 'activityDateTime'));
721 // Only admins and case-workers can change the activity source
722 if (!CRM_Core_Permission
::check('administer CiviCRM') && $this->_context
!= 'caseActivity') {
723 $this->getElement('source_contact_id')->freeze();
726 //need to assign custom data type and subtype to the template
727 $this->assign('customDataType', 'Activity');
728 $this->assign('customDataSubType', $this->_activityTypeId
);
729 $this->assign('entityID', $this->_activityId
);
731 $tags = CRM_Core_BAO_Tag
::getTags('civicrm_activity');
734 $this->add('select', 'tag', ts('Tags'), $tags, FALSE,
735 array('id' => 'tags', 'multiple' => 'multiple', 'class' => 'crm-select2 huge')
739 // we need to hide activity tagset for special activities
740 $specialActivities = array('Open Case');
742 if (!in_array($this->_activityTypeName
, $specialActivities)) {
744 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_activity');
745 CRM_Core_Form_Tag
::buildQuickForm($this, $parentNames, 'civicrm_activity', $this->_activityId
);
748 // if we're viewing, we're assigning different buttons than for adding/editing
749 if ($this->_action
& CRM_Core_Action
::VIEW
) {
750 if (isset($this->_groupTree
)) {
751 CRM_Core_BAO_CustomGroup
::buildCustomDataView($this, $this->_groupTree
);
753 // form should be frozen for view mode
761 $this->addButtons($buttons);
765 'completed' => ts('Are you sure? This is a COMPLETED activity with the DATE in the FUTURE. Click Cancel to change the date / status. Otherwise, click OK to save.'),
766 'scheduled' => ts('Are you sure? This is a SCHEDULED activity with the DATE in the PAST. Click Cancel to change the date / status. Otherwise, click OK to save.'),
768 $js = array('onclick' => "return activityStatus(" . json_encode($message) . ");");
769 $this->addButtons(array(
772 'name' => ts('Save'),
778 'name' => ts('Cancel')
784 if ($this->_activityTypeFile
) {
785 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
787 $className::buildQuickForm($this);
788 $this->addFormRule(array($className, 'formRule'), $this);
791 $this->addFormRule(array('CRM_Activity_Form_Activity', 'formRule'), $this);
793 if (CRM_Core_BAO_Setting
::getItem(
794 CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
795 'activity_assignee_notification'
798 $this->assign('activityAssigneeNotification', TRUE);
801 $this->assign('activityAssigneeNotification', FALSE);
808 * @param array $fields the input form values
809 * @param array $files the uploaded files if any
812 * @internal param array $options additional user data
814 * @return true if no errors, else array of errors
818 static function formRule($fields, $files, $self) {
819 // skip form rule if deleting
820 if (CRM_Utils_Array
::value('_qf_Activity_next_', $fields) == 'Delete') {
824 if (!$self->_single
&& !$fields['activity_type_id']) {
825 $errors['activity_type_id'] = ts('Activity Type is a required field');
828 //Activity type is mandatory if creating new activity, CRM-4515
829 if (array_key_exists('activity_type_id', $fields) && empty($fields['activity_type_id'])) {
830 $errors['activity_type_id'] = ts('Activity Type is required field.');
833 if (CRM_Utils_Array
::value('activity_type_id', $fields) == 3 &&
834 CRM_Utils_Array
::value('status_id', $fields) == 1
836 $errors['status_id'] = ts('You cannot record scheduled email activity.');
838 elseif (CRM_Utils_Array
::value('activity_type_id', $fields) == 4 &&
839 CRM_Utils_Array
::value('status_id', $fields) == 1
841 $errors['status_id'] = ts('You cannot record scheduled SMS activity.');
844 if (!empty($fields['followup_activity_type_id']) && empty($fields['followup_date'])) {
845 $errors['followup_date_time'] = ts('Followup date is a required field.');
847 //Activity type is mandatory if subject or follow-up date is specified for an Follow-up activity, CRM-4515
848 if ((!empty($fields['followup_activity_subject']) ||
!empty($fields['followup_date'])) && empty($fields['followup_activity_type_id'])) {
849 $errors['followup_activity_subject'] = ts('Follow-up Activity type is a required field.');
855 * Function to process the form
859 * @param null $params
862 public function postProcess($params = NULL) {
863 if ($this->_action
& CRM_Core_Action
::DELETE
) {
864 $deleteParams = array('id' => $this->_activityId
);
865 $moveToTrash = CRM_Case_BAO_Case
::isCaseActivity($this->_activityId
);
866 CRM_Activity_BAO_Activity
::deleteActivity($deleteParams, $moveToTrash);
868 // delete tags for the entity
870 'entity_table' => 'civicrm_activity',
871 'entity_id' => $this->_activityId
874 CRM_Core_BAO_EntityTag
::del($tagParams);
876 CRM_Core_Session
::setStatus(ts("Selected Activity has been deleted successfully."), ts('Record Deleted'), 'success');
880 // store the submitted values in an array
882 $params = $this->controller
->exportValues($this->_name
);
885 //set activity type id
886 if (empty($params['activity_type_id'])) {
887 $params['activity_type_id'] = $this->_activityTypeId
;
890 if (!empty($params['hidden_custom']) &&
891 !isset($params['custom'])
893 $customFields = CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
894 $this->_activityTypeId
896 $customFields = CRM_Utils_Array
::crmArrayMerge($customFields,
897 CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
901 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
908 // store the date with proper format
909 $params['activity_date_time'] = CRM_Utils_Date
::processDate($params['activity_date_time'], $params['activity_date_time_time']);
911 // format params as arrays
912 foreach (array('target', 'assignee', 'followup_assignee') as $name) {
913 if (!empty($params["{$name}_contact_id"])) {
914 $params["{$name}_contact_id"] = explode(',', $params["{$name}_contact_id"]);
917 $params["{$name}_contact_id"] = array();
921 // get ids for associated contacts
922 if (!$params['source_contact_id']) {
923 $params['source_contact_id'] = $this->_currentUserId
;
926 if (isset($this->_activityId
)) {
927 $params['id'] = $this->_activityId
;
930 // add attachments as needed
931 CRM_Core_BAO_File
::formatAttachment($params,
938 if (!empty($params['is_multi_activity']) &&
939 !CRM_Utils_Array
::crmIsEmptyArray($params['target_contact_id'])
941 $targetContacts = $params['target_contact_id'];
942 foreach ($targetContacts as $targetContactId) {
943 $params['target_contact_id'] = array($targetContactId);
945 $activity[] = $this->processActivity($params);
950 $activity = $this->processActivity($params);
953 return array('activity' => $activity);
957 * Process activity creation
959 * @param array $params associated array of submitted values
961 * @return $this|null|object
964 protected function processActivity(&$params) {
965 $activityAssigned = array();
966 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
967 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
968 // format assignee params
969 if (!CRM_Utils_Array
::crmIsEmptyArray($params['assignee_contact_id'])) {
970 //skip those assignee contacts which are already assigned
971 //while sending a copy.CRM-4509.
972 $activityAssigned = array_flip($params['assignee_contact_id']);
973 if ($this->_activityId
) {
974 $assigneeContacts = CRM_Activity_BAO_ActivityContact
::getNames($this->_activityId
, $assigneeID);
975 $activityAssigned = array_diff_key($activityAssigned, $assigneeContacts);
979 // call begin post process. Idea is to let injecting file do
980 // any processing before the activity is added/updated.
981 $this->beginPostProcess($params);
983 $activity = CRM_Activity_BAO_Activity
::create($params);
985 // add tags if exists
986 $tagParams = array();
987 if (!empty($params['tag'])) {
988 foreach ($params['tag'] as $tag) {
989 $tagParams[$tag] = 1;
994 CRM_Core_BAO_EntityTag
::create($tagParams, 'civicrm_activity', $activity->id
);
997 if (isset($params['activity_taglist']) && !empty($params['activity_taglist'])) {
998 CRM_Core_Form_Tag
::postProcess($params['activity_taglist'], $activity->id
, 'civicrm_activity', $this);
1001 // call end post process. Idea is to let injecting file do any
1002 // processing needed, after the activity has been added/updated.
1003 $this->endPostProcess($params, $activity);
1006 if (!empty($params['is_multi_activity'])) {
1007 $this->_activityIds
[] = $activity->id
;
1010 $this->_activityId
= $activity->id
;
1013 // create follow up activity if needed
1014 $followupStatus = '';
1015 $followupActivity = NULL;
1016 if (!empty($params['followup_activity_type_id'])) {
1017 $followupActivity = CRM_Activity_BAO_Activity
::createFollowupActivity($activity->id
, $params);
1018 $followupStatus = ts('A followup activity has been scheduled.');
1021 // send copy to assignee contacts.CRM-4509
1024 if (CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
1025 'activity_assignee_notification')) {
1026 $activityIDs = array($activity->id
);
1027 if ($followupActivity) {
1028 $activityIDs = array_merge($activityIDs, array($followupActivity->id
));
1030 $assigneeContacts = CRM_Activity_BAO_ActivityAssignment
::getAssigneeNames($activityIDs, TRUE, FALSE);
1032 if (!CRM_Utils_Array
::crmIsEmptyArray($params['assignee_contact_id'])) {
1033 $mailToContacts = array();
1035 //build an associative array with unique email addresses.
1036 foreach ($activityAssigned as $id => $dnc) {
1037 if (isset($id) && array_key_exists($id, $assigneeContacts)) {
1038 $mailToContacts[$assigneeContacts[$id]['email']] = $assigneeContacts[$id];
1042 if (!CRM_Utils_array
::crmIsEmptyArray($mailToContacts)) {
1043 //include attachments while sending a copy of activity.
1044 $attachments = CRM_Core_BAO_File
::getEntityFile('civicrm_activity', $activity->id
);
1046 $ics = new CRM_Activity_BAO_ICalendar($activity);
1047 $ics->addAttachment($attachments, $mailToContacts);
1049 // CRM-8400 add param with _currentlyViewedContactId for URL link in mail
1050 CRM_Case_BAO_Case
::sendActivityCopy(NULL, $activity->id
, $mailToContacts, $attachments, NULL);
1054 $mailStatus .= ts("A copy of the activity has also been sent to assignee contacts(s).");
1058 // Also send email to follow-up activity assignees if set
1059 if ($followupActivity) {
1060 $mailToFollowupContacts = array();
1061 foreach ($assigneeContacts as $values) {
1062 if ($values['activity_id'] == $followupActivity->id
) {
1063 $mailToFollowupContacts[$values['email']] = $values;
1067 if (!CRM_Utils_array
::crmIsEmptyArray($mailToFollowupContacts)) {
1068 $ics = new CRM_Activity_BAO_ICalendar($followupActivity);
1069 $attachments = CRM_Core_BAO_File
::getEntityFile('civicrm_activity', $followupActivity->id
);
1070 $ics->addAttachment($attachments, $mailToFollowupContacts);
1072 CRM_Case_BAO_Case
::sendActivityCopy(NULL, $followupActivity->id
, $mailToFollowupContacts, $attachments, NULL);
1076 $mailStatus .= '<br />' . ts("A copy of the follow-up activity has also been sent to follow-up assignee contacts(s).");
1081 // set status message
1083 if (!empty($params['subject'])) {
1084 $subject = "'" . $params['subject'] . "'";
1087 CRM_Core_Session
::setStatus(ts('Activity %1 has been saved. %2 %3',
1090 2 => $followupStatus,
1093 ), ts('Saved'), 'success');
1099 * Shorthand for getting id by display name (makes code more readable)
1103 protected function _getIdByDisplayName($displayName) {
1104 return CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
1112 * Shorthand for getting display name by id (makes code more readable)
1116 protected function _getDisplayNameById($id) {
1117 return CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
1125 * Function to let injecting activity type file do any processing
1126 * needed, before the activity is added/updated
1129 function beginPostProcess(&$params) {
1130 if ($this->_activityTypeFile
) {
1131 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
1132 $className::beginPostProcess($this, $params);
1137 * Function to let injecting activity type file do any processing
1138 * needed, after the activity has been added/updated
1141 function endPostProcess(&$params, &$activity) {
1142 if ($this->_activityTypeFile
) {
1143 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
1144 $className::endPostProcess($this, $params, $activity );