Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
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 | /** | |
fe482240 | 24 | * The title of the group. |
6a488035 TO |
25 | * |
26 | * @var string | |
27 | */ | |
28 | protected $_title; | |
29 | ||
30 | /** | |
fe482240 | 31 | * Maximum profile fields that will be displayed. |
62d3ee27 | 32 | * @var int |
6a488035 TO |
33 | */ |
34 | protected $_maxFields = 9; | |
35 | ||
36 | /** | |
fe482240 | 37 | * Variable to store redirect path. |
62d3ee27 | 38 | * @var string |
6a488035 TO |
39 | */ |
40 | protected $_userContext; | |
41 | ||
88aae6d4 A |
42 | /** |
43 | * @var bool | |
44 | */ | |
45 | public $submitOnce = TRUE; | |
46 | ||
6a488035 | 47 | /** |
fe482240 | 48 | * Build all the data structures needed to build the form. |
6a488035 | 49 | */ |
00be9182 | 50 | public function preProcess() { |
6a488035 | 51 | |
7808aae6 | 52 | // Initialize the task and row fields. |
6a488035 TO |
53 | parent::preProcess(); |
54 | ||
7808aae6 | 55 | // Get the contact read only fields to display. |
ff3f9641 | 56 | $readOnlyFields = array_merge(['sort_name' => ts('Added by'), 'target_sort_name' => ts('With Contact')], |
6a488035 TO |
57 | CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, |
58 | 'contact_autocomplete_options', | |
59 | TRUE, NULL, FALSE, 'name', TRUE | |
60 | ) | |
61 | ); | |
62 | ||
7808aae6 | 63 | // Get the read only field data. |
6a488035 TO |
64 | $returnProperties = array_fill_keys(array_keys($readOnlyFields), 1); |
65 | $contactDetails = CRM_Contact_BAO_Contact_Utils::contactDetails($this->_activityHolderIds, | |
66 | 'Activity', $returnProperties | |
67 | ); | |
3bd48a28 | 68 | $readOnlyFields['assignee_display_name'] = ts('Assigned to'); |
22e263ad | 69 | if (!empty($contactDetails)) { |
9b873358 | 70 | foreach ($contactDetails as $key => $value) { |
bb03a208 | 71 | $assignee = CRM_Activity_BAO_ActivityAssignment::retrieveAssigneeIdsByActivityId($key); |
115d6fc1 | 72 | $assigneeContact = []; |
363544d7 | 73 | foreach ($assignee as $values) { |
74 | $assigneeContact[] = CRM_Contact_BAO_Contact::displayName($values); | |
bb03a208 RK |
75 | } |
76 | $contactDetails[$key]['assignee_display_name'] = !empty($assigneeContact) ? implode(';', $assigneeContact) : NULL; | |
77 | } | |
78 | } | |
6a488035 TO |
79 | $this->assign('contactDetails', $contactDetails); |
80 | $this->assign('readOnlyFields', $readOnlyFields); | |
81 | } | |
82 | ||
83 | /** | |
2e2605fe | 84 | * Build the form object. |
6a488035 | 85 | */ |
00be9182 | 86 | public function buildQuickForm() { |
6a488035 TO |
87 | $ufGroupId = $this->get('ufGroupId'); |
88 | ||
89 | if (!$ufGroupId) { | |
2a66c165 | 90 | throw new CRM_Core_Exception('The profile id is missing'); |
6a488035 | 91 | } |
b581842f | 92 | $this->_title = ts('Update multiple activities') . ' - ' . CRM_Core_BAO_UFGroup::getTitle($ufGroupId); |
6a488035 TO |
93 | CRM_Utils_System::setTitle($this->_title); |
94 | ||
95 | $this->addDefaultButtons(ts('Save')); | |
be2fb01f | 96 | $this->_fields = []; |
6a488035 TO |
97 | $this->_fields = CRM_Core_BAO_UFGroup::getFields($ufGroupId, FALSE, CRM_Core_Action::VIEW); |
98 | ||
99 | // remove file type field and then limit fields | |
100 | $suppressFields = FALSE; | |
be2fb01f | 101 | $removehtmlTypes = ['File']; |
6a488035 | 102 | foreach ($this->_fields as $name => $field) { |
2a66c165 | 103 | if (CRM_Core_BAO_CustomField::getKeyID($name) && |
6a488035 TO |
104 | in_array($this->_fields[$name]['html_type'], $removehtmlTypes) |
105 | ) { | |
106 | $suppressFields = TRUE; | |
107 | unset($this->_fields[$name]); | |
108 | } | |
109 | ||
7808aae6 | 110 | // Fix to reduce size as we are using this field in grid. |
bb03a208 | 111 | if (is_array($field['attributes']) && !empty($this->_fields[$name]['attributes']['size']) && $this->_fields[$name]['attributes']['size'] > 19) { |
7808aae6 | 112 | // Shrink class to "form-text-medium". |
6a488035 TO |
113 | $this->_fields[$name]['attributes']['size'] = 19; |
114 | } | |
115 | } | |
116 | ||
117 | $this->_fields = array_slice($this->_fields, 0, $this->_maxFields); | |
118 | ||
be2fb01f CW |
119 | $this->addButtons([ |
120 | [ | |
c5c263ca AH |
121 | 'type' => 'submit', |
122 | 'name' => ts('Update Activities'), | |
123 | 'isDefault' => TRUE, | |
be2fb01f CW |
124 | ], |
125 | [ | |
c5c263ca AH |
126 | 'type' => 'cancel', |
127 | 'name' => ts('Cancel'), | |
be2fb01f CW |
128 | ], |
129 | ]); | |
6a488035 | 130 | |
6a488035 TO |
131 | $this->assign('profileTitle', $this->_title); |
132 | $this->assign('componentIds', $this->_activityHolderIds); | |
6a488035 | 133 | |
7808aae6 | 134 | // Load all campaigns. |
6a488035 | 135 | if (array_key_exists('activity_campaign_id', $this->_fields)) { |
be2fb01f | 136 | $this->_componentCampaigns = []; |
6a488035 TO |
137 | CRM_Core_PseudoConstant::populate($this->_componentCampaigns, |
138 | 'CRM_Activity_DAO_Activity', | |
139 | TRUE, 'campaign_id', 'id', | |
140 | ' id IN (' . implode(' , ', array_values($this->_activityHolderIds)) . ' ) ' | |
141 | ); | |
142 | } | |
143 | ||
144 | $customFields = CRM_Core_BAO_CustomField::getFields('Activity'); | |
2a66c165 | 145 | // It is possible to have fields that are required in CiviCRM not be required in the |
146 | // profile. Overriding that here. Perhaps a better approach would be to | |
147 | // make them required in the schema & read that up through getFields functionality. | |
be2fb01f | 148 | $requiredFields = ['activity_date_time']; |
6a488035 TO |
149 | |
150 | foreach ($this->_activityHolderIds as $activityId) { | |
151 | $typeId = CRM_Core_DAO::getFieldValue("CRM_Activity_DAO_Activity", $activityId, 'activity_type_id'); | |
152 | foreach ($this->_fields as $name => $field) { | |
153 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) { | |
9c1bc317 | 154 | $customValue = $customFields[$customFieldID] ?? NULL; |
a7488080 | 155 | if (!empty($customValue['extends_entity_column_value'])) { |
6a488035 TO |
156 | $entityColumnValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, |
157 | $customValue['extends_entity_column_value'] | |
158 | ); | |
159 | } | |
a7488080 | 160 | if (!empty($entityColumnValue[$typeId]) || |
6a488035 TO |
161 | CRM_Utils_System::isNull($entityColumnValue[$typeId]) |
162 | ) { | |
163 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $activityId); | |
164 | } | |
165 | } | |
166 | else { | |
7808aae6 | 167 | // Handle non custom fields. |
2a66c165 | 168 | if (in_array($field['name'], $requiredFields)) { |
169 | $field['is_required'] = TRUE; | |
170 | } | |
6a488035 TO |
171 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $activityId); |
172 | } | |
173 | } | |
174 | } | |
175 | ||
176 | $this->assign('fields', $this->_fields); | |
177 | ||
7808aae6 | 178 | // Don't set the status message when form is submitted. |
6a488035 TO |
179 | // $buttonName = $this->controller->getButtonName('submit'); |
180 | ||
181 | if ($suppressFields) { | |
657c89d9 | 182 | 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'); |
6a488035 TO |
183 | } |
184 | ||
185 | $this->addDefaultButtons(ts('Update Activities')); | |
186 | } | |
187 | ||
188 | /** | |
c490a46a | 189 | * Set default values for the form. |
6a488035 | 190 | */ |
00be9182 | 191 | public function setDefaultValues() { |
6a488035 TO |
192 | if (empty($this->_fields)) { |
193 | return; | |
194 | } | |
195 | ||
be2fb01f | 196 | $defaults = []; |
6a488035 | 197 | foreach ($this->_activityHolderIds as $activityId) { |
6a488035 TO |
198 | CRM_Core_BAO_UFGroup::setProfileDefaults(NULL, $this->_fields, $defaults, FALSE, $activityId, 'Activity'); |
199 | } | |
200 | ||
201 | return $defaults; | |
202 | } | |
203 | ||
204 | /** | |
fe482240 | 205 | * Process the form after the input has been submitted and validated. |
6a488035 TO |
206 | */ |
207 | public function postProcess() { | |
208 | $params = $this->exportValues(); | |
209 | ||
210 | if (isset($params['field'])) { | |
211 | foreach ($params['field'] as $key => $value) { | |
212 | ||
213 | $value['custom'] = CRM_Core_BAO_CustomField::postProcess($value, | |
6a488035 TO |
214 | $key, 'Activity' |
215 | ); | |
216 | $value['id'] = $key; | |
217 | ||
a7488080 | 218 | if (!empty($value['activity_status_id'])) { |
6a488035 TO |
219 | $value['status_id'] = $value['activity_status_id']; |
220 | } | |
221 | ||
a7488080 | 222 | if (!empty($value['activity_details'])) { |
6a488035 TO |
223 | $value['details'] = $value['activity_details']; |
224 | } | |
225 | ||
a7488080 | 226 | if (!empty($value['activity_location'])) { |
6a488035 TO |
227 | $value['location'] = $value['activity_location']; |
228 | } | |
229 | ||
a7488080 | 230 | if (!empty($value['activity_subject'])) { |
6a488035 TO |
231 | $value['subject'] = $value['activity_subject']; |
232 | } | |
233 | ||
2a66c165 | 234 | $activityId = civicrm_api3('activity', 'create', $value); |
6a488035 | 235 | |
2a66c165 | 236 | // @todo this would be done by the api call above if the parames were passed through. |
3eb99314 | 237 | // @todo extract submit functions & |
238 | // extend CRM_Event_Form_Task_BatchTest::testSubmit with a data provider to test | |
239 | // handling of custom data, specifically checkbox fields. | |
a7488080 | 240 | if (!empty($value['custom']) && |
6a488035 TO |
241 | is_array($value['custom']) |
242 | ) { | |
243 | CRM_Core_BAO_CustomValueTable::store($value['custom'], 'civicrm_activity', $activityId['id']); | |
244 | } | |
245 | } | |
246 | CRM_Core_Session::setStatus("", ts("Updates Saved"), "success"); | |
247 | } | |
248 | else { | |
249 | CRM_Core_Session::setStatus("", ts("No Updates Saved"), "info"); | |
250 | } | |
251 | } | |
96025800 | 252 | |
6a488035 | 253 | } |