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