Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
35 | * This class provides the functionality for batch profile update for members | |
36 | */ | |
37 | class CRM_Member_Form_Task_Batch extends CRM_Member_Form_Task { | |
38 | ||
39 | /** | |
fe482240 | 40 | * The title of the group. |
6a488035 TO |
41 | * |
42 | * @var string | |
43 | */ | |
44 | protected $_title; | |
45 | ||
46 | /** | |
fe482240 | 47 | * Maximum profile fields that will be displayed. |
6a488035 TO |
48 | */ |
49 | protected $_maxFields = 9; | |
50 | ||
51 | /** | |
fe482240 | 52 | * Variable to store redirect path. |
6a488035 TO |
53 | */ |
54 | protected $_userContext; | |
55 | ||
56 | /** | |
fe482240 | 57 | * Build all the data structures needed to build the form. |
6a488035 TO |
58 | * |
59 | * @return void | |
03e04002 | 60 | */ |
00be9182 | 61 | public function preProcess() { |
d424ffde | 62 | // initialize the task and row fields |
6a488035 TO |
63 | parent::preProcess(); |
64 | ||
65 | //get the contact read only fields to display. | |
66 | $readOnlyFields = array_merge(array('sort_name' => ts('Name')), | |
67 | CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
68 | 'contact_autocomplete_options', | |
69 | TRUE, NULL, FALSE, 'name', TRUE | |
70 | ) | |
71 | ); | |
72 | //get the read only field data. | |
73 | $returnProperties = array_fill_keys(array_keys($readOnlyFields), 1); | |
74 | $contactDetails = CRM_Contact_BAO_Contact_Utils::contactDetails($this->_memberIds, | |
75 | 'CiviMember', $returnProperties | |
76 | ); | |
77 | $this->assign('contactDetails', $contactDetails); | |
78 | $this->assign('readOnlyFields', $readOnlyFields); | |
79 | } | |
80 | ||
81 | /** | |
fe482240 | 82 | * Build the form object. |
6a488035 | 83 | * |
6a488035 TO |
84 | * |
85 | * @return void | |
86 | */ | |
00be9182 | 87 | public function buildQuickForm() { |
6a488035 TO |
88 | $ufGroupId = $this->get('ufGroupId'); |
89 | ||
90 | if (!$ufGroupId) { | |
91 | CRM_Core_Error::fatal('ufGroupId is missing'); | |
92 | } | |
b581842f | 93 | $this->_title = ts('Update multiple memberships') . ' - ' . CRM_Core_BAO_UFGroup::getTitle($ufGroupId); |
6a488035 TO |
94 | CRM_Utils_System::setTitle($this->_title); |
95 | ||
96 | $this->addDefaultButtons(ts('Save')); | |
97 | $this->_fields = array(); | |
98 | $this->_fields = CRM_Core_BAO_UFGroup::getFields($ufGroupId, FALSE, CRM_Core_Action::VIEW); | |
99 | ||
100 | // remove file type field and then limit fields | |
101 | $suppressFields = FALSE; | |
657c89d9 | 102 | $removehtmlTypes = array('File'); |
6a488035 TO |
103 | foreach ($this->_fields as $name => $field) { |
104 | if ($cfID = CRM_Core_BAO_CustomField::getKeyID($name) && | |
105 | in_array($this->_fields[$name]['html_type'], $removehtmlTypes) | |
106 | ) { | |
107 | $suppressFields = TRUE; | |
108 | unset($this->_fields[$name]); | |
109 | } | |
110 | ||
111 | //fix to reduce size as we are using this field in grid | |
b581842f | 112 | if (is_array($field['attributes']) && !empty($this->_fields[$name]['attributes']['size']) && $this->_fields[$name]['attributes']['size'] > 19) { |
6a488035 TO |
113 | //shrink class to "form-text-medium" |
114 | $this->_fields[$name]['attributes']['size'] = 19; | |
115 | } | |
116 | } | |
117 | ||
118 | $this->_fields = array_slice($this->_fields, 0, $this->_maxFields); | |
119 | ||
120 | $this->addButtons(array( | |
c5c263ca AH |
121 | array( |
122 | 'type' => 'submit', | |
123 | 'name' => ts('Update Members(s)'), | |
124 | 'isDefault' => TRUE, | |
125 | ), | |
126 | array( | |
127 | 'type' => 'cancel', | |
128 | 'name' => ts('Cancel'), | |
129 | ), | |
130 | )); | |
6a488035 | 131 | |
6a488035 TO |
132 | $this->assign('profileTitle', $this->_title); |
133 | $this->assign('componentIds', $this->_memberIds); | |
6a488035 TO |
134 | |
135 | //load all campaigns. | |
136 | if (array_key_exists('member_campaign_id', $this->_fields)) { | |
137 | $this->_componentCampaigns = array(); | |
138 | CRM_Core_PseudoConstant::populate($this->_componentCampaigns, | |
139 | 'CRM_Member_DAO_Membership', | |
140 | TRUE, 'campaign_id', 'id', | |
141 | ' id IN (' . implode(' , ', array_values($this->_memberIds)) . ' ) ' | |
142 | ); | |
143 | } | |
144 | ||
145 | $customFields = CRM_Core_BAO_CustomField::getFields('Membership'); | |
146 | foreach ($this->_memberIds as $memberId) { | |
147 | $typeId = CRM_Core_DAO::getFieldValue("CRM_Member_DAO_Membership", $memberId, 'membership_type_id'); | |
148 | foreach ($this->_fields as $name => $field) { | |
149 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) { | |
150 | $customValue = CRM_Utils_Array::value($customFieldID, $customFields); | |
1bef172e | 151 | $entityColumnValue = array(); |
a7488080 | 152 | if (!empty($customValue['extends_entity_column_value'])) { |
6a488035 TO |
153 | $entityColumnValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, |
154 | $customValue['extends_entity_column_value'] | |
155 | ); | |
156 | } | |
157 | if ((CRM_Utils_Array::value($typeId, $entityColumnValue)) || | |
158 | CRM_Utils_System::isNull($entityColumnValue[$typeId]) | |
159 | ) { | |
160 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $memberId); | |
161 | } | |
162 | } | |
163 | else { | |
164 | // handle non custom fields | |
165 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $memberId); | |
166 | } | |
167 | } | |
168 | } | |
169 | ||
170 | $this->assign('fields', $this->_fields); | |
171 | ||
172 | // don't set the status message when form is submitted. | |
173 | $buttonName = $this->controller->getButtonName('submit'); | |
174 | ||
175 | if ($suppressFields && $buttonName != '_qf_Batch_next') { | |
657c89d9 | 176 | 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 |
177 | } |
178 | ||
179 | $this->addDefaultButtons(ts('Update Memberships')); | |
180 | } | |
181 | ||
182 | /** | |
c490a46a | 183 | * Set default values for the form. |
6a488035 | 184 | * |
6a488035 | 185 | * |
355ba699 | 186 | * @return void |
6a488035 | 187 | */ |
00be9182 | 188 | public function setDefaultValues() { |
6a488035 TO |
189 | if (empty($this->_fields)) { |
190 | return; | |
191 | } | |
192 | ||
193 | $defaults = array(); | |
194 | foreach ($this->_memberIds as $memberId) { | |
6a488035 TO |
195 | CRM_Core_BAO_UFGroup::setProfileDefaults(NULL, $this->_fields, $defaults, FALSE, $memberId, 'Membership'); |
196 | } | |
197 | ||
198 | return $defaults; | |
199 | } | |
200 | ||
201 | /** | |
fe482240 | 202 | * Process the form after the input has been submitted and validated. |
6a488035 | 203 | * |
6a488035 | 204 | * |
355ba699 | 205 | * @return void |
6a488035 TO |
206 | */ |
207 | public function postProcess() { | |
208 | $params = $this->exportValues(); | |
3eb99314 | 209 | // @todo extract submit functions & |
210 | // extend CRM_Event_Form_Task_BatchTest::testSubmit with a data provider to test | |
211 | // handling of custom data, specifically checkbox fields. | |
6a488035 TO |
212 | $dates = array( |
213 | 'join_date', | |
214 | 'membership_start_date', | |
215 | 'membership_end_date', | |
353ffa53 | 216 | ); |
6a488035 TO |
217 | if (isset($params['field'])) { |
218 | $customFields = array(); | |
219 | foreach ($params['field'] as $key => $value) { | |
220 | $ids['membership'] = $key; | |
a7488080 | 221 | if (!empty($value['membership_source'])) { |
6a488035 TO |
222 | $value['source'] = $value['membership_source']; |
223 | } | |
224 | ||
a7488080 | 225 | if (!empty($value['membership_type'])) { |
6a488035 TO |
226 | $membershipTypeId = $value['membership_type_id'] = $value['membership_type'][1]; |
227 | } | |
228 | ||
229 | unset($value['membership_source']); | |
230 | unset($value['membership_type']); | |
231 | ||
232 | //Get the membership status | |
233 | $value['status_id'] = (CRM_Utils_Array::value('membership_status', $value)) ? $value['membership_status'] : CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $key, 'status_id'); | |
234 | unset($value['membership_status']); | |
235 | foreach ($dates as $val) { | |
236 | if (isset($value[$val])) { | |
237 | $value[$val] = CRM_Utils_Date::processDate($value[$val]); | |
238 | } | |
239 | } | |
240 | if (empty($customFields)) { | |
a7488080 | 241 | if (empty($value['membership_type_id'])) { |
6a488035 TO |
242 | $membershipTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $key, 'membership_type_id'); |
243 | } | |
244 | ||
245 | // membership type custom data | |
246 | $customFields = CRM_Core_BAO_CustomField::getFields('Membership', FALSE, FALSE, $membershipTypeId); | |
247 | ||
248 | $customFields = CRM_Utils_Array::crmArrayMerge($customFields, | |
249 | CRM_Core_BAO_CustomField::getFields('Membership', | |
250 | FALSE, FALSE, NULL, NULL, TRUE | |
251 | ) | |
252 | ); | |
253 | } | |
254 | //check for custom data | |
255 | $value['custom'] = CRM_Core_BAO_CustomField::postProcess($params['field'][$key], | |
6a488035 TO |
256 | $key, |
257 | 'Membership', | |
258 | $membershipTypeId | |
259 | ); | |
260 | ||
261 | $membership = CRM_Member_BAO_Membership::add($value, $ids); | |
262 | ||
263 | // add custom field values | |
a7488080 | 264 | if (!empty($value['custom']) && |
6a488035 TO |
265 | is_array($value['custom']) |
266 | ) { | |
267 | CRM_Core_BAO_CustomValueTable::store($value['custom'], 'civicrm_membership', $membership->id); | |
268 | } | |
269 | } | |
270 | ||
271 | CRM_Core_Session::setStatus(ts("Your updates have been saved."), ts('Saved'), 'success'); | |
272 | } | |
273 | else { | |
274 | CRM_Core_Session::setStatus(ts("No updates have been saved."), ts('Not Saved'), 'alert'); | |
275 | } | |
276 | } | |
96025800 | 277 | |
6a488035 | 278 | } |