3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
35 * This class generates form components for Membership Type
37 class CRM_Member_Form_MembershipStatus
extends CRM_Core_Form
{
39 use CRM_Core_Form_EntityFormTrait
;
42 * Explicitly declare the entity api name.
44 public function getDefaultEntity() {
45 return 'MembershipStatus';
49 * Explicitly declare the form context.
51 public function getDefaultContext() {
56 * Fields for the entity to be assigned to the template.
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
69 protected $entityFields = [];
72 * Set entity fields to be assigned to the form.
74 protected function setEntityFields() {
75 $this->entityFields
= [
78 'description' => ts("Display name for this Membership status (e.g. New, Current, Grace, Expired...)."),
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."),
89 * Set the delete message.
91 * We do this from the constructor in order to do a translation.
93 public function setDeleteMessage() {
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?');
97 public function preProcess() {
98 $this->_id
= $this->get('id');
99 $this->_BAOName
= 'CRM_Member_BAO_MembershipStatus';
103 * Set default values for the form. MobileProvider that in edit/view mode
104 * the default values are retrieved from the database
108 public function setDefaultValues() {
109 $defaults = $this->getEntityDefaults();
111 if ($this->_action
& CRM_Core_Action
::ADD
) {
112 $defaults['is_active'] = 1;
115 //finding default weight to be put
116 if (empty($defaults['weight'])) {
117 $defaults['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Member_DAO_MembershipStatus');
123 * Build the form object.
125 public function buildQuickForm() {
126 self
::buildQuickEntityForm();
127 parent
::buildQuickForm();
129 if ($this->_action
& CRM_Core_Action
::DELETE
) {
134 $name = $this->add('text', 'name', ts('Name'),
135 CRM_Core_DAO
::getAttribute('CRM_Member_DAO_MembershipStatus', 'name')
138 $this->assign('id', $this->_id
);
140 $this->addRule('label', ts('A membership status with this label already exists. Please select another label.'),
141 'objectExists', array('CRM_Member_DAO_MembershipStatus', $this->_id
, 'name')
144 $this->add('select', 'start_event', ts('Start Event'), CRM_Core_SelectValues
::eventDate(), TRUE);
145 $this->add('select', 'start_event_adjust_unit', ts('Start Event Adjustment'), array('' => ts('- select -')) + CRM_Core_SelectValues
::unitList());
146 $this->add('text', 'start_event_adjust_interval', ts('Start Event Adjust Interval'),
147 CRM_Core_DAO
::getAttribute('CRM_Member_DAO_MembershipStatus', 'start_event_adjust_interval')
149 $this->add('select', 'end_event', ts('End Event'), array('' => ts('- select -')) + CRM_Core_SelectValues
::eventDate());
150 $this->add('select', 'end_event_adjust_unit', ts('End Event Adjustment'), array('' => ts('- select -')) + CRM_Core_SelectValues
::unitList());
151 $this->add('text', 'end_event_adjust_interval', ts('End Event Adjust Interval'),
152 CRM_Core_DAO
::getAttribute('CRM_Member_DAO_MembershipStatus', 'end_event_adjust_interval')
154 $this->add('checkbox', 'is_current_member', ts('Current Membership?'));
156 $this->add('text', 'weight', ts('Order'),
157 CRM_Core_DAO
::getAttribute('CRM_Member_DAO_MembershipStatus', 'weight')
159 $this->add('checkbox', 'is_default', ts('Default?'));
160 $this->add('checkbox', 'is_active', ts('Enabled?'));
164 * Process the form submission.
166 public function postProcess() {
167 if ($this->_action
& CRM_Core_Action
::DELETE
) {
169 CRM_Member_BAO_MembershipStatus
::del($this->_id
);
171 catch (CRM_Core_Exception
$e) {
172 CRM_Core_Error
::statusBounce($e->getMessage(), NULL, ts('Delete Failed'));
174 CRM_Core_Session
::setStatus(ts('Selected membership status has been deleted.'), ts('Record Deleted'), 'success');
177 // store the submitted values in an array
178 $params = $this->exportValues();
179 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
180 $params['is_current_member'] = CRM_Utils_Array
::value('is_current_member', $params, FALSE);
181 $params['is_admin'] = CRM_Utils_Array
::value('is_admin', $params, FALSE);
182 $params['is_default'] = CRM_Utils_Array
::value('is_default', $params, FALSE);
184 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
185 $params['id'] = $this->getEntityId();
189 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipStatus', $this->_id
, 'weight', 'id');
191 $params['weight'] = CRM_Utils_Weight
::updateOtherWeights('CRM_Member_DAO_MembershipStatus', $oldWeight, $params['weight']);
193 // only for add mode, set label to name.
194 if ($this->_action
& CRM_Core_Action
::ADD
) {
195 $params['name'] = $params['label'];
198 $membershipStatus = CRM_Member_BAO_MembershipStatus
::add($params);
199 CRM_Core_Session
::setStatus(ts('The membership status \'%1\' has been saved.',
200 array(1 => $membershipStatus->label
)
201 ), ts('Saved'), 'success');