Merge pull request #12113 from totten/JMAConsulting-dev-drupal-10
[civicrm-core.git] / CRM / Member / Form / MembershipStatus.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2018
32 */
33
34 /**
35 * This class generates form components for Membership Type
36 */
37 class CRM_Member_Form_MembershipStatus extends CRM_Member_Form_MembershipConfig {
38
39
40 /**
41 * Explicitly declare the entity api name.
42 */
43 public function getDefaultEntity() {
44 return 'MembershipStatus';
45 }
46
47 /**
48 * Explicitly declare the form context.
49 */
50 public function getDefaultContext() {
51 return 'create';
52 }
53
54 /**
55 * Set default values for the form. MobileProvider that in edit/view mode
56 * the default values are retrieved from the database
57 *
58 *
59 * @return void
60 */
61 public function setDefaultValues() {
62 $defaults = parent::setDefaultValues();
63
64 //finding default weight to be put
65 if (empty($defaults['weight'])) {
66 $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Member_DAO_MembershipStatus');
67 }
68 return $defaults;
69 }
70
71 /**
72 * Build the form object.
73 *
74 * @return void
75 */
76 public function buildQuickForm() {
77 parent::buildQuickForm();
78
79 if ($this->_action & CRM_Core_Action::DELETE) {
80 return;
81 }
82
83 $this->applyFilter('__ALL__', 'trim');
84
85 if ($this->_id) {
86 $name = $this->add('text', 'name', ts('Name'),
87 CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipStatus', 'name')
88 );
89 $name->freeze();
90 $this->assign('id', $this->_id);
91 }
92 $this->add('text', 'label', ts('Label'),
93 CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipStatus', 'label'), TRUE
94 );
95 $this->addRule('label', ts('A membership status with this label already exists. Please select another label.'),
96 'objectExists', array('CRM_Member_DAO_MembershipStatus', $this->_id, 'name')
97 );
98
99 $this->add('select', 'start_event', ts('Start Event'), CRM_Core_SelectValues::eventDate(), TRUE);
100 $this->add('select', 'start_event_adjust_unit', ts('Start Event Adjustment'), array('' => ts('- select -')) + CRM_Core_SelectValues::unitList());
101 $this->add('text', 'start_event_adjust_interval', ts('Start Event Adjust Interval'),
102 CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipStatus', 'start_event_adjust_interval')
103 );
104 $this->add('select', 'end_event', ts('End Event'), array('' => ts('- select -')) + CRM_Core_SelectValues::eventDate());
105 $this->add('select', 'end_event_adjust_unit', ts('End Event Adjustment'), array('' => ts('- select -')) + CRM_Core_SelectValues::unitList());
106 $this->add('text', 'end_event_adjust_interval', ts('End Event Adjust Interval'),
107 CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipStatus', 'end_event_adjust_interval')
108 );
109 $this->add('checkbox', 'is_current_member', ts('Current Membership?'));
110 $this->add('checkbox', 'is_admin', ts('Administrator Only?'));
111
112 $this->add('text', 'weight', ts('Order'),
113 CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipStatus', 'weight')
114 );
115 $this->add('checkbox', 'is_default', ts('Default?'));
116 $this->add('checkbox', 'is_active', ts('Enabled?'));
117 }
118
119 /**
120 * Process the form submission.
121 *
122 *
123 * @return void
124 */
125 public function postProcess() {
126 if ($this->_action & CRM_Core_Action::DELETE) {
127 try {
128 CRM_Member_BAO_MembershipStatus::del($this->_id);
129 }
130 catch (CRM_Core_Exception $e) {
131 CRM_Core_Error::statusBounce($e->getMessage(), NULL, ts('Delete Failed'));
132 }
133 CRM_Core_Session::setStatus(ts('Selected membership status has been deleted.'), ts('Record Deleted'), 'success');
134 }
135 else {
136 $params = $ids = array();
137 // store the submitted values in an array
138 $params = $this->exportValues();
139 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
140 $params['is_current_member'] = CRM_Utils_Array::value('is_current_member', $params, FALSE);
141 $params['is_admin'] = CRM_Utils_Array::value('is_admin', $params, FALSE);
142 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
143
144 if ($this->_action & CRM_Core_Action::UPDATE) {
145 $ids['membershipStatus'] = $this->_id;
146 }
147 $oldWeight = NULL;
148 if ($this->_id) {
149 $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', $this->_id, 'weight', 'id');
150 }
151 $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Member_DAO_MembershipStatus', $oldWeight, $params['weight']);
152
153 // only for add mode, set label to name.
154 if ($this->_action & CRM_Core_Action::ADD) {
155 $params['name'] = $params['label'];
156 }
157
158 $membershipStatus = CRM_Member_BAO_MembershipStatus::add($params, $ids);
159 CRM_Core_Session::setStatus(ts('The membership status \'%1\' has been saved.',
160 array(1 => $membershipStatus->label)
161 ), ts('Saved'), 'success');
162 }
163 }
164
165 }