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