Merge in 5.20
[civicrm-core.git] / CRM / Activity / Form / Task / Batch.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class provides the functionality for batch profile update for Activities
20 */
21 class CRM_Activity_Form_Task_Batch extends CRM_Activity_Form_Task {
22
23 /**
24 * The title of the group.
25 *
26 * @var string
27 */
28 protected $_title;
29
30 /**
31 * Maximum profile fields that will be displayed.
32 * @var int
33 */
34 protected $_maxFields = 9;
35
36 /**
37 * Variable to store redirect path.
38 * @var string
39 */
40 protected $_userContext;
41
42 /**
43 * Build all the data structures needed to build the form.
44 */
45 public function preProcess() {
46
47 // Initialize the task and row fields.
48 parent::preProcess();
49
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
55 )
56 );
57
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
62 );
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);
70 }
71 $contactDetails[$key]['assignee_display_name'] = !empty($assigneeContact) ? implode(';', $assigneeContact) : NULL;
72 }
73 }
74 $this->assign('contactDetails', $contactDetails);
75 $this->assign('readOnlyFields', $readOnlyFields);
76 }
77
78 /**
79 * Build the form object.
80 */
81 public function buildQuickForm() {
82 $ufGroupId = $this->get('ufGroupId');
83
84 if (!$ufGroupId) {
85 throw new CRM_Core_Exception('The profile id is missing');
86 }
87 $this->_title = ts('Update multiple activities') . ' - ' . CRM_Core_BAO_UFGroup::getTitle($ufGroupId);
88 CRM_Utils_System::setTitle($this->_title);
89
90 $this->addDefaultButtons(ts('Save'));
91 $this->_fields = [];
92 $this->_fields = CRM_Core_BAO_UFGroup::getFields($ufGroupId, FALSE, CRM_Core_Action::VIEW);
93
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)
100 ) {
101 $suppressFields = TRUE;
102 unset($this->_fields[$name]);
103 }
104
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;
109 }
110 }
111
112 $this->_fields = array_slice($this->_fields, 0, $this->_maxFields);
113
114 $this->addButtons([
115 [
116 'type' => 'submit',
117 'name' => ts('Update Activities'),
118 'isDefault' => TRUE,
119 ],
120 [
121 'type' => 'cancel',
122 'name' => ts('Cancel'),
123 ],
124 ]);
125
126 $this->assign('profileTitle', $this->_title);
127 $this->assign('componentIds', $this->_activityHolderIds);
128
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)) . ' ) '
136 );
137 }
138
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'];
144
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']
153 );
154 }
155 if (!empty($entityColumnValue[$typeId]) ||
156 CRM_Utils_System::isNull($entityColumnValue[$typeId])
157 ) {
158 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $activityId);
159 }
160 }
161 else {
162 // Handle non custom fields.
163 if (in_array($field['name'], $requiredFields)) {
164 $field['is_required'] = TRUE;
165 }
166 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $activityId);
167 }
168 }
169 }
170
171 $this->assign('fields', $this->_fields);
172
173 // Don't set the status message when form is submitted.
174 // $buttonName = $this->controller->getButtonName('submit');
175
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');
178 }
179
180 $this->addDefaultButtons(ts('Update Activities'));
181 }
182
183 /**
184 * Set default values for the form.
185 */
186 public function setDefaultValues() {
187 if (empty($this->_fields)) {
188 return;
189 }
190
191 $defaults = [];
192 foreach ($this->_activityHolderIds as $activityId) {
193 CRM_Core_BAO_UFGroup::setProfileDefaults(NULL, $this->_fields, $defaults, FALSE, $activityId, 'Activity');
194 }
195
196 return $defaults;
197 }
198
199 /**
200 * Process the form after the input has been submitted and validated.
201 */
202 public function postProcess() {
203 $params = $this->exportValues();
204
205 if (isset($params['field'])) {
206 foreach ($params['field'] as $key => $value) {
207
208 $value['custom'] = CRM_Core_BAO_CustomField::postProcess($value,
209 $key, 'Activity'
210 );
211 $value['id'] = $key;
212
213 if (!empty($value['activity_status_id'])) {
214 $value['status_id'] = $value['activity_status_id'];
215 }
216
217 if (!empty($value['activity_details'])) {
218 $value['details'] = $value['activity_details'];
219 }
220
221 if (!empty($value['activity_location'])) {
222 $value['location'] = $value['activity_location'];
223 }
224
225 if (!empty($value['activity_subject'])) {
226 $value['subject'] = $value['activity_subject'];
227 }
228
229 $activityId = civicrm_api3('activity', 'create', $value);
230
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'])
237 ) {
238 CRM_Core_BAO_CustomValueTable::store($value['custom'], 'civicrm_activity', $activityId['id']);
239 }
240 }
241 CRM_Core_Session::setStatus("", ts("Updates Saved"), "success");
242 }
243 else {
244 CRM_Core_Session::setStatus("", ts("No Updates Saved"), "info");
245 }
246 }
247
248 }