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 | /** | |
36 | * This class generates form components for Membership Type | |
37 | * | |
38 | */ | |
fb3082b2 | 39 | class CRM_Member_Form_MembershipType extends CRM_Member_Form_MembershipConfig { |
6a488035 | 40 | |
763f7a8a | 41 | |
42 | /** | |
43 | * Explicitly declare the entity api name. | |
44 | */ | |
45 | public function getDefaultEntity() { | |
46 | return 'MembershipType'; | |
47 | } | |
48 | ||
49 | /** | |
50 | * Explicitly declare the form context. | |
51 | */ | |
52 | public function getDefaultContext() { | |
53 | return 'create'; | |
54 | } | |
55 | ||
6a488035 | 56 | /** |
100fef9d | 57 | * Max number of contacts we will display for membership-organisation |
6a488035 | 58 | */ |
7da04cde | 59 | const MAX_CONTACTS = 50; |
6a488035 | 60 | |
00be9182 | 61 | public function preProcess() { |
481a74f4 | 62 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, 0); |
6a488035 TO |
63 | $this->_BAOName = 'CRM_Member_BAO_MembershipType'; |
64 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add'); | |
65 | $this->assign('action', $this->_action); | |
66 | ||
67 | $session = CRM_Core_Session::singleton(); | |
68 | $url = CRM_Utils_System::url('civicrm/admin/member/membershipType', 'reset=1'); | |
69 | $session->pushUserContext($url); | |
e2046b33 CW |
70 | |
71 | $this->setPageTitle(ts('Membership Type')); | |
6a488035 TO |
72 | } |
73 | ||
74 | /** | |
c490a46a | 75 | * Set default values for the form. MobileProvider that in edit/view mode |
6a488035 TO |
76 | * the default values are retrieved from the database |
77 | * | |
1322dc53 MW |
78 | * @return array |
79 | * defaults | |
6a488035 TO |
80 | */ |
81 | public function setDefaultValues() { | |
82 | $defaults = parent::setDefaultValues(); | |
83 | ||
6a488035 TO |
84 | //finding default weight to be put |
85 | if (!isset($defaults['weight']) || (!$defaults['weight'])) { | |
86 | $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Member_DAO_MembershipType'); | |
87 | } | |
88 | //setting default relationshipType | |
89 | if (isset($defaults['relationship_type_id'])) { | |
90 | //$defaults['relationship_type_id'] = $defaults['relationship_type_id'].'_a_b'; | |
91 | // Set values for relation type select box | |
92 | $relTypeIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaults['relationship_type_id']); | |
93 | $relDirections = explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaults['relationship_direction']); | |
94 | $defaults['relationship_type_id'] = array(); | |
95 | foreach ($relTypeIds as $key => $value) { | |
96 | $defaults['relationship_type_id'][] = $value . '_' . $relDirections[$key]; | |
97 | } | |
98 | } | |
99 | ||
6a488035 TO |
100 | //setting default fixed_period_start_day & fixed_period_rollover_day |
101 | $periods = array('fixed_period_start_day', 'fixed_period_rollover_day'); | |
102 | foreach ($periods as $per) { | |
103 | if (isset($defaults[$per])) { | |
353ffa53 | 104 | $date = $defaults[$per]; |
6a488035 TO |
105 | |
106 | $defaults[$per] = array(); | |
107 | if ($date > 31) { | |
353ffa53 | 108 | $date = ($date < 999) ? '0' . $date : $date; |
6a488035 TO |
109 | $defaults[$per]['M'] = substr($date, 0, 2); |
110 | $defaults[$per]['d'] = substr($date, 2, 3); | |
111 | } | |
112 | else { | |
113 | //special case when only day is rollover and duration is month | |
114 | $defaults['month_fixed_period_rollover_day']['d'] = $date; | |
115 | } | |
116 | } | |
117 | } | |
118 | ||
119 | return $defaults; | |
120 | } | |
121 | ||
122 | /** | |
fe482240 | 123 | * Build the form object. |
6a488035 | 124 | * |
355ba699 | 125 | * @return void |
a1ebf4b9 MW |
126 | * @throws \CRM_Core_Exception |
127 | * @throws \CiviCRM_API3_Exception | |
6a488035 TO |
128 | */ |
129 | public function buildQuickForm() { | |
130 | parent::buildQuickForm(); | |
131 | ||
132 | if ($this->_action & CRM_Core_Action::DELETE) { | |
133 | return; | |
134 | } | |
135 | ||
136 | $this->applyFilter('__ALL__', 'trim'); | |
80a96508 | 137 | $this->addField('name', [], TRUE); |
138 | $this->addField('description'); | |
139 | $this->addField('minimum_fee'); | |
140 | $this->addField('duration_unit', [], TRUE); | |
141 | $this->addField('period_type', [], TRUE); | |
142 | $this->addField('is_active'); | |
143 | $this->addField('weight'); | |
144 | $this->addField('max_related'); | |
6a488035 TO |
145 | |
146 | $this->addRule('name', ts('A membership type with this name already exists. Please select another name.'), | |
147 | 'objectExists', array('CRM_Member_DAO_MembershipType', $this->_id) | |
148 | ); | |
6a488035 TO |
149 | $this->addRule('minimum_fee', ts('Please enter a monetary value for the Minimum Fee.'), 'money'); |
150 | ||
6a488035 TO |
151 | $this->add('text', 'duration_interval', ts('Duration Interval'), |
152 | CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipType', 'duration_interval') | |
153 | ); | |
154 | ||
fc791e90 CW |
155 | $props = array('api' => array('params' => array('contact_type' => 'Organization'))); |
156 | $this->addEntityRef('member_of_contact_id', ts('Membership Organization'), $props, TRUE); | |
6a488035 TO |
157 | |
158 | //start day | |
159 | $this->add('date', 'fixed_period_start_day', ts('Fixed Period Start Day'), | |
160 | CRM_Core_SelectValues::date(NULL, 'M d'), FALSE | |
161 | ); | |
162 | ||
54afd8a6 | 163 | // Add Auto-renew options if we have a payment processor that supports recurring contributions |
353ffa53 TO |
164 | $isAuthorize = FALSE; |
165 | $options = array(); | |
54afd8a6 | 166 | if (CRM_Financial_BAO_PaymentProcessor::hasPaymentProcessorSupporting(array('Recurring'))) { |
6a488035 | 167 | $isAuthorize = TRUE; |
dbd82592 | 168 | $options = CRM_Core_SelectValues::memberAutoRenew(); |
6a488035 TO |
169 | } |
170 | ||
171 | $this->addRadio('auto_renew', ts('Auto-renew Option'), $options); | |
172 | $this->assign('authorize', $isAuthorize); | |
173 | ||
a1ebf4b9 | 174 | // rollover day |
6a488035 TO |
175 | $this->add('date', 'fixed_period_rollover_day', ts('Fixed Period Rollover Day'), |
176 | CRM_Core_SelectValues::date(NULL, 'M d'), FALSE | |
177 | ); | |
178 | $this->add('date', 'month_fixed_period_rollover_day', ts('Fixed Period Rollover Day'), | |
179 | CRM_Core_SelectValues::date(NULL, 'd'), FALSE | |
180 | ); | |
481a74f4 | 181 | $this->add('select', 'financial_type_id', ts('Financial Type'), |
573fd305 | 182 | array('' => ts('- select -')) + CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, $this->_action), TRUE, array('class' => 'crm-select2') |
6a488035 TO |
183 | ); |
184 | ||
185 | $relTypeInd = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE); | |
186 | if (is_array($relTypeInd)) { | |
187 | asort($relTypeInd); | |
188 | } | |
baccd59e CW |
189 | $memberRel = $this->add('select', 'relationship_type_id', ts('Relationship Type'), |
190 | $relTypeInd, FALSE, array('class' => 'crm-select2 huge', 'multiple' => 1)); | |
6a488035 | 191 | |
80a96508 | 192 | $this->addField('visibility', array('placeholder' => NULL, 'option_url' => NULL)); |
6a488035 TO |
193 | |
194 | $membershipRecords = FALSE; | |
195 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
ff60ec7a | 196 | $result = civicrm_api3("Membership", "get", array("membership_type_id" => $this->_id, "options" => array("limit" => 1))); |
88d349ca | 197 | $membershipRecords = ($result["count"] > 0); |
ff60ec7a | 198 | if ($membershipRecords) { |
6a488035 TO |
199 | $memberRel->freeze(); |
200 | } | |
201 | } | |
202 | ||
203 | $this->assign('membershipRecordsExists', $membershipRecords); | |
204 | ||
6a488035 TO |
205 | $this->addFormRule(array('CRM_Member_Form_MembershipType', 'formRule')); |
206 | ||
207 | $this->assign('membershipTypeId', $this->_id); | |
cebff547 PN |
208 | |
209 | if (CRM_Contribute_BAO_Contribution::checkContributeSettings('deferred_revenue_enabled')) { | |
210 | $deferredFinancialType = CRM_Financial_BAO_FinancialAccount::getDeferredFinancialType(); | |
211 | $this->assign('deferredFinancialType', array_keys($deferredFinancialType)); | |
212 | } | |
6a488035 TO |
213 | } |
214 | ||
215 | /** | |
fe482240 | 216 | * Validation. |
6a488035 | 217 | * |
b2363ea8 TO |
218 | * @param array $params |
219 | * (ref.) an assoc array of name/value pairs. | |
6a488035 | 220 | * |
72b3a70c CW |
221 | * @return bool|array |
222 | * mixed true or array of errors | |
6a488035 | 223 | */ |
00be9182 | 224 | public static function formRule($params) { |
6a488035 TO |
225 | $errors = array(); |
226 | ||
227 | if (!$params['name']) { | |
228 | $errors['name'] = ts('Please enter a membership type name.'); | |
229 | } | |
230 | ||
481a74f4 | 231 | if (($params['minimum_fee'] > 0) && !$params['financial_type_id']) { |
6c68db9f | 232 | $errors['financial_type_id'] = ts('Please enter the financial Type.'); |
6a488035 TO |
233 | } |
234 | ||
6a488035 TO |
235 | if (empty($params['duration_interval']) and $params['duration_unit'] != 'lifetime') { |
236 | $errors['duration_interval'] = ts('Please enter a duration interval.'); | |
237 | } | |
238 | ||
239 | if (in_array(CRM_Utils_Array::value('auto_renew', $params), array( | |
353ffa53 | 240 | 1, |
af9b09df | 241 | 2, |
353ffa53 | 242 | ))) { |
6a488035 TO |
243 | if (($params['duration_interval'] > 1 && $params['duration_unit'] == 'year') || |
244 | ($params['duration_interval'] > 12 && $params['duration_unit'] == 'month') | |
245 | ) { | |
246 | $errors['duration_unit'] = ts('Automatic renewals are not supported by the currently available payment processors when the membership duration is greater than 1 year / 12 months.'); | |
247 | } | |
248 | } | |
249 | ||
6a488035 TO |
250 | if ($params['period_type'] == 'fixed' && |
251 | $params['duration_unit'] == 'day' | |
252 | ) { | |
253 | $errors['period_type'] = ts('Period type should be Rolling when duration unit is Day'); | |
254 | } | |
255 | ||
256 | if (($params['period_type'] == 'fixed') && | |
257 | ($params['duration_unit'] == 'year') | |
258 | ) { | |
259 | $periods = array('fixed_period_start_day', 'fixed_period_rollover_day'); | |
260 | foreach ($periods as $period) { | |
261 | $month = $params[$period]['M']; | |
262 | $date = $params[$period]['d']; | |
263 | if (!$month || !$date) { | |
264 | switch ($period) { | |
265 | case 'fixed_period_start_day': | |
266 | $errors[$period] = ts('Please enter a valid fixed period start day'); | |
267 | break; | |
268 | ||
269 | case 'fixed_period_rollover_day': | |
270 | $errors[$period] = ts('Please enter a valid fixed period rollover day'); | |
271 | break; | |
272 | } | |
273 | } | |
274 | } | |
275 | } | |
276 | ||
277 | if ($params['fixed_period_start_day'] && !empty($params['fixed_period_start_day'])) { | |
278 | $params['fixed_period_start_day']['Y'] = date('Y'); | |
279 | if (!CRM_Utils_Rule::qfDate($params['fixed_period_start_day'])) { | |
280 | $errors['fixed_period_start_day'] = ts('Please enter valid Fixed Period Start Day'); | |
281 | } | |
282 | } | |
283 | ||
284 | if ($params['fixed_period_rollover_day'] && !empty($params['fixed_period_rollover_day'])) { | |
285 | $params['fixed_period_rollover_day']['Y'] = date('Y'); | |
286 | if (!CRM_Utils_Rule::qfDate($params['fixed_period_rollover_day'])) { | |
287 | $errors['fixed_period_rollover_day'] = ts('Please enter valid Fixed Period Rollover Day'); | |
288 | } | |
289 | } | |
290 | ||
291 | return empty($errors) ? TRUE : $errors; | |
292 | } | |
293 | ||
294 | /** | |
fe482240 | 295 | * Process the form submission. |
6a488035 | 296 | * |
355ba699 | 297 | * @return void |
6a488035 TO |
298 | */ |
299 | public function postProcess() { | |
300 | if ($this->_action & CRM_Core_Action::DELETE) { | |
92e4c2a5 | 301 | try { |
353ffa53 | 302 | CRM_Member_BAO_MembershipType::del($this->_id); |
dcc4f6a7 | 303 | } |
353ffa53 | 304 | catch (CRM_Core_Exception $e) { |
dcc4f6a7 | 305 | CRM_Core_Error::statusBounce($e->getMessage(), NULL, ts('Membership Type Not Deleted')); |
306 | } | |
6a488035 TO |
307 | CRM_Core_Session::setStatus(ts('Selected membership type has been deleted.'), ts('Record Deleted'), 'success'); |
308 | } | |
309 | else { | |
310 | $buttonName = $this->controller->getButtonName(); | |
311 | $submitted = $this->controller->exportValues($this->_name); | |
312 | ||
313 | $fields = array( | |
314 | 'name', | |
315 | 'weight', | |
316 | 'is_active', | |
317 | 'member_of_contact_id', | |
318 | 'visibility', | |
319 | 'period_type', | |
320 | 'minimum_fee', | |
321 | 'description', | |
322 | 'auto_renew', | |
323 | 'duration_unit', | |
324 | 'duration_interval', | |
325 | 'financial_type_id', | |
326 | 'fixed_period_start_day', | |
327 | 'fixed_period_rollover_day', | |
328 | 'month_fixed_period_rollover_day', | |
21dfd5f5 | 329 | 'max_related', |
6a488035 TO |
330 | ); |
331 | ||
1322dc53 | 332 | $params = array(); |
6a488035 | 333 | foreach ($fields as $fld) { |
1322dc53 | 334 | $params[$fld] = CRM_Utils_Array::value($fld, $submitted, 'null'); |
6a488035 TO |
335 | } |
336 | ||
6a488035 TO |
337 | if ($params['minimum_fee']) { |
338 | $params['minimum_fee'] = CRM_Utils_Rule::cleanMoney($params['minimum_fee']); | |
339 | } | |
340 | ||
341 | $hasRelTypeVal = FALSE; | |
342 | if (!CRM_Utils_System::isNull($submitted['relationship_type_id'])) { | |
343 | // To insert relation ids and directions with value separator | |
344 | $relTypeDirs = $submitted['relationship_type_id']; | |
345 | $relIds = $relDirection = array(); | |
346 | foreach ($relTypeDirs as $key => $value) { | |
347 | $relationId = explode('_', $value); | |
348 | if (count($relationId) == 3 && | |
349 | is_numeric($relationId[0]) | |
350 | ) { | |
351 | $relIds[] = $relationId[0]; | |
352 | $relDirection[] = $relationId[1] . '_' . $relationId[2]; | |
353 | } | |
354 | } | |
355 | if (!empty($relIds)) { | |
356 | $hasRelTypeVal = TRUE; | |
357 | $params['relationship_type_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $relIds); | |
358 | $params['relationship_direction'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $relDirection); | |
359 | } | |
360 | } | |
361 | if (!$hasRelTypeVal) { | |
1322dc53 | 362 | $params['relationship_type_id'] = $params['relationship_direction'] = $params['max_related'] = 'null'; |
6a488035 TO |
363 | } |
364 | ||
365 | if ($params['duration_unit'] == 'lifetime' && | |
366 | empty($params['duration_interval']) | |
367 | ) { | |
368 | $params['duration_interval'] = 1; | |
369 | } | |
370 | ||
371 | $periods = array('fixed_period_start_day', 'fixed_period_rollover_day'); | |
1322dc53 MW |
372 | foreach ($periods as $period) { |
373 | if (!empty($params[$period]['M']) && !empty($params[$period]['d'])) { | |
374 | $mon = $params[$period]['M']; | |
375 | $dat = $params[$period]['d']; | |
353ffa53 TO |
376 | $mon = ($mon < 10) ? '0' . $mon : $mon; |
377 | $dat = ($dat < 10) ? '0' . $dat : $dat; | |
1322dc53 | 378 | $params[$period] = $mon . $dat; |
6a488035 | 379 | } |
1322dc53 | 380 | elseif ($period == 'fixed_period_rollover_day' && !empty($params['month_fixed_period_rollover_day'])) { |
6a488035 TO |
381 | $params['fixed_period_rollover_day'] = $params['month_fixed_period_rollover_day']['d']; |
382 | unset($params['month_fixed_period_rollover_day']); | |
383 | } | |
384 | else { | |
1322dc53 | 385 | $params[$period] = 'null'; |
6a488035 TO |
386 | } |
387 | } | |
388 | $oldWeight = NULL; | |
389 | ||
390 | if ($this->_id) { | |
391 | $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', | |
392 | $this->_id, 'weight', 'id' | |
393 | ); | |
394 | } | |
395 | $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Member_DAO_MembershipType', | |
396 | $oldWeight, $params['weight'] | |
397 | ); | |
398 | ||
399 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
1322dc53 | 400 | $params['id'] = $this->_id; |
6a488035 TO |
401 | } |
402 | ||
1322dc53 | 403 | $membershipType = CRM_Member_BAO_MembershipType::add($params); |
6a488035 TO |
404 | |
405 | CRM_Core_Session::setStatus(ts('The membership type \'%1\' has been saved.', | |
353ffa53 TO |
406 | array(1 => $membershipType->name) |
407 | ), ts('Saved'), 'success'); | |
6a488035 TO |
408 | $session = CRM_Core_Session::singleton(); |
409 | if ($buttonName == $this->getButtonName('upload', 'new')) { | |
60970bf5 DL |
410 | $session->replaceUserContext( |
411 | CRM_Utils_System::url('civicrm/admin/member/membershipType/add', 'action=add&reset=1') | |
6a488035 TO |
412 | ); |
413 | } | |
414 | } | |
415 | } | |
416 | ||
bb3a214a | 417 | /** |
100fef9d CW |
418 | * @param int $previousID |
419 | * @param int $priceSetId | |
420 | * @param int $membershipTypeId | |
bb3a214a EM |
421 | * @param $optionsIds |
422 | */ | |
6975b8a7 | 423 | public static function checkPreviousPriceField($previousID, $priceSetId, $membershipTypeId, &$optionsIds) { |
6a488035 TO |
424 | if ($previousID) { |
425 | $editedFieldParams = array( | |
426 | 'price_set_id ' => $priceSetId, | |
427 | 'name' => $previousID, | |
428 | ); | |
429 | $editedResults = array(); | |
9da8dc8c | 430 | CRM_Price_BAO_PriceField::retrieve($editedFieldParams, $editedResults); |
6a488035 TO |
431 | if (!empty($editedResults)) { |
432 | $editedFieldParams = array( | |
433 | 'price_field_id' => $editedResults['id'], | |
434 | 'membership_type_id' => $membershipTypeId, | |
435 | ); | |
436 | $editedResults = array(); | |
9da8dc8c | 437 | CRM_Price_BAO_PriceFieldValue::retrieve($editedFieldParams, $editedResults); |
6a488035 TO |
438 | $optionsIds['option_id'][1] = CRM_Utils_Array::value('id', $editedResults); |
439 | } | |
440 | } | |
441 | } | |
96025800 | 442 | |
6a488035 | 443 | } |