Merge pull request #11803 from jitendrapurohit/mail-1
[civicrm-core.git] / CRM / Member / Form / MembershipType.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
32 *
33 */
34
35 /**
36 * This class generates form components for Membership Type
37 *
38 */
39 class CRM_Member_Form_MembershipType extends CRM_Member_Form_MembershipConfig {
40
41
42 /**
43 * Explicitly declare the entity api name.
44 */
45 public function getDefaultEntity() {
46 return 'MembershipType';
47 }
48
49 /**
50 * Explicitly declare the form context.
51 */
52 public function getDefaultContext() {
53 return 'create';
54 }
55
56 /**
57 * Max number of contacts we will display for membership-organisation
58 */
59 const MAX_CONTACTS = 50;
60
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);
66
67 $session = CRM_Core_Session::singleton();
68 $url = CRM_Utils_System::url('civicrm/admin/member/membershipType', 'reset=1');
69 $session->pushUserContext($url);
70
71 $this->setPageTitle(ts('Membership Type'));
72 }
73
74 /**
75 * Set default values for the form. MobileProvider that in edit/view mode
76 * the default values are retrieved from the database
77 *
78 * @return array
79 * defaults
80 */
81 public function setDefaultValues() {
82 $defaults = parent::setDefaultValues();
83
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');
87 }
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];
97 }
98 }
99
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];
105
106 $defaults[$per] = array();
107 if ($date > 31) {
108 $date = ($date < 999) ? '0' . $date : $date;
109 $defaults[$per]['M'] = substr($date, 0, 2);
110 $defaults[$per]['d'] = substr($date, 2, 3);
111 }
112 else {
113 //special case when only day is rollover and duration is month
114 $defaults['month_fixed_period_rollover_day']['d'] = $date;
115 }
116 }
117 }
118
119 return $defaults;
120 }
121
122 /**
123 * Build the form object.
124 *
125 * @return void
126 * @throws \CRM_Core_Exception
127 * @throws \CiviCRM_API3_Exception
128 */
129 public function buildQuickForm() {
130 parent::buildQuickForm();
131
132 if ($this->_action & CRM_Core_Action::DELETE) {
133 return;
134 }
135
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');
145
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)
148 );
149 $this->addRule('minimum_fee', ts('Please enter a monetary value for the Minimum Fee.'), 'money');
150
151 $this->add('text', 'duration_interval', ts('Duration Interval'),
152 CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipType', 'duration_interval')
153 );
154
155 $props = array('api' => array('params' => array('contact_type' => 'Organization')));
156 $this->addEntityRef('member_of_contact_id', ts('Membership Organization'), $props, TRUE);
157
158 //start day
159 $this->add('date', 'fixed_period_start_day', ts('Fixed Period Start Day'),
160 CRM_Core_SelectValues::date(NULL, 'M d'), FALSE
161 );
162
163 // Add Auto-renew options if we have a payment processor that supports recurring contributions
164 $isAuthorize = FALSE;
165 $options = array();
166 if (CRM_Financial_BAO_PaymentProcessor::hasPaymentProcessorSupporting(array('Recurring'))) {
167 $isAuthorize = TRUE;
168 $options = CRM_Core_SelectValues::memberAutoRenew();
169 }
170
171 $this->addRadio('auto_renew', ts('Auto-renew Option'), $options);
172 $this->assign('authorize', $isAuthorize);
173
174 // rollover day
175 $this->add('date', 'fixed_period_rollover_day', ts('Fixed Period Rollover Day'),
176 CRM_Core_SelectValues::date(NULL, 'M d'), FALSE
177 );
178 $this->add('date', 'month_fixed_period_rollover_day', ts('Fixed Period Rollover Day'),
179 CRM_Core_SelectValues::date(NULL, 'd'), FALSE
180 );
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')
183 );
184
185 $relTypeInd = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE);
186 if (is_array($relTypeInd)) {
187 asort($relTypeInd);
188 }
189 $memberRel = $this->add('select', 'relationship_type_id', ts('Relationship Type'),
190 $relTypeInd, FALSE, array('class' => 'crm-select2 huge', 'multiple' => 1));
191
192 $this->addField('visibility', array('placeholder' => NULL, 'option_url' => NULL));
193
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();
200 }
201 }
202
203 $this->assign('membershipRecordsExists', $membershipRecords);
204
205 $this->addFormRule(array('CRM_Member_Form_MembershipType', 'formRule'));
206
207 $this->assign('membershipTypeId', $this->_id);
208
209 if (CRM_Contribute_BAO_Contribution::checkContributeSettings('deferred_revenue_enabled')) {
210 $deferredFinancialType = CRM_Financial_BAO_FinancialAccount::getDeferredFinancialType();
211 $this->assign('deferredFinancialType', array_keys($deferredFinancialType));
212 }
213 }
214
215 /**
216 * Validation.
217 *
218 * @param array $params
219 * (ref.) an assoc array of name/value pairs.
220 *
221 * @return bool|array
222 * mixed true or array of errors
223 */
224 public static function formRule($params) {
225 $errors = array();
226
227 if (!$params['name']) {
228 $errors['name'] = ts('Please enter a membership type name.');
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_interval']) and $params['duration_unit'] != 'lifetime') {
236 $errors['duration_interval'] = ts('Please enter a duration interval.');
237 }
238
239 if (in_array(CRM_Utils_Array::value('auto_renew', $params), array(
240 1,
241 2,
242 ))) {
243 if (($params['duration_interval'] > 1 && $params['duration_unit'] == 'year') ||
244 ($params['duration_interval'] > 12 && $params['duration_unit'] == 'month')
245 ) {
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.');
247 }
248 }
249
250 if ($params['period_type'] == 'fixed' &&
251 $params['duration_unit'] == 'day'
252 ) {
253 $errors['period_type'] = ts('Period type should be Rolling when duration unit is Day');
254 }
255
256 if (($params['period_type'] == 'fixed') &&
257 ($params['duration_unit'] == 'year')
258 ) {
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) {
264 switch ($period) {
265 case 'fixed_period_start_day':
266 $errors[$period] = ts('Please enter a valid fixed period start day');
267 break;
268
269 case 'fixed_period_rollover_day':
270 $errors[$period] = ts('Please enter a valid fixed period rollover day');
271 break;
272 }
273 }
274 }
275 }
276
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');
281 }
282 }
283
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');
288 }
289 }
290
291 return empty($errors) ? TRUE : $errors;
292 }
293
294 /**
295 * Process the form submission.
296 *
297 * @return void
298 */
299 public function postProcess() {
300 if ($this->_action & CRM_Core_Action::DELETE) {
301 try {
302 CRM_Member_BAO_MembershipType::del($this->_id);
303 }
304 catch (CRM_Core_Exception $e) {
305 CRM_Core_Error::statusBounce($e->getMessage(), NULL, ts('Membership Type Not Deleted'));
306 }
307 CRM_Core_Session::setStatus(ts('Selected membership type has been deleted.'), ts('Record Deleted'), 'success');
308 }
309 else {
310 $buttonName = $this->controller->getButtonName();
311 $submitted = $this->controller->exportValues($this->_name);
312
313 $fields = array(
314 'name',
315 'weight',
316 'is_active',
317 'member_of_contact_id',
318 'visibility',
319 'period_type',
320 'minimum_fee',
321 'description',
322 'auto_renew',
323 'duration_unit',
324 'duration_interval',
325 'financial_type_id',
326 'fixed_period_start_day',
327 'fixed_period_rollover_day',
328 'month_fixed_period_rollover_day',
329 'max_related',
330 );
331
332 $params = array();
333 foreach ($fields as $fld) {
334 $params[$fld] = CRM_Utils_Array::value($fld, $submitted, 'null');
335 }
336
337 if ($params['minimum_fee']) {
338 $params['minimum_fee'] = CRM_Utils_Rule::cleanMoney($params['minimum_fee']);
339 }
340
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])
350 ) {
351 $relIds[] = $relationId[0];
352 $relDirection[] = $relationId[1] . '_' . $relationId[2];
353 }
354 }
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);
359 }
360 }
361 if (!$hasRelTypeVal) {
362 $params['relationship_type_id'] = $params['relationship_direction'] = $params['max_related'] = 'null';
363 }
364
365 if ($params['duration_unit'] == 'lifetime' &&
366 empty($params['duration_interval'])
367 ) {
368 $params['duration_interval'] = 1;
369 }
370
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;
379 }
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']);
383 }
384 else {
385 $params[$period] = 'null';
386 }
387 }
388 $oldWeight = NULL;
389
390 if ($this->_id) {
391 $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType',
392 $this->_id, 'weight', 'id'
393 );
394 }
395 $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Member_DAO_MembershipType',
396 $oldWeight, $params['weight']
397 );
398
399 if ($this->_action & CRM_Core_Action::UPDATE) {
400 $params['id'] = $this->_id;
401 }
402
403 $membershipType = CRM_Member_BAO_MembershipType::add($params);
404
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')
412 );
413 }
414 }
415 }
416
417 /**
418 * @param int $previousID
419 * @param int $priceSetId
420 * @param int $membershipTypeId
421 * @param $optionsIds
422 */
423 public static function checkPreviousPriceField($previousID, $priceSetId, $membershipTypeId, &$optionsIds) {
424 if ($previousID) {
425 $editedFieldParams = array(
426 'price_set_id ' => $priceSetId,
427 'name' => $previousID,
428 );
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,
435 );
436 $editedResults = array();
437 CRM_Price_BAO_PriceFieldValue::retrieve($editedFieldParams, $editedResults);
438 $optionsIds['option_id'][1] = CRM_Utils_Array::value('id', $editedResults);
439 }
440 }
441 }
442
443 }