3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
37 * This class generates form components for Membership Type
40 class CRM_Member_Form_MembershipType
extends CRM_Member_Form
{
43 * max number of contacts we will display for membership-organisation
45 CONST MAX_CONTACTS
= 50;
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
);
53 $session = CRM_Core_Session
::singleton();
54 $url = CRM_Utils_System
::url('civicrm/admin/member/membershipType', 'reset=1');
55 $session->pushUserContext($url);
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
66 public function setDefaultValues() {
67 $defaults = parent
::setDefaultValues();
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']);
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');
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];
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];
97 $defaults[$per] = array();
99 $date = ($date < 999) ?
'0' . $date : $date;
100 $defaults[$per]['M'] = substr($date, 0, 2);
101 $defaults[$per]['d'] = substr($date, 2, 3);
104 //special case when only day is rollover and duration is month
105 $defaults['month_fixed_period_rollover_day']['d'] = $date;
114 * Function to build the form
119 public function buildQuickForm() {
120 parent
::buildQuickForm();
122 if ($this->_action
& CRM_Core_Action
::DELETE
) {
126 $this->applyFilter('__ALL__', 'trim');
127 $this->add('text', 'name', ts('Name'), CRM_Core_DAO
::getAttribute('CRM_Member_DAO_MembershipType', 'name'), TRUE);
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
)
132 $this->add('text', 'description', ts('Description'),
133 CRM_Core_DAO
::getAttribute('CRM_Member_DAO_MembershipType', 'description')
135 $this->add('text', 'minimum_fee', ts('Minimum Fee'),
136 CRM_Core_DAO
::getAttribute('CRM_Member_DAO_MembershipType', 'minimum_fee')
138 $this->addRule('minimum_fee', ts('Please enter a monetary value for the Minimum Fee.'), 'money');
140 $this->addElement('select', 'duration_unit', ts('Duration'), CRM_Core_SelectValues
::unitList('duration'));
143 $this->addElement('select', 'period_type', ts('Period Type'), CRM_Core_SelectValues
::periodType());
145 $this->add('text', 'duration_interval', ts('Duration Interval'),
146 CRM_Core_DAO
::getAttribute('CRM_Member_DAO_MembershipType', 'duration_interval')
149 $dataUrl = CRM_Utils_System
::url(
151 "className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1&context=membershipType&reset=1&org=1",
154 $this->assign('dataUrl', $dataUrl);
156 $memberOrg = &$this->add('text', 'member_of_contact', ts('Membership Organization'), NULL, TRUE);
157 $this->add('hidden', 'member_of_contact_id', '', array('id' => 'member_of_contact_id'));
158 if ($memberOrg->getValue()) {
159 $this->assign('member_org', $memberOrg->getValue());
163 $this->add('date', 'fixed_period_start_day', ts('Fixed Period Start Day'),
164 CRM_Core_SelectValues
::date(NULL, 'M d'), FALSE
168 $paymentProcessor = CRM_Core_PseudoConstant
::paymentProcessor(FALSE, FALSE, 'is_recur = 1');
169 $isAuthorize = FALSE;
171 if (is_array($paymentProcessor) && !empty($paymentProcessor)) {
173 $options = array(ts('No auto-renew option'), ts('Give option, but not required'), ts('Auto-renew required '));
176 $this->addRadio('auto_renew', ts('Auto-renew Option'), $options);
177 $this->assign('authorize', $isAuthorize);
180 $this->add('date', 'fixed_period_rollover_day', ts('Fixed Period Rollover Day'),
181 CRM_Core_SelectValues
::date(NULL, 'M d'), FALSE
183 $this->add('date', 'month_fixed_period_rollover_day', ts('Fixed Period Rollover Day'),
184 CRM_Core_SelectValues
::date(NULL, 'd'), FALSE
187 $this->add('select', 'financial_type_id', ts( 'Financial Type' ),
188 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant
::financialType()
191 $relTypeInd = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE);
192 if (is_array($relTypeInd)) {
195 $memberRel = &$this->add('select', 'relationship_type_id', ts('Relationship Type'),
196 array('' => ts('- select -')) +
$relTypeInd);
197 $memberRel->setMultiple(TRUE);
199 $this->add('select', 'visibility', ts('Visibility'), CRM_Core_SelectValues
::memberVisibility());
200 $this->add('text', 'weight', ts('Order'),
201 CRM_Core_DAO
::getAttribute('CRM_Member_DAO_MembershipType', 'weight')
203 $this->add('checkbox', 'is_active', ts('Enabled?'));
205 $membershipRecords = FALSE;
206 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
207 $membershipType = new CRM_Member_BAO_Membership();
208 $membershipType->membership_type_id
= $this->_id
;
209 if ($membershipType->find(TRUE)) {
210 $membershipRecords = TRUE;
211 $memberRel->freeze();
215 $this->assign('membershipRecordsExists', $membershipRecords);
217 $this->add('text', 'max_related', ts('Max related'),
218 CRM_Core_DAO
::getAttribute('CRM_Member_DAO_MembershipType', 'max_related')
221 $this->addFormRule(array('CRM_Member_Form_MembershipType', 'formRule'));
223 $this->assign('membershipTypeId', $this->_id
);
227 * Function for validation
229 * @param array $params (ref.) an assoc array of name/value pairs
231 * @return mixed true or array of errors
235 static function formRule($params) {
238 if (!$params['name']) {
239 $errors['name'] = ts('Please enter a membership type name.');
242 if ($params['member_of_contact'] && !is_numeric($params['member_of_contact_id'])) {
243 $errors['member_of_contact'] = ts('Please select valid organization contact.');
246 if (empty( $params['financial_type_id'] ) ) {
247 $errors['financial_type_id'] = ts('Please enter a financial type.');
250 if (($params['minimum_fee'] > 0 ) && !$params['financial_type_id'] ) {
251 $errors['financial_type_id'] = ts('Please enter the financial type.');
254 if (empty($params['duration_unit'])) {
255 $errors['duration_unit'] = ts('Please enter a duration unit.');
258 if (empty($params['duration_interval']) and $params['duration_unit'] != 'lifetime') {
259 $errors['duration_interval'] = ts('Please enter a duration interval.');
262 if (in_array(CRM_Utils_Array
::value('auto_renew', $params), array(
264 if (($params['duration_interval'] > 1 && $params['duration_unit'] == 'year') ||
265 ($params['duration_interval'] > 12 && $params['duration_unit'] == 'month')
267 $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.');
271 if (empty($params['period_type'])) {
272 $errors['period_type'] = ts('Please select a period type.');
275 if ($params['period_type'] == 'fixed' &&
276 $params['duration_unit'] == 'day'
278 $errors['period_type'] = ts('Period type should be Rolling when duration unit is Day');
281 if (($params['period_type'] == 'fixed') &&
282 ($params['duration_unit'] == 'year')
284 $periods = array('fixed_period_start_day', 'fixed_period_rollover_day');
285 foreach ($periods as $period) {
286 $month = $params[$period]['M'];
287 $date = $params[$period]['d'];
288 if (!$month ||
!$date) {
290 case 'fixed_period_start_day':
291 $errors[$period] = ts('Please enter a valid fixed period start day');
294 case 'fixed_period_rollover_day':
295 $errors[$period] = ts('Please enter a valid fixed period rollover day');
302 if ($params['fixed_period_start_day'] && !empty($params['fixed_period_start_day'])) {
303 $params['fixed_period_start_day']['Y'] = date('Y');
304 if (!CRM_Utils_Rule
::qfDate($params['fixed_period_start_day'])) {
305 $errors['fixed_period_start_day'] = ts('Please enter valid Fixed Period Start Day');
309 if ($params['fixed_period_rollover_day'] && !empty($params['fixed_period_rollover_day'])) {
310 $params['fixed_period_rollover_day']['Y'] = date('Y');
311 if (!CRM_Utils_Rule
::qfDate($params['fixed_period_rollover_day'])) {
312 $errors['fixed_period_rollover_day'] = ts('Please enter valid Fixed Period Rollover Day');
316 return empty($errors) ?
TRUE : $errors;
320 * Function to process the form
326 public function postProcess() {
327 if ($this->_action
& CRM_Core_Action
::DELETE
) {
328 CRM_Utils_Weight
::delWeight('CRM_Member_DAO_MembershipType', $this->_id
);
329 CRM_Member_BAO_MembershipType
::del($this->_id
);
330 CRM_Core_Session
::setStatus(ts('Selected membership type has been deleted.'), ts('Record Deleted'), 'success');
333 $buttonName = $this->controller
->getButtonName();
334 $submitted = $this->controller
->exportValues($this->_name
);
340 'member_of_contact_id',
349 'fixed_period_start_day',
350 'fixed_period_rollover_day',
351 'month_fixed_period_rollover_day',
355 $params = $ids = array();
356 foreach ($fields as $fld) {
357 $params[$fld] = CRM_Utils_Array
::value($fld, $submitted, 'NULL');
361 if ($params['minimum_fee']) {
362 $params['minimum_fee'] = CRM_Utils_Rule
::cleanMoney($params['minimum_fee']);
365 $hasRelTypeVal = FALSE;
366 if (!CRM_Utils_System
::isNull($submitted['relationship_type_id'])) {
367 // To insert relation ids and directions with value separator
368 $relTypeDirs = $submitted['relationship_type_id'];
369 $relIds = $relDirection = array();
370 foreach ($relTypeDirs as $key => $value) {
371 $relationId = explode('_', $value);
372 if (count($relationId) == 3 &&
373 is_numeric($relationId[0])
375 $relIds[] = $relationId[0];
376 $relDirection[] = $relationId[1] . '_' . $relationId[2];
379 if (!empty($relIds)) {
380 $hasRelTypeVal = TRUE;
381 $params['relationship_type_id'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $relIds);
382 $params['relationship_direction'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $relDirection);
385 if (!$hasRelTypeVal) {
386 $params['relationship_type_id'] = $params['relationship_direction'] = $params['max_related'] = 'NULL';
389 if ($params['duration_unit'] == 'lifetime' &&
390 empty($params['duration_interval'])
392 $params['duration_interval'] = 1;
395 $periods = array('fixed_period_start_day', 'fixed_period_rollover_day');
396 foreach ($periods as $per) {
397 if (CRM_Utils_Array
::value('M', $params[$per]) &&
398 CRM_Utils_Array
::value('d', $params[$per])
400 $mon = $params[$per]['M'];
401 $dat = $params[$per]['d'];
402 $mon = ($mon < 9) ?
'0' . $mon : $mon;
403 $dat = ($dat < 9) ?
'0' . $dat : $dat;
404 $params[$per] = $mon . $dat;
406 else if($per == 'fixed_period_rollover_day' && !empty($params['month_fixed_period_rollover_day'])){
407 $params['fixed_period_rollover_day'] = $params['month_fixed_period_rollover_day']['d'];
408 unset($params['month_fixed_period_rollover_day']);
411 $params[$per] = 'NULL';
417 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType',
418 $this->_id
, 'weight', 'id'
421 $params['weight'] = CRM_Utils_Weight
::updateOtherWeights('CRM_Member_DAO_MembershipType',
422 $oldWeight, $params['weight']
425 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
426 $ids['membershipType'] = $this->_id
;
429 $membershipType = CRM_Member_BAO_MembershipType
::add($params, $ids);
431 CRM_Core_Session
::setStatus(ts('The membership type \'%1\' has been saved.',
432 array(1 => $membershipType->name
)
433 ), ts('Saved'), 'success');
434 $session = CRM_Core_Session
::singleton();
435 if ($buttonName == $this->getButtonName('upload', 'new')) {
436 $session->replaceUserContext(
437 CRM_Utils_System
::url('civicrm/admin/member/membershipType/add', 'action=add&reset=1')
443 public static function checkPreviousPriceField($previousID, $priceSetId, $membershipTypeId, &$optionsIds) {
445 $editedFieldParams = array(
446 'price_set_id ' => $priceSetId,
447 'name' => $previousID,
449 $editedResults = array();
450 CRM_Price_BAO_Field
::retrieve($editedFieldParams, $editedResults);
451 if (!empty($editedResults)) {
452 $editedFieldParams = array(
453 'price_field_id' => $editedResults['id'],
454 'membership_type_id' => $membershipTypeId,
456 $editedResults = array();
457 CRM_Price_BAO_FieldValue
::retrieve($editedFieldParams, $editedResults);
458 $optionsIds['option_id'][1] = CRM_Utils_Array
::value('id', $editedResults);