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