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 members | |
20 | */ | |
21 | class CRM_Member_Form_Task_Batch extends CRM_Member_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. |
971e129b | 32 | * @var int |
6a488035 TO |
33 | */ |
34 | protected $_maxFields = 9; | |
35 | ||
36 | /** | |
fe482240 | 37 | * Variable to store redirect path. |
971e129b | 38 | * @var string |
6a488035 TO |
39 | */ |
40 | protected $_userContext; | |
41 | ||
42 | /** | |
fe482240 | 43 | * Build all the data structures needed to build the form. |
6a488035 TO |
44 | * |
45 | * @return void | |
03e04002 | 46 | */ |
00be9182 | 47 | public function preProcess() { |
d424ffde | 48 | // initialize the task and row fields |
6a488035 TO |
49 | parent::preProcess(); |
50 | ||
51 | //get the contact read only fields to display. | |
be2fb01f | 52 | $readOnlyFields = array_merge(['sort_name' => ts('Name')], |
6a488035 TO |
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 | /** | |
fe482240 | 68 | * Build the form object. |
6a488035 | 69 | * |
6a488035 TO |
70 | * |
71 | * @return void | |
72 | */ | |
00be9182 | 73 | public function buildQuickForm() { |
6a488035 TO |
74 | $ufGroupId = $this->get('ufGroupId'); |
75 | ||
76 | if (!$ufGroupId) { | |
507bc932 | 77 | CRM_Core_Error::statusBounce(ts('ufGroupId is missing')); |
6a488035 | 78 | } |
b581842f | 79 | $this->_title = ts('Update multiple memberships') . ' - ' . CRM_Core_BAO_UFGroup::getTitle($ufGroupId); |
203b4734 | 80 | $this->setTitle($this->_title); |
6a488035 TO |
81 | |
82 | $this->addDefaultButtons(ts('Save')); | |
be2fb01f | 83 | $this->_fields = []; |
6a488035 TO |
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; | |
be2fb01f | 88 | $removehtmlTypes = ['File']; |
6a488035 TO |
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 | |
b581842f | 98 | if (is_array($field['attributes']) && !empty($this->_fields[$name]['attributes']['size']) && $this->_fields[$name]['attributes']['size'] > 19) { |
6a488035 TO |
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 | ||
be2fb01f CW |
106 | $this->addButtons([ |
107 | [ | |
c5c263ca AH |
108 | 'type' => 'submit', |
109 | 'name' => ts('Update Members(s)'), | |
110 | 'isDefault' => TRUE, | |
be2fb01f CW |
111 | ], |
112 | [ | |
c5c263ca AH |
113 | 'type' => 'cancel', |
114 | 'name' => ts('Cancel'), | |
be2fb01f CW |
115 | ], |
116 | ]); | |
6a488035 | 117 | |
6a488035 TO |
118 | $this->assign('profileTitle', $this->_title); |
119 | $this->assign('componentIds', $this->_memberIds); | |
6a488035 TO |
120 | |
121 | //load all campaigns. | |
122 | if (array_key_exists('member_campaign_id', $this->_fields)) { | |
be2fb01f | 123 | $this->_componentCampaigns = []; |
6a488035 TO |
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)) { | |
9c1bc317 | 136 | $customValue = $customFields[$customFieldID] ?? NULL; |
be2fb01f | 137 | $entityColumnValue = []; |
a7488080 | 138 | if (!empty($customValue['extends_entity_column_value'])) { |
6a488035 TO |
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') { | |
657c89d9 | 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'); |
6a488035 TO |
163 | } |
164 | ||
165 | $this->addDefaultButtons(ts('Update Memberships')); | |
166 | } | |
167 | ||
168 | /** | |
c490a46a | 169 | * Set default values for the form. |
6a488035 | 170 | * |
6a488035 | 171 | * |
355ba699 | 172 | * @return void |
6a488035 | 173 | */ |
00be9182 | 174 | public function setDefaultValues() { |
6a488035 TO |
175 | if (empty($this->_fields)) { |
176 | return; | |
177 | } | |
178 | ||
be2fb01f | 179 | $defaults = []; |
6a488035 | 180 | foreach ($this->_memberIds as $memberId) { |
6a488035 TO |
181 | CRM_Core_BAO_UFGroup::setProfileDefaults(NULL, $this->_fields, $defaults, FALSE, $memberId, 'Membership'); |
182 | } | |
183 | ||
184 | return $defaults; | |
185 | } | |
186 | ||
187 | /** | |
fe482240 | 188 | * Process the form after the input has been submitted and validated. |
6a488035 | 189 | * |
1813ed91 | 190 | * @throws \CRM_Core_Exception |
191 | * @throws \CiviCRM_API3_Exception | |
6a488035 TO |
192 | */ |
193 | public function postProcess() { | |
194 | $params = $this->exportValues(); | |
1813ed91 | 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) { | |
be2fb01f | 212 | $dates = [ |
b2c9a0e3 | 213 | 'membership_join_date', |
6a488035 TO |
214 | 'membership_start_date', |
215 | 'membership_end_date', | |
be2fb01f | 216 | ]; |
1813ed91 | 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 | } | |
6a488035 | 223 | |
1813ed91 | 224 | if (!empty($value['membership_type'])) { |
225 | $membershipTypeId = $value['membership_type_id'] = $value['membership_type'][1]; | |
226 | } | |
6a488035 | 227 | |
1813ed91 | 228 | unset($value['membership_source']); |
229 | unset($value['membership_type']); | |
6a488035 | 230 | |
1813ed91 | 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'); | |
6a488035 | 242 | } |
6a488035 | 243 | |
1813ed91 | 244 | // membership type custom data |
245 | $customFields = CRM_Core_BAO_CustomField::getFields('Membership', FALSE, FALSE, $membershipTypeId); | |
6a488035 | 246 | |
1813ed91 | 247 | $customFields = CRM_Utils_Array::crmArrayMerge($customFields, |
248 | CRM_Core_BAO_CustomField::getFields('Membership', | |
249 | FALSE, FALSE, NULL, NULL, TRUE | |
250 | ) | |
6a488035 | 251 | ); |
1813ed91 | 252 | } |
253 | //check for custom data | |
254 | $value['custom'] = CRM_Core_BAO_CustomField::postProcess($params['field'][$key], | |
255 | $key, | |
256 | 'Membership', | |
257 | $membershipTypeId | |
258 | ); | |
6a488035 | 259 | |
1813ed91 | 260 | $membership = CRM_Member_BAO_Membership::add($value); |
6a488035 | 261 | |
1813ed91 | 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); | |
6a488035 | 267 | } |
6a488035 | 268 | } |
1813ed91 | 269 | return $value; |
6a488035 | 270 | } |
96025800 | 271 | |
6a488035 | 272 | } |