3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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();
125 * The _fields var can be used by sub class to set/unset/edit the
126 * form fields based on their requirement
129 function setFields() {
130 $this->_fields
= array(
133 'label' => ts('Subject'),
134 'attributes' => CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity',
140 'label' => ts('Duration'),
141 'attributes' => array('size' => 4, 'maxlength' => 8),
146 'label' => ts('Location'),
148 CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity',
155 'label' => ts('Details'),
156 // forces a smaller edit window
157 'attributes' => array('rows' => 4, 'cols' => 60),
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('multiple' => TRUE, 'create' => TRUE),
183 'followup_assignee_contact_id' => array(
184 'type' => 'entityRef',
185 'label' => ts('Assigned To'),
186 'attributes' => array('multiple' => TRUE, 'create' => TRUE),
188 'followup_activity_type_id' => array(
190 'label' => ts('Followup Activity'),
191 'attributes' => array('' => '- ' . ts('select activity') . ' -') + CRM_Core_PseudoConstant
::ActivityType(FALSE),
192 'extra' => array('class' => 'crm-select2'),
194 // Add optional 'Subject' field for the Follow-up Activiity, CRM-4491
195 'followup_activity_subject' => array(
197 'label' => ts('Subject'),
198 'attributes' => CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity',
204 if (($this->_context
== 'standalone') &&
205 ($printPDF = CRM_Utils_Array
::key('Print PDF Letter', $this->_fields
['followup_activity_type_id']['attributes']))
207 unset($this->_fields
['followup_activity_type_id']['attributes'][$printPDF]);
212 * Function to build the form
217 function preProcess() {
218 $this->_cdType
= CRM_Utils_Array
::value('type', $_GET);
219 $this->assign('cdType', FALSE);
220 if ($this->_cdType
) {
221 $this->assign('cdType', TRUE);
222 return CRM_Custom_Form_CustomData
::preProcess($this);
225 $this->_atypefile
= CRM_Utils_Array
::value('atypefile', $_GET);
226 $this->assign('atypefile', FALSE);
227 if ($this->_atypefile
) {
228 $this->assign('atypefile', TRUE);
231 $session = CRM_Core_Session
::singleton();
232 $this->_currentUserId
= $session->get('userID');
234 $this->_currentlyViewedContactId
= $this->get('contactId');
235 if (!$this->_currentlyViewedContactId
) {
236 $this->_currentlyViewedContactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
238 $this->assign('contactId', $this->_currentlyViewedContactId
);
241 if (!isset($this->_context
)) {
242 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this);
243 if (CRM_Contact_Form_Search
::isSearchContext($this->_context
)) {
244 $this->_context
= 'search';
246 elseif (!in_array($this->_context
, array('dashlet', 'dashletFullscreen'))
247 && $this->_currentlyViewedContactId
249 $this->_context
= 'activity';
251 $this->_compContext
= CRM_Utils_Request
::retrieve('compContext', 'String', $this);
254 $this->assign('context', $this->_context
);
256 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this);
258 if ($this->_action
& CRM_Core_Action
::DELETE
) {
259 if (!CRM_Core_Permission
::check('delete activities')) {
260 CRM_Core_Error
::fatal(ts('You do not have permission to access this page'));
265 //when we come from contact search, activity id never comes.
266 //so don't try to get from object, it might gives you wrong one.
268 // if we're not adding new one, there must be an id to
269 // an activity we're trying to work on.
270 if ($this->_action
!= CRM_Core_Action
::ADD
&&
271 get_class($this->controller
) != 'CRM_Contact_Controller_Search'
273 $this->_activityId
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
276 $this->_activityTypeId
= CRM_Utils_Request
::retrieve('atype', 'Positive', $this);
277 $this->assign('atype', $this->_activityTypeId
);
279 //check for required permissions, CRM-6264
280 if ($this->_activityId
&&
281 in_array($this->_action
, array(
282 CRM_Core_Action
::UPDATE
,
283 CRM_Core_Action
::VIEW
285 !CRM_Activity_BAO_Activity
::checkPermission($this->_activityId
, $this->_action
)
287 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
289 if (($this->_action
& CRM_Core_Action
::VIEW
) &&
290 CRM_Activity_BAO_Activity
::checkPermission($this->_activityId
, CRM_Core_Action
::UPDATE
)
292 $this->assign('permission', 'edit');
295 if (!$this->_activityTypeId
&& $this->_activityId
) {
296 $this->_activityTypeId
= CRM_Core_DAO
::getFieldValue('CRM_Activity_DAO_Activity',
302 //Assigning Activity type name
303 if ($this->_activityTypeId
) {
304 $activityTName = CRM_Core_OptionGroup
::values('activity_type', FALSE, FALSE, FALSE, 'AND v.value = ' . $this->_activityTypeId
, 'name');
305 if ($activityTName[$this->_activityTypeId
]) {
306 $this->_activityTypeName
= $activityTName[$this->_activityTypeId
];
307 $this->assign('activityTName', $activityTName[$this->_activityTypeId
]);
312 if (isset($activityTName)) {
313 $activityName = CRM_Utils_Array
::value($this->_activityTypeId
, $activityTName);
314 $this->assign('pageTitle', ts('%1 Activity', array( 1 => $activityName)));
316 if ($this->_currentlyViewedContactId
) {
317 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_currentlyViewedContactId
);
318 // Check if this is default domain contact CRM-10482
319 if (CRM_Contact_BAO_Contact
::checkDomainContact($this->_currentlyViewedContactId
)) {
320 $displayName .= ' (' . ts('default organization') . ')';
322 CRM_Utils_System
::setTitle($displayName . ' - ' . $activityName);
325 CRM_Utils_System
::setTitle(ts('%1 Activity', array( 1 => $activityName)));
329 //check the mode when this form is called either single or as
331 if ($this->_activityTypeId ||
332 $this->_context
== 'standalone' ||
333 $this->_currentlyViewedContactId
335 $this->_single
= TRUE;
336 $this->assign('urlPath', 'civicrm/activity');
339 //set the appropriate action
340 $url = CRM_Utils_System
::currentPath();
341 $urlArray = explode('/', $url);
342 $searchPath = array_pop($urlArray);
343 $searchType = 'basic';
344 $this->_action
= CRM_Core_Action
::BASIC
;
345 switch ($searchPath) {
347 $searchType = $searchPath;
348 $this->_action
= CRM_Core_Action
::BASIC
;
352 $searchType = $searchPath;
353 $this->_action
= CRM_Core_Action
::ADVANCED
;
357 $searchType = $searchPath;
358 $this->_action
= CRM_Core_Action
::PROFILE
;
362 $this->_action
= CRM_Core_Action
::COPY
;
363 $searchType = $searchPath;
367 parent
::preProcess();
368 $this->_single
= FALSE;
370 $this->assign('urlPath', "civicrm/contact/search/$searchType");
371 $this->assign('urlPathVar', "_qf_Activity_display=true&qfKey={$this->controller->_key}");
374 $this->assign('single', $this->_single
);
375 $this->assign('action', $this->_action
);
377 if ($this->_action
& CRM_Core_Action
::VIEW
) {
378 // get the tree of custom fields
379 $this->_groupTree
= &CRM_Core_BAO_CustomGroup
::getTree('Activity', $this,
380 $this->_activityId
, 0, $this->_activityTypeId
384 if ($this->_activityTypeId
) {
385 //set activity type name and description to template
386 list($this->_activityTypeName
, $activityTypeDescription) = CRM_Core_BAO_OptionValue
::getActivityTypeDetails($this->_activityTypeId
);
387 $this->assign('activityTypeName', $this->_activityTypeName
);
388 $this->assign('activityTypeDescription', $activityTypeDescription);
392 $urlParams = $urlString = NULL;
393 $qfKey = CRM_Utils_Request
::retrieve('key', 'String', $this);
395 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $this);
399 if (!CRM_Utils_Rule
::qfKey($qfKey)) {
403 if ($this->_context
== 'fulltext') {
405 $urlParams = 'force=1';
406 $urlString = 'civicrm/contact/search/custom';
407 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
409 $urlParams .= '&context=fulltext&action=view';
410 $urlString = 'civicrm/contact/view/activity';
413 $urlParams .= "$keyName=$qfKey";
415 $this->assign('searchKey', $qfKey);
417 elseif (in_array($this->_context
, array(
424 $urlParams = 'reset=1';
425 $urlString = 'civicrm/dashboard';
427 elseif ($this->_context
== 'search') {
428 $urlParams = 'force=1';
430 $urlParams .= "&qfKey=$qfKey";
432 $path = CRM_Utils_System
::currentPath();
433 if ($this->_compContext
== 'advanced' ) {
434 $urlString = 'civicrm/contact/search/advanced';
436 elseif ($path == 'civicrm/contact/search'
437 ||
$path == 'civicrm/contact/search/advanced'
438 ||
$path == 'civicrm/contact/search/custom') {
442 $urlString = 'civicrm/activity/search';
444 $this->assign('searchKey', $qfKey);
446 elseif ($this->_context
!= 'caseActivity') {
447 $urlParams = "action=browse&reset=1&cid={$this->_currentlyViewedContactId}&selectedChild=activity";
448 $urlString = 'civicrm/contact/view';
452 $session->pushUserContext(CRM_Utils_System
::url($urlString, $urlParams));
455 // hack to retrieve activity type id from post variables
456 if (!$this->_activityTypeId
) {
457 $this->_activityTypeId
= CRM_Utils_Array
::value('activity_type_id', $_POST);
460 // when custom data is included in this page
461 if (!empty($_POST['hidden_custom'])) {
462 // we need to set it in the session for the below code to work
464 //need to assign custom data subtype to the template
465 $this->set('type', 'Activity');
466 $this->set('subType', $this->_activityTypeId
);
467 $this->set('entityId', $this->_activityId
);
468 CRM_Custom_Form_CustomData
::preProcess($this);
469 CRM_Custom_Form_CustomData
::buildQuickForm($this);
470 CRM_Custom_Form_CustomData
::setDefaultValues($this);
473 // add attachments part
474 CRM_Core_BAO_File
::buildAttachment($this, 'civicrm_activity', $this->_activityId
, NULL, TRUE);
476 // figure out the file name for activity type, if any
477 if ($this->_activityTypeId
&&
478 $this->_activityTypeFile
=
479 CRM_Activity_BAO_Activity
::getFileForActivityTypeId($this->_activityTypeId
, $this->_crmDir
)
481 $this->assign('activityTypeFile', $this->_activityTypeFile
);
482 $this->assign('crmDir', $this->_crmDir
);
487 if ($this->_activityTypeFile
) {
488 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
489 $className::preProcess($this);
492 $this->_values
= $this->get('values');
493 if (!is_array($this->_values
)) {
494 $this->_values
= array();
495 if (isset($this->_activityId
) && $this->_activityId
) {
496 $params = array('id' => $this->_activityId
);
497 CRM_Activity_BAO_Activity
::retrieve($params, $this->_values
);
499 $this->set('values', $this->_values
);
504 * This function sets the default values for the form. For edit/view mode
505 * the default values are retrieved from the database
511 function setDefaultValues() {
512 if ($this->_cdType
) {
513 return CRM_Custom_Form_CustomData
::setDefaultValues($this);
516 $defaults = $this->_values
;
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');
530 if ($this->_context
!= 'standalone') {
531 $this->assign('target_contact_value',
532 CRM_Utils_Array
::value('target_contact_value', $defaults)
534 $this->assign('assignee_contact_value',
535 CRM_Utils_Array
::value('assignee_contact_value', $defaults)
539 // Fixme: why are we getting the wrong keys from upstream?
540 $defaults['target_contact_id'] = $defaults['target_contact'];
541 $defaults['assignee_contact_id'] = $defaults['assignee_contact'];
543 // set default tags if exists
544 $defaults['tag'] = CRM_Core_BAO_EntityTag
::getTag($this->_activityId
, 'civicrm_activity');
547 // if it's a new activity, we need to set default values for associated contact fields
548 $this->_sourceContactId
= $this->_currentUserId
;
549 $this->_targetContactId
= $this->_currentlyViewedContactId
;
551 $defaults['source_contact_id'] = $this->_sourceContactId
;
552 $defaults['target_contact_id'] = $this->_targetContactId
;
554 list($defaults['activity_date_time'], $defaults['activity_date_time_time']) =
555 CRM_Utils_Date
::setDateDefaults(NULL, 'activityDateTime');
558 if ($this->_activityTypeId
) {
559 $defaults['activity_type_id'] = $this->_activityTypeId
;
562 if ($this->_action
& (CRM_Core_Action
::DELETE | CRM_Core_Action
::RENEW
)) {
563 $this->assign('delName', CRM_Utils_Array
::value('subject', $defaults));
566 if ($this->_activityTypeFile
) {
567 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
568 $defaults +
= $className::setDefaultValues($this);
570 if (empty($defaults['priority_id'])) {
571 $priority = CRM_Core_PseudoConstant
::get('CRM_Activity_DAO_Activity', 'priority_id');
572 $defaults['priority_id'] = array_search('Normal', $priority);
574 if (empty($defaults['status_id'])) {
575 $defaults['status_id'] = CRM_Core_OptionGroup
::getDefaultValue('activity_status');
580 public function buildQuickForm() {
581 if ($this->_action
& (CRM_Core_Action
::DELETE | CRM_Core_Action
::RENEW
)) {
582 //enable form element (ActivityLinks sets this true)
583 $this->assign('suppressForm', FALSE);
585 $button = ts('Delete');
586 if ($this->_action
& CRM_Core_Action
::RENEW
) {
587 $button = ts('Restore');
589 $this->addButtons(array(
593 'spacing' => ' ',
598 'name' => ts('Cancel')
604 if ($this->_cdType
) {
605 return CRM_Custom_Form_CustomData
::buildQuickForm($this);
608 //build other activity links
609 CRM_Activity_Form_ActivityLinks
::commonBuildQuickForm($this);
611 //enable form element (ActivityLinks sets this true)
612 $this->assign('suppressForm', FALSE);
614 $element = & $this->add('select', 'activity_type_id', ts('Activity Type'),
615 $this->_fields
['followup_activity_type_id']['attributes'],
617 'onchange' => "CRM.buildCustomData( 'Activity', this.value );",
618 'class' => 'crm-select2',
622 //freeze for update mode.
623 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
627 foreach ($this->_fields
as $field => $values) {
628 if (!empty($this->_fields
[$field])) {
629 $attribute = CRM_Utils_Array
::value('attributes', $values);
630 $required = !empty($values['required']);
632 if ($values['type'] == 'wysiwyg') {
633 $this->addWysiwyg($field, $values['label'], $attribute, $required);
635 elseif ($values['type'] == 'select' && empty($attribute)) {
636 $this->addSelect($field, array('entity' => 'activity'), $required);
638 elseif ($values['type'] == 'entityRef') {
639 $this->addEntityRef($field, $values['label'], $attribute, $required);
642 $this->add($values['type'], $field, $values['label'], $attribute, $required, CRM_Utils_Array
::value('extra', $values));
647 //CRM-7362 --add campaigns.
648 CRM_Campaign_BAO_Campaign
::addCampaign($this, CRM_Utils_Array
::value('campaign_id', $this->_values
));
650 //add engagement level CRM-7775
651 $buildEngagementLevel = FALSE;
652 if (CRM_Campaign_BAO_Campaign
::isCampaignEnable() &&
653 CRM_Campaign_BAO_Campaign
::accessCampaign()
655 $buildEngagementLevel = TRUE;
656 $this->addSelect('engagement_level',array('entity' => 'activity'));
657 $this->addRule('engagement_level',
658 ts('Please enter the engagement index as a number (integers only).'),
662 $this->assign('buildEngagementLevel', $buildEngagementLevel);
664 // check for survey activity
665 $this->_isSurveyActivity
= FALSE;
667 if ($this->_activityId
&& CRM_Campaign_BAO_Campaign
::isCampaignEnable() &&
668 CRM_Campaign_BAO_Campaign
::accessCampaign()
671 $this->_isSurveyActivity
= CRM_Campaign_BAO_Survey
::isSurveyActivity($this->_activityId
);
672 if ($this->_isSurveyActivity
) {
673 $surveyId = CRM_Core_DAO
::getFieldValue('CRM_Activity_DAO_Activity',
677 $responseOptions = CRM_Campaign_BAO_Survey
::getResponsesOptions($surveyId);
678 if ($responseOptions) {
679 $this->add('select', 'result', ts('Result'),
680 array('' => ts('- select -')) +
array_combine($responseOptions, $responseOptions)
685 $surveyTitle = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'title');
687 $this->assign('surveyTitle', $surveyTitle);
690 $this->assign('surveyActivity', $this->_isSurveyActivity
);
692 // this option should be available only during add mode
693 if ($this->_action
!= CRM_Core_Action
::UPDATE
) {
694 $this->add('advcheckbox', 'is_multi_activity', ts('Create a separate activity for each contact.'));
697 $this->addRule('duration',
698 ts('Please enter the duration as number of minutes (integers only).'), 'positiveInteger'
700 $this->addDateTime('activity_date_time', ts('Date'), TRUE, array('formatType' => 'activityDateTime'));
703 $this->addDateTime('followup_date', ts('in'), FALSE, array('formatType' => 'activityDateTime'));
705 // Only admins and case-workers can change the activity source
706 if (!CRM_Core_Permission
::check('administer CiviCRM') && $this->_context
!= 'caseActivity') {
707 $this->getElement('source_contact_id')->freeze();
710 //need to assign custom data type and subtype to the template
711 $this->assign('customDataType', 'Activity');
712 $this->assign('customDataSubType', $this->_activityTypeId
);
713 $this->assign('entityID', $this->_activityId
);
715 $tags = CRM_Core_BAO_Tag
::getTags('civicrm_activity');
718 $this->add('select', 'tag', ts('Tags'), $tags, FALSE,
719 array('id' => 'tags', 'multiple' => 'multiple', 'class' => 'crm-select2 huge')
723 // we need to hide activity tagset for special activities
724 $specialActivities = array('Open Case');
726 if (!in_array($this->_activityTypeName
, $specialActivities)) {
728 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_activity');
729 CRM_Core_Form_Tag
::buildQuickForm($this, $parentNames, 'civicrm_activity', $this->_activityId
, TRUE, TRUE);
732 // if we're viewing, we're assigning different buttons than for adding/editing
733 if ($this->_action
& CRM_Core_Action
::VIEW
) {
734 if (isset($this->_groupTree
)) {
735 CRM_Core_BAO_CustomGroup
::buildCustomDataView($this, $this->_groupTree
);
738 // do check for permissions
739 if (CRM_Case_BAO_Case
::checkPermission($this->_activityId
, 'File On Case', $this->_activityTypeId
)) {
742 'name' => ts('File on case'),
743 'subName' => 'file_on_case',
744 'js' => array('onClick' => "javascript:fileOnCase( \"file\", $this->_activityId ); return false;")
747 // form should be frozen for view mode
755 $this->addButtons($buttons);
759 '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.'),
760 '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.'),
762 $js = array('onclick' => "return activityStatus(" . json_encode($message) . ");");
763 $this->addButtons(array(
766 'name' => ts('Save'),
772 'name' => ts('Cancel')
778 if ($this->_activityTypeFile
) {
779 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
781 $className::buildQuickForm($this);
782 $this->addFormRule(array($className, 'formRule'), $this);
785 $this->addFormRule(array('CRM_Activity_Form_Activity', 'formRule'), $this);
787 if (CRM_Core_BAO_Setting
::getItem(
788 CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
789 'activity_assignee_notification'
792 $this->assign('activityAssigneeNotification', TRUE);
795 $this->assign('activityAssigneeNotification', FALSE);
802 * @param array $fields the input form values
803 * @param array $files the uploaded files if any
804 * @param array $options additional user data
806 * @return true if no errors, else array of errors
810 static function formRule($fields, $files, $self) {
811 // skip form rule if deleting
812 if (CRM_Utils_Array
::value('_qf_Activity_next_', $fields) == 'Delete') {
816 if (!$self->_single
&& !$fields['activity_type_id']) {
817 $errors['activity_type_id'] = ts('Activity Type is a required field');
820 //Activity type is mandatory if creating new activity, CRM-4515
821 if (array_key_exists('activity_type_id', $fields) && empty($fields['activity_type_id'])) {
822 $errors['activity_type_id'] = ts('Activity Type is required field.');
825 if (CRM_Utils_Array
::value('activity_type_id', $fields) == 3 &&
826 CRM_Utils_Array
::value('status_id', $fields) == 1
828 $errors['status_id'] = ts('You cannot record scheduled email activity.');
830 elseif (CRM_Utils_Array
::value('activity_type_id', $fields) == 4 &&
831 CRM_Utils_Array
::value('status_id', $fields) == 1
833 $errors['status_id'] = ts('You cannot record scheduled SMS activity.');
836 if (!empty($fields['followup_activity_type_id']) && empty($fields['followup_date'])) {
837 $errors['followup_date_time'] = ts('Followup date is a required field.');
839 //Activity type is mandatory if subject or follow-up date is specified for an Follow-up activity, CRM-4515
840 if ((!empty($fields['followup_activity_subject']) ||
!empty($fields['followup_date'])) && empty($fields['followup_activity_type_id'])) {
841 $errors['followup_activity_subject'] = ts('Follow-up Activity type is a required field.');
847 * Function to process the form
853 public function postProcess($params = NULL) {
854 if ($this->_action
& CRM_Core_Action
::DELETE
) {
855 $deleteParams = array('id' => $this->_activityId
);
856 $moveToTrash = CRM_Case_BAO_Case
::isCaseActivity($this->_activityId
);
857 CRM_Activity_BAO_Activity
::deleteActivity($deleteParams, $moveToTrash);
859 // delete tags for the entity
861 'entity_table' => 'civicrm_activity',
862 'entity_id' => $this->_activityId
865 CRM_Core_BAO_EntityTag
::del($tagParams);
867 CRM_Core_Session
::setStatus(ts("Selected Activity has been deleted successfully."), ts('Record Deleted'), 'success');
871 // store the submitted values in an array
873 $params = $this->controller
->exportValues($this->_name
);
876 //set activity type id
877 if (empty($params['activity_type_id'])) {
878 $params['activity_type_id'] = $this->_activityTypeId
;
881 if (!empty($params['hidden_custom']) &&
882 !isset($params['custom'])
884 $customFields = CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
885 $this->_activityTypeId
887 $customFields = CRM_Utils_Array
::crmArrayMerge($customFields,
888 CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
892 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
899 // store the date with proper format
900 $params['activity_date_time'] = CRM_Utils_Date
::processDate($params['activity_date_time'], $params['activity_date_time_time']);
902 // format params as arrays
903 foreach (array('target', 'assignee', 'followup_assignee') as $name) {
904 if (!empty($params["{$name}_contact_id"])) {
905 $params["{$name}_contact_id"] = explode(',', $params["{$name}_contact_id"]);
908 $params["{$name}_contact_id"] = array();
912 // get ids for associated contacts
913 if (!$params['source_contact_id']) {
914 $params['source_contact_id'] = $this->_currentUserId
;
917 if (isset($this->_activityId
)) {
918 $params['id'] = $this->_activityId
;
921 // add attachments as needed
922 CRM_Core_BAO_File
::formatAttachment($params,
928 // format target params
929 if (!$this->_single
) {
930 $params['target_contact_id'] = $this->_contactIds
;
934 if (!empty($params['is_multi_activity']) &&
935 !CRM_Utils_Array
::crmIsEmptyArray($params['target_contact_id'])
937 $targetContacts = $params['target_contact_id'];
938 foreach ($targetContacts as $targetContactId) {
939 $params['target_contact_id'] = array($targetContactId);
941 $activity[] = $this->processActivity($params);
946 $activity = $this->processActivity($params);
949 return array('activity' => $activity);
953 * Process activity creation
955 * @param array $params associated array of submitted values
958 protected function processActivity(&$params) {
959 $activityAssigned = array();
960 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
961 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
962 // format assignee params
963 if (!CRM_Utils_Array
::crmIsEmptyArray($params['assignee_contact_id'])) {
964 //skip those assignee contacts which are already assigned
965 //while sending a copy.CRM-4509.
966 $activityAssigned = array_flip($params['assignee_contact_id']);
967 if ($this->_activityId
) {
968 $assigneeContacts = CRM_Activity_BAO_ActivityContact
::getNames($this->_activityId
, $assigneeID);
969 $activityAssigned = array_diff_key($activityAssigned, $assigneeContacts);
973 // call begin post process. Idea is to let injecting file do
974 // any processing before the activity is added/updated.
975 $this->beginPostProcess($params);
977 $activity = CRM_Activity_BAO_Activity
::create($params);
979 // add tags if exists
980 $tagParams = array();
981 if (!empty($params['tag'])) {
982 foreach ($params['tag'] as $tag) {
983 $tagParams[$tag] = 1;
988 CRM_Core_BAO_EntityTag
::create($tagParams, 'civicrm_activity', $activity->id
);
991 if (isset($params['activity_taglist']) && !empty($params['activity_taglist'])) {
992 CRM_Core_Form_Tag
::postProcess($params['activity_taglist'], $activity->id
, 'civicrm_activity', $this);
995 // call end post process. Idea is to let injecting file do any
996 // processing needed, after the activity has been added/updated.
997 $this->endPostProcess($params, $activity);
1000 if (!empty($params['is_multi_activity'])) {
1001 $this->_activityIds
[] = $activity->id
;
1004 $this->_activityId
= $activity->id
;
1007 // create follow up activity if needed
1008 $followupStatus = '';
1009 $followupActivity = NULL;
1010 if (!empty($params['followup_activity_type_id'])) {
1011 $followupActivity = CRM_Activity_BAO_Activity
::createFollowupActivity($activity->id
, $params);
1012 $followupStatus = ts('A followup activity has been scheduled.');
1015 // send copy to assignee contacts.CRM-4509
1018 if (CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
1019 'activity_assignee_notification')) {
1020 $activityIDs = array($activity->id
);
1021 if ($followupActivity) {
1022 $activityIDs = array_merge($activityIDs, array($followupActivity->id
));
1024 $assigneeContacts = CRM_Activity_BAO_ActivityAssignment
::getAssigneeNames($activityIDs, TRUE, FALSE);
1026 if (!CRM_Utils_Array
::crmIsEmptyArray($params['assignee_contact_id'])) {
1027 $mailToContacts = array();
1029 //build an associative array with unique email addresses.
1030 foreach ($activityAssigned as $id => $dnc) {
1031 if (isset($id) && array_key_exists($id, $assigneeContacts)) {
1032 $mailToContacts[$assigneeContacts[$id]['email']] = $assigneeContacts[$id];
1036 if (!CRM_Utils_array
::crmIsEmptyArray($mailToContacts)) {
1037 //include attachments while sending a copy of activity.
1038 $attachments = CRM_Core_BAO_File
::getEntityFile('civicrm_activity', $activity->id
);
1040 $ics = new CRM_Activity_BAO_ICalendar($activity);
1041 $ics->addAttachment($attachments, $mailToContacts);
1043 // CRM-8400 add param with _currentlyViewedContactId for URL link in mail
1044 CRM_Case_BAO_Case
::sendActivityCopy(NULL, $activity->id
, $mailToContacts, $attachments, NULL);
1048 $mailStatus .= ts("A copy of the activity has also been sent to assignee contacts(s).");
1052 // Also send email to follow-up activity assignees if set
1053 if ($followupActivity) {
1054 $mailToFollowupContacts = array();
1055 foreach ($assigneeContacts as $values) {
1056 if ($values['activity_id'] == $followupActivity->id
) {
1057 $mailToFollowupContacts[$values['email']] = $values;
1061 if (!CRM_Utils_array
::crmIsEmptyArray($mailToFollowupContacts)) {
1062 $ics = new CRM_Activity_BAO_ICalendar($followupActivity);
1063 $attachments = CRM_Core_BAO_File
::getEntityFile('civicrm_activity', $followupActivity->id
);
1064 $ics->addAttachment($attachments, $mailToFollowupContacts);
1066 CRM_Case_BAO_Case
::sendActivityCopy(NULL, $followupActivity->id
, $mailToFollowupContacts, $attachments, NULL);
1070 $mailStatus .= '<br />' . ts("A copy of the follow-up activity has also been sent to follow-up assignee contacts(s).");
1075 // set status message
1077 if (!empty($params['subject'])) {
1078 $subject = "'" . $params['subject'] . "'";
1081 CRM_Core_Session
::setStatus(ts('Activity %1 has been saved. %2 %3',
1084 2 => $followupStatus,
1087 ), ts('Saved'), 'success');
1093 * Shorthand for getting id by display name (makes code more readable)
1097 protected function _getIdByDisplayName($displayName) {
1098 return CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
1106 * Shorthand for getting display name by id (makes code more readable)
1110 protected function _getDisplayNameById($id) {
1111 return CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
1119 * Function to let injecting activity type file do any processing
1120 * needed, before the activity is added/updated
1123 function beginPostProcess(&$params) {
1124 if ($this->_activityTypeFile
) {
1125 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
1126 $className::beginPostProcess($this, $params);
1131 * Function to let injecting activity type file do any processing
1132 * needed, after the activity has been added/updated
1135 function endPostProcess(&$params, &$activity) {
1136 if ($this->_activityTypeFile
) {
1137 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
1138 $className::endPostProcess($this, $params, $activity );