3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * This class generates form components for Membership Type
40 class CRM_Member_Form_MembershipType
extends CRM_Member_Form_MembershipConfig
{
43 * Max number of contacts we will display for membership-organisation
45 const MAX_CONTACTS
= 50;
47 public 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);
57 $this->setPageTitle(ts('Membership Type'));
61 * Set default values for the form. MobileProvider that in edit/view mode
62 * the default values are retrieved from the database
67 public function setDefaultValues() {
68 $defaults = parent
::setDefaultValues();
70 //finding default weight to be put
71 if (!isset($defaults['weight']) ||
(!$defaults['weight'])) {
72 $defaults['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Member_DAO_MembershipType');
74 //setting default relationshipType
75 if (isset($defaults['relationship_type_id'])) {
76 //$defaults['relationship_type_id'] = $defaults['relationship_type_id'].'_a_b';
77 // Set values for relation type select box
78 $relTypeIds = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $defaults['relationship_type_id']);
79 $relDirections = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $defaults['relationship_direction']);
80 $defaults['relationship_type_id'] = array();
81 foreach ($relTypeIds as $key => $value) {
82 $defaults['relationship_type_id'][] = $value . '_' . $relDirections[$key];
87 //setting default fixed_period_start_day & fixed_period_rollover_day
88 $periods = array('fixed_period_start_day', 'fixed_period_rollover_day');
89 foreach ($periods as $per) {
90 if (isset($defaults[$per])) {
91 $date = $defaults[$per];
93 $defaults[$per] = array();
95 $date = ($date < 999) ?
'0' . $date : $date;
96 $defaults[$per]['M'] = substr($date, 0, 2);
97 $defaults[$per]['d'] = substr($date, 2, 3);
100 //special case when only day is rollover and duration is month
101 $defaults['month_fixed_period_rollover_day']['d'] = $date;
110 * Build the form object
114 public function buildQuickForm() {
115 parent
::buildQuickForm();
117 if ($this->_action
& CRM_Core_Action
::DELETE
) {
121 $this->applyFilter('__ALL__', 'trim');
122 $this->add('text', 'name', ts('Name'), CRM_Core_DAO
::getAttribute('CRM_Member_DAO_MembershipType', 'name'), TRUE);
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
)
127 $this->add('text', 'description', ts('Description'),
128 CRM_Core_DAO
::getAttribute('CRM_Member_DAO_MembershipType', 'description')
130 $this->add('text', 'minimum_fee', ts('Minimum Fee'),
131 CRM_Core_DAO
::getAttribute('CRM_Member_DAO_MembershipType', 'minimum_fee')
133 $this->addRule('minimum_fee', ts('Please enter a monetary value for the Minimum Fee.'), 'money');
135 $this->addSelect('duration_unit', array(), TRUE);
138 $this->addSelect('period_type', array(), TRUE);
140 $this->add('text', 'duration_interval', ts('Duration Interval'),
141 CRM_Core_DAO
::getAttribute('CRM_Member_DAO_MembershipType', 'duration_interval')
144 $props = array('api' => array('params' => array('contact_type' => 'Organization')));
145 $this->addEntityRef('member_of_contact_id', ts('Membership Organization'), $props, TRUE);
148 $this->add('date', 'fixed_period_start_day', ts('Fixed Period Start Day'),
149 CRM_Core_SelectValues
::date(NULL, 'M d'), FALSE
153 $paymentProcessor = CRM_Core_PseudoConstant
::paymentProcessor(FALSE, FALSE, 'is_recur = 1');
154 $isAuthorize = FALSE;
156 if (is_array($paymentProcessor) && !empty($paymentProcessor)) {
158 $options = CRM_Core_SelectValues
::memberAutoRenew();
161 $this->addRadio('auto_renew', ts('Auto-renew Option'), $options);
162 $this->assign('authorize', $isAuthorize);
165 $this->add('date', 'fixed_period_rollover_day', ts('Fixed Period Rollover Day'),
166 CRM_Core_SelectValues
::date(NULL, 'M d'), FALSE
168 $this->add('date', 'month_fixed_period_rollover_day', ts('Fixed Period Rollover Day'),
169 CRM_Core_SelectValues
::date(NULL, 'd'), FALSE
172 $this->add('select', 'financial_type_id', ts( 'Financial Type' ),
173 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant
::financialType(), TRUE, array('class' => 'crm-select2')
176 $relTypeInd = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE);
177 if (is_array($relTypeInd)) {
180 $memberRel = &$this->add('select', 'relationship_type_id', ts('Relationship Type'),
181 array('' => ts('- select -')) +
$relTypeInd);
182 $memberRel->setMultiple(TRUE);
184 $this->addSelect('visibility', array('placeholder' => NULL, 'option_url' => NULL));
186 $this->add('text', 'weight', ts('Order'),
187 CRM_Core_DAO
::getAttribute('CRM_Member_DAO_MembershipType', 'weight')
189 $this->add('checkbox', 'is_active', ts('Enabled?'));
191 $membershipRecords = FALSE;
192 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
193 $membershipType = new CRM_Member_BAO_Membership();
194 $membershipType->membership_type_id
= $this->_id
;
195 if ($membershipType->find(TRUE)) {
196 $membershipRecords = TRUE;
197 $memberRel->freeze();
201 $this->assign('membershipRecordsExists', $membershipRecords);
203 $this->add('text', 'max_related', ts('Max related'),
204 CRM_Core_DAO
::getAttribute('CRM_Member_DAO_MembershipType', 'max_related')
207 $this->addFormRule(array('CRM_Member_Form_MembershipType', 'formRule'));
209 $this->assign('membershipTypeId', $this->_id
);
215 * @param array $params (ref.) an assoc array of name/value pairs
217 * @return mixed true or array of errors
220 public static function formRule($params) {
223 if (!$params['name']) {
224 $errors['name'] = ts('Please enter a membership type name.');
227 if (($params['minimum_fee'] > 0 ) && !$params['financial_type_id'] ) {
228 $errors['financial_type_id'] = ts('Please enter the financial Type.');
231 if (empty($params['duration_interval']) and $params['duration_unit'] != 'lifetime') {
232 $errors['duration_interval'] = ts('Please enter a duration interval.');
235 if (in_array(CRM_Utils_Array
::value('auto_renew', $params), array(
237 if (($params['duration_interval'] > 1 && $params['duration_unit'] == 'year') ||
238 ($params['duration_interval'] > 12 && $params['duration_unit'] == 'month')
240 $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 if ($params['period_type'] == 'fixed' &&
245 $params['duration_unit'] == 'day'
247 $errors['period_type'] = ts('Period type should be Rolling when duration unit is Day');
250 if (($params['period_type'] == 'fixed') &&
251 ($params['duration_unit'] == 'year')
253 $periods = array('fixed_period_start_day', 'fixed_period_rollover_day');
254 foreach ($periods as $period) {
255 $month = $params[$period]['M'];
256 $date = $params[$period]['d'];
257 if (!$month ||
!$date) {
259 case 'fixed_period_start_day':
260 $errors[$period] = ts('Please enter a valid fixed period start day');
263 case 'fixed_period_rollover_day':
264 $errors[$period] = ts('Please enter a valid fixed period rollover day');
271 if ($params['fixed_period_start_day'] && !empty($params['fixed_period_start_day'])) {
272 $params['fixed_period_start_day']['Y'] = date('Y');
273 if (!CRM_Utils_Rule
::qfDate($params['fixed_period_start_day'])) {
274 $errors['fixed_period_start_day'] = ts('Please enter valid Fixed Period Start Day');
278 if ($params['fixed_period_rollover_day'] && !empty($params['fixed_period_rollover_day'])) {
279 $params['fixed_period_rollover_day']['Y'] = date('Y');
280 if (!CRM_Utils_Rule
::qfDate($params['fixed_period_rollover_day'])) {
281 $errors['fixed_period_rollover_day'] = ts('Please enter valid Fixed Period Rollover Day');
285 return empty($errors) ?
TRUE : $errors;
289 * Process the form submission
294 public function postProcess() {
295 if ($this->_action
& CRM_Core_Action
::DELETE
) {
297 CRM_Member_BAO_MembershipType
::del($this->_id
);
299 catch(CRM_Core_Exception
$e) {
300 CRM_Core_Error
::statusBounce($e->getMessage(), NULL, ts('Membership Type Not Deleted'));
302 CRM_Core_Session
::setStatus(ts('Selected membership type has been deleted.'), ts('Record Deleted'), 'success');
305 $buttonName = $this->controller
->getButtonName();
306 $submitted = $this->controller
->exportValues($this->_name
);
312 'member_of_contact_id',
321 'fixed_period_start_day',
322 'fixed_period_rollover_day',
323 'month_fixed_period_rollover_day',
327 $params = $ids = array();
328 foreach ($fields as $fld) {
329 $params[$fld] = CRM_Utils_Array
::value($fld, $submitted, 'NULL');
333 if ($params['minimum_fee']) {
334 $params['minimum_fee'] = CRM_Utils_Rule
::cleanMoney($params['minimum_fee']);
337 $hasRelTypeVal = FALSE;
338 if (!CRM_Utils_System
::isNull($submitted['relationship_type_id'])) {
339 // To insert relation ids and directions with value separator
340 $relTypeDirs = $submitted['relationship_type_id'];
341 $relIds = $relDirection = array();
342 foreach ($relTypeDirs as $key => $value) {
343 $relationId = explode('_', $value);
344 if (count($relationId) == 3 &&
345 is_numeric($relationId[0])
347 $relIds[] = $relationId[0];
348 $relDirection[] = $relationId[1] . '_' . $relationId[2];
351 if (!empty($relIds)) {
352 $hasRelTypeVal = TRUE;
353 $params['relationship_type_id'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $relIds);
354 $params['relationship_direction'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $relDirection);
357 if (!$hasRelTypeVal) {
358 $params['relationship_type_id'] = $params['relationship_direction'] = $params['max_related'] = 'NULL';
361 if ($params['duration_unit'] == 'lifetime' &&
362 empty($params['duration_interval'])
364 $params['duration_interval'] = 1;
367 $periods = array('fixed_period_start_day', 'fixed_period_rollover_day');
368 foreach ($periods as $per) {
369 if (!empty($params[$per]['M']) && !empty($params[$per]['d'])) {
370 $mon = $params[$per]['M'];
371 $dat = $params[$per]['d'];
372 $mon = ($mon < 10) ?
'0' . $mon : $mon;
373 $dat = ($dat < 10) ?
'0' . $dat : $dat;
374 $params[$per] = $mon . $dat;
376 else if($per == 'fixed_period_rollover_day' && !empty($params['month_fixed_period_rollover_day'])){
377 $params['fixed_period_rollover_day'] = $params['month_fixed_period_rollover_day']['d'];
378 unset($params['month_fixed_period_rollover_day']);
381 $params[$per] = 'NULL';
387 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType',
388 $this->_id
, 'weight', 'id'
391 $params['weight'] = CRM_Utils_Weight
::updateOtherWeights('CRM_Member_DAO_MembershipType',
392 $oldWeight, $params['weight']
395 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
396 $ids['membershipType'] = $this->_id
;
399 $membershipType = CRM_Member_BAO_MembershipType
::add($params, $ids);
401 CRM_Core_Session
::setStatus(ts('The membership type \'%1\' has been saved.',
402 array(1 => $membershipType->name
)
403 ), ts('Saved'), 'success');
404 $session = CRM_Core_Session
::singleton();
405 if ($buttonName == $this->getButtonName('upload', 'new')) {
406 $session->replaceUserContext(
407 CRM_Utils_System
::url('civicrm/admin/member/membershipType/add', 'action=add&reset=1')
414 * @param int $previousID
415 * @param int $priceSetId
416 * @param int $membershipTypeId
419 public static function checkPreviousPriceField($previousID, $priceSetId, $membershipTypeId, &$optionsIds) {
421 $editedFieldParams = array(
422 'price_set_id ' => $priceSetId,
423 'name' => $previousID,
425 $editedResults = array();
426 CRM_Price_BAO_PriceField
::retrieve($editedFieldParams, $editedResults);
427 if (!empty($editedResults)) {
428 $editedFieldParams = array(
429 'price_field_id' => $editedResults['id'],
430 'membership_type_id' => $membershipTypeId,
432 $editedResults = array();
433 CRM_Price_BAO_PriceFieldValue
::retrieve($editedFieldParams, $editedResults);
434 $optionsIds['option_id'][1] = CRM_Utils_Array
::value('id', $editedResults);