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 * Explicitly declare the entity api name.
128 public function getDefaultEntity() {
133 * The _fields var can be used by sub class to set/unset/edit the
134 * form fields based on their requirement
136 public function setFields() {
137 $this->_fields
= array(
140 'label' => ts('Subject'),
141 'attributes' => CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity',
147 'label' => ts('Duration'),
148 'attributes' => array('size' => 4, 'maxlength' => 8),
153 'label' => ts('Location'),
154 'attributes' => CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity', 'location'),
159 'label' => ts('Details'),
160 // forces a smaller edit window
161 'attributes' => array('rows' => 4, 'cols' => 60),
164 'status_id' => array(
168 'priority_id' => array(
172 'source_contact_id' => array(
173 'type' => 'entityRef',
174 'label' => ts('Added By'),
177 'target_contact_id' => array(
178 'type' => 'entityRef',
179 'label' => ts('With Contact'),
180 'attributes' => array('multiple' => TRUE, 'create' => TRUE),
182 'assignee_contact_id' => array(
183 'type' => 'entityRef',
184 'label' => ts('Assigned to'),
185 'attributes' => array(
188 'api' => array('params' => array('is_deceased' => 0)),
191 'followup_assignee_contact_id' => array(
192 'type' => 'entityRef',
193 'label' => ts('Assigned to'),
194 'attributes' => array(
197 'api' => array('params' => array('is_deceased' => 0)),
200 'followup_activity_type_id' => array(
202 'label' => ts('Followup Activity'),
203 'attributes' => array('' => '- ' . ts('select activity') . ' -') + CRM_Core_PseudoConstant
::ActivityType(FALSE),
204 'extra' => array('class' => 'crm-select2'),
206 // Add optional 'Subject' field for the Follow-up Activiity, CRM-4491
207 'followup_activity_subject' => array(
209 'label' => ts('Subject'),
210 'attributes' => CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity',
216 if (($this->_context
== 'standalone') &&
217 ($printPDF = CRM_Utils_Array
::key('Print PDF Letter', $this->_fields
['followup_activity_type_id']['attributes']))
219 unset($this->_fields
['followup_activity_type_id']['attributes'][$printPDF]);
224 * Build the form object.
228 public function preProcess() {
229 $this->_cdType
= CRM_Utils_Array
::value('type', $_GET);
230 $this->assign('cdType', FALSE);
231 if ($this->_cdType
) {
232 $this->assign('cdType', TRUE);
233 return CRM_Custom_Form_CustomData
::preProcess($this);
235 CRM_Core_Form_RecurringEntity
::preProcess('civicrm_activity');
236 $this->_atypefile
= CRM_Utils_Array
::value('atypefile', $_GET);
237 $this->assign('atypefile', FALSE);
238 if ($this->_atypefile
) {
239 $this->assign('atypefile', TRUE);
242 $session = CRM_Core_Session
::singleton();
243 $this->_currentUserId
= $session->get('userID');
245 $this->_currentlyViewedContactId
= $this->get('contactId');
246 if (!$this->_currentlyViewedContactId
) {
247 $this->_currentlyViewedContactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
249 $this->assign('contactId', $this->_currentlyViewedContactId
);
252 if (!isset($this->_context
)) {
253 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this);
254 if (CRM_Contact_Form_Search
::isSearchContext($this->_context
)) {
255 $this->_context
= 'search';
257 elseif (!in_array($this->_context
, array('dashlet', 'dashletFullscreen'))
258 && $this->_currentlyViewedContactId
260 $this->_context
= 'activity';
262 $this->_compContext
= CRM_Utils_Request
::retrieve('compContext', 'String', $this);
265 $this->assign('context', $this->_context
);
267 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this);
269 if ($this->_action
& CRM_Core_Action
::DELETE
) {
270 if (!CRM_Core_Permission
::check('delete activities')) {
271 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
276 //when we come from contact search, activity id never comes.
277 //so don't try to get from object, it might gives you wrong one.
279 // if we're not adding new one, there must be an id to
280 // an activity we're trying to work on.
281 if ($this->_action
!= CRM_Core_Action
::ADD
&&
282 get_class($this->controller
) != 'CRM_Contact_Controller_Search'
284 $this->_activityId
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
287 $this->_activityTypeId
= CRM_Utils_Request
::retrieve('atype', 'Positive', $this);
288 $this->assign('atype', $this->_activityTypeId
);
290 $this->assign('activityId', $this->_activityId
);
292 //check for required permissions, CRM-6264
293 if ($this->_activityId
&&
294 in_array($this->_action
, array(
295 CRM_Core_Action
::UPDATE
,
296 CRM_Core_Action
::VIEW
,
298 !CRM_Activity_BAO_Activity
::checkPermission($this->_activityId
, $this->_action
)
300 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
302 if (($this->_action
& CRM_Core_Action
::VIEW
) &&
303 CRM_Activity_BAO_Activity
::checkPermission($this->_activityId
, CRM_Core_Action
::UPDATE
)
305 $this->assign('permission', 'edit');
308 if (!$this->_activityTypeId
&& $this->_activityId
) {
309 $this->_activityTypeId
= CRM_Core_DAO
::getFieldValue('CRM_Activity_DAO_Activity',
315 //Assigning Activity type name
316 if ($this->_activityTypeId
) {
317 $activityTName = CRM_Core_OptionGroup
::values('activity_type', FALSE, FALSE, FALSE, 'AND v.value = ' . $this->_activityTypeId
, 'name');
318 if ($activityTName[$this->_activityTypeId
]) {
319 $this->_activityTypeName
= $activityTName[$this->_activityTypeId
];
320 $this->assign('activityTName', $activityTName[$this->_activityTypeId
]);
325 if (isset($activityTName)) {
326 $activityName = CRM_Utils_Array
::value($this->_activityTypeId
, $activityTName);
327 $this->assign('pageTitle', ts('%1 Activity', array(1 => $activityName)));
329 if ($this->_currentlyViewedContactId
) {
330 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_currentlyViewedContactId
);
331 // Check if this is default domain contact CRM-10482
332 if (CRM_Contact_BAO_Contact
::checkDomainContact($this->_currentlyViewedContactId
)) {
333 $displayName .= ' (' . ts('default organization') . ')';
335 CRM_Utils_System
::setTitle($displayName . ' - ' . $activityName);
338 CRM_Utils_System
::setTitle(ts('%1 Activity', array(1 => $activityName)));
342 //check the mode when this form is called either single or as
344 if ($this->_activityTypeId ||
345 $this->_context
== 'standalone' ||
346 $this->_currentlyViewedContactId
348 $this->_single
= TRUE;
349 $this->assign('urlPath', 'civicrm/activity');
352 //set the appropriate action
353 $url = CRM_Utils_System
::currentPath();
354 $urlArray = explode('/', $url);
355 $searchPath = array_pop($urlArray);
356 $searchType = 'basic';
357 $this->_action
= CRM_Core_Action
::BASIC
;
358 switch ($searchPath) {
360 $searchType = $searchPath;
361 $this->_action
= CRM_Core_Action
::BASIC
;
365 $searchType = $searchPath;
366 $this->_action
= CRM_Core_Action
::ADVANCED
;
370 $searchType = $searchPath;
371 $this->_action
= CRM_Core_Action
::PROFILE
;
375 $this->_action
= CRM_Core_Action
::COPY
;
376 $searchType = $searchPath;
380 parent
::preProcess();
381 $this->_single
= FALSE;
383 $this->assign('urlPath', "civicrm/contact/search/$searchType");
384 $this->assign('urlPathVar', "_qf_Activity_display=true&qfKey={$this->controller->_key}");
387 $this->assign('single', $this->_single
);
388 $this->assign('action', $this->_action
);
390 if ($this->_action
& CRM_Core_Action
::VIEW
) {
391 // get the tree of custom fields
392 $this->_groupTree
= &CRM_Core_BAO_CustomGroup
::getTree('Activity', $this,
393 $this->_activityId
, 0, $this->_activityTypeId
397 if ($this->_activityTypeId
) {
398 //set activity type name and description to template
399 list($this->_activityTypeName
, $activityTypeDescription) = CRM_Core_BAO_OptionValue
::getActivityTypeDetails($this->_activityTypeId
);
400 $this->assign('activityTypeName', $this->_activityTypeName
);
401 $this->assign('activityTypeDescription', $activityTypeDescription);
405 $urlParams = $urlString = NULL;
406 $qfKey = CRM_Utils_Request
::retrieve('key', 'String', $this);
408 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $this);
412 if (!CRM_Utils_Rule
::qfKey($qfKey)) {
416 if ($this->_context
== 'fulltext') {
418 $urlParams = 'force=1';
419 $urlString = 'civicrm/contact/search/custom';
420 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
422 $urlParams .= '&context=fulltext&action=view';
423 $urlString = 'civicrm/contact/view/activity';
426 $urlParams .= "$keyName=$qfKey";
428 $this->assign('searchKey', $qfKey);
430 elseif (in_array($this->_context
, array(
437 $urlParams = 'reset=1';
438 $urlString = 'civicrm/dashboard';
440 elseif ($this->_context
== 'search') {
441 $urlParams = 'force=1';
443 $urlParams .= "&qfKey=$qfKey";
445 $path = CRM_Utils_System
::currentPath();
446 if ($this->_compContext
== 'advanced') {
447 $urlString = 'civicrm/contact/search/advanced';
449 elseif ($path == 'civicrm/group/search'
450 ||
$path == 'civicrm/contact/search'
451 ||
$path == 'civicrm/contact/search/advanced'
452 ||
$path == 'civicrm/contact/search/custom'
453 ||
$path == 'civicrm/group/search'
458 $urlString = 'civicrm/activity/search';
460 $this->assign('searchKey', $qfKey);
462 elseif ($this->_context
!= 'caseActivity') {
463 $urlParams = "action=browse&reset=1&cid={$this->_currentlyViewedContactId}&selectedChild=activity";
464 $urlString = 'civicrm/contact/view';
468 $session->pushUserContext(CRM_Utils_System
::url($urlString, $urlParams));
471 // hack to retrieve activity type id from post variables
472 if (!$this->_activityTypeId
) {
473 $this->_activityTypeId
= CRM_Utils_Array
::value('activity_type_id', $_POST);
476 // when custom data is included in this page
477 if (!empty($_POST['hidden_custom'])) {
478 // we need to set it in the session for the below code to work
480 //need to assign custom data subtype to the template
481 $this->set('type', 'Activity');
482 $this->set('subType', $this->_activityTypeId
);
483 $this->set('entityId', $this->_activityId
);
484 CRM_Custom_Form_CustomData
::preProcess($this);
485 CRM_Custom_Form_CustomData
::buildQuickForm($this);
486 CRM_Custom_Form_CustomData
::setDefaultValues($this);
489 // add attachments part
490 CRM_Core_BAO_File
::buildAttachment($this, 'civicrm_activity', $this->_activityId
, NULL, TRUE);
492 // figure out the file name for activity type, if any
493 if ($this->_activityTypeId
&&
494 $this->_activityTypeFile
= CRM_Activity_BAO_Activity
::getFileForActivityTypeId($this->_activityTypeId
, $this->_crmDir
)
496 $this->assign('activityTypeFile', $this->_activityTypeFile
);
497 $this->assign('crmDir', $this->_crmDir
);
502 if ($this->_activityTypeFile
) {
503 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
504 $className::preProcess($this);
507 $this->_values
= $this->get('values');
508 if (!is_array($this->_values
)) {
509 $this->_values
= array();
510 if (isset($this->_activityId
) && $this->_activityId
) {
511 $params = array('id' => $this->_activityId
);
512 CRM_Activity_BAO_Activity
::retrieve($params, $this->_values
);
514 $this->set('values', $this->_values
);
517 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
518 CRM_Core_Form_RecurringEntity
::preProcess('civicrm_activity');
523 * Set default values for the form. For edit/view mode
524 * the default values are retrieved from the database
529 public function setDefaultValues() {
530 if ($this->_cdType
) {
531 return CRM_Custom_Form_CustomData
::setDefaultValues($this);
534 $defaults = $this->_values + CRM_Core_Form_RecurringEntity
::setDefaultValues();
535 // if we're editing...
536 if (isset($this->_activityId
)) {
537 if (empty($defaults['activity_date_time'])) {
538 list($defaults['activity_date_time'], $defaults['activity_date_time_time']) = CRM_Utils_Date
::setDateDefaults(NULL, 'activityDateTime');
540 elseif ($this->_action
& CRM_Core_Action
::UPDATE
) {
541 $this->assign('current_activity_date_time', $defaults['activity_date_time']);
542 list($defaults['activity_date_time'],
543 $defaults['activity_date_time_time']
544 ) = CRM_Utils_Date
::setDateDefaults($defaults['activity_date_time'], 'activityDateTime');
545 list($defaults['repetition_start_date'], $defaults['repetition_start_date_time']) = CRM_Utils_Date
::setDateDefaults($defaults['activity_date_time'], 'activityDateTime');
548 if ($this->_context
!= 'standalone') {
549 $this->assign('target_contact_value',
550 CRM_Utils_Array
::value('target_contact_value', $defaults)
552 $this->assign('assignee_contact_value',
553 CRM_Utils_Array
::value('assignee_contact_value', $defaults)
557 // Fixme: why are we getting the wrong keys from upstream?
558 $defaults['target_contact_id'] = CRM_Utils_Array
::value('target_contact', $defaults);
559 $defaults['assignee_contact_id'] = CRM_Utils_Array
::value('assignee_contact', $defaults);
561 // set default tags if exists
562 $defaults['tag'] = CRM_Core_BAO_EntityTag
::getTag($this->_activityId
, 'civicrm_activity');
565 // if it's a new activity, we need to set default values for associated contact fields
566 $this->_sourceContactId
= $this->_currentUserId
;
567 $this->_targetContactId
= $this->_currentlyViewedContactId
;
569 $defaults['source_contact_id'] = $this->_sourceContactId
;
570 $defaults['target_contact_id'] = $this->_targetContactId
;
572 list($defaults['activity_date_time'], $defaults['activity_date_time_time'])
573 = CRM_Utils_Date
::setDateDefaults(NULL, 'activityDateTime');
576 if ($this->_activityTypeId
) {
577 $defaults['activity_type_id'] = $this->_activityTypeId
;
580 if (!$this->_single
&& !empty($this->_contactIds
)) {
581 $defaults['target_contact_id'] = $this->_contactIds
;
584 // CRM-15472 - 50 is around the practial limit of how many items a select2 entityRef can handle
585 if (!empty($defaults['target_contact_id'])) {
586 $count = count(is_array($defaults['target_contact_id']) ?
$defaults['target_contact_id'] : explode(',', $defaults['target_contact_id']));
588 $this->freeze(array('target_contact_id'));
592 if ($this->_action
& (CRM_Core_Action
::DELETE | CRM_Core_Action
::RENEW
)) {
593 $this->assign('delName', CRM_Utils_Array
::value('subject', $defaults));
596 if ($this->_activityTypeFile
) {
597 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
598 $defaults +
= $className::setDefaultValues($this);
600 if (empty($defaults['priority_id'])) {
601 $priority = CRM_Core_PseudoConstant
::get('CRM_Activity_DAO_Activity', 'priority_id');
602 $defaults['priority_id'] = array_search('Normal', $priority);
604 if (empty($defaults['status_id'])) {
605 $defaults['status_id'] = CRM_Core_OptionGroup
::getDefaultValue('activity_status');
610 public function buildQuickForm() {
611 if ($this->_action
& (CRM_Core_Action
::DELETE | CRM_Core_Action
::RENEW
)) {
612 //enable form element (ActivityLinks sets this true)
613 $this->assign('suppressForm', FALSE);
615 $button = ts('Delete');
616 if ($this->_action
& CRM_Core_Action
::RENEW
) {
617 $button = ts('Restore');
619 $this->addButtons(array(
623 'spacing' => ' ',
628 'name' => ts('Cancel'),
634 if ($this->_cdType
) {
635 return CRM_Custom_Form_CustomData
::buildQuickForm($this);
638 //build other activity links
639 CRM_Activity_Form_ActivityLinks
::commonBuildQuickForm($this);
641 //enable form element (ActivityLinks sets this true)
642 $this->assign('suppressForm', FALSE);
644 $element = &$this->add('select', 'activity_type_id', ts('Activity Type'),
645 array('' => '- ' . ts('select') . ' -') +
$this->_fields
['followup_activity_type_id']['attributes'],
647 'onchange' => "CRM.buildCustomData( 'Activity', this.value );",
648 'class' => 'crm-select2 required',
652 //freeze for update mode.
653 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
657 //Call to RecurringEntity buildQuickForm for add/update mode
658 if ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD
)) {
659 CRM_Core_Form_RecurringEntity
::buildQuickForm($this);
662 foreach ($this->_fields
as $field => $values) {
663 if (!empty($this->_fields
[$field])) {
664 $attribute = CRM_Utils_Array
::value('attributes', $values);
665 $required = !empty($values['required']);
667 if ($values['type'] == 'wysiwyg') {
668 $this->addWysiwyg($field, $values['label'], $attribute, $required);
670 elseif ($values['type'] == 'select' && empty($attribute)) {
671 $this->addSelect($field, array('entity' => 'activity'), $required);
673 elseif ($values['type'] == 'entityRef') {
674 $this->addEntityRef($field, $values['label'], $attribute, $required);
677 $this->add($values['type'], $field, $values['label'], $attribute, $required, CRM_Utils_Array
::value('extra', $values));
682 //CRM-7362 --add campaigns.
683 CRM_Campaign_BAO_Campaign
::addCampaign($this, CRM_Utils_Array
::value('campaign_id', $this->_values
));
685 //add engagement level CRM-7775
686 $buildEngagementLevel = FALSE;
687 if (CRM_Campaign_BAO_Campaign
::isCampaignEnable() &&
688 CRM_Campaign_BAO_Campaign
::accessCampaign()
690 $buildEngagementLevel = TRUE;
691 $this->addSelect('engagement_level', array('entity' => 'activity'));
692 $this->addRule('engagement_level',
693 ts('Please enter the engagement index as a number (integers only).'),
697 $this->assign('buildEngagementLevel', $buildEngagementLevel);
699 // check for survey activity
700 $this->_isSurveyActivity
= FALSE;
702 if ($this->_activityId
&& CRM_Campaign_BAO_Campaign
::isCampaignEnable() &&
703 CRM_Campaign_BAO_Campaign
::accessCampaign()
706 $this->_isSurveyActivity
= CRM_Campaign_BAO_Survey
::isSurveyActivity($this->_activityId
);
707 if ($this->_isSurveyActivity
) {
708 $surveyId = CRM_Core_DAO
::getFieldValue('CRM_Activity_DAO_Activity',
712 $responseOptions = CRM_Campaign_BAO_Survey
::getResponsesOptions($surveyId);
713 if ($responseOptions) {
714 $this->add('select', 'result', ts('Result'),
715 array('' => ts('- select -')) +
array_combine($responseOptions, $responseOptions)
720 $surveyTitle = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'title');
722 $this->assign('surveyTitle', $surveyTitle);
725 $this->assign('surveyActivity', $this->_isSurveyActivity
);
727 // this option should be available only during add mode
728 if ($this->_action
!= CRM_Core_Action
::UPDATE
) {
729 $this->add('advcheckbox', 'is_multi_activity', ts('Create a separate activity for each contact.'));
732 $this->addRule('duration',
733 ts('Please enter the duration as number of minutes (integers only).'), 'positiveInteger'
735 $this->addDateTime('activity_date_time', ts('Date'), TRUE, array('formatType' => 'activityDateTime'));
738 $this->addDateTime('followup_date', ts('in'), FALSE, array('formatType' => 'activityDateTime'));
740 // Only admins and case-workers can change the activity source
741 if (!CRM_Core_Permission
::check('administer CiviCRM') && $this->_context
!= 'caseActivity') {
742 $this->getElement('source_contact_id')->freeze();
745 //need to assign custom data type and subtype to the template
746 $this->assign('customDataType', 'Activity');
747 $this->assign('customDataSubType', $this->_activityTypeId
);
748 $this->assign('entityID', $this->_activityId
);
750 CRM_Core_BAO_Tag
::getTags('civicrm_activity', $tags, NULL,
751 ' ', TRUE);
754 $this->add('select', 'tag', ts('Tags'), $tags, FALSE,
755 array('id' => 'tags', 'multiple' => 'multiple', 'class' => 'crm-select2 huge')
759 // we need to hide activity tagset for special activities
760 $specialActivities = array('Open Case');
762 if (!in_array($this->_activityTypeName
, $specialActivities)) {
764 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_activity');
765 CRM_Core_Form_Tag
::buildQuickForm($this, $parentNames, 'civicrm_activity', $this->_activityId
);
768 // if we're viewing, we're assigning different buttons than for adding/editing
769 if ($this->_action
& CRM_Core_Action
::VIEW
) {
770 if (isset($this->_groupTree
)) {
771 CRM_Core_BAO_CustomGroup
::buildCustomDataView($this, $this->_groupTree
);
773 // form should be frozen for view mode
779 'name' => ts('Done'),
781 $this->addButtons($buttons);
785 '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.'),
786 '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.'),
788 $js = array('onclick' => "return activityStatus(" . json_encode($message) . ");");
789 $this->addButtons(array(
792 'name' => ts('Save'),
798 'name' => ts('Cancel'),
804 if ($this->_activityTypeFile
) {
805 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
807 $className::buildQuickForm($this);
808 $this->addFormRule(array($className, 'formRule'), $this);
811 $this->addFormRule(array('CRM_Activity_Form_Activity', 'formRule'), $this);
813 if (CRM_Core_BAO_Setting
::getItem(
814 CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
815 'activity_assignee_notification'
818 $this->assign('activityAssigneeNotification', TRUE);
821 $this->assign('activityAssigneeNotification', FALSE);
828 * @param array $fields
829 * The input form values.
830 * @param array $files
831 * The uploaded files if any.
835 * true if no errors, else array of errors
837 public static function formRule($fields, $files, $self) {
838 // skip form rule if deleting
839 if (CRM_Utils_Array
::value('_qf_Activity_next_', $fields) == 'Delete') {
843 if ((array_key_exists('activity_type_id', $fields) ||
!$self->_single
) && empty($fields['activity_type_id'])) {
844 $errors['activity_type_id'] = ts('Activity Type is a required field');
847 if (CRM_Utils_Array
::value('activity_type_id', $fields) == 3 &&
848 CRM_Utils_Array
::value('status_id', $fields) == 1
850 $errors['status_id'] = ts('You cannot record scheduled email activity.');
852 elseif (CRM_Utils_Array
::value('activity_type_id', $fields) == 4 &&
853 CRM_Utils_Array
::value('status_id', $fields) == 1
855 $errors['status_id'] = ts('You cannot record scheduled SMS activity.');
858 if (!empty($fields['followup_activity_type_id']) && empty($fields['followup_date'])) {
859 $errors['followup_date_time'] = ts('Followup date is a required field.');
861 //Activity type is mandatory if subject or follow-up date is specified for an Follow-up activity, CRM-4515
862 if ((!empty($fields['followup_activity_subject']) ||
!empty($fields['followup_date'])) && empty($fields['followup_activity_type_id'])) {
863 $errors['followup_activity_subject'] = ts('Follow-up Activity type is a required field.');
869 * Process the form submission.
872 * @param array $params
875 public function postProcess($params = NULL) {
876 if ($this->_action
& CRM_Core_Action
::DELETE
) {
877 $deleteParams = array('id' => $this->_activityId
);
878 $moveToTrash = CRM_Case_BAO_Case
::isCaseActivity($this->_activityId
);
879 CRM_Activity_BAO_Activity
::deleteActivity($deleteParams, $moveToTrash);
881 // delete tags for the entity
883 'entity_table' => 'civicrm_activity',
884 'entity_id' => $this->_activityId
,
887 CRM_Core_BAO_EntityTag
::del($tagParams);
889 CRM_Core_Session
::setStatus(ts("Selected Activity has been deleted successfully."), ts('Record Deleted'), 'success');
893 // store the submitted values in an array
895 $params = $this->controller
->exportValues($this->_name
);
898 //set activity type id
899 if (empty($params['activity_type_id'])) {
900 $params['activity_type_id'] = $this->_activityTypeId
;
903 if (!empty($params['hidden_custom']) &&
904 !isset($params['custom'])
906 $customFields = CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
907 $this->_activityTypeId
909 $customFields = CRM_Utils_Array
::crmArrayMerge($customFields,
910 CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
914 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
921 // store the date with proper format
922 $params['activity_date_time'] = CRM_Utils_Date
::processDate($params['activity_date_time'], $params['activity_date_time_time']);
924 // format params as arrays
925 foreach (array('target', 'assignee', 'followup_assignee') as $name) {
926 if (!empty($params["{$name}_contact_id"])) {
927 $params["{$name}_contact_id"] = explode(',', $params["{$name}_contact_id"]);
930 $params["{$name}_contact_id"] = array();
934 // get ids for associated contacts
935 if (!$params['source_contact_id']) {
936 $params['source_contact_id'] = $this->_currentUserId
;
939 if (isset($this->_activityId
)) {
940 $params['id'] = $this->_activityId
;
943 // add attachments as needed
944 CRM_Core_BAO_File
::formatAttachment($params,
951 if (!empty($params['is_multi_activity']) &&
952 !CRM_Utils_Array
::crmIsEmptyArray($params['target_contact_id'])
954 $targetContacts = $params['target_contact_id'];
955 foreach ($targetContacts as $targetContactId) {
956 $params['target_contact_id'] = array($targetContactId);
958 $activity[] = $this->processActivity($params);
963 $activity = $this->processActivity($params);
966 $activityIds = empty($this->_activityIds
) ?
array($this->_activityId
) : $this->_activityIds
;
967 foreach ($activityIds as $activityId) {
968 // set params for repeat configuration in create mode
969 $params['entity_id'] = $activityId;
970 $params['entity_table'] = 'civicrm_activity';
971 if (!empty($params['entity_id']) && !empty($params['entity_table'])) {
972 $checkParentExistsForThisId = CRM_Core_BAO_RecurringEntity
::getParentFor($params['entity_id'], $params['entity_table']);
973 if ($checkParentExistsForThisId) {
974 $params['parent_entity_id'] = $checkParentExistsForThisId;
975 $scheduleReminderDetails = CRM_Core_BAO_RecurringEntity
::getReminderDetailsByEntityId($checkParentExistsForThisId, $params['entity_table']);
978 $params['parent_entity_id'] = $params['entity_id'];
979 $scheduleReminderDetails = CRM_Core_BAO_RecurringEntity
::getReminderDetailsByEntityId($params['entity_id'], $params['entity_table']);
981 if (property_exists($scheduleReminderDetails, 'id')) {
982 $params['schedule_reminder_id'] = $scheduleReminderDetails->id
;
985 $params['dateColumns'] = array('activity_date_time');
987 // Set default repetition start if it was not provided.
988 if (empty($params['repetition_start_date'])) {
989 $params['repetition_start_date'] = $params['activity_date_time'];
993 unset($params['id']);
994 $linkedEntities = array(
996 'table' => 'civicrm_activity_contact',
997 'findCriteria' => array(
998 'activity_id' => $activityId,
1000 'linkedColumns' => array('activity_id'),
1001 'isRecurringEntityRecord' => FALSE,
1004 CRM_Core_Form_RecurringEntity
::postProcess($params, 'civicrm_activity', $linkedEntities);
1007 return array('activity' => $activity);
1011 * Process activity creation.
1013 * @param array $params
1014 * Associated array of submitted values.
1016 * @return self|null|object
1018 protected function processActivity(&$params) {
1019 $activityAssigned = array();
1020 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
1021 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
1022 // format assignee params
1023 if (!CRM_Utils_Array
::crmIsEmptyArray($params['assignee_contact_id'])) {
1024 //skip those assignee contacts which are already assigned
1025 //while sending a copy.CRM-4509.
1026 $activityAssigned = array_flip($params['assignee_contact_id']);
1027 if ($this->_activityId
) {
1028 $assigneeContacts = CRM_Activity_BAO_ActivityContact
::getNames($this->_activityId
, $assigneeID);
1029 $activityAssigned = array_diff_key($activityAssigned, $assigneeContacts);
1033 // call begin post process. Idea is to let injecting file do
1034 // any processing before the activity is added/updated.
1035 $this->beginPostProcess($params);
1037 $activity = CRM_Activity_BAO_Activity
::create($params);
1039 // add tags if exists
1040 $tagParams = array();
1041 if (!empty($params['tag'])) {
1042 foreach ($params['tag'] as $tag) {
1043 $tagParams[$tag] = 1;
1048 CRM_Core_BAO_EntityTag
::create($tagParams, 'civicrm_activity', $activity->id
);
1051 if (isset($params['activity_taglist']) && !empty($params['activity_taglist'])) {
1052 CRM_Core_Form_Tag
::postProcess($params['activity_taglist'], $activity->id
, 'civicrm_activity', $this);
1055 // call end post process. Idea is to let injecting file do any
1056 // processing needed, after the activity has been added/updated.
1057 $this->endPostProcess($params, $activity);
1060 if (!empty($params['is_multi_activity'])) {
1061 $this->_activityIds
[] = $activity->id
;
1064 $this->_activityId
= $activity->id
;
1067 // create follow up activity if needed
1068 $followupStatus = '';
1069 $followupActivity = NULL;
1070 if (!empty($params['followup_activity_type_id'])) {
1071 $followupActivity = CRM_Activity_BAO_Activity
::createFollowupActivity($activity->id
, $params);
1072 $followupStatus = ts('A followup activity has been scheduled.');
1075 // send copy to assignee contacts.CRM-4509
1078 if (CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
1079 'activity_assignee_notification')
1081 $activityIDs = array($activity->id
);
1082 if ($followupActivity) {
1083 $activityIDs = array_merge($activityIDs, array($followupActivity->id
));
1085 $assigneeContacts = CRM_Activity_BAO_ActivityAssignment
::getAssigneeNames($activityIDs, TRUE, FALSE);
1087 if (!CRM_Utils_Array
::crmIsEmptyArray($params['assignee_contact_id'])) {
1088 $mailToContacts = array();
1090 //build an associative array with unique email addresses.
1091 foreach ($activityAssigned as $id => $dnc) {
1092 if (isset($id) && array_key_exists($id, $assigneeContacts)) {
1093 $mailToContacts[$assigneeContacts[$id]['email']] = $assigneeContacts[$id];
1097 if (!CRM_Utils_array
::crmIsEmptyArray($mailToContacts)) {
1098 //include attachments while sending a copy of activity.
1099 $attachments = CRM_Core_BAO_File
::getEntityFile('civicrm_activity', $activity->id
);
1101 $ics = new CRM_Activity_BAO_ICalendar($activity);
1102 $ics->addAttachment($attachments, $mailToContacts);
1104 // CRM-8400 add param with _currentlyViewedContactId for URL link in mail
1105 CRM_Case_BAO_Case
::sendActivityCopy(NULL, $activity->id
, $mailToContacts, $attachments, NULL);
1109 $mailStatus .= ts("A copy of the activity has also been sent to assignee contacts(s).");
1113 // Also send email to follow-up activity assignees if set
1114 if ($followupActivity) {
1115 $mailToFollowupContacts = array();
1116 foreach ($assigneeContacts as $values) {
1117 if ($values['activity_id'] == $followupActivity->id
) {
1118 $mailToFollowupContacts[$values['email']] = $values;
1122 if (!CRM_Utils_array
::crmIsEmptyArray($mailToFollowupContacts)) {
1123 $ics = new CRM_Activity_BAO_ICalendar($followupActivity);
1124 $attachments = CRM_Core_BAO_File
::getEntityFile('civicrm_activity', $followupActivity->id
);
1125 $ics->addAttachment($attachments, $mailToFollowupContacts);
1127 CRM_Case_BAO_Case
::sendActivityCopy(NULL, $followupActivity->id
, $mailToFollowupContacts, $attachments, NULL);
1131 $mailStatus .= '<br />' . ts("A copy of the follow-up activity has also been sent to follow-up assignee contacts(s).");
1136 // set status message
1138 if (!empty($params['subject'])) {
1139 $subject = "'" . $params['subject'] . "'";
1142 CRM_Core_Session
::setStatus(ts('Activity %1 has been saved. %2 %3',
1145 2 => $followupStatus,
1148 ), ts('Saved'), 'success');
1154 * Shorthand for getting id by display name (makes code more readable)
1155 * @param $displayName
1156 * @return null|string
1158 protected function _getIdByDisplayName($displayName) {
1159 return CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
1167 * Shorthand for getting display name by id (makes code more readable)
1169 * @return null|string
1171 protected function _getDisplayNameById($id) {
1172 return CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
1180 * Let injecting activity type file do any processing.
1181 * needed, before the activity is added/updated
1183 * @param array $params
1185 public function beginPostProcess(&$params) {
1186 if ($this->_activityTypeFile
) {
1187 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
1188 $className::beginPostProcess($this, $params);
1193 * Let injecting activity type file do any processing
1194 * needed, after the activity has been added/updated
1196 * @param array $params
1199 public function endPostProcess(&$params, &$activity) {
1200 if ($this->_activityTypeFile
) {
1201 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
1202 $className::endPostProcess($this, $params, $activity);