Merge pull request #22584 from civicrm/5.46
[civicrm-core.git] / CRM / Member / Form / MembershipType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
19 * This class generates form components for Membership Type
20 *
21 */
fb3082b2 22class CRM_Member_Form_MembershipType extends CRM_Member_Form_MembershipConfig {
6a488035 23
2147e326
MW
24 use CRM_Core_Form_EntityFormTrait;
25
26 /**
27 * Fields for the entity to be assigned to the template.
28 *
29 * Fields may have keys
30 * - name (required to show in tpl from the array)
31 * - description (optional, will appear below the field)
32 * - not-auto-addable - this class will not attempt to add the field using addField.
33 * (this will be automatically set if the field does not have html in it's metadata
34 * or is not a core field on the form's entity).
35 * - help (option) add help to the field - e.g ['id' => 'id-source', 'file' => 'CRM/Contact/Form/Contact']]
36 * - template - use a field specific template to render this field
37 * - required
38 * - is_freeze (field should be frozen).
39 *
40 * @var array
41 */
42 protected $entityFields = [];
43
44 /**
45 * Set entity fields to be assigned to the form.
46 */
47 protected function setEntityFields() {
fc093e40 48 $this->entityFields = [
49 'name' => [
50 'required' => 'TRUE',
51 'name' => 'name',
52 'description' => ts("e.g. 'Student', 'Senior', 'Honor Society'..."),
53 ],
54 'description' => [
55 'name' => 'description',
56 'description' => ts("Description of this membership type for internal use. May include eligibility, benefits, terms, etc."),
57 ],
58 'member_of_contact_id' => [
59 'name' => 'member_of_contact_id',
60 'description' => ts("Members assigned this membership type belong to which organization (e.g. this is for membership in 'Save the Whales - Northwest Chapter'). NOTE: This organization/group/chapter must exist as a CiviCRM Organization type contact."),
61 ],
62 'minimum_fee' => [
63 'name' => 'minimum_fee',
64 'description' => ts('Minimum fee required for this membership type. For free/complimentary memberships - set minimum fee to zero (0). NOTE: When using CiviCRM to process sales taxes this should be the tax exclusive amount.'),
65 'formatter' => 'crmMoney',
66 ],
67 'financial_type_id' => [
68 'name' => 'financial_type_id',
69 'description' => ts('Select the financial type assigned to fees for this membership type (for example \'Membership Fees\'). This is required for all membership types - including free or complimentary memberships.'),
70 ],
71 'auto_renew' => [
72 'name' => 'auto_renew',
73 'options' => CRM_Core_SelectValues::memberAutoRenew(),
74 'place_holder' => ts('You will need to select and configure a supported payment processor (currently Authorize.Net, PayPal Pro, or PayPal Website Standard) in order to offer automatically renewing memberships.'),
75 ],
76 'duration_interval' => [
77 'name' => 'duration_interval',
d3d3b685 78 'required' => TRUE,
fc093e40 79 ],
80 'duration_unit' => [
81 'name' => 'duration_unit',
82 'description' => ts('Duration of this membership (e.g. 30 days, 2 months, 5 years, 1 lifetime)'),
d3d3b685 83 'required' => TRUE,
fc093e40 84 ],
85 'period_type' => [
86 'name' => 'period_type',
87 'description' => ts("Select 'rolling' if membership periods begin at date of signup. Select 'fixed' if membership periods begin on a set calendar date."),
88 'help' => ['id' => 'period-type', 'file' => "CRM/Member/Page/MembershipType.hlp"],
d3d3b685 89 'required' => TRUE,
fc093e40 90 ],
91 'fixed_period_start_day' => [
92 'name' => 'fixed_period_start_day',
93 'description' => ts("Month and day on which a <strong>fixed</strong> period membership or subscription begins. Example: A fixed period membership with Start Day set to Jan 01 means that membership periods would be 1/1/06 - 12/31/06 for anyone signing up during 2006."),
94 ],
95 'fixed_period_rollover_day' => [
96 'name' => 'fixed_period_rollover_day',
97 'description' => ts('Membership signups on or after this date cover the following calendar year as well. Example: If the rollover day is November 30, membership period for signups during December will cover the following year.'),
98 ],
99 'relationship_type_id' => [
100 'name' => 'relationship_type_id',
101 ],
102 'max_related' => [
103 'name' => 'max_related',
104 'description' => ts('Maximum number of related memberships (leave blank for unlimited).'),
105 ],
106 'visibility' => [
107 'name' => 'visibility',
108 'description' => ts("Can this membership type be used for self-service signups ('Public'), or is it only for CiviCRM users with 'Edit Contributions' permission ('Admin')."),
109 ],
110 'weight' => [
111 'name' => 'weight',
112 ],
113 'is_active' => [
114 'name' => 'is_active',
115 ],
116 ];
117
be2fb01f 118 if (!CRM_Financial_BAO_PaymentProcessor::hasPaymentProcessorSupporting(['Recurring'])) {
fc093e40 119 $this->entityFields['auto_renew']['not-auto-addable'] = TRUE;
120 $this->entityFields['auto_renew']['documentation_link'] = ['page' => 'user/contributions/payment-processors'];
121 }
2147e326
MW
122 }
123
124 /**
125 * Deletion message to be assigned to the form.
126 *
127 * @var string
128 */
129 protected $deleteMessage;
763f7a8a 130
131 /**
132 * Explicitly declare the entity api name.
133 */
134 public function getDefaultEntity() {
135 return 'MembershipType';
136 }
137
2147e326
MW
138 /**
139 * Set the delete message.
140 *
141 * We do this from the constructor in order to do a translation.
142 */
143 public function setDeleteMessage() {
144 $this->deleteMessage = ts('WARNING: Deleting this option will result in the loss of all membership records of this type.') . ts('This may mean the loss of a substantial amount of data, and the action cannot be undone.') . ts('Do you want to continue?');
145 }
146
763f7a8a 147 /**
148 * Explicitly declare the form context.
149 */
150 public function getDefaultContext() {
151 return 'create';
152 }
153
6a488035 154 /**
100fef9d 155 * Max number of contacts we will display for membership-organisation
6a488035 156 */
7da04cde 157 const MAX_CONTACTS = 50;
6a488035 158
00be9182 159 public function preProcess() {
481a74f4 160 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, 0);
6a488035
TO
161 $this->_BAOName = 'CRM_Member_BAO_MembershipType';
162 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add');
163 $this->assign('action', $this->_action);
164
165 $session = CRM_Core_Session::singleton();
166 $url = CRM_Utils_System::url('civicrm/admin/member/membershipType', 'reset=1');
167 $session->pushUserContext($url);
e2046b33
CW
168
169 $this->setPageTitle(ts('Membership Type'));
6a488035
TO
170 }
171
172 /**
c490a46a 173 * Set default values for the form. MobileProvider that in edit/view mode
6a488035
TO
174 * the default values are retrieved from the database
175 *
1322dc53
MW
176 * @return array
177 * defaults
6a488035
TO
178 */
179 public function setDefaultValues() {
180 $defaults = parent::setDefaultValues();
181
6a488035
TO
182 //finding default weight to be put
183 if (!isset($defaults['weight']) || (!$defaults['weight'])) {
184 $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Member_DAO_MembershipType');
185 }
186 //setting default relationshipType
187 if (isset($defaults['relationship_type_id'])) {
188 //$defaults['relationship_type_id'] = $defaults['relationship_type_id'].'_a_b';
189 // Set values for relation type select box
190 $relTypeIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaults['relationship_type_id']);
191 $relDirections = explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaults['relationship_direction']);
be2fb01f 192 $defaults['relationship_type_id'] = [];
6a488035
TO
193 foreach ($relTypeIds as $key => $value) {
194 $defaults['relationship_type_id'][] = $value . '_' . $relDirections[$key];
195 }
196 }
197
6a488035 198 //setting default fixed_period_start_day & fixed_period_rollover_day
be2fb01f 199 $periods = ['fixed_period_start_day', 'fixed_period_rollover_day'];
6a488035
TO
200 foreach ($periods as $per) {
201 if (isset($defaults[$per])) {
353ffa53 202 $date = $defaults[$per];
6a488035 203
be2fb01f 204 $defaults[$per] = [];
6a488035 205 if ($date > 31) {
353ffa53 206 $date = ($date < 999) ? '0' . $date : $date;
6a488035
TO
207 $defaults[$per]['M'] = substr($date, 0, 2);
208 $defaults[$per]['d'] = substr($date, 2, 3);
209 }
210 else {
211 //special case when only day is rollover and duration is month
212 $defaults['month_fixed_period_rollover_day']['d'] = $date;
213 }
214 }
215 }
216
217 return $defaults;
218 }
219
220 /**
fe482240 221 * Build the form object.
6a488035 222 *
355ba699 223 * @return void
a1ebf4b9
MW
224 * @throws \CRM_Core_Exception
225 * @throws \CiviCRM_API3_Exception
6a488035
TO
226 */
227 public function buildQuickForm() {
2147e326 228 self::buildQuickEntityForm();
6a488035
TO
229
230 if ($this->_action & CRM_Core_Action::DELETE) {
231 return;
232 }
c41292e8 233 // This is a temporary variable as we work towards moving over towards using the EntityField.tpl.
234 // Fields in this array have been tested & in the tpl have been switched over to metadata.
235 // Note this kinda 'works from the top' - ie. once we hit a field that needs some thought we need
236 // to stop & make that one work.
237 $this->assign('tpl_standardised_fields', ['name', 'description', 'member_of_contact_id', 'minimum_fee']);
6a488035 238
6a488035 239 $this->addRule('name', ts('A membership type with this name already exists. Please select another name.'),
be2fb01f 240 'objectExists', ['CRM_Member_DAO_MembershipType', $this->_id]
6a488035 241 );
6a488035
TO
242 $this->addRule('minimum_fee', ts('Please enter a monetary value for the Minimum Fee.'), 'money');
243
be2fb01f 244 $props = ['api' => ['params' => ['contact_type' => 'Organization']]];
fc791e90 245 $this->addEntityRef('member_of_contact_id', ts('Membership Organization'), $props, TRUE);
6a488035
TO
246
247 //start day
248 $this->add('date', 'fixed_period_start_day', ts('Fixed Period Start Day'),
249 CRM_Core_SelectValues::date(NULL, 'M d'), FALSE
250 );
251
54afd8a6 252 // Add Auto-renew options if we have a payment processor that supports recurring contributions
353ffa53 253 $isAuthorize = FALSE;
be2fb01f
CW
254 $options = [];
255 if (CRM_Financial_BAO_PaymentProcessor::hasPaymentProcessorSupporting(['Recurring'])) {
6a488035 256 $isAuthorize = TRUE;
dbd82592 257 $options = CRM_Core_SelectValues::memberAutoRenew();
6a488035
TO
258 }
259
260 $this->addRadio('auto_renew', ts('Auto-renew Option'), $options);
261 $this->assign('authorize', $isAuthorize);
262
a1ebf4b9 263 // rollover day
6a488035
TO
264 $this->add('date', 'fixed_period_rollover_day', ts('Fixed Period Rollover Day'),
265 CRM_Core_SelectValues::date(NULL, 'M d'), FALSE
266 );
267 $this->add('date', 'month_fixed_period_rollover_day', ts('Fixed Period Rollover Day'),
268 CRM_Core_SelectValues::date(NULL, 'd'), FALSE
269 );
481a74f4 270 $this->add('select', 'financial_type_id', ts('Financial Type'),
be2fb01f 271 ['' => ts('- select -')] + CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, $this->_action), TRUE, ['class' => 'crm-select2']
6a488035
TO
272 );
273
274 $relTypeInd = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE);
275 if (is_array($relTypeInd)) {
276 asort($relTypeInd);
277 }
baccd59e 278 $memberRel = $this->add('select', 'relationship_type_id', ts('Relationship Type'),
be2fb01f 279 $relTypeInd, FALSE, ['class' => 'crm-select2 huge', 'multiple' => 1]);
6a488035 280
be2fb01f 281 $this->addField('visibility', ['placeholder' => NULL, 'option_url' => NULL]);
6a488035
TO
282
283 $membershipRecords = FALSE;
284 if ($this->_action & CRM_Core_Action::UPDATE) {
be2fb01f 285 $result = civicrm_api3("Membership", "get", ["membership_type_id" => $this->_id, "options" => ["limit" => 1]]);
88d349ca 286 $membershipRecords = ($result["count"] > 0);
ff60ec7a 287 if ($membershipRecords) {
6a488035
TO
288 $memberRel->freeze();
289 }
290 }
291
292 $this->assign('membershipRecordsExists', $membershipRecords);
293
be2fb01f 294 $this->addFormRule(['CRM_Member_Form_MembershipType', 'formRule']);
6a488035
TO
295
296 $this->assign('membershipTypeId', $this->_id);
62d635fc 297 $this->assign('deferredFinancialType', Civi::settings()->get('deferred_revenue_enabled') ? array_keys(CRM_Financial_BAO_FinancialAccount::getDeferredFinancialType()) : NULL);
6a488035
TO
298 }
299
300 /**
fe482240 301 * Validation.
6a488035 302 *
b2363ea8
TO
303 * @param array $params
304 * (ref.) an assoc array of name/value pairs.
6a488035 305 *
72b3a70c
CW
306 * @return bool|array
307 * mixed true or array of errors
6a488035 308 */
00be9182 309 public static function formRule($params) {
be2fb01f 310 $errors = [];
6a488035
TO
311
312 if (!$params['name']) {
313 $errors['name'] = ts('Please enter a membership type name.');
314 }
315
481a74f4 316 if (($params['minimum_fee'] > 0) && !$params['financial_type_id']) {
6c68db9f 317 $errors['financial_type_id'] = ts('Please enter the financial Type.');
6a488035
TO
318 }
319
6a488035
TO
320 if (empty($params['duration_interval']) and $params['duration_unit'] != 'lifetime') {
321 $errors['duration_interval'] = ts('Please enter a duration interval.');
322 }
323
be2fb01f 324 if (in_array(CRM_Utils_Array::value('auto_renew', $params), [
353ffa53 325 1,
af9b09df 326 2,
be2fb01f 327 ])) {
6a488035
TO
328 if (($params['duration_interval'] > 1 && $params['duration_unit'] == 'year') ||
329 ($params['duration_interval'] > 12 && $params['duration_unit'] == 'month')
330 ) {
331 $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.');
332 }
333 }
334
6a488035
TO
335 if ($params['period_type'] == 'fixed' &&
336 $params['duration_unit'] == 'day'
337 ) {
338 $errors['period_type'] = ts('Period type should be Rolling when duration unit is Day');
339 }
340
341 if (($params['period_type'] == 'fixed') &&
342 ($params['duration_unit'] == 'year')
343 ) {
be2fb01f 344 $periods = ['fixed_period_start_day', 'fixed_period_rollover_day'];
6a488035
TO
345 foreach ($periods as $period) {
346 $month = $params[$period]['M'];
347 $date = $params[$period]['d'];
348 if (!$month || !$date) {
349 switch ($period) {
350 case 'fixed_period_start_day':
351 $errors[$period] = ts('Please enter a valid fixed period start day');
352 break;
353
354 case 'fixed_period_rollover_day':
355 $errors[$period] = ts('Please enter a valid fixed period rollover day');
356 break;
357 }
358 }
359 }
360 }
361
362 if ($params['fixed_period_start_day'] && !empty($params['fixed_period_start_day'])) {
42347843 363 $params['fixed_period_start_day']['Y'] = CRM_Utils_Time::date('Y');
6a488035
TO
364 if (!CRM_Utils_Rule::qfDate($params['fixed_period_start_day'])) {
365 $errors['fixed_period_start_day'] = ts('Please enter valid Fixed Period Start Day');
366 }
367 }
368
369 if ($params['fixed_period_rollover_day'] && !empty($params['fixed_period_rollover_day'])) {
42347843 370 $params['fixed_period_rollover_day']['Y'] = CRM_Utils_Time::date('Y');
6a488035
TO
371 if (!CRM_Utils_Rule::qfDate($params['fixed_period_rollover_day'])) {
372 $errors['fixed_period_rollover_day'] = ts('Please enter valid Fixed Period Rollover Day');
373 }
374 }
375
376 return empty($errors) ? TRUE : $errors;
377 }
378
379 /**
fe482240 380 * Process the form submission.
6a488035 381 *
355ba699 382 * @return void
6a488035
TO
383 */
384 public function postProcess() {
385 if ($this->_action & CRM_Core_Action::DELETE) {
92e4c2a5 386 try {
353ffa53 387 CRM_Member_BAO_MembershipType::del($this->_id);
dcc4f6a7 388 }
353ffa53 389 catch (CRM_Core_Exception $e) {
dcc4f6a7 390 CRM_Core_Error::statusBounce($e->getMessage(), NULL, ts('Membership Type Not Deleted'));
391 }
6a488035
TO
392 CRM_Core_Session::setStatus(ts('Selected membership type has been deleted.'), ts('Record Deleted'), 'success');
393 }
394 else {
2147e326 395 $params = $this->exportValues();
6a488035 396
6a488035
TO
397 if ($params['minimum_fee']) {
398 $params['minimum_fee'] = CRM_Utils_Rule::cleanMoney($params['minimum_fee']);
399 }
400
401 $hasRelTypeVal = FALSE;
2147e326 402 if (!CRM_Utils_System::isNull($params['relationship_type_id'])) {
6a488035 403 // To insert relation ids and directions with value separator
2147e326 404 $relTypeDirs = $params['relationship_type_id'];
be2fb01f 405 $relIds = $relDirection = [];
6a488035
TO
406 foreach ($relTypeDirs as $key => $value) {
407 $relationId = explode('_', $value);
408 if (count($relationId) == 3 &&
409 is_numeric($relationId[0])
410 ) {
411 $relIds[] = $relationId[0];
412 $relDirection[] = $relationId[1] . '_' . $relationId[2];
413 }
414 }
415 if (!empty($relIds)) {
416 $hasRelTypeVal = TRUE;
417 $params['relationship_type_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $relIds);
418 $params['relationship_direction'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $relDirection);
419 }
420 }
421 if (!$hasRelTypeVal) {
1322dc53 422 $params['relationship_type_id'] = $params['relationship_direction'] = $params['max_related'] = 'null';
6a488035
TO
423 }
424
425 if ($params['duration_unit'] == 'lifetime' &&
426 empty($params['duration_interval'])
427 ) {
428 $params['duration_interval'] = 1;
429 }
430
be2fb01f 431 $periods = ['fixed_period_start_day', 'fixed_period_rollover_day'];
1322dc53
MW
432 foreach ($periods as $period) {
433 if (!empty($params[$period]['M']) && !empty($params[$period]['d'])) {
434 $mon = $params[$period]['M'];
435 $dat = $params[$period]['d'];
353ffa53
TO
436 $mon = ($mon < 10) ? '0' . $mon : $mon;
437 $dat = ($dat < 10) ? '0' . $dat : $dat;
1322dc53 438 $params[$period] = $mon . $dat;
6a488035 439 }
1322dc53 440 elseif ($period == 'fixed_period_rollover_day' && !empty($params['month_fixed_period_rollover_day'])) {
6a488035
TO
441 $params['fixed_period_rollover_day'] = $params['month_fixed_period_rollover_day']['d'];
442 unset($params['month_fixed_period_rollover_day']);
443 }
444 else {
1322dc53 445 $params[$period] = 'null';
6a488035
TO
446 }
447 }
448 $oldWeight = NULL;
449
450 if ($this->_id) {
451 $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType',
452 $this->_id, 'weight', 'id'
453 );
454 }
455 $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Member_DAO_MembershipType',
456 $oldWeight, $params['weight']
457 );
458
459 if ($this->_action & CRM_Core_Action::UPDATE) {
1322dc53 460 $params['id'] = $this->_id;
6a488035
TO
461 }
462
2147e326
MW
463 $membershipTypeResult = civicrm_api3('MembershipType', 'create', $params);
464 $membershipTypeName = $membershipTypeResult['values'][$membershipTypeResult['id']]['name'];
6a488035 465
2147e326 466 CRM_Core_Session::setStatus(ts("The membership type '%1' has been saved.",
be2fb01f 467 [1 => $membershipTypeName]
353ffa53 468 ), ts('Saved'), 'success');
6a488035 469 $session = CRM_Core_Session::singleton();
2147e326 470 $buttonName = $this->controller->getButtonName();
6a488035 471 if ($buttonName == $this->getButtonName('upload', 'new')) {
60970bf5
DL
472 $session->replaceUserContext(
473 CRM_Utils_System::url('civicrm/admin/member/membershipType/add', 'action=add&reset=1')
6a488035
TO
474 );
475 }
476 }
477 }
478
bb3a214a 479 /**
100fef9d
CW
480 * @param int $previousID
481 * @param int $priceSetId
482 * @param int $membershipTypeId
bb3a214a
EM
483 * @param $optionsIds
484 */
6975b8a7 485 public static function checkPreviousPriceField($previousID, $priceSetId, $membershipTypeId, &$optionsIds) {
6a488035 486 if ($previousID) {
be2fb01f 487 $editedFieldParams = [
6a488035
TO
488 'price_set_id ' => $priceSetId,
489 'name' => $previousID,
be2fb01f
CW
490 ];
491 $editedResults = [];
9da8dc8c 492 CRM_Price_BAO_PriceField::retrieve($editedFieldParams, $editedResults);
6a488035 493 if (!empty($editedResults)) {
be2fb01f 494 $editedFieldParams = [
6a488035
TO
495 'price_field_id' => $editedResults['id'],
496 'membership_type_id' => $membershipTypeId,
be2fb01f
CW
497 ];
498 $editedResults = [];
9da8dc8c 499 CRM_Price_BAO_PriceFieldValue::retrieve($editedFieldParams, $editedResults);
9c1bc317 500 $optionsIds['option_id'][1] = $editedResults['id'] ?? NULL;
6a488035
TO
501 }
502 }
503 }
96025800 504
6a488035 505}