Merge pull request #12091 from eileenmcnaughton/paypal_exp
[civicrm-core.git] / CRM / Member / Form / MembershipStatus.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
32 */
33
34/**
35 * This class generates form components for Membership Type
6a488035 36 */
3a0ba1c5 37class CRM_Member_Form_MembershipStatus extends CRM_Core_Form {
6a488035 38
3a0ba1c5 39 use CRM_Core_Form_EntityFormTrait;
763f7a8a 40
41 /**
42 * Explicitly declare the entity api name.
43 */
44 public function getDefaultEntity() {
45 return 'MembershipStatus';
46 }
47
48 /**
49 * Explicitly declare the form context.
50 */
51 public function getDefaultContext() {
52 return 'create';
53 }
54
3a0ba1c5 55 /**
56 * Fields for the entity to be assigned to the template.
57 *
58 * Fields may have keys
59 * - name (required to show in tpl from the array)
60 * - description (optional, will appear below the field)
61 * - not-auto-addable - this class will not attempt to add the field using addField.
62 * (this will be automatically set if the field does not have html in it's metadata
63 * or is not a core field on the form's entity).
64 * - help (optional) add help to the field - e.g ['id' => 'id-source', 'file' => 'CRM/Contact/Form/Contact']]
65 * - template - use a field specific template to render this field
a2fdee87 66 * - required
3a0ba1c5 67 * @var array
68 */
69 protected $entityFields = [];
70
71 /**
72 * Set entity fields to be assigned to the form.
73 */
74 protected function setEntityFields() {
75 $this->entityFields = [
76 'label' => [
77 'name' => 'label',
78 'description' => ts("Display name for this Membership status (e.g. New, Current, Grace, Expired...)."),
79 'required' => TRUE,
80 ],
81 'is_admin' => [
82 'name' => 'is_admin',
83 '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."),
84 ],
85 ];
86 }
87
88 /**
89 * Set the delete message.
90 *
91 * We do this from the constructor in order to do a translation.
92 */
93 public function setDeleteMessage() {
a2fdee87 94 $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 95 }
96
97 public function preProcess() {
98 $this->_id = $this->get('id');
99 $this->_BAOName = 'CRM_Member_BAO_MembershipStatus';
100 }
101
6a488035 102 /**
c490a46a 103 * Set default values for the form. MobileProvider that in edit/view mode
6a488035
TO
104 * the default values are retrieved from the database
105 *
3a0ba1c5 106 * @return array
6a488035
TO
107 */
108 public function setDefaultValues() {
3a0ba1c5 109 $defaults = array();
110
111 if ($this->getEntityId()) {
112 $params = array('id' => $this->getEntityId());
113 $baoName = $this->_BAOName;
114 $baoName::retrieve($params, $defaults);
115 }
116
117 if ($this->_action & CRM_Core_Action::ADD) {
118 $defaults['is_active'] = 1;
119 }
6a488035
TO
120
121 //finding default weight to be put
a7488080 122 if (empty($defaults['weight'])) {
6a488035
TO
123 $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Member_DAO_MembershipStatus');
124 }
125 return $defaults;
126 }
127
128 /**
fe482240 129 * Build the form object.
6a488035
TO
130 */
131 public function buildQuickForm() {
3a0ba1c5 132 self::buildQuickEntityForm();
6a488035
TO
133 parent::buildQuickForm();
134
135 if ($this->_action & CRM_Core_Action::DELETE) {
136 return;
137 }
138
6a488035
TO
139 if ($this->_id) {
140 $name = $this->add('text', 'name', ts('Name'),
141 CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipStatus', 'name')
142 );
143 $name->freeze();
144 $this->assign('id', $this->_id);
145 }
6a488035
TO
146 $this->addRule('label', ts('A membership status with this label already exists. Please select another label.'),
147 'objectExists', array('CRM_Member_DAO_MembershipStatus', $this->_id, 'name')
148 );
149
150 $this->add('select', 'start_event', ts('Start Event'), CRM_Core_SelectValues::eventDate(), TRUE);
6795600d 151 $this->add('select', 'start_event_adjust_unit', ts('Start Event Adjustment'), array('' => ts('- select -')) + CRM_Core_SelectValues::unitList());
6a488035
TO
152 $this->add('text', 'start_event_adjust_interval', ts('Start Event Adjust Interval'),
153 CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipStatus', 'start_event_adjust_interval')
154 );
6795600d
CW
155 $this->add('select', 'end_event', ts('End Event'), array('' => ts('- select -')) + CRM_Core_SelectValues::eventDate());
156 $this->add('select', 'end_event_adjust_unit', ts('End Event Adjustment'), array('' => ts('- select -')) + CRM_Core_SelectValues::unitList());
6a488035
TO
157 $this->add('text', 'end_event_adjust_interval', ts('End Event Adjust Interval'),
158 CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipStatus', 'end_event_adjust_interval')
159 );
160 $this->add('checkbox', 'is_current_member', ts('Current Membership?'));
6a488035 161
7ecddde4 162 $this->add('text', 'weight', ts('Order'),
6a488035
TO
163 CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipStatus', 'weight')
164 );
165 $this->add('checkbox', 'is_default', ts('Default?'));
166 $this->add('checkbox', 'is_active', ts('Enabled?'));
167 }
168
169 /**
fe482240 170 * Process the form submission.
6a488035
TO
171 */
172 public function postProcess() {
173 if ($this->_action & CRM_Core_Action::DELETE) {
92e4c2a5 174 try {
dcc4f6a7 175 CRM_Member_BAO_MembershipStatus::del($this->_id);
176 }
353ffa53 177 catch (CRM_Core_Exception $e) {
dcc4f6a7 178 CRM_Core_Error::statusBounce($e->getMessage(), NULL, ts('Delete Failed'));
179 }
6a488035
TO
180 CRM_Core_Session::setStatus(ts('Selected membership status has been deleted.'), ts('Record Deleted'), 'success');
181 }
182 else {
6a488035
TO
183 // store the submitted values in an array
184 $params = $this->exportValues();
d4cab3e4 185 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
186 $params['is_current_member'] = CRM_Utils_Array::value('is_current_member', $params, FALSE);
187 $params['is_admin'] = CRM_Utils_Array::value('is_admin', $params, FALSE);
188 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
6a488035
TO
189
190 if ($this->_action & CRM_Core_Action::UPDATE) {
3a0ba1c5 191 $params['id'] = $this->getEntityId();
6a488035
TO
192 }
193 $oldWeight = NULL;
194 if ($this->_id) {
195 $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', $this->_id, 'weight', 'id');
196 }
197 $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Member_DAO_MembershipStatus', $oldWeight, $params['weight']);
198
199 // only for add mode, set label to name.
200 if ($this->_action & CRM_Core_Action::ADD) {
201 $params['name'] = $params['label'];
202 }
203
3a0ba1c5 204 $membershipStatus = CRM_Member_BAO_MembershipStatus::add($params);
6a488035 205 CRM_Core_Session::setStatus(ts('The membership status \'%1\' has been saved.',
353ffa53
TO
206 array(1 => $membershipStatus->label)
207 ), ts('Saved'), 'success');
6a488035
TO
208 }
209 }
96025800 210
6a488035 211}