3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class provides the functionality for batch profile update for Activities
21 class CRM_Activity_Form_Task_Batch
extends CRM_Activity_Form_Task
{
24 * The title of the group.
31 * Maximum profile fields that will be displayed.
34 protected $_maxFields = 9;
37 * Variable to store redirect path.
40 protected $_userContext;
43 * Build all the data structures needed to build the form.
45 public function preProcess() {
47 // Initialize the task and row fields.
50 // Get the contact read only fields to display.
51 $readOnlyFields = array_merge(['sort_name' => ts('Added By'), 'target_sort_name' => ts('With Contact')],
52 CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
53 'contact_autocomplete_options',
54 TRUE, NULL, FALSE, 'name', TRUE
58 // Get the read only field data.
59 $returnProperties = array_fill_keys(array_keys($readOnlyFields), 1);
60 $contactDetails = CRM_Contact_BAO_Contact_Utils
::contactDetails($this->_activityHolderIds
,
61 'Activity', $returnProperties
63 $readOnlyFields['assignee_display_name'] = ts('Assigned to');
64 if (!empty($contactDetails)) {
65 foreach ($contactDetails as $key => $value) {
66 $assignee = CRM_Activity_BAO_ActivityAssignment
::retrieveAssigneeIdsByActivityId($key);
67 $assigneeContact = [];
68 foreach ($assignee as $values) {
69 $assigneeContact[] = CRM_Contact_BAO_Contact
::displayName($values);
71 $contactDetails[$key]['assignee_display_name'] = !empty($assigneeContact) ?
implode(';', $assigneeContact) : NULL;
74 $this->assign('contactDetails', $contactDetails);
75 $this->assign('readOnlyFields', $readOnlyFields);
79 * Build the form object.
81 public function buildQuickForm() {
82 $ufGroupId = $this->get('ufGroupId');
85 throw new CRM_Core_Exception('The profile id is missing');
87 $this->_title
= ts('Update multiple activities') . ' - ' . CRM_Core_BAO_UFGroup
::getTitle($ufGroupId);
88 CRM_Utils_System
::setTitle($this->_title
);
90 $this->addDefaultButtons(ts('Save'));
92 $this->_fields
= CRM_Core_BAO_UFGroup
::getFields($ufGroupId, FALSE, CRM_Core_Action
::VIEW
);
94 // remove file type field and then limit fields
95 $suppressFields = FALSE;
96 $removehtmlTypes = ['File'];
97 foreach ($this->_fields
as $name => $field) {
98 if (CRM_Core_BAO_CustomField
::getKeyID($name) &&
99 in_array($this->_fields
[$name]['html_type'], $removehtmlTypes)
101 $suppressFields = TRUE;
102 unset($this->_fields
[$name]);
105 // Fix to reduce size as we are using this field in grid.
106 if (is_array($field['attributes']) && !empty($this->_fields
[$name]['attributes']['size']) && $this->_fields
[$name]['attributes']['size'] > 19) {
107 // Shrink class to "form-text-medium".
108 $this->_fields
[$name]['attributes']['size'] = 19;
112 $this->_fields
= array_slice($this->_fields
, 0, $this->_maxFields
);
117 'name' => ts('Update Activities'),
122 'name' => ts('Cancel'),
126 $this->assign('profileTitle', $this->_title
);
127 $this->assign('componentIds', $this->_activityHolderIds
);
129 // Load all campaigns.
130 if (array_key_exists('activity_campaign_id', $this->_fields
)) {
131 $this->_componentCampaigns
= [];
132 CRM_Core_PseudoConstant
::populate($this->_componentCampaigns
,
133 'CRM_Activity_DAO_Activity',
134 TRUE, 'campaign_id', 'id',
135 ' id IN (' . implode(' , ', array_values($this->_activityHolderIds
)) . ' ) '
139 $customFields = CRM_Core_BAO_CustomField
::getFields('Activity');
140 // It is possible to have fields that are required in CiviCRM not be required in the
141 // profile. Overriding that here. Perhaps a better approach would be to
142 // make them required in the schema & read that up through getFields functionality.
143 $requiredFields = ['activity_date_time'];
145 foreach ($this->_activityHolderIds
as $activityId) {
146 $typeId = CRM_Core_DAO
::getFieldValue("CRM_Activity_DAO_Activity", $activityId, 'activity_type_id');
147 foreach ($this->_fields
as $name => $field) {
148 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
149 $customValue = CRM_Utils_Array
::value($customFieldID, $customFields);
150 if (!empty($customValue['extends_entity_column_value'])) {
151 $entityColumnValue = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
152 $customValue['extends_entity_column_value']
155 if (!empty($entityColumnValue[$typeId]) ||
156 CRM_Utils_System
::isNull($entityColumnValue[$typeId])
158 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $activityId);
162 // Handle non custom fields.
163 if (in_array($field['name'], $requiredFields)) {
164 $field['is_required'] = TRUE;
166 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $activityId);
171 $this->assign('fields', $this->_fields
);
173 // Don't set the status message when form is submitted.
174 // $buttonName = $this->controller->getButtonName('submit');
176 if ($suppressFields) {
177 CRM_Core_Session
::setStatus(ts("File type field(s) in the selected profile are not supported for Update multiple activities."), ts('Some Fields Excluded'), 'info');
180 $this->addDefaultButtons(ts('Update Activities'));
184 * Set default values for the form.
186 public function setDefaultValues() {
187 if (empty($this->_fields
)) {
192 foreach ($this->_activityHolderIds
as $activityId) {
193 CRM_Core_BAO_UFGroup
::setProfileDefaults(NULL, $this->_fields
, $defaults, FALSE, $activityId, 'Activity');
200 * Process the form after the input has been submitted and validated.
202 public function postProcess() {
203 $params = $this->exportValues();
205 if (isset($params['field'])) {
206 foreach ($params['field'] as $key => $value) {
208 $value['custom'] = CRM_Core_BAO_CustomField
::postProcess($value,
213 if (!empty($value['activity_status_id'])) {
214 $value['status_id'] = $value['activity_status_id'];
217 if (!empty($value['activity_details'])) {
218 $value['details'] = $value['activity_details'];
221 if (!empty($value['activity_location'])) {
222 $value['location'] = $value['activity_location'];
225 if (!empty($value['activity_subject'])) {
226 $value['subject'] = $value['activity_subject'];
229 $activityId = civicrm_api3('activity', 'create', $value);
231 // @todo this would be done by the api call above if the parames were passed through.
232 // @todo extract submit functions &
233 // extend CRM_Event_Form_Task_BatchTest::testSubmit with a data provider to test
234 // handling of custom data, specifically checkbox fields.
235 if (!empty($value['custom']) &&
236 is_array($value['custom'])
238 CRM_Core_BAO_CustomValueTable
::store($value['custom'], 'civicrm_activity', $activityId['id']);
241 CRM_Core_Session
::setStatus("", ts("Updates Saved"), "success");
244 CRM_Core_Session
::setStatus("", ts("No Updates Saved"), "info");