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