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