3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class generates form components for Activity
40 class CRM_Activity_Form_Activity
extends CRM_Contact_Form_Task
{
43 * The id of the object being edited / created
50 * Store activity ids when multiple activities are created
54 public $_activityIds = array();
57 * The id of activity type
61 public $_activityTypeId;
64 * The name of activity type
68 public $_activityTypeName;
71 * The id of currently viewed contact
75 public $_currentlyViewedContactId;
78 * The id of source contact and target contact
82 protected $_sourceContactId;
83 protected $_targetContactId;
84 protected $_asigneeContactId;
91 public $_activityTypeFile;
94 * The id of the logged in user, used when add / edit
98 public $_currentUserId;
101 * The array of form field attributes
108 * The the directory inside CRM, to include activity type file from
112 protected $_crmDir = 'Activity';
119 protected $_isSurveyActivity;
121 protected $_values = array();
123 protected $unsavedWarn = TRUE;
126 * The _fields var can be used by sub class to set/unset/edit the
127 * form fields based on their requirement
130 function setFields() {
131 $this->_fields
= array(
134 'label' => ts('Subject'),
135 'attributes' => CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity',
141 'label' => ts('Duration'),
142 'attributes' => array('size' => 4, 'maxlength' => 8),
147 'label' => ts('Location'),
149 CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity',
156 'label' => ts('Details'),
157 // forces a smaller edit window
158 'attributes' => array('rows' => 4, 'cols' => 60),
161 'status_id' => array(
165 'priority_id' => array(
169 'source_contact_id' => array(
170 'type' => 'entityRef',
171 'label' => ts('Added By'),
174 'target_contact_id' => array(
175 'type' => 'entityRef',
176 'label' => ts('With Contact'),
177 'attributes' => array('multiple' => TRUE, 'create' => TRUE)
179 'assignee_contact_id' => array(
180 'type' => 'entityRef',
181 'label' => ts('Assigned to'),
182 'attributes' => array('multiple' => TRUE, 'create' => TRUE, 'api' => array('params' => array('is_deceased' => 0))),
184 'followup_assignee_contact_id' => array(
185 'type' => 'entityRef',
186 'label' => ts('Assigned to'),
187 'attributes' => array('multiple' => TRUE, 'create' => TRUE, 'api' => array('params' => array('is_deceased' => 0))),
189 'followup_activity_type_id' => array(
191 'label' => ts('Followup Activity'),
192 'attributes' => array('' => '- ' . ts('select activity') . ' -') + CRM_Core_PseudoConstant
::ActivityType(FALSE),
193 'extra' => array('class' => 'crm-select2'),
195 // Add optional 'Subject' field for the Follow-up Activiity, CRM-4491
196 'followup_activity_subject' => array(
198 'label' => ts('Subject'),
199 'attributes' => CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity',
205 if (($this->_context
== 'standalone') &&
206 ($printPDF = CRM_Utils_Array
::key('Print PDF Letter', $this->_fields
['followup_activity_type_id']['attributes']))
208 unset($this->_fields
['followup_activity_type_id']['attributes'][$printPDF]);
213 * Build the form object
218 function preProcess() {
219 $this->_cdType
= CRM_Utils_Array
::value('type', $_GET);
220 $this->assign('cdType', FALSE);
221 if ($this->_cdType
) {
222 $this->assign('cdType', TRUE);
223 return CRM_Custom_Form_CustomData
::preProcess($this);
225 CRM_Core_Form_RecurringEntity
::preProcess('civicrm_activity');
226 $this->_atypefile
= CRM_Utils_Array
::value('atypefile', $_GET);
227 $this->assign('atypefile', FALSE);
228 if ($this->_atypefile
) {
229 $this->assign('atypefile', TRUE);
232 $session = CRM_Core_Session
::singleton();
233 $this->_currentUserId
= $session->get('userID');
235 $this->_currentlyViewedContactId
= $this->get('contactId');
236 if (!$this->_currentlyViewedContactId
) {
237 $this->_currentlyViewedContactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
239 $this->assign('contactId', $this->_currentlyViewedContactId
);
242 if (!isset($this->_context
)) {
243 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this);
244 if (CRM_Contact_Form_Search
::isSearchContext($this->_context
)) {
245 $this->_context
= 'search';
247 elseif (!in_array($this->_context
, array('dashlet', 'dashletFullscreen'))
248 && $this->_currentlyViewedContactId
250 $this->_context
= 'activity';
252 $this->_compContext
= CRM_Utils_Request
::retrieve('compContext', 'String', $this);
255 $this->assign('context', $this->_context
);
257 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this);
259 if ($this->_action
& CRM_Core_Action
::DELETE
) {
260 if (!CRM_Core_Permission
::check('delete activities')) {
261 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
266 //when we come from contact search, activity id never comes.
267 //so don't try to get from object, it might gives you wrong one.
269 // if we're not adding new one, there must be an id to
270 // an activity we're trying to work on.
271 if ($this->_action
!= CRM_Core_Action
::ADD
&&
272 get_class($this->controller
) != 'CRM_Contact_Controller_Search'
274 $this->_activityId
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
277 $this->_activityTypeId
= CRM_Utils_Request
::retrieve('atype', 'Positive', $this);
278 $this->assign('atype', $this->_activityTypeId
);
280 $this->assign('activityId', $this->_activityId
);
282 //check for required permissions, CRM-6264
283 if ($this->_activityId
&&
284 in_array($this->_action
, array(
285 CRM_Core_Action
::UPDATE
,
286 CRM_Core_Action
::VIEW
288 !CRM_Activity_BAO_Activity
::checkPermission($this->_activityId
, $this->_action
)
290 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
292 if (($this->_action
& CRM_Core_Action
::VIEW
) &&
293 CRM_Activity_BAO_Activity
::checkPermission($this->_activityId
, CRM_Core_Action
::UPDATE
)
295 $this->assign('permission', 'edit');
298 if (!$this->_activityTypeId
&& $this->_activityId
) {
299 $this->_activityTypeId
= CRM_Core_DAO
::getFieldValue('CRM_Activity_DAO_Activity',
305 //Assigning Activity type name
306 if ($this->_activityTypeId
) {
307 $activityTName = CRM_Core_OptionGroup
::values('activity_type', FALSE, FALSE, FALSE, 'AND v.value = ' . $this->_activityTypeId
, 'name');
308 if ($activityTName[$this->_activityTypeId
]) {
309 $this->_activityTypeName
= $activityTName[$this->_activityTypeId
];
310 $this->assign('activityTName', $activityTName[$this->_activityTypeId
]);
315 if (isset($activityTName)) {
316 $activityName = CRM_Utils_Array
::value($this->_activityTypeId
, $activityTName);
317 $this->assign('pageTitle', ts('%1 Activity', array( 1 => $activityName)));
319 if ($this->_currentlyViewedContactId
) {
320 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_currentlyViewedContactId
);
321 // Check if this is default domain contact CRM-10482
322 if (CRM_Contact_BAO_Contact
::checkDomainContact($this->_currentlyViewedContactId
)) {
323 $displayName .= ' (' . ts('default organization') . ')';
325 CRM_Utils_System
::setTitle($displayName . ' - ' . $activityName);
328 CRM_Utils_System
::setTitle(ts('%1 Activity', array( 1 => $activityName)));
332 //check the mode when this form is called either single or as
334 if ($this->_activityTypeId ||
335 $this->_context
== 'standalone' ||
336 $this->_currentlyViewedContactId
338 $this->_single
= TRUE;
339 $this->assign('urlPath', 'civicrm/activity');
342 //set the appropriate action
343 $url = CRM_Utils_System
::currentPath();
344 $urlArray = explode('/', $url);
345 $searchPath = array_pop($urlArray);
346 $searchType = 'basic';
347 $this->_action
= CRM_Core_Action
::BASIC
;
348 switch ($searchPath) {
350 $searchType = $searchPath;
351 $this->_action
= CRM_Core_Action
::BASIC
;
355 $searchType = $searchPath;
356 $this->_action
= CRM_Core_Action
::ADVANCED
;
360 $searchType = $searchPath;
361 $this->_action
= CRM_Core_Action
::PROFILE
;
365 $this->_action
= CRM_Core_Action
::COPY
;
366 $searchType = $searchPath;
370 parent
::preProcess();
371 $this->_single
= FALSE;
373 $this->assign('urlPath', "civicrm/contact/search/$searchType");
374 $this->assign('urlPathVar', "_qf_Activity_display=true&qfKey={$this->controller->_key}");
377 $this->assign('single', $this->_single
);
378 $this->assign('action', $this->_action
);
380 if ($this->_action
& CRM_Core_Action
::VIEW
) {
381 // get the tree of custom fields
382 $this->_groupTree
= &CRM_Core_BAO_CustomGroup
::getTree('Activity', $this,
383 $this->_activityId
, 0, $this->_activityTypeId
387 if ($this->_activityTypeId
) {
388 //set activity type name and description to template
389 list($this->_activityTypeName
, $activityTypeDescription) = CRM_Core_BAO_OptionValue
::getActivityTypeDetails($this->_activityTypeId
);
390 $this->assign('activityTypeName', $this->_activityTypeName
);
391 $this->assign('activityTypeDescription', $activityTypeDescription);
395 $urlParams = $urlString = NULL;
396 $qfKey = CRM_Utils_Request
::retrieve('key', 'String', $this);
398 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $this);
402 if (!CRM_Utils_Rule
::qfKey($qfKey)) {
406 if ($this->_context
== 'fulltext') {
408 $urlParams = 'force=1';
409 $urlString = 'civicrm/contact/search/custom';
410 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
412 $urlParams .= '&context=fulltext&action=view';
413 $urlString = 'civicrm/contact/view/activity';
416 $urlParams .= "$keyName=$qfKey";
418 $this->assign('searchKey', $qfKey);
420 elseif (in_array($this->_context
, array(
427 $urlParams = 'reset=1';
428 $urlString = 'civicrm/dashboard';
430 elseif ($this->_context
== 'search') {
431 $urlParams = 'force=1';
433 $urlParams .= "&qfKey=$qfKey";
435 $path = CRM_Utils_System
::currentPath();
436 if ($this->_compContext
== 'advanced' ) {
437 $urlString = 'civicrm/contact/search/advanced';
439 elseif ($path == 'civicrm/group/search'
440 ||
$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
);
507 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
508 CRM_Core_Form_RecurringEntity
::preProcess('civicrm_activity');
513 * Set default values for the form. For edit/view mode
514 * the default values are retrieved from the database
520 function setDefaultValues() {
521 if ($this->_cdType
) {
522 return CRM_Custom_Form_CustomData
::setDefaultValues($this);
525 $defaults = $this->_values
;
526 // if we're editing...
527 if (isset($this->_activityId
)) {
528 if (empty($defaults['activity_date_time'])) {
529 list($defaults['activity_date_time'], $defaults['activity_date_time_time']) = CRM_Utils_Date
::setDateDefaults(NULL, 'activityDateTime');
531 elseif ($this->_action
& CRM_Core_Action
::UPDATE
) {
532 $this->assign('current_activity_date_time', $defaults['activity_date_time']);
533 list($defaults['activity_date_time'],
534 $defaults['activity_date_time_time']
535 ) = CRM_Utils_Date
::setDateDefaults($defaults['activity_date_time'], 'activityDateTime');
536 list($defaults['repetition_start_date'], $defaults['repetition_start_date_time']) = CRM_Utils_Date
::setDateDefaults($defaults['activity_date_time'], 'activityDateTime');
537 $recurringEntityDefaults = array();
538 $recurringEntityDefaults = CRM_Core_Form_RecurringEntity
::setDefaultValues();
539 $defaults = array_merge($defaults, $recurringEntityDefaults);
542 if ($this->_context
!= 'standalone') {
543 $this->assign('target_contact_value',
544 CRM_Utils_Array
::value('target_contact_value', $defaults)
546 $this->assign('assignee_contact_value',
547 CRM_Utils_Array
::value('assignee_contact_value', $defaults)
551 // Fixme: why are we getting the wrong keys from upstream?
552 $defaults['target_contact_id'] = CRM_Utils_Array
::value('target_contact', $defaults);
553 $defaults['assignee_contact_id'] = CRM_Utils_Array
::value('assignee_contact', $defaults);
555 // set default tags if exists
556 $defaults['tag'] = CRM_Core_BAO_EntityTag
::getTag($this->_activityId
, 'civicrm_activity');
559 // if it's a new activity, we need to set default values for associated contact fields
560 $this->_sourceContactId
= $this->_currentUserId
;
561 $this->_targetContactId
= $this->_currentlyViewedContactId
;
563 $defaults['source_contact_id'] = $this->_sourceContactId
;
564 $defaults['target_contact_id'] = $this->_targetContactId
;
566 list($defaults['activity_date_time'], $defaults['activity_date_time_time']) =
567 CRM_Utils_Date
::setDateDefaults(NULL, 'activityDateTime');
570 if ($this->_activityTypeId
) {
571 $defaults['activity_type_id'] = $this->_activityTypeId
;
574 if (!$this->_single
&& !empty($this->_contactIds
)) {
575 $defaults['target_contact_id'] = $this->_contactIds
;
578 // CRM-15472 - 50 is around the practial limit of how many items a select2 entityRef can handle
579 if (!empty($defaults['target_contact_id'])) {
580 $count = count(is_array($defaults['target_contact_id']) ?
$defaults['target_contact_id'] : explode(',', $defaults['target_contact_id']));
582 $this->freeze(array('target_contact_id'));
586 if ($this->_action
& (CRM_Core_Action
::DELETE | CRM_Core_Action
::RENEW
)) {
587 $this->assign('delName', CRM_Utils_Array
::value('subject', $defaults));
590 if ($this->_activityTypeFile
) {
591 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
592 $defaults +
= $className::setDefaultValues($this);
594 if (empty($defaults['priority_id'])) {
595 $priority = CRM_Core_PseudoConstant
::get('CRM_Activity_DAO_Activity', 'priority_id');
596 $defaults['priority_id'] = array_search('Normal', $priority);
598 if (empty($defaults['status_id'])) {
599 $defaults['status_id'] = CRM_Core_OptionGroup
::getDefaultValue('activity_status');
604 public function buildQuickForm() {
605 if ($this->_action
& (CRM_Core_Action
::DELETE | CRM_Core_Action
::RENEW
)) {
606 //enable form element (ActivityLinks sets this true)
607 $this->assign('suppressForm', FALSE);
609 $button = ts('Delete');
610 if ($this->_action
& CRM_Core_Action
::RENEW
) {
611 $button = ts('Restore');
613 $this->addButtons(array(
617 'spacing' => ' ',
622 'name' => ts('Cancel')
628 if ($this->_cdType
) {
629 return CRM_Custom_Form_CustomData
::buildQuickForm($this);
632 //build other activity links
633 CRM_Activity_Form_ActivityLinks
::commonBuildQuickForm($this);
635 //enable form element (ActivityLinks sets this true)
636 $this->assign('suppressForm', FALSE);
638 $element = & $this->add('select', 'activity_type_id', ts('Activity Type'),
639 $this->_fields
['followup_activity_type_id']['attributes'],
641 'onchange' => "CRM.buildCustomData( 'Activity', this.value );",
642 'class' => 'crm-select2',
646 //freeze for update mode.
647 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
651 //Call to RecurringEntity buildQuickForm for add/update mode
652 if ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD
)) {
653 CRM_Core_Form_RecurringEntity
::buildQuickForm($this);
656 foreach ($this->_fields
as $field => $values) {
657 if (!empty($this->_fields
[$field])) {
658 $attribute = CRM_Utils_Array
::value('attributes', $values);
659 $required = !empty($values['required']);
661 if ($values['type'] == 'wysiwyg') {
662 $this->addWysiwyg($field, $values['label'], $attribute, $required);
664 elseif ($values['type'] == 'select' && empty($attribute)) {
665 $this->addSelect($field, array('entity' => 'activity'), $required);
667 elseif ($values['type'] == 'entityRef') {
668 $this->addEntityRef($field, $values['label'], $attribute, $required);
671 $this->add($values['type'], $field, $values['label'], $attribute, $required, CRM_Utils_Array
::value('extra', $values));
676 //CRM-7362 --add campaigns.
677 CRM_Campaign_BAO_Campaign
::addCampaign($this, CRM_Utils_Array
::value('campaign_id', $this->_values
));
679 //add engagement level CRM-7775
680 $buildEngagementLevel = FALSE;
681 if (CRM_Campaign_BAO_Campaign
::isCampaignEnable() &&
682 CRM_Campaign_BAO_Campaign
::accessCampaign()
684 $buildEngagementLevel = TRUE;
685 $this->addSelect('engagement_level',array('entity' => 'activity'));
686 $this->addRule('engagement_level',
687 ts('Please enter the engagement index as a number (integers only).'),
691 $this->assign('buildEngagementLevel', $buildEngagementLevel);
693 // check for survey activity
694 $this->_isSurveyActivity
= FALSE;
696 if ($this->_activityId
&& CRM_Campaign_BAO_Campaign
::isCampaignEnable() &&
697 CRM_Campaign_BAO_Campaign
::accessCampaign()
700 $this->_isSurveyActivity
= CRM_Campaign_BAO_Survey
::isSurveyActivity($this->_activityId
);
701 if ($this->_isSurveyActivity
) {
702 $surveyId = CRM_Core_DAO
::getFieldValue('CRM_Activity_DAO_Activity',
706 $responseOptions = CRM_Campaign_BAO_Survey
::getResponsesOptions($surveyId);
707 if ($responseOptions) {
708 $this->add('select', 'result', ts('Result'),
709 array('' => ts('- select -')) +
array_combine($responseOptions, $responseOptions)
714 $surveyTitle = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'title');
716 $this->assign('surveyTitle', $surveyTitle);
719 $this->assign('surveyActivity', $this->_isSurveyActivity
);
721 // this option should be available only during add mode
722 if ($this->_action
!= CRM_Core_Action
::UPDATE
) {
723 $this->add('advcheckbox', 'is_multi_activity', ts('Create a separate activity for each contact.'));
726 $this->addRule('duration',
727 ts('Please enter the duration as number of minutes (integers only).'), 'positiveInteger'
729 $this->addDateTime('activity_date_time', ts('Date'), TRUE, array('formatType' => 'activityDateTime'));
732 $this->addDateTime('followup_date', ts('in'), FALSE, array('formatType' => 'activityDateTime'));
734 // Only admins and case-workers can change the activity source
735 if (!CRM_Core_Permission
::check('administer CiviCRM') && $this->_context
!= 'caseActivity') {
736 $this->getElement('source_contact_id')->freeze();
739 //need to assign custom data type and subtype to the template
740 $this->assign('customDataType', 'Activity');
741 $this->assign('customDataSubType', $this->_activityTypeId
);
742 $this->assign('entityID', $this->_activityId
);
744 $tags = CRM_Core_BAO_Tag
::getTags('civicrm_activity');
747 $this->add('select', 'tag', ts('Tags'), $tags, FALSE,
748 array('id' => 'tags', 'multiple' => 'multiple', 'class' => 'crm-select2 huge')
752 // we need to hide activity tagset for special activities
753 $specialActivities = array('Open Case');
755 if (!in_array($this->_activityTypeName
, $specialActivities)) {
757 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_activity');
758 CRM_Core_Form_Tag
::buildQuickForm($this, $parentNames, 'civicrm_activity', $this->_activityId
);
761 // if we're viewing, we're assigning different buttons than for adding/editing
762 if ($this->_action
& CRM_Core_Action
::VIEW
) {
763 if (isset($this->_groupTree
)) {
764 CRM_Core_BAO_CustomGroup
::buildCustomDataView($this, $this->_groupTree
);
766 // form should be frozen for view mode
774 $this->addButtons($buttons);
778 '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.'),
779 '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.'),
781 $js = array('onclick' => "return activityStatus(" . json_encode($message) . ");");
782 $this->addButtons(array(
785 'name' => ts('Save'),
791 'name' => ts('Cancel')
797 if ($this->_activityTypeFile
) {
798 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
800 $className::buildQuickForm($this);
801 $this->addFormRule(array($className, 'formRule'), $this);
804 $this->addFormRule(array('CRM_Activity_Form_Activity', 'formRule'), $this);
806 if (CRM_Core_BAO_Setting
::getItem(
807 CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
808 'activity_assignee_notification'
811 $this->assign('activityAssigneeNotification', TRUE);
814 $this->assign('activityAssigneeNotification', FALSE);
821 * @param array $fields the input form values
822 * @param array $files the uploaded files if any
825 * @return true if no errors, else array of errors
829 static function formRule($fields, $files, $self) {
830 // skip form rule if deleting
831 if (CRM_Utils_Array
::value('_qf_Activity_next_', $fields) == 'Delete') {
835 if (!$self->_single
&& !$fields['activity_type_id']) {
836 $errors['activity_type_id'] = ts('Activity Type is a required field');
839 //Activity type is mandatory if creating new activity, CRM-4515
840 if (array_key_exists('activity_type_id', $fields) && empty($fields['activity_type_id'])) {
841 $errors['activity_type_id'] = ts('Activity Type is required field.');
844 if (CRM_Utils_Array
::value('activity_type_id', $fields) == 3 &&
845 CRM_Utils_Array
::value('status_id', $fields) == 1
847 $errors['status_id'] = ts('You cannot record scheduled email activity.');
849 elseif (CRM_Utils_Array
::value('activity_type_id', $fields) == 4 &&
850 CRM_Utils_Array
::value('status_id', $fields) == 1
852 $errors['status_id'] = ts('You cannot record scheduled SMS activity.');
855 if (!empty($fields['followup_activity_type_id']) && empty($fields['followup_date'])) {
856 $errors['followup_date_time'] = ts('Followup date is a required field.');
858 //Activity type is mandatory if subject or follow-up date is specified for an Follow-up activity, CRM-4515
859 if ((!empty($fields['followup_activity_subject']) ||
!empty($fields['followup_date'])) && empty($fields['followup_activity_type_id'])) {
860 $errors['followup_activity_subject'] = ts('Follow-up Activity type is a required field.');
866 * Process the form submission
870 * @param array $params
873 public function postProcess($params = NULL) {
874 if ($this->_action
& CRM_Core_Action
::DELETE
) {
875 $deleteParams = array('id' => $this->_activityId
);
876 $moveToTrash = CRM_Case_BAO_Case
::isCaseActivity($this->_activityId
);
877 CRM_Activity_BAO_Activity
::deleteActivity($deleteParams, $moveToTrash);
879 // delete tags for the entity
881 'entity_table' => 'civicrm_activity',
882 'entity_id' => $this->_activityId
885 CRM_Core_BAO_EntityTag
::del($tagParams);
887 CRM_Core_Session
::setStatus(ts("Selected Activity has been deleted successfully."), ts('Record Deleted'), 'success');
891 // store the submitted values in an array
893 $params = $this->controller
->exportValues($this->_name
);
896 //set activity type id
897 if (empty($params['activity_type_id'])) {
898 $params['activity_type_id'] = $this->_activityTypeId
;
901 if (!empty($params['hidden_custom']) &&
902 !isset($params['custom'])
904 $customFields = CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
905 $this->_activityTypeId
907 $customFields = CRM_Utils_Array
::crmArrayMerge($customFields,
908 CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
912 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
919 // store the date with proper format
920 $params['activity_date_time'] = CRM_Utils_Date
::processDate($params['activity_date_time'], $params['activity_date_time_time']);
922 // format params as arrays
923 foreach (array('target', 'assignee', 'followup_assignee') as $name) {
924 if (!empty($params["{$name}_contact_id"])) {
925 $params["{$name}_contact_id"] = explode(',', $params["{$name}_contact_id"]);
928 $params["{$name}_contact_id"] = array();
932 // get ids for associated contacts
933 if (!$params['source_contact_id']) {
934 $params['source_contact_id'] = $this->_currentUserId
;
937 if (isset($this->_activityId
)) {
938 $params['id'] = $this->_activityId
;
941 // add attachments as needed
942 CRM_Core_BAO_File
::formatAttachment($params,
949 if (!empty($params['is_multi_activity']) &&
950 !CRM_Utils_Array
::crmIsEmptyArray($params['target_contact_id'])
952 $targetContacts = $params['target_contact_id'];
953 foreach ($targetContacts as $targetContactId) {
954 $params['target_contact_id'] = array($targetContactId);
956 $activity[] = $this->processActivity($params);
961 $activity = $this->processActivity($params);
964 //Set for repeat configuration in create mode
965 $params['entity_id'] = $this->_activityId
;
966 $params['entity_table'] = 'civicrm_activity';
967 $scheduleReminderDetails = array();
968 if (!empty($params['entity_id']) && !empty($params['entity_table'])) {
969 $checkParentExistsForThisId = CRM_Core_BAO_RecurringEntity
::getParentFor($params['entity_id'], $params['entity_table']);
970 if ($checkParentExistsForThisId) {
971 $params['parent_entity_id'] = $checkParentExistsForThisId;
972 $scheduleReminderDetails = CRM_Core_BAO_RecurringEntity
::getReminderDetailsByEntityId($checkParentExistsForThisId, $params['entity_table']);
975 $params['parent_entity_id'] = $params['entity_id'];
976 $scheduleReminderDetails = CRM_Core_BAO_RecurringEntity
::getReminderDetailsByEntityId($params['entity_id'], $params['entity_table']);
978 if (property_exists($scheduleReminderDetails, 'id')) {
979 $params['schedule_reminder_id'] = $scheduleReminderDetails->id
;
982 $params['dateColumns'] = array('activity_date_time');
985 unset($params['id']);
986 $linkedEntities = array(
988 'table' => 'civicrm_activity_contact',
989 'findCriteria' => array(
990 'activity_id' => $this->_activityId
,
992 'linkedColumns' => array('activity_id'),
993 'isRecurringEntityRecord' => FALSE,
996 CRM_Core_Form_RecurringEntity
::postProcess($params, 'civicrm_activity', $linkedEntities);
998 return array('activity' => $activity);
1002 * Process activity creation
1004 * @param array $params associated array of submitted values
1006 * @return $this|null|object
1009 protected function processActivity(&$params) {
1010 $activityAssigned = array();
1011 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
1012 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
1013 // format assignee params
1014 if (!CRM_Utils_Array
::crmIsEmptyArray($params['assignee_contact_id'])) {
1015 //skip those assignee contacts which are already assigned
1016 //while sending a copy.CRM-4509.
1017 $activityAssigned = array_flip($params['assignee_contact_id']);
1018 if ($this->_activityId
) {
1019 $assigneeContacts = CRM_Activity_BAO_ActivityContact
::getNames($this->_activityId
, $assigneeID);
1020 $activityAssigned = array_diff_key($activityAssigned, $assigneeContacts);
1024 // call begin post process. Idea is to let injecting file do
1025 // any processing before the activity is added/updated.
1026 $this->beginPostProcess($params);
1028 $activity = CRM_Activity_BAO_Activity
::create($params);
1030 // add tags if exists
1031 $tagParams = array();
1032 if (!empty($params['tag'])) {
1033 foreach ($params['tag'] as $tag) {
1034 $tagParams[$tag] = 1;
1039 CRM_Core_BAO_EntityTag
::create($tagParams, 'civicrm_activity', $activity->id
);
1042 if (isset($params['activity_taglist']) && !empty($params['activity_taglist'])) {
1043 CRM_Core_Form_Tag
::postProcess($params['activity_taglist'], $activity->id
, 'civicrm_activity', $this);
1046 // call end post process. Idea is to let injecting file do any
1047 // processing needed, after the activity has been added/updated.
1048 $this->endPostProcess($params, $activity);
1051 if (!empty($params['is_multi_activity'])) {
1052 $this->_activityIds
[] = $activity->id
;
1055 $this->_activityId
= $activity->id
;
1058 // create follow up activity if needed
1059 $followupStatus = '';
1060 $followupActivity = NULL;
1061 if (!empty($params['followup_activity_type_id'])) {
1062 $followupActivity = CRM_Activity_BAO_Activity
::createFollowupActivity($activity->id
, $params);
1063 $followupStatus = ts('A followup activity has been scheduled.');
1066 // send copy to assignee contacts.CRM-4509
1069 if (CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
1070 'activity_assignee_notification')) {
1071 $activityIDs = array($activity->id
);
1072 if ($followupActivity) {
1073 $activityIDs = array_merge($activityIDs, array($followupActivity->id
));
1075 $assigneeContacts = CRM_Activity_BAO_ActivityAssignment
::getAssigneeNames($activityIDs, TRUE, FALSE);
1077 if (!CRM_Utils_Array
::crmIsEmptyArray($params['assignee_contact_id'])) {
1078 $mailToContacts = array();
1080 //build an associative array with unique email addresses.
1081 foreach ($activityAssigned as $id => $dnc) {
1082 if (isset($id) && array_key_exists($id, $assigneeContacts)) {
1083 $mailToContacts[$assigneeContacts[$id]['email']] = $assigneeContacts[$id];
1087 if (!CRM_Utils_array
::crmIsEmptyArray($mailToContacts)) {
1088 //include attachments while sending a copy of activity.
1089 $attachments = CRM_Core_BAO_File
::getEntityFile('civicrm_activity', $activity->id
);
1091 $ics = new CRM_Activity_BAO_ICalendar($activity);
1092 $ics->addAttachment($attachments, $mailToContacts);
1094 // CRM-8400 add param with _currentlyViewedContactId for URL link in mail
1095 CRM_Case_BAO_Case
::sendActivityCopy(NULL, $activity->id
, $mailToContacts, $attachments, NULL);
1099 $mailStatus .= ts("A copy of the activity has also been sent to assignee contacts(s).");
1103 // Also send email to follow-up activity assignees if set
1104 if ($followupActivity) {
1105 $mailToFollowupContacts = array();
1106 foreach ($assigneeContacts as $values) {
1107 if ($values['activity_id'] == $followupActivity->id
) {
1108 $mailToFollowupContacts[$values['email']] = $values;
1112 if (!CRM_Utils_array
::crmIsEmptyArray($mailToFollowupContacts)) {
1113 $ics = new CRM_Activity_BAO_ICalendar($followupActivity);
1114 $attachments = CRM_Core_BAO_File
::getEntityFile('civicrm_activity', $followupActivity->id
);
1115 $ics->addAttachment($attachments, $mailToFollowupContacts);
1117 CRM_Case_BAO_Case
::sendActivityCopy(NULL, $followupActivity->id
, $mailToFollowupContacts, $attachments, NULL);
1121 $mailStatus .= '<br />' . ts("A copy of the follow-up activity has also been sent to follow-up assignee contacts(s).");
1126 // set status message
1128 if (!empty($params['subject'])) {
1129 $subject = "'" . $params['subject'] . "'";
1132 CRM_Core_Session
::setStatus(ts('Activity %1 has been saved. %2 %3',
1135 2 => $followupStatus,
1138 ), ts('Saved'), 'success');
1144 * Shorthand for getting id by display name (makes code more readable)
1148 protected function _getIdByDisplayName($displayName) {
1149 return CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
1157 * Shorthand for getting display name by id (makes code more readable)
1161 protected function _getDisplayNameById($id) {
1162 return CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
1170 * Let injecting activity type file do any processing
1171 * needed, before the activity is added/updated
1173 * @param array $params
1175 function beginPostProcess(&$params) {
1176 if ($this->_activityTypeFile
) {
1177 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
1178 $className::beginPostProcess($this, $params);
1183 * Let injecting activity type file do any processing
1184 * needed, after the activity has been added/updated
1186 * @param array $params
1189 function endPostProcess(&$params, &$activity) {
1190 if ($this->_activityTypeFile
) {
1191 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
1192 $className::endPostProcess($this, $params, $activity);