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 | ||
42 | /** | |
fe482240 | 43 | * Build all the data structures needed to build the form. |
6a488035 | 44 | */ |
00be9182 | 45 | public function preProcess() { |
6a488035 | 46 | |
7808aae6 | 47 | // Initialize the task and row fields. |
6a488035 TO |
48 | parent::preProcess(); |
49 | ||
7808aae6 | 50 | // Get the contact read only fields to display. |
be2fb01f | 51 | $readOnlyFields = array_merge(['sort_name' => ts('Added By'), 'target_sort_name' => ts('With Contact')], |
6a488035 TO |
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 | ||
7808aae6 | 58 | // Get the read only field data. |
6a488035 TO |
59 | $returnProperties = array_fill_keys(array_keys($readOnlyFields), 1); |
60 | $contactDetails = CRM_Contact_BAO_Contact_Utils::contactDetails($this->_activityHolderIds, | |
61 | 'Activity', $returnProperties | |
62 | ); | |
3bd48a28 | 63 | $readOnlyFields['assignee_display_name'] = ts('Assigned to'); |
22e263ad | 64 | if (!empty($contactDetails)) { |
9b873358 | 65 | foreach ($contactDetails as $key => $value) { |
bb03a208 | 66 | $assignee = CRM_Activity_BAO_ActivityAssignment::retrieveAssigneeIdsByActivityId($key); |
115d6fc1 | 67 | $assigneeContact = []; |
363544d7 | 68 | foreach ($assignee as $values) { |
69 | $assigneeContact[] = CRM_Contact_BAO_Contact::displayName($values); | |
bb03a208 RK |
70 | } |
71 | $contactDetails[$key]['assignee_display_name'] = !empty($assigneeContact) ? implode(';', $assigneeContact) : NULL; | |
72 | } | |
73 | } | |
6a488035 TO |
74 | $this->assign('contactDetails', $contactDetails); |
75 | $this->assign('readOnlyFields', $readOnlyFields); | |
76 | } | |
77 | ||
78 | /** | |
2e2605fe | 79 | * Build the form object. |
6a488035 | 80 | */ |
00be9182 | 81 | public function buildQuickForm() { |
6a488035 TO |
82 | $ufGroupId = $this->get('ufGroupId'); |
83 | ||
84 | if (!$ufGroupId) { | |
2a66c165 | 85 | throw new CRM_Core_Exception('The profile id is missing'); |
6a488035 | 86 | } |
b581842f | 87 | $this->_title = ts('Update multiple activities') . ' - ' . CRM_Core_BAO_UFGroup::getTitle($ufGroupId); |
6a488035 TO |
88 | CRM_Utils_System::setTitle($this->_title); |
89 | ||
90 | $this->addDefaultButtons(ts('Save')); | |
be2fb01f | 91 | $this->_fields = []; |
6a488035 TO |
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; | |
be2fb01f | 96 | $removehtmlTypes = ['File']; |
6a488035 | 97 | foreach ($this->_fields as $name => $field) { |
2a66c165 | 98 | if (CRM_Core_BAO_CustomField::getKeyID($name) && |
6a488035 TO |
99 | in_array($this->_fields[$name]['html_type'], $removehtmlTypes) |
100 | ) { | |
101 | $suppressFields = TRUE; | |
102 | unset($this->_fields[$name]); | |
103 | } | |
104 | ||
7808aae6 | 105 | // Fix to reduce size as we are using this field in grid. |
bb03a208 | 106 | if (is_array($field['attributes']) && !empty($this->_fields[$name]['attributes']['size']) && $this->_fields[$name]['attributes']['size'] > 19) { |
7808aae6 | 107 | // Shrink class to "form-text-medium". |
6a488035 TO |
108 | $this->_fields[$name]['attributes']['size'] = 19; |
109 | } | |
110 | } | |
111 | ||
112 | $this->_fields = array_slice($this->_fields, 0, $this->_maxFields); | |
113 | ||
be2fb01f CW |
114 | $this->addButtons([ |
115 | [ | |
c5c263ca AH |
116 | 'type' => 'submit', |
117 | 'name' => ts('Update Activities'), | |
118 | 'isDefault' => TRUE, | |
be2fb01f CW |
119 | ], |
120 | [ | |
c5c263ca AH |
121 | 'type' => 'cancel', |
122 | 'name' => ts('Cancel'), | |
be2fb01f CW |
123 | ], |
124 | ]); | |
6a488035 | 125 | |
6a488035 TO |
126 | $this->assign('profileTitle', $this->_title); |
127 | $this->assign('componentIds', $this->_activityHolderIds); | |
6a488035 | 128 | |
7808aae6 | 129 | // Load all campaigns. |
6a488035 | 130 | if (array_key_exists('activity_campaign_id', $this->_fields)) { |
be2fb01f | 131 | $this->_componentCampaigns = []; |
6a488035 TO |
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'); | |
2a66c165 | 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. | |
be2fb01f | 143 | $requiredFields = ['activity_date_time']; |
6a488035 TO |
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); | |
a7488080 | 150 | if (!empty($customValue['extends_entity_column_value'])) { |
6a488035 TO |
151 | $entityColumnValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, |
152 | $customValue['extends_entity_column_value'] | |
153 | ); | |
154 | } | |
a7488080 | 155 | if (!empty($entityColumnValue[$typeId]) || |
6a488035 TO |
156 | CRM_Utils_System::isNull($entityColumnValue[$typeId]) |
157 | ) { | |
158 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $activityId); | |
159 | } | |
160 | } | |
161 | else { | |
7808aae6 | 162 | // Handle non custom fields. |
2a66c165 | 163 | if (in_array($field['name'], $requiredFields)) { |
164 | $field['is_required'] = TRUE; | |
165 | } | |
6a488035 TO |
166 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $activityId); |
167 | } | |
168 | } | |
169 | } | |
170 | ||
171 | $this->assign('fields', $this->_fields); | |
172 | ||
7808aae6 | 173 | // Don't set the status message when form is submitted. |
6a488035 TO |
174 | // $buttonName = $this->controller->getButtonName('submit'); |
175 | ||
176 | if ($suppressFields) { | |
657c89d9 | 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'); |
6a488035 TO |
178 | } |
179 | ||
180 | $this->addDefaultButtons(ts('Update Activities')); | |
181 | } | |
182 | ||
183 | /** | |
c490a46a | 184 | * Set default values for the form. |
6a488035 | 185 | */ |
00be9182 | 186 | public function setDefaultValues() { |
6a488035 TO |
187 | if (empty($this->_fields)) { |
188 | return; | |
189 | } | |
190 | ||
be2fb01f | 191 | $defaults = []; |
6a488035 | 192 | foreach ($this->_activityHolderIds as $activityId) { |
6a488035 TO |
193 | CRM_Core_BAO_UFGroup::setProfileDefaults(NULL, $this->_fields, $defaults, FALSE, $activityId, 'Activity'); |
194 | } | |
195 | ||
196 | return $defaults; | |
197 | } | |
198 | ||
199 | /** | |
fe482240 | 200 | * Process the form after the input has been submitted and validated. |
6a488035 TO |
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, | |
6a488035 TO |
209 | $key, 'Activity' |
210 | ); | |
211 | $value['id'] = $key; | |
212 | ||
a7488080 | 213 | if (!empty($value['activity_status_id'])) { |
6a488035 TO |
214 | $value['status_id'] = $value['activity_status_id']; |
215 | } | |
216 | ||
a7488080 | 217 | if (!empty($value['activity_details'])) { |
6a488035 TO |
218 | $value['details'] = $value['activity_details']; |
219 | } | |
220 | ||
a7488080 | 221 | if (!empty($value['activity_location'])) { |
6a488035 TO |
222 | $value['location'] = $value['activity_location']; |
223 | } | |
224 | ||
a7488080 | 225 | if (!empty($value['activity_subject'])) { |
6a488035 TO |
226 | $value['subject'] = $value['activity_subject']; |
227 | } | |
228 | ||
2a66c165 | 229 | $activityId = civicrm_api3('activity', 'create', $value); |
6a488035 | 230 | |
2a66c165 | 231 | // @todo this would be done by the api call above if the parames were passed through. |
3eb99314 | 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. | |
a7488080 | 235 | if (!empty($value['custom']) && |
6a488035 TO |
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 | } | |
96025800 | 247 | |
6a488035 | 248 | } |