3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 public 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'),
148 'attributes' => CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity', 'location'),
153 'label' => ts('Details'),
154 // forces a smaller edit window
155 'attributes' => array('rows' => 4, 'cols' => 60),
158 'status_id' => array(
162 'priority_id' => array(
166 'source_contact_id' => array(
167 'type' => 'entityRef',
168 'label' => ts('Added By'),
171 'target_contact_id' => array(
172 'type' => 'entityRef',
173 'label' => ts('With Contact'),
174 'attributes' => array('multiple' => TRUE, 'create' => TRUE)
176 'assignee_contact_id' => array(
177 'type' => 'entityRef',
178 'label' => ts('Assigned to'),
179 'attributes' => array('multiple' => TRUE, 'create' => TRUE, 'api' => array('params' => array('is_deceased' => 0))),
181 'followup_assignee_contact_id' => array(
182 'type' => 'entityRef',
183 'label' => ts('Assigned to'),
184 'attributes' => array('multiple' => TRUE, 'create' => TRUE, 'api' => array('params' => array('is_deceased' => 0))),
186 'followup_activity_type_id' => array(
188 'label' => ts('Followup Activity'),
189 'attributes' => array('' => '- ' . ts('select activity') . ' -') + CRM_Core_PseudoConstant
::ActivityType(FALSE),
190 'extra' => array('class' => 'crm-select2'),
192 // Add optional 'Subject' field for the Follow-up Activiity, CRM-4491
193 'followup_activity_subject' => array(
195 'label' => ts('Subject'),
196 'attributes' => CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity',
202 if (($this->_context
== 'standalone') &&
203 ($printPDF = CRM_Utils_Array
::key('Print PDF Letter', $this->_fields
['followup_activity_type_id']['attributes']))
205 unset($this->_fields
['followup_activity_type_id']['attributes'][$printPDF]);
210 * Build the form object
214 public function preProcess() {
215 $this->_cdType
= CRM_Utils_Array
::value('type', $_GET);
216 $this->assign('cdType', FALSE);
217 if ($this->_cdType
) {
218 $this->assign('cdType', TRUE);
219 return CRM_Custom_Form_CustomData
::preProcess($this);
221 CRM_Core_Form_RecurringEntity
::preProcess('civicrm_activity');
222 $this->_atypefile
= CRM_Utils_Array
::value('atypefile', $_GET);
223 $this->assign('atypefile', FALSE);
224 if ($this->_atypefile
) {
225 $this->assign('atypefile', TRUE);
228 $session = CRM_Core_Session
::singleton();
229 $this->_currentUserId
= $session->get('userID');
231 $this->_currentlyViewedContactId
= $this->get('contactId');
232 if (!$this->_currentlyViewedContactId
) {
233 $this->_currentlyViewedContactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
235 $this->assign('contactId', $this->_currentlyViewedContactId
);
238 if (!isset($this->_context
)) {
239 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this);
240 if (CRM_Contact_Form_Search
::isSearchContext($this->_context
)) {
241 $this->_context
= 'search';
243 elseif (!in_array($this->_context
, array('dashlet', 'dashletFullscreen'))
244 && $this->_currentlyViewedContactId
246 $this->_context
= 'activity';
248 $this->_compContext
= CRM_Utils_Request
::retrieve('compContext', 'String', $this);
251 $this->assign('context', $this->_context
);
253 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this);
255 if ($this->_action
& CRM_Core_Action
::DELETE
) {
256 if (!CRM_Core_Permission
::check('delete activities')) {
257 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
262 //when we come from contact search, activity id never comes.
263 //so don't try to get from object, it might gives you wrong one.
265 // if we're not adding new one, there must be an id to
266 // an activity we're trying to work on.
267 if ($this->_action
!= CRM_Core_Action
::ADD
&&
268 get_class($this->controller
) != 'CRM_Contact_Controller_Search'
270 $this->_activityId
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
273 $this->_activityTypeId
= CRM_Utils_Request
::retrieve('atype', 'Positive', $this);
274 $this->assign('atype', $this->_activityTypeId
);
276 $this->assign('activityId', $this->_activityId
);
278 //check for required permissions, CRM-6264
279 if ($this->_activityId
&&
280 in_array($this->_action
, array(
281 CRM_Core_Action
::UPDATE
,
282 CRM_Core_Action
::VIEW
284 !CRM_Activity_BAO_Activity
::checkPermission($this->_activityId
, $this->_action
)
286 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
288 if (($this->_action
& CRM_Core_Action
::VIEW
) &&
289 CRM_Activity_BAO_Activity
::checkPermission($this->_activityId
, CRM_Core_Action
::UPDATE
)
291 $this->assign('permission', 'edit');
294 if (!$this->_activityTypeId
&& $this->_activityId
) {
295 $this->_activityTypeId
= CRM_Core_DAO
::getFieldValue('CRM_Activity_DAO_Activity',
301 //Assigning Activity type name
302 if ($this->_activityTypeId
) {
303 $activityTName = CRM_Core_OptionGroup
::values('activity_type', FALSE, FALSE, FALSE, 'AND v.value = ' . $this->_activityTypeId
, 'name');
304 if ($activityTName[$this->_activityTypeId
]) {
305 $this->_activityTypeName
= $activityTName[$this->_activityTypeId
];
306 $this->assign('activityTName', $activityTName[$this->_activityTypeId
]);
311 if (isset($activityTName)) {
312 $activityName = CRM_Utils_Array
::value($this->_activityTypeId
, $activityTName);
313 $this->assign('pageTitle', ts('%1 Activity', array( 1 => $activityName)));
315 if ($this->_currentlyViewedContactId
) {
316 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_currentlyViewedContactId
);
317 // Check if this is default domain contact CRM-10482
318 if (CRM_Contact_BAO_Contact
::checkDomainContact($this->_currentlyViewedContactId
)) {
319 $displayName .= ' (' . ts('default organization') . ')';
321 CRM_Utils_System
::setTitle($displayName . ' - ' . $activityName);
324 CRM_Utils_System
::setTitle(ts('%1 Activity', array( 1 => $activityName)));
328 //check the mode when this form is called either single or as
330 if ($this->_activityTypeId ||
331 $this->_context
== 'standalone' ||
332 $this->_currentlyViewedContactId
334 $this->_single
= TRUE;
335 $this->assign('urlPath', 'civicrm/activity');
338 //set the appropriate action
339 $url = CRM_Utils_System
::currentPath();
340 $urlArray = explode('/', $url);
341 $searchPath = array_pop($urlArray);
342 $searchType = 'basic';
343 $this->_action
= CRM_Core_Action
::BASIC
;
344 switch ($searchPath) {
346 $searchType = $searchPath;
347 $this->_action
= CRM_Core_Action
::BASIC
;
351 $searchType = $searchPath;
352 $this->_action
= CRM_Core_Action
::ADVANCED
;
356 $searchType = $searchPath;
357 $this->_action
= CRM_Core_Action
::PROFILE
;
361 $this->_action
= CRM_Core_Action
::COPY
;
362 $searchType = $searchPath;
366 parent
::preProcess();
367 $this->_single
= FALSE;
369 $this->assign('urlPath', "civicrm/contact/search/$searchType");
370 $this->assign('urlPathVar', "_qf_Activity_display=true&qfKey={$this->controller->_key}");
373 $this->assign('single', $this->_single
);
374 $this->assign('action', $this->_action
);
376 if ($this->_action
& CRM_Core_Action
::VIEW
) {
377 // get the tree of custom fields
378 $this->_groupTree
= &CRM_Core_BAO_CustomGroup
::getTree('Activity', $this,
379 $this->_activityId
, 0, $this->_activityTypeId
383 if ($this->_activityTypeId
) {
384 //set activity type name and description to template
385 list($this->_activityTypeName
, $activityTypeDescription) = CRM_Core_BAO_OptionValue
::getActivityTypeDetails($this->_activityTypeId
);
386 $this->assign('activityTypeName', $this->_activityTypeName
);
387 $this->assign('activityTypeDescription', $activityTypeDescription);
391 $urlParams = $urlString = NULL;
392 $qfKey = CRM_Utils_Request
::retrieve('key', 'String', $this);
394 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $this);
398 if (!CRM_Utils_Rule
::qfKey($qfKey)) {
402 if ($this->_context
== 'fulltext') {
404 $urlParams = 'force=1';
405 $urlString = 'civicrm/contact/search/custom';
406 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
408 $urlParams .= '&context=fulltext&action=view';
409 $urlString = 'civicrm/contact/view/activity';
412 $urlParams .= "$keyName=$qfKey";
414 $this->assign('searchKey', $qfKey);
416 elseif (in_array($this->_context
, array(
423 $urlParams = 'reset=1';
424 $urlString = 'civicrm/dashboard';
426 elseif ($this->_context
== 'search') {
427 $urlParams = 'force=1';
429 $urlParams .= "&qfKey=$qfKey";
431 $path = CRM_Utils_System
::currentPath();
432 if ($this->_compContext
== 'advanced' ) {
433 $urlString = 'civicrm/contact/search/advanced';
435 elseif ($path == 'civicrm/group/search'
436 ||
$path == 'civicrm/contact/search'
437 ||
$path == 'civicrm/contact/search/advanced'
438 ||
$path == 'civicrm/contact/search/custom'
439 ||
$path == 'civicrm/group/search') {
443 $urlString = 'civicrm/activity/search';
445 $this->assign('searchKey', $qfKey);
447 elseif ($this->_context
!= 'caseActivity') {
448 $urlParams = "action=browse&reset=1&cid={$this->_currentlyViewedContactId}&selectedChild=activity";
449 $urlString = 'civicrm/contact/view';
453 $session->pushUserContext(CRM_Utils_System
::url($urlString, $urlParams));
456 // hack to retrieve activity type id from post variables
457 if (!$this->_activityTypeId
) {
458 $this->_activityTypeId
= CRM_Utils_Array
::value('activity_type_id', $_POST);
461 // when custom data is included in this page
462 if (!empty($_POST['hidden_custom'])) {
463 // we need to set it in the session for the below code to work
465 //need to assign custom data subtype to the template
466 $this->set('type', 'Activity');
467 $this->set('subType', $this->_activityTypeId
);
468 $this->set('entityId', $this->_activityId
);
469 CRM_Custom_Form_CustomData
::preProcess($this);
470 CRM_Custom_Form_CustomData
::buildQuickForm($this);
471 CRM_Custom_Form_CustomData
::setDefaultValues($this);
474 // add attachments part
475 CRM_Core_BAO_File
::buildAttachment($this, 'civicrm_activity', $this->_activityId
, NULL, TRUE);
477 // figure out the file name for activity type, if any
478 if ($this->_activityTypeId
&&
479 $this->_activityTypeFile
=
480 CRM_Activity_BAO_Activity
::getFileForActivityTypeId($this->_activityTypeId
, $this->_crmDir
)
482 $this->assign('activityTypeFile', $this->_activityTypeFile
);
483 $this->assign('crmDir', $this->_crmDir
);
488 if ($this->_activityTypeFile
) {
489 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
490 $className::preProcess($this);
493 $this->_values
= $this->get('values');
494 if (!is_array($this->_values
)) {
495 $this->_values
= array();
496 if (isset($this->_activityId
) && $this->_activityId
) {
497 $params = array('id' => $this->_activityId
);
498 CRM_Activity_BAO_Activity
::retrieve($params, $this->_values
);
500 $this->set('values', $this->_values
);
503 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
504 CRM_Core_Form_RecurringEntity
::preProcess('civicrm_activity');
509 * Set default values for the form. For edit/view mode
510 * the default values are retrieved from the database
515 public function setDefaultValues() {
516 if ($this->_cdType
) {
517 return CRM_Custom_Form_CustomData
::setDefaultValues($this);
520 $defaults = $this->_values
;
521 // if we're editing...
522 if (isset($this->_activityId
)) {
523 if (empty($defaults['activity_date_time'])) {
524 list($defaults['activity_date_time'], $defaults['activity_date_time_time']) = CRM_Utils_Date
::setDateDefaults(NULL, 'activityDateTime');
526 elseif ($this->_action
& CRM_Core_Action
::UPDATE
) {
527 $this->assign('current_activity_date_time', $defaults['activity_date_time']);
528 list($defaults['activity_date_time'],
529 $defaults['activity_date_time_time']
530 ) = CRM_Utils_Date
::setDateDefaults($defaults['activity_date_time'], 'activityDateTime');
531 list($defaults['repetition_start_date'], $defaults['repetition_start_date_time']) = CRM_Utils_Date
::setDateDefaults($defaults['activity_date_time'], 'activityDateTime');
532 $recurringEntityDefaults = array();
533 $recurringEntityDefaults = CRM_Core_Form_RecurringEntity
::setDefaultValues();
534 $defaults = array_merge($defaults, $recurringEntityDefaults);
537 if ($this->_context
!= 'standalone') {
538 $this->assign('target_contact_value',
539 CRM_Utils_Array
::value('target_contact_value', $defaults)
541 $this->assign('assignee_contact_value',
542 CRM_Utils_Array
::value('assignee_contact_value', $defaults)
546 // Fixme: why are we getting the wrong keys from upstream?
547 $defaults['target_contact_id'] = CRM_Utils_Array
::value('target_contact', $defaults);
548 $defaults['assignee_contact_id'] = CRM_Utils_Array
::value('assignee_contact', $defaults);
550 // set default tags if exists
551 $defaults['tag'] = CRM_Core_BAO_EntityTag
::getTag($this->_activityId
, 'civicrm_activity');
554 // if it's a new activity, we need to set default values for associated contact fields
555 $this->_sourceContactId
= $this->_currentUserId
;
556 $this->_targetContactId
= $this->_currentlyViewedContactId
;
558 $defaults['source_contact_id'] = $this->_sourceContactId
;
559 $defaults['target_contact_id'] = $this->_targetContactId
;
561 list($defaults['activity_date_time'], $defaults['activity_date_time_time']) =
562 CRM_Utils_Date
::setDateDefaults(NULL, 'activityDateTime');
565 if ($this->_activityTypeId
) {
566 $defaults['activity_type_id'] = $this->_activityTypeId
;
569 if (!$this->_single
&& !empty($this->_contactIds
)) {
570 $defaults['target_contact_id'] = $this->_contactIds
;
573 // CRM-15472 - 50 is around the practial limit of how many items a select2 entityRef can handle
574 if (!empty($defaults['target_contact_id'])) {
575 $count = count(is_array($defaults['target_contact_id']) ?
$defaults['target_contact_id'] : explode(',', $defaults['target_contact_id']));
577 $this->freeze(array('target_contact_id'));
581 if ($this->_action
& (CRM_Core_Action
::DELETE | CRM_Core_Action
::RENEW
)) {
582 $this->assign('delName', CRM_Utils_Array
::value('subject', $defaults));
585 if ($this->_activityTypeFile
) {
586 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
587 $defaults +
= $className::setDefaultValues($this);
589 if (empty($defaults['priority_id'])) {
590 $priority = CRM_Core_PseudoConstant
::get('CRM_Activity_DAO_Activity', 'priority_id');
591 $defaults['priority_id'] = array_search('Normal', $priority);
593 if (empty($defaults['status_id'])) {
594 $defaults['status_id'] = CRM_Core_OptionGroup
::getDefaultValue('activity_status');
599 public function buildQuickForm() {
600 if ($this->_action
& (CRM_Core_Action
::DELETE | CRM_Core_Action
::RENEW
)) {
601 //enable form element (ActivityLinks sets this true)
602 $this->assign('suppressForm', FALSE);
604 $button = ts('Delete');
605 if ($this->_action
& CRM_Core_Action
::RENEW
) {
606 $button = ts('Restore');
608 $this->addButtons(array(
612 'spacing' => ' ',
617 'name' => ts('Cancel')
623 if ($this->_cdType
) {
624 return CRM_Custom_Form_CustomData
::buildQuickForm($this);
627 //build other activity links
628 CRM_Activity_Form_ActivityLinks
::commonBuildQuickForm($this);
630 //enable form element (ActivityLinks sets this true)
631 $this->assign('suppressForm', FALSE);
633 $element = & $this->add('select', 'activity_type_id', ts('Activity Type'),
634 $this->_fields
['followup_activity_type_id']['attributes'],
636 'onchange' => "CRM.buildCustomData( 'Activity', this.value );",
637 'class' => 'crm-select2',
641 //freeze for update mode.
642 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
646 //Call to RecurringEntity buildQuickForm for add/update mode
647 if ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD
)) {
648 CRM_Core_Form_RecurringEntity
::buildQuickForm($this);
651 foreach ($this->_fields
as $field => $values) {
652 if (!empty($this->_fields
[$field])) {
653 $attribute = CRM_Utils_Array
::value('attributes', $values);
654 $required = !empty($values['required']);
656 if ($values['type'] == 'wysiwyg') {
657 $this->addWysiwyg($field, $values['label'], $attribute, $required);
659 elseif ($values['type'] == 'select' && empty($attribute)) {
660 $this->addSelect($field, array('entity' => 'activity'), $required);
662 elseif ($values['type'] == 'entityRef') {
663 $this->addEntityRef($field, $values['label'], $attribute, $required);
666 $this->add($values['type'], $field, $values['label'], $attribute, $required, CRM_Utils_Array
::value('extra', $values));
671 //CRM-7362 --add campaigns.
672 CRM_Campaign_BAO_Campaign
::addCampaign($this, CRM_Utils_Array
::value('campaign_id', $this->_values
));
674 //add engagement level CRM-7775
675 $buildEngagementLevel = FALSE;
676 if (CRM_Campaign_BAO_Campaign
::isCampaignEnable() &&
677 CRM_Campaign_BAO_Campaign
::accessCampaign()
679 $buildEngagementLevel = TRUE;
680 $this->addSelect('engagement_level',array('entity' => 'activity'));
681 $this->addRule('engagement_level',
682 ts('Please enter the engagement index as a number (integers only).'),
686 $this->assign('buildEngagementLevel', $buildEngagementLevel);
688 // check for survey activity
689 $this->_isSurveyActivity
= FALSE;
691 if ($this->_activityId
&& CRM_Campaign_BAO_Campaign
::isCampaignEnable() &&
692 CRM_Campaign_BAO_Campaign
::accessCampaign()
695 $this->_isSurveyActivity
= CRM_Campaign_BAO_Survey
::isSurveyActivity($this->_activityId
);
696 if ($this->_isSurveyActivity
) {
697 $surveyId = CRM_Core_DAO
::getFieldValue('CRM_Activity_DAO_Activity',
701 $responseOptions = CRM_Campaign_BAO_Survey
::getResponsesOptions($surveyId);
702 if ($responseOptions) {
703 $this->add('select', 'result', ts('Result'),
704 array('' => ts('- select -')) +
array_combine($responseOptions, $responseOptions)
709 $surveyTitle = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'title');
711 $this->assign('surveyTitle', $surveyTitle);
714 $this->assign('surveyActivity', $this->_isSurveyActivity
);
716 // this option should be available only during add mode
717 if ($this->_action
!= CRM_Core_Action
::UPDATE
) {
718 $this->add('advcheckbox', 'is_multi_activity', ts('Create a separate activity for each contact.'));
721 $this->addRule('duration',
722 ts('Please enter the duration as number of minutes (integers only).'), 'positiveInteger'
724 $this->addDateTime('activity_date_time', ts('Date'), TRUE, array('formatType' => 'activityDateTime'));
727 $this->addDateTime('followup_date', ts('in'), FALSE, array('formatType' => 'activityDateTime'));
729 // Only admins and case-workers can change the activity source
730 if (!CRM_Core_Permission
::check('administer CiviCRM') && $this->_context
!= 'caseActivity') {
731 $this->getElement('source_contact_id')->freeze();
734 //need to assign custom data type and subtype to the template
735 $this->assign('customDataType', 'Activity');
736 $this->assign('customDataSubType', $this->_activityTypeId
);
737 $this->assign('entityID', $this->_activityId
);
739 CRM_Core_BAO_Tag
::getTags('civicrm_activity', $tags, NULL,
740 ' ', TRUE);
743 $this->add('select', 'tag', ts('Tags'), $tags, FALSE,
744 array('id' => 'tags', 'multiple' => 'multiple', 'class' => 'crm-select2 huge')
748 // we need to hide activity tagset for special activities
749 $specialActivities = array('Open Case');
751 if (!in_array($this->_activityTypeName
, $specialActivities)) {
753 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_activity');
754 CRM_Core_Form_Tag
::buildQuickForm($this, $parentNames, 'civicrm_activity', $this->_activityId
);
757 // if we're viewing, we're assigning different buttons than for adding/editing
758 if ($this->_action
& CRM_Core_Action
::VIEW
) {
759 if (isset($this->_groupTree
)) {
760 CRM_Core_BAO_CustomGroup
::buildCustomDataView($this, $this->_groupTree
);
762 // form should be frozen for view mode
770 $this->addButtons($buttons);
774 '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.'),
775 '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.'),
777 $js = array('onclick' => "return activityStatus(" . json_encode($message) . ");");
778 $this->addButtons(array(
781 'name' => ts('Save'),
787 'name' => ts('Cancel')
793 if ($this->_activityTypeFile
) {
794 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
796 $className::buildQuickForm($this);
797 $this->addFormRule(array($className, 'formRule'), $this);
800 $this->addFormRule(array('CRM_Activity_Form_Activity', 'formRule'), $this);
802 if (CRM_Core_BAO_Setting
::getItem(
803 CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
804 'activity_assignee_notification'
807 $this->assign('activityAssigneeNotification', TRUE);
810 $this->assign('activityAssigneeNotification', FALSE);
817 * @param array $fields the input form values
818 * @param array $files the uploaded files if any
821 * @return true if no errors, else array of errors
824 public static function formRule($fields, $files, $self) {
825 // skip form rule if deleting
826 if (CRM_Utils_Array
::value('_qf_Activity_next_', $fields) == 'Delete') {
830 if (!$self->_single
&& !$fields['activity_type_id']) {
831 $errors['activity_type_id'] = ts('Activity Type is a required field');
834 //Activity type is mandatory if creating new activity, CRM-4515
835 if (array_key_exists('activity_type_id', $fields) && empty($fields['activity_type_id'])) {
836 $errors['activity_type_id'] = ts('Activity Type is required field.');
839 if (CRM_Utils_Array
::value('activity_type_id', $fields) == 3 &&
840 CRM_Utils_Array
::value('status_id', $fields) == 1
842 $errors['status_id'] = ts('You cannot record scheduled email activity.');
844 elseif (CRM_Utils_Array
::value('activity_type_id', $fields) == 4 &&
845 CRM_Utils_Array
::value('status_id', $fields) == 1
847 $errors['status_id'] = ts('You cannot record scheduled SMS activity.');
850 if (!empty($fields['followup_activity_type_id']) && empty($fields['followup_date'])) {
851 $errors['followup_date_time'] = ts('Followup date is a required field.');
853 //Activity type is mandatory if subject or follow-up date is specified for an Follow-up activity, CRM-4515
854 if ((!empty($fields['followup_activity_subject']) ||
!empty($fields['followup_date'])) && empty($fields['followup_activity_type_id'])) {
855 $errors['followup_activity_subject'] = ts('Follow-up Activity type is a required field.');
861 * Process the form submission
864 * @param array $params
867 public function postProcess($params = NULL) {
868 if ($this->_action
& CRM_Core_Action
::DELETE
) {
869 $deleteParams = array('id' => $this->_activityId
);
870 $moveToTrash = CRM_Case_BAO_Case
::isCaseActivity($this->_activityId
);
871 CRM_Activity_BAO_Activity
::deleteActivity($deleteParams, $moveToTrash);
873 // delete tags for the entity
875 'entity_table' => 'civicrm_activity',
876 'entity_id' => $this->_activityId
879 CRM_Core_BAO_EntityTag
::del($tagParams);
881 CRM_Core_Session
::setStatus(ts("Selected Activity has been deleted successfully."), ts('Record Deleted'), 'success');
885 // store the submitted values in an array
887 $params = $this->controller
->exportValues($this->_name
);
890 //set activity type id
891 if (empty($params['activity_type_id'])) {
892 $params['activity_type_id'] = $this->_activityTypeId
;
895 if (!empty($params['hidden_custom']) &&
896 !isset($params['custom'])
898 $customFields = CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
899 $this->_activityTypeId
901 $customFields = CRM_Utils_Array
::crmArrayMerge($customFields,
902 CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
906 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
913 // store the date with proper format
914 $params['activity_date_time'] = CRM_Utils_Date
::processDate($params['activity_date_time'], $params['activity_date_time_time']);
916 // format params as arrays
917 foreach (array('target', 'assignee', 'followup_assignee') as $name) {
918 if (!empty($params["{$name}_contact_id"])) {
919 $params["{$name}_contact_id"] = explode(',', $params["{$name}_contact_id"]);
922 $params["{$name}_contact_id"] = array();
926 // get ids for associated contacts
927 if (!$params['source_contact_id']) {
928 $params['source_contact_id'] = $this->_currentUserId
;
931 if (isset($this->_activityId
)) {
932 $params['id'] = $this->_activityId
;
935 // add attachments as needed
936 CRM_Core_BAO_File
::formatAttachment($params,
943 if (!empty($params['is_multi_activity']) &&
944 !CRM_Utils_Array
::crmIsEmptyArray($params['target_contact_id'])
946 $targetContacts = $params['target_contact_id'];
947 foreach ($targetContacts as $targetContactId) {
948 $params['target_contact_id'] = array($targetContactId);
950 $activity[] = $this->processActivity($params);
955 $activity = $this->processActivity($params);
958 //Set for repeat configuration in create mode
959 $params['entity_id'] = $this->_activityId
;
960 $params['entity_table'] = 'civicrm_activity';
961 $scheduleReminderDetails = array();
962 if (!empty($params['entity_id']) && !empty($params['entity_table'])) {
963 $checkParentExistsForThisId = CRM_Core_BAO_RecurringEntity
::getParentFor($params['entity_id'], $params['entity_table']);
964 if ($checkParentExistsForThisId) {
965 $params['parent_entity_id'] = $checkParentExistsForThisId;
966 $scheduleReminderDetails = CRM_Core_BAO_RecurringEntity
::getReminderDetailsByEntityId($checkParentExistsForThisId, $params['entity_table']);
969 $params['parent_entity_id'] = $params['entity_id'];
970 $scheduleReminderDetails = CRM_Core_BAO_RecurringEntity
::getReminderDetailsByEntityId($params['entity_id'], $params['entity_table']);
972 if (property_exists($scheduleReminderDetails, 'id')) {
973 $params['schedule_reminder_id'] = $scheduleReminderDetails->id
;
976 $params['dateColumns'] = array('activity_date_time');
979 unset($params['id']);
980 $linkedEntities = array(
982 'table' => 'civicrm_activity_contact',
983 'findCriteria' => array(
984 'activity_id' => $this->_activityId
,
986 'linkedColumns' => array('activity_id'),
987 'isRecurringEntityRecord' => FALSE,
990 CRM_Core_Form_RecurringEntity
::postProcess($params, 'civicrm_activity', $linkedEntities);
992 return array('activity' => $activity);
996 * Process activity creation
998 * @param array $params associated array of submitted values
1000 * @return $this|null|object
1002 protected function processActivity(&$params) {
1003 $activityAssigned = array();
1004 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
1005 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
1006 // format assignee params
1007 if (!CRM_Utils_Array
::crmIsEmptyArray($params['assignee_contact_id'])) {
1008 //skip those assignee contacts which are already assigned
1009 //while sending a copy.CRM-4509.
1010 $activityAssigned = array_flip($params['assignee_contact_id']);
1011 if ($this->_activityId
) {
1012 $assigneeContacts = CRM_Activity_BAO_ActivityContact
::getNames($this->_activityId
, $assigneeID);
1013 $activityAssigned = array_diff_key($activityAssigned, $assigneeContacts);
1017 // call begin post process. Idea is to let injecting file do
1018 // any processing before the activity is added/updated.
1019 $this->beginPostProcess($params);
1021 $activity = CRM_Activity_BAO_Activity
::create($params);
1023 // add tags if exists
1024 $tagParams = array();
1025 if (!empty($params['tag'])) {
1026 foreach ($params['tag'] as $tag) {
1027 $tagParams[$tag] = 1;
1032 CRM_Core_BAO_EntityTag
::create($tagParams, 'civicrm_activity', $activity->id
);
1035 if (isset($params['activity_taglist']) && !empty($params['activity_taglist'])) {
1036 CRM_Core_Form_Tag
::postProcess($params['activity_taglist'], $activity->id
, 'civicrm_activity', $this);
1039 // call end post process. Idea is to let injecting file do any
1040 // processing needed, after the activity has been added/updated.
1041 $this->endPostProcess($params, $activity);
1044 if (!empty($params['is_multi_activity'])) {
1045 $this->_activityIds
[] = $activity->id
;
1048 $this->_activityId
= $activity->id
;
1051 // create follow up activity if needed
1052 $followupStatus = '';
1053 $followupActivity = NULL;
1054 if (!empty($params['followup_activity_type_id'])) {
1055 $followupActivity = CRM_Activity_BAO_Activity
::createFollowupActivity($activity->id
, $params);
1056 $followupStatus = ts('A followup activity has been scheduled.');
1059 // send copy to assignee contacts.CRM-4509
1062 if (CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
1063 'activity_assignee_notification')) {
1064 $activityIDs = array($activity->id
);
1065 if ($followupActivity) {
1066 $activityIDs = array_merge($activityIDs, array($followupActivity->id
));
1068 $assigneeContacts = CRM_Activity_BAO_ActivityAssignment
::getAssigneeNames($activityIDs, TRUE, FALSE);
1070 if (!CRM_Utils_Array
::crmIsEmptyArray($params['assignee_contact_id'])) {
1071 $mailToContacts = array();
1073 //build an associative array with unique email addresses.
1074 foreach ($activityAssigned as $id => $dnc) {
1075 if (isset($id) && array_key_exists($id, $assigneeContacts)) {
1076 $mailToContacts[$assigneeContacts[$id]['email']] = $assigneeContacts[$id];
1080 if (!CRM_Utils_array
::crmIsEmptyArray($mailToContacts)) {
1081 //include attachments while sending a copy of activity.
1082 $attachments = CRM_Core_BAO_File
::getEntityFile('civicrm_activity', $activity->id
);
1084 $ics = new CRM_Activity_BAO_ICalendar($activity);
1085 $ics->addAttachment($attachments, $mailToContacts);
1087 // CRM-8400 add param with _currentlyViewedContactId for URL link in mail
1088 CRM_Case_BAO_Case
::sendActivityCopy(NULL, $activity->id
, $mailToContacts, $attachments, NULL);
1092 $mailStatus .= ts("A copy of the activity has also been sent to assignee contacts(s).");
1096 // Also send email to follow-up activity assignees if set
1097 if ($followupActivity) {
1098 $mailToFollowupContacts = array();
1099 foreach ($assigneeContacts as $values) {
1100 if ($values['activity_id'] == $followupActivity->id
) {
1101 $mailToFollowupContacts[$values['email']] = $values;
1105 if (!CRM_Utils_array
::crmIsEmptyArray($mailToFollowupContacts)) {
1106 $ics = new CRM_Activity_BAO_ICalendar($followupActivity);
1107 $attachments = CRM_Core_BAO_File
::getEntityFile('civicrm_activity', $followupActivity->id
);
1108 $ics->addAttachment($attachments, $mailToFollowupContacts);
1110 CRM_Case_BAO_Case
::sendActivityCopy(NULL, $followupActivity->id
, $mailToFollowupContacts, $attachments, NULL);
1114 $mailStatus .= '<br />' . ts("A copy of the follow-up activity has also been sent to follow-up assignee contacts(s).");
1119 // set status message
1121 if (!empty($params['subject'])) {
1122 $subject = "'" . $params['subject'] . "'";
1125 CRM_Core_Session
::setStatus(ts('Activity %1 has been saved. %2 %3',
1128 2 => $followupStatus,
1131 ), ts('Saved'), 'success');
1137 * Shorthand for getting id by display name (makes code more readable)
1140 protected function _getIdByDisplayName($displayName) {
1141 return CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
1149 * Shorthand for getting display name by id (makes code more readable)
1152 protected function _getDisplayNameById($id) {
1153 return CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
1161 * Let injecting activity type file do any processing
1162 * needed, before the activity is added/updated
1164 * @param array $params
1166 public function beginPostProcess(&$params) {
1167 if ($this->_activityTypeFile
) {
1168 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
1169 $className::beginPostProcess($this, $params);
1174 * Let injecting activity type file do any processing
1175 * needed, after the activity has been added/updated
1177 * @param array $params
1180 public function endPostProcess(&$params, &$activity) {
1181 if ($this->_activityTypeFile
) {
1182 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
1183 $className::endPostProcess($this, $params, $activity);