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