3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
36 * This class generates form components for Membership Type
39 class CRM_Member_Form_MembershipType
extends CRM_Member_Form_MembershipConfig
{
43 * Explicitly declare the entity api name.
45 public function getDefaultEntity() {
46 return 'MembershipType';
50 * Explicitly declare the form context.
52 public function getDefaultContext() {
57 * Max number of contacts we will display for membership-organisation
59 const MAX_CONTACTS
= 50;
61 public function preProcess() {
62 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this, FALSE, 0);
63 $this->_BAOName
= 'CRM_Member_BAO_MembershipType';
64 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'add');
65 $this->assign('action', $this->_action
);
67 $session = CRM_Core_Session
::singleton();
68 $url = CRM_Utils_System
::url('civicrm/admin/member/membershipType', 'reset=1');
69 $session->pushUserContext($url);
71 $this->setPageTitle(ts('Membership Type'));
75 * Set default values for the form. MobileProvider that in edit/view mode
76 * the default values are retrieved from the database
81 public function setDefaultValues() {
82 $defaults = parent
::setDefaultValues();
84 //finding default weight to be put
85 if (!isset($defaults['weight']) ||
(!$defaults['weight'])) {
86 $defaults['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Member_DAO_MembershipType');
88 //setting default relationshipType
89 if (isset($defaults['relationship_type_id'])) {
90 //$defaults['relationship_type_id'] = $defaults['relationship_type_id'].'_a_b';
91 // Set values for relation type select box
92 $relTypeIds = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $defaults['relationship_type_id']);
93 $relDirections = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $defaults['relationship_direction']);
94 $defaults['relationship_type_id'] = array();
95 foreach ($relTypeIds as $key => $value) {
96 $defaults['relationship_type_id'][] = $value . '_' . $relDirections[$key];
100 //setting default fixed_period_start_day & fixed_period_rollover_day
101 $periods = array('fixed_period_start_day', 'fixed_period_rollover_day');
102 foreach ($periods as $per) {
103 if (isset($defaults[$per])) {
104 $date = $defaults[$per];
106 $defaults[$per] = array();
108 $date = ($date < 999) ?
'0' . $date : $date;
109 $defaults[$per]['M'] = substr($date, 0, 2);
110 $defaults[$per]['d'] = substr($date, 2, 3);
113 //special case when only day is rollover and duration is month
114 $defaults['month_fixed_period_rollover_day']['d'] = $date;
123 * Build the form object.
126 * @throws \CRM_Core_Exception
127 * @throws \CiviCRM_API3_Exception
129 public function buildQuickForm() {
130 parent
::buildQuickForm();
132 if ($this->_action
& CRM_Core_Action
::DELETE
) {
136 $this->applyFilter('__ALL__', 'trim');
137 $this->addField('name', [], TRUE);
138 $this->addField('description');
139 $this->addField('minimum_fee');
140 $this->addField('duration_unit', [], TRUE);
141 $this->addField('period_type', [], TRUE);
142 $this->addField('is_active');
143 $this->addField('weight');
144 $this->addField('max_related');
146 $this->addRule('name', ts('A membership type with this name already exists. Please select another name.'),
147 'objectExists', array('CRM_Member_DAO_MembershipType', $this->_id
)
149 $this->addRule('minimum_fee', ts('Please enter a monetary value for the Minimum Fee.'), 'money');
151 $this->add('text', 'duration_interval', ts('Duration Interval'),
152 CRM_Core_DAO
::getAttribute('CRM_Member_DAO_MembershipType', 'duration_interval')
155 $props = array('api' => array('params' => array('contact_type' => 'Organization')));
156 $this->addEntityRef('member_of_contact_id', ts('Membership Organization'), $props, TRUE);
159 $this->add('date', 'fixed_period_start_day', ts('Fixed Period Start Day'),
160 CRM_Core_SelectValues
::date(NULL, 'M d'), FALSE
163 // Add Auto-renew options if we have a payment processor that supports recurring contributions
164 $isAuthorize = FALSE;
166 if (CRM_Financial_BAO_PaymentProcessor
::hasPaymentProcessorSupporting(array('Recurring'))) {
168 $options = CRM_Core_SelectValues
::memberAutoRenew();
171 $this->addRadio('auto_renew', ts('Auto-renew Option'), $options);
172 $this->assign('authorize', $isAuthorize);
175 $this->add('date', 'fixed_period_rollover_day', ts('Fixed Period Rollover Day'),
176 CRM_Core_SelectValues
::date(NULL, 'M d'), FALSE
178 $this->add('date', 'month_fixed_period_rollover_day', ts('Fixed Period Rollover Day'),
179 CRM_Core_SelectValues
::date(NULL, 'd'), FALSE
181 $this->add('select', 'financial_type_id', ts('Financial Type'),
182 array('' => ts('- select -')) + CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes, $this->_action
), TRUE, array('class' => 'crm-select2')
185 $relTypeInd = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE);
186 if (is_array($relTypeInd)) {
189 $memberRel = $this->add('select', 'relationship_type_id', ts('Relationship Type'),
190 $relTypeInd, FALSE, array('class' => 'crm-select2 huge', 'multiple' => 1));
192 $this->addField('visibility', array('placeholder' => NULL, 'option_url' => NULL));
194 $membershipRecords = FALSE;
195 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
196 $result = civicrm_api3("Membership", "get", array("membership_type_id" => $this->_id
, "options" => array("limit" => 1)));
197 $membershipRecords = ($result["count"] > 0);
198 if ($membershipRecords) {
199 $memberRel->freeze();
203 $this->assign('membershipRecordsExists', $membershipRecords);
205 $this->addFormRule(array('CRM_Member_Form_MembershipType', 'formRule'));
207 $this->assign('membershipTypeId', $this->_id
);
209 if (CRM_Contribute_BAO_Contribution
::checkContributeSettings('deferred_revenue_enabled')) {
210 $deferredFinancialType = CRM_Financial_BAO_FinancialAccount
::getDeferredFinancialType();
211 $this->assign('deferredFinancialType', array_keys($deferredFinancialType));
218 * @param array $params
219 * (ref.) an assoc array of name/value pairs.
222 * mixed true or array of errors
224 public static function formRule($params) {
227 if (!$params['name']) {
228 $errors['name'] = ts('Please enter a membership type name.');
231 if (($params['minimum_fee'] > 0) && !$params['financial_type_id']) {
232 $errors['financial_type_id'] = ts('Please enter the financial Type.');
235 if (empty($params['duration_interval']) and $params['duration_unit'] != 'lifetime') {
236 $errors['duration_interval'] = ts('Please enter a duration interval.');
239 if (in_array(CRM_Utils_Array
::value('auto_renew', $params), array(
243 if (($params['duration_interval'] > 1 && $params['duration_unit'] == 'year') ||
244 ($params['duration_interval'] > 12 && $params['duration_unit'] == 'month')
246 $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.');
250 if ($params['period_type'] == 'fixed' &&
251 $params['duration_unit'] == 'day'
253 $errors['period_type'] = ts('Period type should be Rolling when duration unit is Day');
256 if (($params['period_type'] == 'fixed') &&
257 ($params['duration_unit'] == 'year')
259 $periods = array('fixed_period_start_day', 'fixed_period_rollover_day');
260 foreach ($periods as $period) {
261 $month = $params[$period]['M'];
262 $date = $params[$period]['d'];
263 if (!$month ||
!$date) {
265 case 'fixed_period_start_day':
266 $errors[$period] = ts('Please enter a valid fixed period start day');
269 case 'fixed_period_rollover_day':
270 $errors[$period] = ts('Please enter a valid fixed period rollover day');
277 if ($params['fixed_period_start_day'] && !empty($params['fixed_period_start_day'])) {
278 $params['fixed_period_start_day']['Y'] = date('Y');
279 if (!CRM_Utils_Rule
::qfDate($params['fixed_period_start_day'])) {
280 $errors['fixed_period_start_day'] = ts('Please enter valid Fixed Period Start Day');
284 if ($params['fixed_period_rollover_day'] && !empty($params['fixed_period_rollover_day'])) {
285 $params['fixed_period_rollover_day']['Y'] = date('Y');
286 if (!CRM_Utils_Rule
::qfDate($params['fixed_period_rollover_day'])) {
287 $errors['fixed_period_rollover_day'] = ts('Please enter valid Fixed Period Rollover Day');
291 return empty($errors) ?
TRUE : $errors;
295 * Process the form submission.
299 public function postProcess() {
300 if ($this->_action
& CRM_Core_Action
::DELETE
) {
302 CRM_Member_BAO_MembershipType
::del($this->_id
);
304 catch (CRM_Core_Exception
$e) {
305 CRM_Core_Error
::statusBounce($e->getMessage(), NULL, ts('Membership Type Not Deleted'));
307 CRM_Core_Session
::setStatus(ts('Selected membership type has been deleted.'), ts('Record Deleted'), 'success');
310 $buttonName = $this->controller
->getButtonName();
311 $submitted = $this->controller
->exportValues($this->_name
);
317 'member_of_contact_id',
326 'fixed_period_start_day',
327 'fixed_period_rollover_day',
328 'month_fixed_period_rollover_day',
333 foreach ($fields as $fld) {
334 $params[$fld] = CRM_Utils_Array
::value($fld, $submitted, 'null');
337 if ($params['minimum_fee']) {
338 $params['minimum_fee'] = CRM_Utils_Rule
::cleanMoney($params['minimum_fee']);
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])
351 $relIds[] = $relationId[0];
352 $relDirection[] = $relationId[1] . '_' . $relationId[2];
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);
361 if (!$hasRelTypeVal) {
362 $params['relationship_type_id'] = $params['relationship_direction'] = $params['max_related'] = 'null';
365 if ($params['duration_unit'] == 'lifetime' &&
366 empty($params['duration_interval'])
368 $params['duration_interval'] = 1;
371 $periods = array('fixed_period_start_day', 'fixed_period_rollover_day');
372 foreach ($periods as $period) {
373 if (!empty($params[$period]['M']) && !empty($params[$period]['d'])) {
374 $mon = $params[$period]['M'];
375 $dat = $params[$period]['d'];
376 $mon = ($mon < 10) ?
'0' . $mon : $mon;
377 $dat = ($dat < 10) ?
'0' . $dat : $dat;
378 $params[$period] = $mon . $dat;
380 elseif ($period == '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']);
385 $params[$period] = 'null';
391 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType',
392 $this->_id
, 'weight', 'id'
395 $params['weight'] = CRM_Utils_Weight
::updateOtherWeights('CRM_Member_DAO_MembershipType',
396 $oldWeight, $params['weight']
399 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
400 $params['id'] = $this->_id
;
403 $membershipType = CRM_Member_BAO_MembershipType
::add($params);
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')) {
410 $session->replaceUserContext(
411 CRM_Utils_System
::url('civicrm/admin/member/membershipType/add', 'action=add&reset=1')
418 * @param int $previousID
419 * @param int $priceSetId
420 * @param int $membershipTypeId
423 public static function checkPreviousPriceField($previousID, $priceSetId, $membershipTypeId, &$optionsIds) {
425 $editedFieldParams = array(
426 'price_set_id ' => $priceSetId,
427 'name' => $previousID,
429 $editedResults = array();
430 CRM_Price_BAO_PriceField
::retrieve($editedFieldParams, $editedResults);
431 if (!empty($editedResults)) {
432 $editedFieldParams = array(
433 'price_field_id' => $editedResults['id'],
434 'membership_type_id' => $membershipTypeId,
436 $editedResults = array();
437 CRM_Price_BAO_PriceFieldValue
::retrieve($editedFieldParams, $editedResults);
438 $optionsIds['option_id'][1] = CRM_Utils_Array
::value('id', $editedResults);