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