fix header
[civicrm-core.git] / CRM / Price / Form / Set.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
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 * Form to process actions on Price Sets.
36 */
37 class CRM_Price_Form_Set extends CRM_Core_Form {
38
39 /**
40 * The set id saved to the session for an update.
41 *
42 * @var int
43 */
44 protected $_sid;
45
46 /**
47 * Set variables up before form is built.
48 */
49 public function preProcess() {
50 // current set id
51 $this->_sid = $this->get('sid');
52
53 // setting title for html page
54 $title = ts('New Price Set');
55 if ($this->_sid) {
56 $title = CRM_Price_BAO_PriceSet::getTitle($this->_sid);
57 }
58 if ($this->_action & CRM_Core_Action::UPDATE) {
59 $title = ts('Edit %1', array(1 => $title));
60 }
61 elseif ($this->_action & CRM_Core_Action::VIEW) {
62 $title = ts('Preview %1', array(1 => $title));
63 }
64 CRM_Utils_System::setTitle($title);
65
66 $url = CRM_Utils_System::url('civicrm/admin/price', 'reset=1');
67 $breadCrumb = array(
68 array(
69 'title' => ts('Price Sets'),
70 'url' => $url,
71 ),
72 );
73 CRM_Utils_System::appendBreadCrumb($breadCrumb);
74 }
75
76 /**
77 * Global form rule.
78 *
79 * @param array $fields
80 * The input form values.
81 * @param array $files
82 * The uploaded files if any.
83 * @param array $options
84 * Additional user data.
85 *
86 * @return bool|array
87 * true if no errors, else array of errors
88 */
89 public static function formRule($fields, $files, $options) {
90 $errors = array();
91 $count = count(CRM_Utils_Array::value('extends', $fields));
92 //price sets configured for membership
93 if ($count && array_key_exists(CRM_Core_Component::getComponentID('CiviMember'), $fields['extends'])) {
94 if ($count > 1) {
95 $errors['extends'] = ts('If you plan on using this price set for membership signup and renewal, you can not also use it for Events or Contributions. However, a membership price set may include additional fields for non-membership options that require an additional fee (e.g. magazine subscription).');
96 }
97 }
98 // Checks the given price set does not start with a digit
99 if (strlen($fields['title']) && is_numeric($fields['title'][0])) {
100 $errors['title'] = ts("Name cannot not start with a digit");
101 }
102 return empty($errors) ? TRUE : $errors;
103 }
104
105 /**
106 * Build the form object.
107 */
108 public function buildQuickForm() {
109 $this->applyFilter('__ALL__', 'trim');
110
111 $this->assign('sid', $this->_sid);
112
113 // title
114 $this->add('text', 'title', ts('Set Name'), CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'title'), TRUE);
115 $this->addRule('title', ts('Name already exists in Database.'),
116 'objectExists', array('CRM_Price_DAO_PriceSet', $this->_sid, 'title')
117 );
118
119 $priceSetUsedTables = $extends = array();
120 if ($this->_action == CRM_Core_Action::UPDATE && $this->_sid) {
121 $priceSetUsedTables = CRM_Price_BAO_PriceSet::getUsedBy($this->_sid, 'table');
122 }
123
124 $config = CRM_Core_Config::singleton();
125 $showContribution = FALSE;
126 $enabledComponents = CRM_Core_Component::getEnabledComponents();
127
128 foreach ($enabledComponents as $name => $compObj) {
129 switch ($name) {
130 case 'CiviEvent':
131 $option = $this->createElement('checkbox', $compObj->componentID, NULL, ts('Event'));
132 if (!empty($priceSetUsedTables)) {
133 foreach (array('civicrm_event', 'civicrm_participant') as $table) {
134 if (in_array($table, $priceSetUsedTables)) {
135 $option->freeze();
136 break;
137 }
138 }
139 }
140 $extends[] = $option;
141 break;
142
143 case 'CiviContribute':
144 $option = $this->createElement('checkbox', $compObj->componentID, NULL, ts('Contribution'));
145 if (!empty($priceSetUsedTables)) {
146 foreach (array('civicrm_contribution', 'civicrm_contribution_page') as $table) {
147 if (in_array($table, $priceSetUsedTables)) {
148 $option->freeze();
149 break;
150 }
151 }
152 }
153 $extends[] = $option;
154 break;
155
156 case 'CiviMember':
157 $option = $this->createElement('checkbox', $compObj->componentID, NULL, ts('Membership'));
158 if (!empty($priceSetUsedTables)) {
159 foreach (array('civicrm_membership', 'civicrm_contribution_page') as $table) {
160 if (in_array($table, $priceSetUsedTables)) {
161 $option->freeze();
162 break;
163 }
164 }
165 }
166 $extends[] = $option;
167 break;
168 }
169 }
170
171 $this->addElement('text', 'min_amount', ts('Minimum Amount'));
172
173 if (CRM_Utils_System::isNull($extends)) {
174 $this->assign('extends', FALSE);
175 }
176 else {
177 $this->assign('extends', TRUE);
178 }
179
180 $this->addGroup($extends, 'extends', ts('Used For'), '&nbsp;', TRUE);
181
182 $this->addRule('extends', ts('%1 is a required field.', array(1 => ts('Used For'))), 'required');
183
184 // financial type
185 $financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType();
186
187 foreach ($financialType as $finTypeId => $type) {
188 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()
189 && !CRM_Core_Permission::check('add contributions of type ' . $type)
190 ) {
191 unset($financialType[$finTypeId]);
192 }
193 }
194
195 $this->add('select', 'financial_type_id',
196 ts('Default Financial Type'),
197 array('' => ts('- select -')) + $financialType, 'required'
198 );
199
200 // help text
201 $this->add('textarea', 'help_pre', ts('Pre-form Help'),
202 CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'help_pre')
203 );
204 $this->add('textarea', 'help_post', ts('Post-form Help'),
205 CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'help_post')
206 );
207
208 // is this set active ?
209 $this->addElement('checkbox', 'is_active', ts('Is this Price Set active?'));
210
211 $this->addButtons(array(
212 array(
213 'type' => 'next',
214 'name' => ts('Save'),
215 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
216 'isDefault' => TRUE,
217 ),
218 array(
219 'type' => 'cancel',
220 'name' => ts('Cancel'),
221 ),
222 ));
223
224 $this->addFormRule(array('CRM_Price_Form_Set', 'formRule'));
225
226 // views are implemented as frozen form
227 if ($this->_action & CRM_Core_Action::VIEW) {
228 $this->freeze();
229 //$this->addElement('button', 'done', ts('Done'), array('onclick' => "location.href='civicrm/admin/price?reset=1&action=browse'"));
230 }
231 }
232
233 /**
234 * Set default values for the form. Note that in edit/view mode.
235 *
236 * The default values are retrieved from the database.
237 *
238 * @return array
239 * array of default values
240 */
241 public function setDefaultValues() {
242 $defaults = array('is_active' => TRUE);
243 if ($this->_sid) {
244 $params = array('id' => $this->_sid);
245 CRM_Price_BAO_PriceSet::retrieve($params, $defaults);
246 $extends = explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaults['extends']);
247 unset($defaults['extends']);
248 foreach ($extends as $compId) {
249 $defaults['extends'][$compId] = 1;
250 }
251 }
252
253 return $defaults;
254 }
255
256 /**
257 * Process the form.
258 */
259 public function postProcess() {
260 // get the submitted form values.
261 $params = $this->controller->exportValues('Set');
262 $nameLength = CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'name');
263 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
264 $params['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $params, FALSE);
265
266 $compIds = array();
267 $extends = CRM_Utils_Array::value('extends', $params);
268 if (is_array($extends)) {
269 foreach ($extends as $compId => $selected) {
270 if ($selected) {
271 $compIds[] = $compId;
272 }
273 }
274 }
275 $params['extends'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $compIds);
276
277 if ($this->_action & CRM_Core_Action::UPDATE) {
278 $params['id'] = $this->_sid;
279 }
280 else {
281 $params['name'] = CRM_Utils_String::titleToVar($params['title'],
282 CRM_Utils_Array::value('maxlength', $nameLength));
283 }
284
285 $set = CRM_Price_BAO_PriceSet::create($params);
286 if ($this->_action & CRM_Core_Action::UPDATE) {
287 CRM_Core_Session::setStatus(ts('The Set \'%1\' has been saved.', array(1 => $set->title)), ts('Saved'), 'success');
288 }
289 else {
290 // Jump directly to adding a field if popups are disabled
291 $action = CRM_Core_Resources::singleton()->ajaxPopupsEnabled ? 'browse' : 'add';
292 $url = CRM_Utils_System::url('civicrm/admin/price/field', array(
293 'reset' => 1,
294 'action' => $action,
295 'sid' => $set->id,
296 'new' => 1,
297 ));
298 CRM_Core_Session::setStatus(ts("Your Set '%1' has been added. You can add fields to this set now.",
299 array(1 => $set->title)
300 ), ts('Saved'), 'success');
301 $session = CRM_Core_Session::singleton();
302 $session->replaceUserContext($url);
303 }
304 }
305
306 }