3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for Membership Type
21 class CRM_Member_Form_MembershipStatus
extends CRM_Core_Form
{
23 use CRM_Core_Form_EntityFormTrait
;
26 * Explicitly declare the entity api name.
28 public function getDefaultEntity() {
29 return 'MembershipStatus';
33 * Explicitly declare the form context.
35 public function getDefaultContext() {
40 * Fields for the entity to be assigned to the template.
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
53 protected $entityFields = [];
56 * Set entity fields to be assigned to the form.
58 protected function setEntityFields() {
59 $this->entityFields
= [
62 'description' => ts("Display name for this Membership status (e.g. New, Current, Grace, Expired...)."),
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."),
73 * Set the delete message.
75 * We do this from the constructor in order to do a translation.
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?');
81 public function preProcess() {
82 $this->_id
= $this->get('id');
83 $this->_BAOName
= 'CRM_Member_BAO_MembershipStatus';
87 * Set default values for the form. MobileProvider that in edit/view mode
88 * the default values are retrieved from the database
92 public function setDefaultValues() {
93 $defaults = $this->getEntityDefaults();
95 if ($this->_action
& CRM_Core_Action
::ADD
) {
96 $defaults['is_active'] = 1;
99 //finding default weight to be put
100 if (empty($defaults['weight'])) {
101 $defaults['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Member_DAO_MembershipStatus');
107 * Build the form object.
109 public function buildQuickForm() {
110 self
::buildQuickEntityForm();
111 parent
::buildQuickForm();
113 if ($this->_action
& CRM_Core_Action
::DELETE
) {
118 $name = $this->add('text', 'name', ts('Name'),
119 CRM_Core_DAO
::getAttribute('CRM_Member_DAO_MembershipStatus', 'name')
122 $this->assign('id', $this->_id
);
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']
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')
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')
138 $this->add('checkbox', 'is_current_member', ts('Current Membership?'));
140 $this->add('number', 'weight', ts('Order'),
141 CRM_Core_DAO
::getAttribute('CRM_Member_DAO_MembershipStatus', 'weight')
143 $this->add('checkbox', 'is_default', ts('Default?'));
144 $this->add('checkbox', 'is_active', ts('Enabled?'));
148 * Process the form submission.
150 public function postProcess() {
151 if ($this->_action
& CRM_Core_Action
::DELETE
) {
153 CRM_Member_BAO_MembershipStatus
::del($this->_id
);
155 catch (CRM_Core_Exception
$e) {
156 CRM_Core_Error
::statusBounce($e->getMessage(), NULL, ts('Delete Failed'));
158 CRM_Core_Session
::setStatus(ts('Selected membership status has been deleted.'), ts('Record Deleted'), 'success');
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);
168 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
169 $params['id'] = $this->getEntityId();
173 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipStatus', $this->_id
, 'weight', 'id');
175 $params['weight'] = CRM_Utils_Weight
::updateOtherWeights('CRM_Member_DAO_MembershipStatus', $oldWeight, $params['weight']);
177 // only for add mode, set label to name.
178 if ($this->_action
& CRM_Core_Action
::ADD
) {
179 $params['name'] = $params['label'];
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');