Merge pull request #15861 from JMAConsulting/core-1398
[civicrm-core.git] / CRM / Member / Form / MembershipStatus.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
6a488035 20 */
3a0ba1c5 21class CRM_Member_Form_MembershipStatus extends CRM_Core_Form {
6a488035 22
3a0ba1c5 23 use CRM_Core_Form_EntityFormTrait;
763f7a8a 24
25 /**
26 * Explicitly declare the entity api name.
27 */
28 public function getDefaultEntity() {
29 return 'MembershipStatus';
30 }
31
32 /**
33 * Explicitly declare the form context.
34 */
35 public function getDefaultContext() {
36 return 'create';
37 }
38
3a0ba1c5 39 /**
40 * Fields for the entity to be assigned to the template.
41 *
42 * Fields may have keys
43 * - name (required to show in tpl from the array)
44 * - description (optional, will appear below the field)
45 * - not-auto-addable - this class will not attempt to add the field using addField.
46 * (this will be automatically set if the field does not have html in it's metadata
47 * or is not a core field on the form's entity).
48 * - help (optional) add help to the field - e.g ['id' => 'id-source', 'file' => 'CRM/Contact/Form/Contact']]
49 * - template - use a field specific template to render this field
a2fdee87 50 * - required
3a0ba1c5 51 * @var array
52 */
53 protected $entityFields = [];
54
55 /**
56 * Set entity fields to be assigned to the form.
57 */
58 protected function setEntityFields() {
59 $this->entityFields = [
60 'label' => [
61 'name' => 'label',
62 'description' => ts("Display name for this Membership status (e.g. New, Current, Grace, Expired...)."),
63 'required' => TRUE,
64 ],
65 'is_admin' => [
66 'name' => 'is_admin',
67 'description' => ts("Check this box if this status is for use by administrative staff only. If checked, this status is never automatically assigned by CiviMember. It is assigned to a contact's Membership by checking the <strong>Status Override</strong> flag when adding or editing the Membership record. Start and End Event settings are ignored for Administrator statuses. EXAMPLE: This setting can be useful for special case statuses like 'Non-expiring', 'Barred' or 'Expelled', etc."),
68 ],
69 ];
70 }
71
72 /**
73 * Set the delete message.
74 *
75 * We do this from the constructor in order to do a translation.
76 */
77 public function setDeleteMessage() {
971e129b 78 $this->deleteMessage = ts('You will not be able to delete this membership status if there are existing memberships with this status. You will need to check all your membership status rules afterwards to ensure that a valid status will always be available.') . " " . ts('Do you want to continue?');
3a0ba1c5 79 }
80
81 public function preProcess() {
82 $this->_id = $this->get('id');
83 $this->_BAOName = 'CRM_Member_BAO_MembershipStatus';
84 }
85
6a488035 86 /**
c490a46a 87 * Set default values for the form. MobileProvider that in edit/view mode
6a488035
TO
88 * the default values are retrieved from the database
89 *
3a0ba1c5 90 * @return array
6a488035
TO
91 */
92 public function setDefaultValues() {
612215eb 93 $defaults = $this->getEntityDefaults();
3a0ba1c5 94
95 if ($this->_action & CRM_Core_Action::ADD) {
96 $defaults['is_active'] = 1;
97 }
6a488035
TO
98
99 //finding default weight to be put
a7488080 100 if (empty($defaults['weight'])) {
6a488035
TO
101 $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Member_DAO_MembershipStatus');
102 }
103 return $defaults;
104 }
105
106 /**
fe482240 107 * Build the form object.
6a488035
TO
108 */
109 public function buildQuickForm() {
3a0ba1c5 110 self::buildQuickEntityForm();
6a488035
TO
111 parent::buildQuickForm();
112
113 if ($this->_action & CRM_Core_Action::DELETE) {
114 return;
115 }
116
6a488035
TO
117 if ($this->_id) {
118 $name = $this->add('text', 'name', ts('Name'),
119 CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipStatus', 'name')
120 );
121 $name->freeze();
122 $this->assign('id', $this->_id);
123 }
6a488035 124 $this->addRule('label', ts('A membership status with this label already exists. Please select another label.'),
be2fb01f 125 'objectExists', ['CRM_Member_DAO_MembershipStatus', $this->_id, 'name']
6a488035
TO
126 );
127
128 $this->add('select', 'start_event', ts('Start Event'), CRM_Core_SelectValues::eventDate(), TRUE);
be2fb01f 129 $this->add('select', 'start_event_adjust_unit', ts('Start Event Adjustment'), ['' => ts('- select -')] + CRM_Core_SelectValues::unitList());
6a488035
TO
130 $this->add('text', 'start_event_adjust_interval', ts('Start Event Adjust Interval'),
131 CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipStatus', 'start_event_adjust_interval')
132 );
be2fb01f
CW
133 $this->add('select', 'end_event', ts('End Event'), ['' => ts('- select -')] + CRM_Core_SelectValues::eventDate());
134 $this->add('select', 'end_event_adjust_unit', ts('End Event Adjustment'), ['' => ts('- select -')] + CRM_Core_SelectValues::unitList());
6a488035
TO
135 $this->add('text', 'end_event_adjust_interval', ts('End Event Adjust Interval'),
136 CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipStatus', 'end_event_adjust_interval')
137 );
138 $this->add('checkbox', 'is_current_member', ts('Current Membership?'));
6a488035 139
f20d2b0d 140 $this->add('number', 'weight', ts('Order'),
6a488035
TO
141 CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipStatus', 'weight')
142 );
143 $this->add('checkbox', 'is_default', ts('Default?'));
144 $this->add('checkbox', 'is_active', ts('Enabled?'));
145 }
146
147 /**
fe482240 148 * Process the form submission.
6a488035
TO
149 */
150 public function postProcess() {
151 if ($this->_action & CRM_Core_Action::DELETE) {
92e4c2a5 152 try {
dcc4f6a7 153 CRM_Member_BAO_MembershipStatus::del($this->_id);
154 }
353ffa53 155 catch (CRM_Core_Exception $e) {
dcc4f6a7 156 CRM_Core_Error::statusBounce($e->getMessage(), NULL, ts('Delete Failed'));
157 }
6a488035
TO
158 CRM_Core_Session::setStatus(ts('Selected membership status has been deleted.'), ts('Record Deleted'), 'success');
159 }
160 else {
6a488035
TO
161 // store the submitted values in an array
162 $params = $this->exportValues();
d4cab3e4 163 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
164 $params['is_current_member'] = CRM_Utils_Array::value('is_current_member', $params, FALSE);
165 $params['is_admin'] = CRM_Utils_Array::value('is_admin', $params, FALSE);
166 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
6a488035
TO
167
168 if ($this->_action & CRM_Core_Action::UPDATE) {
3a0ba1c5 169 $params['id'] = $this->getEntityId();
6a488035
TO
170 }
171 $oldWeight = NULL;
172 if ($this->_id) {
173 $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', $this->_id, 'weight', 'id');
174 }
175 $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Member_DAO_MembershipStatus', $oldWeight, $params['weight']);
176
177 // only for add mode, set label to name.
178 if ($this->_action & CRM_Core_Action::ADD) {
179 $params['name'] = $params['label'];
180 }
181
3a0ba1c5 182 $membershipStatus = CRM_Member_BAO_MembershipStatus::add($params);
6a488035 183 CRM_Core_Session::setStatus(ts('The membership status \'%1\' has been saved.',
be2fb01f 184 [1 => $membershipStatus->label]
353ffa53 185 ), ts('Saved'), 'success');
6a488035
TO
186 }
187 }
96025800 188
6a488035 189}