Merge pull request #19116 from eileenmcnaughton/pay_edit
[civicrm-core.git] / CRM / Member / Form / MembershipStatus.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class generates form components for Membership Type
20 */
21 class CRM_Member_Form_MembershipStatus extends CRM_Core_Form {
22
23 use CRM_Core_Form_EntityFormTrait;
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
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
50 * - required
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() {
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?');
79 }
80
81 public function preProcess() {
82 $this->_id = $this->get('id');
83 $this->_BAOName = 'CRM_Member_BAO_MembershipStatus';
84 }
85
86 /**
87 * Set default values for the form. MobileProvider that in edit/view mode
88 * the default values are retrieved from the database
89 *
90 * @return array
91 */
92 public function setDefaultValues() {
93 $defaults = $this->getEntityDefaults();
94
95 if ($this->_action & CRM_Core_Action::ADD) {
96 $defaults['is_active'] = 1;
97 }
98
99 //finding default weight to be put
100 if (empty($defaults['weight'])) {
101 $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Member_DAO_MembershipStatus');
102 }
103 return $defaults;
104 }
105
106 /**
107 * Build the form object.
108 */
109 public function buildQuickForm() {
110 self::buildQuickEntityForm();
111 parent::buildQuickForm();
112
113 if ($this->_action & CRM_Core_Action::DELETE) {
114 return;
115 }
116
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 }
124 $this->addRule('label', ts('A membership status with this label already exists. Please select another label.'),
125 'objectExists', ['CRM_Member_DAO_MembershipStatus', $this->_id, 'name']
126 );
127
128 $this->add('select', 'start_event', ts('Start Event'), CRM_Core_SelectValues::eventDate(), TRUE);
129 $this->add('select', 'start_event_adjust_unit', ts('Start Event Adjustment'), ['' => ts('- select -')] + CRM_Core_SelectValues::unitList());
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 );
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());
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?'));
139
140 $this->add('number', 'weight', ts('Order'),
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 /**
148 * Process the form submission.
149 */
150 public function postProcess() {
151 if ($this->_action & CRM_Core_Action::DELETE) {
152 try {
153 CRM_Member_BAO_MembershipStatus::del($this->_id);
154 }
155 catch (CRM_Core_Exception $e) {
156 CRM_Core_Error::statusBounce($e->getMessage(), NULL, ts('Delete Failed'));
157 }
158 CRM_Core_Session::setStatus(ts('Selected membership status has been deleted.'), ts('Record Deleted'), 'success');
159 }
160 else {
161 // store the submitted values in an array
162 $params = $this->exportValues();
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);
167
168 if ($this->_action & CRM_Core_Action::UPDATE) {
169 $params['id'] = $this->getEntityId();
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
182 $membershipStatus = CRM_Member_BAO_MembershipStatus::add($params);
183 CRM_Core_Session::setStatus(ts('The membership status \'%1\' has been saved.',
184 [1 => $membershipStatus->label]
185 ), ts('Saved'), 'success');
186 }
187 }
188
189 }