Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
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() { | |
fc093e40 | 65 | $this->entityFields = [ |
66 | 'name' => [ | |
67 | 'required' => 'TRUE', | |
68 | 'name' => 'name', | |
69 | 'description' => ts("e.g. 'Student', 'Senior', 'Honor Society'..."), | |
70 | ], | |
71 | 'description' => [ | |
72 | 'name' => 'description', | |
73 | 'description' => ts("Description of this membership type for internal use. May include eligibility, benefits, terms, etc."), | |
74 | ], | |
75 | 'member_of_contact_id' => [ | |
76 | 'name' => 'member_of_contact_id', | |
77 | 'description' => ts("Members assigned this membership type belong to which organization (e.g. this is for membership in 'Save the Whales - Northwest Chapter'). NOTE: This organization/group/chapter must exist as a CiviCRM Organization type contact."), | |
78 | ], | |
79 | 'minimum_fee' => [ | |
80 | 'name' => 'minimum_fee', | |
81 | 'description' => ts('Minimum fee required for this membership type. For free/complimentary memberships - set minimum fee to zero (0). NOTE: When using CiviCRM to process sales taxes this should be the tax exclusive amount.'), | |
82 | 'formatter' => 'crmMoney', | |
83 | ], | |
84 | 'financial_type_id' => [ | |
85 | 'name' => 'financial_type_id', | |
86 | 'description' => ts('Select the financial type assigned to fees for this membership type (for example \'Membership Fees\'). This is required for all membership types - including free or complimentary memberships.'), | |
87 | ], | |
88 | 'auto_renew' => [ | |
89 | 'name' => 'auto_renew', | |
90 | 'options' => CRM_Core_SelectValues::memberAutoRenew(), | |
91 | 'place_holder' => ts('You will need to select and configure a supported payment processor (currently Authorize.Net, PayPal Pro, or PayPal Website Standard) in order to offer automatically renewing memberships.'), | |
92 | ], | |
93 | 'duration_interval' => [ | |
94 | 'name' => 'duration_interval', | |
d3d3b685 | 95 | 'required' => TRUE, |
fc093e40 | 96 | ], |
97 | 'duration_unit' => [ | |
98 | 'name' => 'duration_unit', | |
99 | 'description' => ts('Duration of this membership (e.g. 30 days, 2 months, 5 years, 1 lifetime)'), | |
d3d3b685 | 100 | 'required' => TRUE, |
fc093e40 | 101 | ], |
102 | 'period_type' => [ | |
103 | 'name' => 'period_type', | |
104 | 'description' => ts("Select 'rolling' if membership periods begin at date of signup. Select 'fixed' if membership periods begin on a set calendar date."), | |
105 | 'help' => ['id' => 'period-type', 'file' => "CRM/Member/Page/MembershipType.hlp"], | |
d3d3b685 | 106 | 'required' => TRUE, |
fc093e40 | 107 | ], |
108 | 'fixed_period_start_day' => [ | |
109 | 'name' => 'fixed_period_start_day', | |
110 | 'description' => ts("Month and day on which a <strong>fixed</strong> period membership or subscription begins. Example: A fixed period membership with Start Day set to Jan 01 means that membership periods would be 1/1/06 - 12/31/06 for anyone signing up during 2006."), | |
111 | ], | |
112 | 'fixed_period_rollover_day' => [ | |
113 | 'name' => 'fixed_period_rollover_day', | |
114 | 'description' => ts('Membership signups on or after this date cover the following calendar year as well. Example: If the rollover day is November 30, membership period for signups during December will cover the following year.'), | |
115 | ], | |
116 | 'relationship_type_id' => [ | |
117 | 'name' => 'relationship_type_id', | |
118 | ], | |
119 | 'max_related' => [ | |
120 | 'name' => 'max_related', | |
121 | 'description' => ts('Maximum number of related memberships (leave blank for unlimited).'), | |
122 | ], | |
123 | 'visibility' => [ | |
124 | 'name' => 'visibility', | |
125 | 'description' => ts("Can this membership type be used for self-service signups ('Public'), or is it only for CiviCRM users with 'Edit Contributions' permission ('Admin')."), | |
126 | ], | |
127 | 'weight' => [ | |
128 | 'name' => 'weight', | |
129 | ], | |
130 | 'is_active' => [ | |
131 | 'name' => 'is_active', | |
132 | ], | |
133 | ]; | |
134 | ||
be2fb01f | 135 | if (!CRM_Financial_BAO_PaymentProcessor::hasPaymentProcessorSupporting(['Recurring'])) { |
fc093e40 | 136 | $this->entityFields['auto_renew']['not-auto-addable'] = TRUE; |
137 | $this->entityFields['auto_renew']['documentation_link'] = ['page' => 'user/contributions/payment-processors']; | |
138 | } | |
2147e326 MW |
139 | } |
140 | ||
141 | /** | |
142 | * Deletion message to be assigned to the form. | |
143 | * | |
144 | * @var string | |
145 | */ | |
146 | protected $deleteMessage; | |
763f7a8a | 147 | |
148 | /** | |
149 | * Explicitly declare the entity api name. | |
150 | */ | |
151 | public function getDefaultEntity() { | |
152 | return 'MembershipType'; | |
153 | } | |
154 | ||
2147e326 MW |
155 | /** |
156 | * Set the delete message. | |
157 | * | |
158 | * We do this from the constructor in order to do a translation. | |
159 | */ | |
160 | public function setDeleteMessage() { | |
161 | $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?'); | |
162 | } | |
163 | ||
763f7a8a | 164 | /** |
165 | * Explicitly declare the form context. | |
166 | */ | |
167 | public function getDefaultContext() { | |
168 | return 'create'; | |
169 | } | |
170 | ||
6a488035 | 171 | /** |
100fef9d | 172 | * Max number of contacts we will display for membership-organisation |
6a488035 | 173 | */ |
7da04cde | 174 | const MAX_CONTACTS = 50; |
6a488035 | 175 | |
00be9182 | 176 | public function preProcess() { |
481a74f4 | 177 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, 0); |
6a488035 TO |
178 | $this->_BAOName = 'CRM_Member_BAO_MembershipType'; |
179 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add'); | |
180 | $this->assign('action', $this->_action); | |
181 | ||
182 | $session = CRM_Core_Session::singleton(); | |
183 | $url = CRM_Utils_System::url('civicrm/admin/member/membershipType', 'reset=1'); | |
184 | $session->pushUserContext($url); | |
e2046b33 CW |
185 | |
186 | $this->setPageTitle(ts('Membership Type')); | |
6a488035 TO |
187 | } |
188 | ||
189 | /** | |
c490a46a | 190 | * Set default values for the form. MobileProvider that in edit/view mode |
6a488035 TO |
191 | * the default values are retrieved from the database |
192 | * | |
1322dc53 MW |
193 | * @return array |
194 | * defaults | |
6a488035 TO |
195 | */ |
196 | public function setDefaultValues() { | |
197 | $defaults = parent::setDefaultValues(); | |
198 | ||
6a488035 TO |
199 | //finding default weight to be put |
200 | if (!isset($defaults['weight']) || (!$defaults['weight'])) { | |
201 | $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Member_DAO_MembershipType'); | |
202 | } | |
203 | //setting default relationshipType | |
204 | if (isset($defaults['relationship_type_id'])) { | |
205 | //$defaults['relationship_type_id'] = $defaults['relationship_type_id'].'_a_b'; | |
206 | // Set values for relation type select box | |
207 | $relTypeIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaults['relationship_type_id']); | |
208 | $relDirections = explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaults['relationship_direction']); | |
be2fb01f | 209 | $defaults['relationship_type_id'] = []; |
6a488035 TO |
210 | foreach ($relTypeIds as $key => $value) { |
211 | $defaults['relationship_type_id'][] = $value . '_' . $relDirections[$key]; | |
212 | } | |
213 | } | |
214 | ||
6a488035 | 215 | //setting default fixed_period_start_day & fixed_period_rollover_day |
be2fb01f | 216 | $periods = ['fixed_period_start_day', 'fixed_period_rollover_day']; |
6a488035 TO |
217 | foreach ($periods as $per) { |
218 | if (isset($defaults[$per])) { | |
353ffa53 | 219 | $date = $defaults[$per]; |
6a488035 | 220 | |
be2fb01f | 221 | $defaults[$per] = []; |
6a488035 | 222 | if ($date > 31) { |
353ffa53 | 223 | $date = ($date < 999) ? '0' . $date : $date; |
6a488035 TO |
224 | $defaults[$per]['M'] = substr($date, 0, 2); |
225 | $defaults[$per]['d'] = substr($date, 2, 3); | |
226 | } | |
227 | else { | |
228 | //special case when only day is rollover and duration is month | |
229 | $defaults['month_fixed_period_rollover_day']['d'] = $date; | |
230 | } | |
231 | } | |
232 | } | |
233 | ||
234 | return $defaults; | |
235 | } | |
236 | ||
237 | /** | |
fe482240 | 238 | * Build the form object. |
6a488035 | 239 | * |
355ba699 | 240 | * @return void |
a1ebf4b9 MW |
241 | * @throws \CRM_Core_Exception |
242 | * @throws \CiviCRM_API3_Exception | |
6a488035 TO |
243 | */ |
244 | public function buildQuickForm() { | |
2147e326 | 245 | self::buildQuickEntityForm(); |
6a488035 TO |
246 | |
247 | if ($this->_action & CRM_Core_Action::DELETE) { | |
248 | return; | |
249 | } | |
c41292e8 | 250 | // This is a temporary variable as we work towards moving over towards using the EntityField.tpl. |
251 | // Fields in this array have been tested & in the tpl have been switched over to metadata. | |
252 | // Note this kinda 'works from the top' - ie. once we hit a field that needs some thought we need | |
253 | // to stop & make that one work. | |
254 | $this->assign('tpl_standardised_fields', ['name', 'description', 'member_of_contact_id', 'minimum_fee']); | |
6a488035 | 255 | |
6a488035 | 256 | $this->addRule('name', ts('A membership type with this name already exists. Please select another name.'), |
be2fb01f | 257 | 'objectExists', ['CRM_Member_DAO_MembershipType', $this->_id] |
6a488035 | 258 | ); |
6a488035 TO |
259 | $this->addRule('minimum_fee', ts('Please enter a monetary value for the Minimum Fee.'), 'money'); |
260 | ||
be2fb01f | 261 | $props = ['api' => ['params' => ['contact_type' => 'Organization']]]; |
fc791e90 | 262 | $this->addEntityRef('member_of_contact_id', ts('Membership Organization'), $props, TRUE); |
6a488035 TO |
263 | |
264 | //start day | |
265 | $this->add('date', 'fixed_period_start_day', ts('Fixed Period Start Day'), | |
266 | CRM_Core_SelectValues::date(NULL, 'M d'), FALSE | |
267 | ); | |
268 | ||
54afd8a6 | 269 | // Add Auto-renew options if we have a payment processor that supports recurring contributions |
353ffa53 | 270 | $isAuthorize = FALSE; |
be2fb01f CW |
271 | $options = []; |
272 | if (CRM_Financial_BAO_PaymentProcessor::hasPaymentProcessorSupporting(['Recurring'])) { | |
6a488035 | 273 | $isAuthorize = TRUE; |
dbd82592 | 274 | $options = CRM_Core_SelectValues::memberAutoRenew(); |
6a488035 TO |
275 | } |
276 | ||
277 | $this->addRadio('auto_renew', ts('Auto-renew Option'), $options); | |
278 | $this->assign('authorize', $isAuthorize); | |
279 | ||
a1ebf4b9 | 280 | // rollover day |
6a488035 TO |
281 | $this->add('date', 'fixed_period_rollover_day', ts('Fixed Period Rollover Day'), |
282 | CRM_Core_SelectValues::date(NULL, 'M d'), FALSE | |
283 | ); | |
284 | $this->add('date', 'month_fixed_period_rollover_day', ts('Fixed Period Rollover Day'), | |
285 | CRM_Core_SelectValues::date(NULL, 'd'), FALSE | |
286 | ); | |
481a74f4 | 287 | $this->add('select', 'financial_type_id', ts('Financial Type'), |
be2fb01f | 288 | ['' => ts('- select -')] + CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, $this->_action), TRUE, ['class' => 'crm-select2'] |
6a488035 TO |
289 | ); |
290 | ||
291 | $relTypeInd = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE); | |
292 | if (is_array($relTypeInd)) { | |
293 | asort($relTypeInd); | |
294 | } | |
baccd59e | 295 | $memberRel = $this->add('select', 'relationship_type_id', ts('Relationship Type'), |
be2fb01f | 296 | $relTypeInd, FALSE, ['class' => 'crm-select2 huge', 'multiple' => 1]); |
6a488035 | 297 | |
be2fb01f | 298 | $this->addField('visibility', ['placeholder' => NULL, 'option_url' => NULL]); |
6a488035 TO |
299 | |
300 | $membershipRecords = FALSE; | |
301 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
be2fb01f | 302 | $result = civicrm_api3("Membership", "get", ["membership_type_id" => $this->_id, "options" => ["limit" => 1]]); |
88d349ca | 303 | $membershipRecords = ($result["count"] > 0); |
ff60ec7a | 304 | if ($membershipRecords) { |
6a488035 TO |
305 | $memberRel->freeze(); |
306 | } | |
307 | } | |
308 | ||
309 | $this->assign('membershipRecordsExists', $membershipRecords); | |
310 | ||
be2fb01f | 311 | $this->addFormRule(['CRM_Member_Form_MembershipType', 'formRule']); |
6a488035 TO |
312 | |
313 | $this->assign('membershipTypeId', $this->_id); | |
cebff547 | 314 | |
a17bec97 | 315 | if (Civi::settings()->get('deferred_revenue_enabled')) { |
cebff547 PN |
316 | $deferredFinancialType = CRM_Financial_BAO_FinancialAccount::getDeferredFinancialType(); |
317 | $this->assign('deferredFinancialType', array_keys($deferredFinancialType)); | |
318 | } | |
6a488035 TO |
319 | } |
320 | ||
321 | /** | |
fe482240 | 322 | * Validation. |
6a488035 | 323 | * |
b2363ea8 TO |
324 | * @param array $params |
325 | * (ref.) an assoc array of name/value pairs. | |
6a488035 | 326 | * |
72b3a70c CW |
327 | * @return bool|array |
328 | * mixed true or array of errors | |
6a488035 | 329 | */ |
00be9182 | 330 | public static function formRule($params) { |
be2fb01f | 331 | $errors = []; |
6a488035 TO |
332 | |
333 | if (!$params['name']) { | |
334 | $errors['name'] = ts('Please enter a membership type name.'); | |
335 | } | |
336 | ||
481a74f4 | 337 | if (($params['minimum_fee'] > 0) && !$params['financial_type_id']) { |
6c68db9f | 338 | $errors['financial_type_id'] = ts('Please enter the financial Type.'); |
6a488035 TO |
339 | } |
340 | ||
6a488035 TO |
341 | if (empty($params['duration_interval']) and $params['duration_unit'] != 'lifetime') { |
342 | $errors['duration_interval'] = ts('Please enter a duration interval.'); | |
343 | } | |
344 | ||
be2fb01f | 345 | if (in_array(CRM_Utils_Array::value('auto_renew', $params), [ |
353ffa53 | 346 | 1, |
af9b09df | 347 | 2, |
be2fb01f | 348 | ])) { |
6a488035 TO |
349 | if (($params['duration_interval'] > 1 && $params['duration_unit'] == 'year') || |
350 | ($params['duration_interval'] > 12 && $params['duration_unit'] == 'month') | |
351 | ) { | |
352 | $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.'); | |
353 | } | |
354 | } | |
355 | ||
6a488035 TO |
356 | if ($params['period_type'] == 'fixed' && |
357 | $params['duration_unit'] == 'day' | |
358 | ) { | |
359 | $errors['period_type'] = ts('Period type should be Rolling when duration unit is Day'); | |
360 | } | |
361 | ||
362 | if (($params['period_type'] == 'fixed') && | |
363 | ($params['duration_unit'] == 'year') | |
364 | ) { | |
be2fb01f | 365 | $periods = ['fixed_period_start_day', 'fixed_period_rollover_day']; |
6a488035 TO |
366 | foreach ($periods as $period) { |
367 | $month = $params[$period]['M']; | |
368 | $date = $params[$period]['d']; | |
369 | if (!$month || !$date) { | |
370 | switch ($period) { | |
371 | case 'fixed_period_start_day': | |
372 | $errors[$period] = ts('Please enter a valid fixed period start day'); | |
373 | break; | |
374 | ||
375 | case 'fixed_period_rollover_day': | |
376 | $errors[$period] = ts('Please enter a valid fixed period rollover day'); | |
377 | break; | |
378 | } | |
379 | } | |
380 | } | |
381 | } | |
382 | ||
383 | if ($params['fixed_period_start_day'] && !empty($params['fixed_period_start_day'])) { | |
384 | $params['fixed_period_start_day']['Y'] = date('Y'); | |
385 | if (!CRM_Utils_Rule::qfDate($params['fixed_period_start_day'])) { | |
386 | $errors['fixed_period_start_day'] = ts('Please enter valid Fixed Period Start Day'); | |
387 | } | |
388 | } | |
389 | ||
390 | if ($params['fixed_period_rollover_day'] && !empty($params['fixed_period_rollover_day'])) { | |
391 | $params['fixed_period_rollover_day']['Y'] = date('Y'); | |
392 | if (!CRM_Utils_Rule::qfDate($params['fixed_period_rollover_day'])) { | |
393 | $errors['fixed_period_rollover_day'] = ts('Please enter valid Fixed Period Rollover Day'); | |
394 | } | |
395 | } | |
396 | ||
397 | return empty($errors) ? TRUE : $errors; | |
398 | } | |
399 | ||
400 | /** | |
fe482240 | 401 | * Process the form submission. |
6a488035 | 402 | * |
355ba699 | 403 | * @return void |
6a488035 TO |
404 | */ |
405 | public function postProcess() { | |
406 | if ($this->_action & CRM_Core_Action::DELETE) { | |
92e4c2a5 | 407 | try { |
353ffa53 | 408 | CRM_Member_BAO_MembershipType::del($this->_id); |
dcc4f6a7 | 409 | } |
353ffa53 | 410 | catch (CRM_Core_Exception $e) { |
dcc4f6a7 | 411 | CRM_Core_Error::statusBounce($e->getMessage(), NULL, ts('Membership Type Not Deleted')); |
412 | } | |
6a488035 TO |
413 | CRM_Core_Session::setStatus(ts('Selected membership type has been deleted.'), ts('Record Deleted'), 'success'); |
414 | } | |
415 | else { | |
2147e326 | 416 | $params = $this->exportValues(); |
6a488035 | 417 | |
6a488035 TO |
418 | if ($params['minimum_fee']) { |
419 | $params['minimum_fee'] = CRM_Utils_Rule::cleanMoney($params['minimum_fee']); | |
420 | } | |
421 | ||
422 | $hasRelTypeVal = FALSE; | |
2147e326 | 423 | if (!CRM_Utils_System::isNull($params['relationship_type_id'])) { |
6a488035 | 424 | // To insert relation ids and directions with value separator |
2147e326 | 425 | $relTypeDirs = $params['relationship_type_id']; |
be2fb01f | 426 | $relIds = $relDirection = []; |
6a488035 TO |
427 | foreach ($relTypeDirs as $key => $value) { |
428 | $relationId = explode('_', $value); | |
429 | if (count($relationId) == 3 && | |
430 | is_numeric($relationId[0]) | |
431 | ) { | |
432 | $relIds[] = $relationId[0]; | |
433 | $relDirection[] = $relationId[1] . '_' . $relationId[2]; | |
434 | } | |
435 | } | |
436 | if (!empty($relIds)) { | |
437 | $hasRelTypeVal = TRUE; | |
438 | $params['relationship_type_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $relIds); | |
439 | $params['relationship_direction'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $relDirection); | |
440 | } | |
441 | } | |
442 | if (!$hasRelTypeVal) { | |
1322dc53 | 443 | $params['relationship_type_id'] = $params['relationship_direction'] = $params['max_related'] = 'null'; |
6a488035 TO |
444 | } |
445 | ||
446 | if ($params['duration_unit'] == 'lifetime' && | |
447 | empty($params['duration_interval']) | |
448 | ) { | |
449 | $params['duration_interval'] = 1; | |
450 | } | |
451 | ||
be2fb01f | 452 | $periods = ['fixed_period_start_day', 'fixed_period_rollover_day']; |
1322dc53 MW |
453 | foreach ($periods as $period) { |
454 | if (!empty($params[$period]['M']) && !empty($params[$period]['d'])) { | |
455 | $mon = $params[$period]['M']; | |
456 | $dat = $params[$period]['d']; | |
353ffa53 TO |
457 | $mon = ($mon < 10) ? '0' . $mon : $mon; |
458 | $dat = ($dat < 10) ? '0' . $dat : $dat; | |
1322dc53 | 459 | $params[$period] = $mon . $dat; |
6a488035 | 460 | } |
1322dc53 | 461 | elseif ($period == 'fixed_period_rollover_day' && !empty($params['month_fixed_period_rollover_day'])) { |
6a488035 TO |
462 | $params['fixed_period_rollover_day'] = $params['month_fixed_period_rollover_day']['d']; |
463 | unset($params['month_fixed_period_rollover_day']); | |
464 | } | |
465 | else { | |
1322dc53 | 466 | $params[$period] = 'null'; |
6a488035 TO |
467 | } |
468 | } | |
469 | $oldWeight = NULL; | |
470 | ||
471 | if ($this->_id) { | |
472 | $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', | |
473 | $this->_id, 'weight', 'id' | |
474 | ); | |
475 | } | |
476 | $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Member_DAO_MembershipType', | |
477 | $oldWeight, $params['weight'] | |
478 | ); | |
479 | ||
480 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
1322dc53 | 481 | $params['id'] = $this->_id; |
6a488035 TO |
482 | } |
483 | ||
2147e326 MW |
484 | $membershipTypeResult = civicrm_api3('MembershipType', 'create', $params); |
485 | $membershipTypeName = $membershipTypeResult['values'][$membershipTypeResult['id']]['name']; | |
6a488035 | 486 | |
2147e326 | 487 | CRM_Core_Session::setStatus(ts("The membership type '%1' has been saved.", |
be2fb01f | 488 | [1 => $membershipTypeName] |
353ffa53 | 489 | ), ts('Saved'), 'success'); |
6a488035 | 490 | $session = CRM_Core_Session::singleton(); |
2147e326 | 491 | $buttonName = $this->controller->getButtonName(); |
6a488035 | 492 | if ($buttonName == $this->getButtonName('upload', 'new')) { |
60970bf5 DL |
493 | $session->replaceUserContext( |
494 | CRM_Utils_System::url('civicrm/admin/member/membershipType/add', 'action=add&reset=1') | |
6a488035 TO |
495 | ); |
496 | } | |
497 | } | |
498 | } | |
499 | ||
bb3a214a | 500 | /** |
100fef9d CW |
501 | * @param int $previousID |
502 | * @param int $priceSetId | |
503 | * @param int $membershipTypeId | |
bb3a214a EM |
504 | * @param $optionsIds |
505 | */ | |
6975b8a7 | 506 | public static function checkPreviousPriceField($previousID, $priceSetId, $membershipTypeId, &$optionsIds) { |
6a488035 | 507 | if ($previousID) { |
be2fb01f | 508 | $editedFieldParams = [ |
6a488035 TO |
509 | 'price_set_id ' => $priceSetId, |
510 | 'name' => $previousID, | |
be2fb01f CW |
511 | ]; |
512 | $editedResults = []; | |
9da8dc8c | 513 | CRM_Price_BAO_PriceField::retrieve($editedFieldParams, $editedResults); |
6a488035 | 514 | if (!empty($editedResults)) { |
be2fb01f | 515 | $editedFieldParams = [ |
6a488035 TO |
516 | 'price_field_id' => $editedResults['id'], |
517 | 'membership_type_id' => $membershipTypeId, | |
be2fb01f CW |
518 | ]; |
519 | $editedResults = []; | |
9da8dc8c | 520 | CRM_Price_BAO_PriceFieldValue::retrieve($editedFieldParams, $editedResults); |
6a488035 TO |
521 | $optionsIds['option_id'][1] = CRM_Utils_Array::value('id', $editedResults); |
522 | } | |
523 | } | |
524 | } | |
96025800 | 525 | |
6a488035 | 526 | } |