3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class generates form components for Activity
40 class CRM_Activity_Form_Activity
extends CRM_Contact_Form_Task
{
43 * The id of the object being edited / created
50 * store activity ids when multiple activities are created
54 public $_activityIds = array();
57 * The id of activity type
61 public $_activityTypeId;
64 * The name of activity type
68 public $_activityTypeName;
71 * The id of currently viewed contact
75 public $_currentlyViewedContactId;
78 * The id of source contact and target contact
82 protected $_sourceContactId;
83 protected $_targetContactId;
84 protected $_asigneeContactId;
91 public $_activityTypeFile;
94 * The id of the logged in user, used when add / edit
98 public $_currentUserId;
101 * The array of form field attributes
108 * The the directory inside CRM, to include activity type file from
112 protected $_crmDir = 'Activity';
120 protected $_isSurveyActivity;
122 protected $_values = array();
124 protected $unsavedWarn = TRUE;
127 * The _fields var can be used by sub class to set/unset/edit the
128 * form fields based on their requirement
131 function setFields() {
132 $this->_fields
= array(
135 'label' => ts('Subject'),
136 'attributes' => CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity',
142 'label' => ts('Duration'),
143 'attributes' => array('size' => 4, 'maxlength' => 8),
148 'label' => ts('Location'),
150 CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity',
157 'label' => ts('Details'),
158 // forces a smaller edit window
159 'attributes' => array('rows' => 4, 'cols' => 60),
162 'status_id' => array(
166 'priority_id' => array(
170 'source_contact_id' => array(
171 'type' => 'entityRef',
172 'label' => ts('Added By'),
175 'target_contact_id' => array(
176 'type' => 'entityRef',
177 'label' => ts('With Contact'),
178 'attributes' => array('multiple' => TRUE, 'create' => TRUE)
180 'assignee_contact_id' => array(
181 'type' => 'entityRef',
182 'label' => ts('Assigned To'),
183 'attributes' => array('multiple' => TRUE, 'create' => TRUE, 'api' => array('params' => array('is_deceased' => 0))),
185 'followup_assignee_contact_id' => array(
186 'type' => 'entityRef',
187 'label' => ts('Assigned To'),
188 'attributes' => array('multiple' => TRUE, 'create' => TRUE, 'api' => array('params' => array('is_deceased' => 0))),
190 'followup_activity_type_id' => array(
192 'label' => ts('Followup Activity'),
193 'attributes' => array('' => '- ' . ts('select activity') . ' -') + CRM_Core_PseudoConstant
::ActivityType(FALSE),
194 'extra' => array('class' => 'crm-select2'),
196 // Add optional 'Subject' field for the Follow-up Activiity, CRM-4491
197 'followup_activity_subject' => array(
199 'label' => ts('Subject'),
200 'attributes' => CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity',
206 if (($this->_context
== 'standalone') &&
207 ($printPDF = CRM_Utils_Array
::key('Print PDF Letter', $this->_fields
['followup_activity_type_id']['attributes']))
209 unset($this->_fields
['followup_activity_type_id']['attributes'][$printPDF]);
214 * Function to build the form
219 function preProcess() {
220 $this->_cdType
= CRM_Utils_Array
::value('type', $_GET);
221 $this->assign('cdType', FALSE);
222 if ($this->_cdType
) {
223 $this->assign('cdType', TRUE);
224 return CRM_Custom_Form_CustomData
::preProcess($this);
226 CRM_Core_Form_RecurringEntity
::preProcess('civicrm_activity');
227 $this->_atypefile
= CRM_Utils_Array
::value('atypefile', $_GET);
228 $this->assign('atypefile', FALSE);
229 if ($this->_atypefile
) {
230 $this->assign('atypefile', TRUE);
233 $session = CRM_Core_Session
::singleton();
234 $this->_currentUserId
= $session->get('userID');
236 $this->_currentlyViewedContactId
= $this->get('contactId');
237 if (!$this->_currentlyViewedContactId
) {
238 $this->_currentlyViewedContactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
240 $this->assign('contactId', $this->_currentlyViewedContactId
);
243 if (!isset($this->_context
)) {
244 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this);
245 if (CRM_Contact_Form_Search
::isSearchContext($this->_context
)) {
246 $this->_context
= 'search';
248 elseif (!in_array($this->_context
, array('dashlet', 'dashletFullscreen'))
249 && $this->_currentlyViewedContactId
251 $this->_context
= 'activity';
253 $this->_compContext
= CRM_Utils_Request
::retrieve('compContext', 'String', $this);
256 $this->assign('context', $this->_context
);
258 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this);
260 if ($this->_action
& CRM_Core_Action
::DELETE
) {
261 if (!CRM_Core_Permission
::check('delete activities')) {
262 CRM_Core_Error
::fatal(ts('You do not have permission to access this page'));
267 //when we come from contact search, activity id never comes.
268 //so don't try to get from object, it might gives you wrong one.
270 // if we're not adding new one, there must be an id to
271 // an activity we're trying to work on.
272 if ($this->_action
!= CRM_Core_Action
::ADD
&&
273 get_class($this->controller
) != 'CRM_Contact_Controller_Search'
275 $this->_activityId
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
278 $this->_activityTypeId
= CRM_Utils_Request
::retrieve('atype', 'Positive', $this);
279 $this->assign('atype', $this->_activityTypeId
);
281 $this->assign('activityId', $this->_activityId
);
283 //check for required permissions, CRM-6264
284 if ($this->_activityId
&&
285 in_array($this->_action
, array(
286 CRM_Core_Action
::UPDATE
,
287 CRM_Core_Action
::VIEW
289 !CRM_Activity_BAO_Activity
::checkPermission($this->_activityId
, $this->_action
)
291 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
293 if (($this->_action
& CRM_Core_Action
::VIEW
) &&
294 CRM_Activity_BAO_Activity
::checkPermission($this->_activityId
, CRM_Core_Action
::UPDATE
)
296 $this->assign('permission', 'edit');
299 if (!$this->_activityTypeId
&& $this->_activityId
) {
300 $this->_activityTypeId
= CRM_Core_DAO
::getFieldValue('CRM_Activity_DAO_Activity',
306 //Assigning Activity type name
307 if ($this->_activityTypeId
) {
308 $activityTName = CRM_Core_OptionGroup
::values('activity_type', FALSE, FALSE, FALSE, 'AND v.value = ' . $this->_activityTypeId
, 'name');
309 if ($activityTName[$this->_activityTypeId
]) {
310 $this->_activityTypeName
= $activityTName[$this->_activityTypeId
];
311 $this->assign('activityTName', $activityTName[$this->_activityTypeId
]);
316 if (isset($activityTName)) {
317 $activityName = CRM_Utils_Array
::value($this->_activityTypeId
, $activityTName);
318 $this->assign('pageTitle', ts('%1 Activity', array( 1 => $activityName)));
320 if ($this->_currentlyViewedContactId
) {
321 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_currentlyViewedContactId
);
322 // Check if this is default domain contact CRM-10482
323 if (CRM_Contact_BAO_Contact
::checkDomainContact($this->_currentlyViewedContactId
)) {
324 $displayName .= ' (' . ts('default organization') . ')';
326 CRM_Utils_System
::setTitle($displayName . ' - ' . $activityName);
329 CRM_Utils_System
::setTitle(ts('%1 Activity', array( 1 => $activityName)));
333 //check the mode when this form is called either single or as
335 if ($this->_activityTypeId ||
336 $this->_context
== 'standalone' ||
337 $this->_currentlyViewedContactId
339 $this->_single
= TRUE;
340 $this->assign('urlPath', 'civicrm/activity');
343 //set the appropriate action
344 $url = CRM_Utils_System
::currentPath();
345 $urlArray = explode('/', $url);
346 $searchPath = array_pop($urlArray);
347 $searchType = 'basic';
348 $this->_action
= CRM_Core_Action
::BASIC
;
349 switch ($searchPath) {
351 $searchType = $searchPath;
352 $this->_action
= CRM_Core_Action
::BASIC
;
356 $searchType = $searchPath;
357 $this->_action
= CRM_Core_Action
::ADVANCED
;
361 $searchType = $searchPath;
362 $this->_action
= CRM_Core_Action
::PROFILE
;
366 $this->_action
= CRM_Core_Action
::COPY
;
367 $searchType = $searchPath;
371 parent
::preProcess();
372 $this->_single
= FALSE;
374 $this->assign('urlPath', "civicrm/contact/search/$searchType");
375 $this->assign('urlPathVar', "_qf_Activity_display=true&qfKey={$this->controller->_key}");
378 $this->assign('single', $this->_single
);
379 $this->assign('action', $this->_action
);
381 if ($this->_action
& CRM_Core_Action
::VIEW
) {
382 // get the tree of custom fields
383 $this->_groupTree
= &CRM_Core_BAO_CustomGroup
::getTree('Activity', $this,
384 $this->_activityId
, 0, $this->_activityTypeId
388 if ($this->_activityTypeId
) {
389 //set activity type name and description to template
390 list($this->_activityTypeName
, $activityTypeDescription) = CRM_Core_BAO_OptionValue
::getActivityTypeDetails($this->_activityTypeId
);
391 $this->assign('activityTypeName', $this->_activityTypeName
);
392 $this->assign('activityTypeDescription', $activityTypeDescription);
396 $urlParams = $urlString = NULL;
397 $qfKey = CRM_Utils_Request
::retrieve('key', 'String', $this);
399 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $this);
403 if (!CRM_Utils_Rule
::qfKey($qfKey)) {
407 if ($this->_context
== 'fulltext') {
409 $urlParams = 'force=1';
410 $urlString = 'civicrm/contact/search/custom';
411 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
413 $urlParams .= '&context=fulltext&action=view';
414 $urlString = 'civicrm/contact/view/activity';
417 $urlParams .= "$keyName=$qfKey";
419 $this->assign('searchKey', $qfKey);
421 elseif (in_array($this->_context
, array(
428 $urlParams = 'reset=1';
429 $urlString = 'civicrm/dashboard';
431 elseif ($this->_context
== 'search') {
432 $urlParams = 'force=1';
434 $urlParams .= "&qfKey=$qfKey";
436 $path = CRM_Utils_System
::currentPath();
437 if ($this->_compContext
== 'advanced' ) {
438 $urlString = 'civicrm/contact/search/advanced';
440 elseif ($path == 'civicrm/contact/search'
441 ||
$path == 'civicrm/contact/search/advanced'
442 ||
$path == 'civicrm/contact/search/custom'
443 ||
$path == 'civicrm/group/search') {
447 $urlString = 'civicrm/activity/search';
449 $this->assign('searchKey', $qfKey);
451 elseif ($this->_context
!= 'caseActivity') {
452 $urlParams = "action=browse&reset=1&cid={$this->_currentlyViewedContactId}&selectedChild=activity";
453 $urlString = 'civicrm/contact/view';
457 $session->pushUserContext(CRM_Utils_System
::url($urlString, $urlParams));
460 // hack to retrieve activity type id from post variables
461 if (!$this->_activityTypeId
) {
462 $this->_activityTypeId
= CRM_Utils_Array
::value('activity_type_id', $_POST);
465 // when custom data is included in this page
466 if (!empty($_POST['hidden_custom'])) {
467 // we need to set it in the session for the below code to work
469 //need to assign custom data subtype to the template
470 $this->set('type', 'Activity');
471 $this->set('subType', $this->_activityTypeId
);
472 $this->set('entityId', $this->_activityId
);
473 CRM_Custom_Form_CustomData
::preProcess($this);
474 CRM_Custom_Form_CustomData
::buildQuickForm($this);
475 CRM_Custom_Form_CustomData
::setDefaultValues($this);
478 // add attachments part
479 CRM_Core_BAO_File
::buildAttachment($this, 'civicrm_activity', $this->_activityId
, NULL, TRUE);
481 // figure out the file name for activity type, if any
482 if ($this->_activityTypeId
&&
483 $this->_activityTypeFile
=
484 CRM_Activity_BAO_Activity
::getFileForActivityTypeId($this->_activityTypeId
, $this->_crmDir
)
486 $this->assign('activityTypeFile', $this->_activityTypeFile
);
487 $this->assign('crmDir', $this->_crmDir
);
492 if ($this->_activityTypeFile
) {
493 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
494 $className::preProcess($this);
497 $this->_values
= $this->get('values');
498 if (!is_array($this->_values
)) {
499 $this->_values
= array();
500 if (isset($this->_activityId
) && $this->_activityId
) {
501 $params = array('id' => $this->_activityId
);
502 CRM_Activity_BAO_Activity
::retrieve($params, $this->_values
);
504 $this->set('values', $this->_values
);
507 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
508 CRM_Core_Form_RecurringEntity
::preProcess('civicrm_activity');
513 * This function sets the 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 * @internal param array $options additional user data
827 * @return true if no errors, else array of errors
831 static function formRule($fields, $files, $self) {
832 // skip form rule if deleting
833 if (CRM_Utils_Array
::value('_qf_Activity_next_', $fields) == 'Delete') {
837 if (!$self->_single
&& !$fields['activity_type_id']) {
838 $errors['activity_type_id'] = ts('Activity Type is a required field');
841 //Activity type is mandatory if creating new activity, CRM-4515
842 if (array_key_exists('activity_type_id', $fields) && empty($fields['activity_type_id'])) {
843 $errors['activity_type_id'] = ts('Activity Type is required field.');
846 if (CRM_Utils_Array
::value('activity_type_id', $fields) == 3 &&
847 CRM_Utils_Array
::value('status_id', $fields) == 1
849 $errors['status_id'] = ts('You cannot record scheduled email activity.');
851 elseif (CRM_Utils_Array
::value('activity_type_id', $fields) == 4 &&
852 CRM_Utils_Array
::value('status_id', $fields) == 1
854 $errors['status_id'] = ts('You cannot record scheduled SMS activity.');
857 if (!empty($fields['followup_activity_type_id']) && empty($fields['followup_date'])) {
858 $errors['followup_date_time'] = ts('Followup date is a required field.');
860 //Activity type is mandatory if subject or follow-up date is specified for an Follow-up activity, CRM-4515
861 if ((!empty($fields['followup_activity_subject']) ||
!empty($fields['followup_date'])) && empty($fields['followup_activity_type_id'])) {
862 $errors['followup_activity_subject'] = ts('Follow-up Activity type is a required field.');
868 * Function to process the form
872 * @param null $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 //Set for repeat configuration in create mode
967 $params['entity_id'] = $this->_activityId
;
968 $params['entity_table'] = 'civicrm_activity';
969 $scheduleReminderDetails = array();
970 if (!empty($params['entity_id']) && !empty($params['entity_table'])) {
971 $checkParentExistsForThisId = CRM_Core_BAO_RecurringEntity
::getParentFor($params['entity_id'], $params['entity_table']);
972 if ($checkParentExistsForThisId) {
973 $params['parent_entity_id'] = $checkParentExistsForThisId;
974 $scheduleReminderDetails = CRM_Core_BAO_RecurringEntity
::getReminderDetailsByEntityId($checkParentExistsForThisId, $params['entity_table']);
977 $params['parent_entity_id'] = $params['entity_id'];
978 $scheduleReminderDetails = CRM_Core_BAO_RecurringEntity
::getReminderDetailsByEntityId($params['entity_id'], $params['entity_table']);
980 $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 * Function to let injecting activity type file do any processing
1171 * needed, before the activity is added/updated
1174 function beginPostProcess(&$params) {
1175 if ($this->_activityTypeFile
) {
1176 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
1177 $className::beginPostProcess($this, $params);
1182 * Function to let injecting activity type file do any processing
1183 * needed, after the activity has been added/updated
1186 function endPostProcess(&$params, &$activity) {
1187 if ($this->_activityTypeFile
) {
1188 $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
1189 $className::endPostProcess($this, $params, $activity );