Merge pull request #17719 from civicrm/5.27
[civicrm-core.git] / CRM / Member / 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 members
20 */
21 class CRM_Member_Form_Task_Batch extends CRM_Member_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 * @return void
46 */
47 public function preProcess() {
48 // initialize the task and row fields
49 parent::preProcess();
50
51 //get the contact read only fields to display.
52 $readOnlyFields = array_merge(['sort_name' => ts('Name')],
53 CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
54 'contact_autocomplete_options',
55 TRUE, NULL, FALSE, 'name', TRUE
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->_memberIds,
61 'CiviMember', $returnProperties
62 );
63 $this->assign('contactDetails', $contactDetails);
64 $this->assign('readOnlyFields', $readOnlyFields);
65 }
66
67 /**
68 * Build the form object.
69 *
70 *
71 * @return void
72 */
73 public function buildQuickForm() {
74 $ufGroupId = $this->get('ufGroupId');
75
76 if (!$ufGroupId) {
77 CRM_Core_Error::statusBounce('ufGroupId is missing');
78 }
79 $this->_title = ts('Update multiple memberships') . ' - ' . CRM_Core_BAO_UFGroup::getTitle($ufGroupId);
80 CRM_Utils_System::setTitle($this->_title);
81
82 $this->addDefaultButtons(ts('Save'));
83 $this->_fields = [];
84 $this->_fields = CRM_Core_BAO_UFGroup::getFields($ufGroupId, FALSE, CRM_Core_Action::VIEW);
85
86 // remove file type field and then limit fields
87 $suppressFields = FALSE;
88 $removehtmlTypes = ['File'];
89 foreach ($this->_fields as $name => $field) {
90 if ($cfID = CRM_Core_BAO_CustomField::getKeyID($name) &&
91 in_array($this->_fields[$name]['html_type'], $removehtmlTypes)
92 ) {
93 $suppressFields = TRUE;
94 unset($this->_fields[$name]);
95 }
96
97 //fix to reduce size as we are using this field in grid
98 if (is_array($field['attributes']) && !empty($this->_fields[$name]['attributes']['size']) && $this->_fields[$name]['attributes']['size'] > 19) {
99 //shrink class to "form-text-medium"
100 $this->_fields[$name]['attributes']['size'] = 19;
101 }
102 }
103
104 $this->_fields = array_slice($this->_fields, 0, $this->_maxFields);
105
106 $this->addButtons([
107 [
108 'type' => 'submit',
109 'name' => ts('Update Members(s)'),
110 'isDefault' => TRUE,
111 ],
112 [
113 'type' => 'cancel',
114 'name' => ts('Cancel'),
115 ],
116 ]);
117
118 $this->assign('profileTitle', $this->_title);
119 $this->assign('componentIds', $this->_memberIds);
120
121 //load all campaigns.
122 if (array_key_exists('member_campaign_id', $this->_fields)) {
123 $this->_componentCampaigns = [];
124 CRM_Core_PseudoConstant::populate($this->_componentCampaigns,
125 'CRM_Member_DAO_Membership',
126 TRUE, 'campaign_id', 'id',
127 ' id IN (' . implode(' , ', array_values($this->_memberIds)) . ' ) '
128 );
129 }
130
131 $customFields = CRM_Core_BAO_CustomField::getFields('Membership');
132 foreach ($this->_memberIds as $memberId) {
133 $typeId = CRM_Core_DAO::getFieldValue("CRM_Member_DAO_Membership", $memberId, 'membership_type_id');
134 foreach ($this->_fields as $name => $field) {
135 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) {
136 $customValue = $customFields[$customFieldID] ?? NULL;
137 $entityColumnValue = [];
138 if (!empty($customValue['extends_entity_column_value'])) {
139 $entityColumnValue = explode(CRM_Core_DAO::VALUE_SEPARATOR,
140 $customValue['extends_entity_column_value']
141 );
142 }
143 if ((CRM_Utils_Array::value($typeId, $entityColumnValue)) ||
144 CRM_Utils_System::isNull($entityColumnValue[$typeId])
145 ) {
146 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $memberId);
147 }
148 }
149 else {
150 // handle non custom fields
151 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $memberId);
152 }
153 }
154 }
155
156 $this->assign('fields', $this->_fields);
157
158 // don't set the status message when form is submitted.
159 $buttonName = $this->controller->getButtonName('submit');
160
161 if ($suppressFields && $buttonName != '_qf_Batch_next') {
162 CRM_Core_Session::setStatus(ts("File type field(s) in the selected profile are not supported for Update multiple memberships."), ts('Unsupported Field Type'), 'error');
163 }
164
165 $this->addDefaultButtons(ts('Update Memberships'));
166 }
167
168 /**
169 * Set default values for the form.
170 *
171 *
172 * @return void
173 */
174 public function setDefaultValues() {
175 if (empty($this->_fields)) {
176 return;
177 }
178
179 $defaults = [];
180 foreach ($this->_memberIds as $memberId) {
181 CRM_Core_BAO_UFGroup::setProfileDefaults(NULL, $this->_fields, $defaults, FALSE, $memberId, 'Membership');
182 }
183
184 return $defaults;
185 }
186
187 /**
188 * Process the form after the input has been submitted and validated.
189 *
190 * @throws \CRM_Core_Exception
191 * @throws \CiviCRM_API3_Exception
192 */
193 public function postProcess() {
194 $params = $this->exportValues();
195 if (isset($params['field'])) {
196 $this->submit($params);
197 CRM_Core_Session::setStatus(ts('Your updates have been saved.'), ts('Saved'), 'success');
198 }
199 else {
200 CRM_Core_Session::setStatus(ts('No updates have been saved.'), ts('Not Saved'), 'alert');
201 }
202 }
203
204 /**
205 * @param array $params
206 *
207 * @return mixed
208 * @throws \CRM_Core_Exception
209 * @throws \CiviCRM_API3_Exception
210 */
211 public function submit(array $params) {
212 $dates = [
213 'membership_join_date',
214 'membership_start_date',
215 'membership_end_date',
216 ];
217 $customFields = [];
218 foreach ($params['field'] as $key => $value) {
219 $value['id'] = $key;
220 if (!empty($value['membership_source'])) {
221 $value['source'] = $value['membership_source'];
222 }
223
224 if (!empty($value['membership_type'])) {
225 $membershipTypeId = $value['membership_type_id'] = $value['membership_type'][1];
226 }
227
228 unset($value['membership_source']);
229 unset($value['membership_type']);
230
231 //Get the membership status
232 $value['status_id'] = (CRM_Utils_Array::value('membership_status', $value)) ? $value['membership_status'] : CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $key, 'status_id');
233 unset($value['membership_status']);
234 foreach ($dates as $val) {
235 if (isset($value[$val])) {
236 $value[$val] = CRM_Utils_Date::processDate($value[$val]);
237 }
238 }
239 if (empty($customFields)) {
240 if (empty($value['membership_type_id'])) {
241 $membershipTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $key, 'membership_type_id');
242 }
243
244 // membership type custom data
245 $customFields = CRM_Core_BAO_CustomField::getFields('Membership', FALSE, FALSE, $membershipTypeId);
246
247 $customFields = CRM_Utils_Array::crmArrayMerge($customFields,
248 CRM_Core_BAO_CustomField::getFields('Membership',
249 FALSE, FALSE, NULL, NULL, TRUE
250 )
251 );
252 }
253 //check for custom data
254 $value['custom'] = CRM_Core_BAO_CustomField::postProcess($params['field'][$key],
255 $key,
256 'Membership',
257 $membershipTypeId
258 );
259
260 $membership = CRM_Member_BAO_Membership::add($value);
261
262 // add custom field values
263 if (!empty($value['custom']) &&
264 is_array($value['custom'])
265 ) {
266 CRM_Core_BAO_CustomValueTable::store($value['custom'], 'civicrm_membership', $membership->id);
267 }
268 }
269 return $value;
270 }
271
272 }