30d1fea66318b986f957a9bee68e53e413ed86e7
[civicrm-core.git] / CRM / Price / Form / Set.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 * @access protected
46 */
47 protected $_sid;
48
49 /**
50 * Function to set variables up before form is built
51 *
52 * @param null
53 *
54 * @return void
55 * @access public
56 */
57 public function preProcess() {
58 // current set id
59 $this->_sid = $this->get('sid');
60
61 // setting title for html page
62 $title = ts('New Price Set');
63 if ($this->_sid) {
64 $title = CRM_Price_BAO_PriceSet::getTitle($this->_sid);
65 }
66 if ($this->_action & CRM_Core_Action::UPDATE) {
67 $title = ts('Edit %1', array(1 => $title));
68 }
69 elseif ($this->_action & CRM_Core_Action::VIEW) {
70 $title = ts('Preview %1', array(1 => $title));
71 }
72 CRM_Utils_System::setTitle($title);
73
74 $url = CRM_Utils_System::url('civicrm/admin/price', 'reset=1');
75 $breadCrumb = array(array('title' => ts('Price Sets'),
76 'url' => $url,
77 ));
78 CRM_Utils_System::appendBreadCrumb($breadCrumb);
79 }
80
81 /**
82 * global form rule
83 *
84 * @param array $fields the input form values
85 * @param array $files the uploaded files if any
86 * @param array $options additional user data
87 *
88 * @return true if no errors, else array of errors
89 * @access public
90 * @static
91 */
92 static function formRule($fields, $files, $options) {
93 $errors = array();
94 $count = count(CRM_Utils_Array::value('extends', $fields));
95 //price sets configured for membership
96 if ($count && array_key_exists(CRM_Core_Component::getComponentID('CiviMember'), $fields['extends'])) {
97 if ($count > 1) {
98 $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).');
99 }
100 }
101 //checks the given price set doesnot start with digit
102 $title = $fields['title'];
103 // gives the ascii value
104 $asciiValue = ord($title{0});
105 if ($asciiValue >= 48 && $asciiValue <= 57) {
106 $errors['title'] = ts("Set's Name should not start with digit");
107 }
108 return empty($errors) ? TRUE : $errors;
109 }
110
111 /**
112 * Function to actually build the form
113 *
114 * @param null
115 *
116 * @return void
117 * @access public
118 */
119 public function buildQuickForm() {
120 $this->applyFilter('__ALL__', 'trim');
121
122 $this->assign('sid', $this->_sid);
123
124 // title
125 $this->add('text', 'title', ts('Set Name'), CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'title'), TRUE);
126 $this->addRule('title', ts('Name already exists in Database.'),
127 'objectExists', array('CRM_Price_DAO_PriceSet', $this->_sid, 'title')
128 );
129
130 $priceSetUsedTables = $extends = array();
131 if ($this->_action == CRM_Core_Action::UPDATE && $this->_sid) {
132 $priceSetUsedTables = CRM_Price_BAO_PriceSet::getUsedBy($this->_sid, 'table');
133 }
134
135 $config = CRM_Core_Config::singleton();
136 $showContribution = FALSE;
137 $enabledComponents = CRM_Core_Component::getEnabledComponents();
138
139 foreach ($enabledComponents as $name => $compObj) {
140 switch ($name) {
141 case 'CiviEvent':
142 $option = $this->createElement('checkbox', $compObj->componentID, NULL, ts('Event'));
143 if (!empty($priceSetUsedTables)) {
144 foreach (array('civicrm_event', 'civicrm_participant') as $table) {
145 if (in_array($table, $priceSetUsedTables)) {
146 $option->freeze();
147 break;
148 }
149 }
150 }
151 $extends[] = $option;
152 break;
153 case 'CiviContribute':
154 $option = $this->createElement('checkbox', $compObj->componentID, NULL, ts('Contribution'));
155 if (!empty($priceSetUsedTables)) {
156 foreach (array('civicrm_contribution', 'civicrm_contribution_page') as $table) {
157 if (in_array($table, $priceSetUsedTables)) {
158 $option->freeze();
159 break;
160 }
161 }
162 }
163 $extends[] = $option;
164 break;
165 case 'CiviMember':
166 $option = $this->createElement('checkbox', $compObj->componentID, NULL, ts('Membership'));
167 if (!empty($priceSetUsedTables)) {
168 foreach (array('civicrm_membership', 'civicrm_contribution_page') as $table) {
169 if (in_array($table, $priceSetUsedTables)) {
170 $option->freeze();
171 break;
172 }
173 }
174 }
175 $extends[] = $option;
176 break;
177 }
178 }
179
180 if (CRM_Utils_System::isNull($extends)) {
181 $this->assign('extends', FALSE);
182 }
183 else {
184 $this->assign('extends', TRUE);
185 }
186
187 $this->addGroup($extends, 'extends', ts('Used For'), '&nbsp;', TRUE);
188
189 $this->addRule('extends', ts('%1 is a required field.', array(1 => ts('Used For'))), 'required');
190
191 // financial type
192 $financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType();
193
194 $this->add('select', 'financial_type_id',
195 ts('Default Financial Type'),
196 array('' => ts('- select -')) + $financialType, 'required'
197 );
198
199 // help text
200 $this->add('textarea', 'help_pre', ts('Pre-form Help'),
201 CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'help_pre')
202 );
203 $this->add('textarea', 'help_post', ts('Post-form Help'),
204 CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'help_post')
205 );
206
207 // is this set active ?
208 $this->addElement('checkbox', 'is_active', ts('Is this Price Set active?'));
209
210 $this->addButtons(array(
211 array(
212 'type' => 'next',
213 'name' => ts('Save'),
214 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
215 'isDefault' => TRUE,
216 ),
217 array(
218 'type' => 'cancel',
219 'name' => ts('Cancel'),
220 ),
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 * This function sets the default values for the form. Note that in edit/view mode
235 * the default values are retrieved from the database
236 *
237 * @param null
238 *
239 * @return array array of default values
240 * @access public
241 */
242 function setDefaultValues() {
243 $defaults = array('is_active' => TRUE);
244 if ($this->_sid) {
245 $params = array('id' => $this->_sid);
246 CRM_Price_BAO_PriceSet::retrieve($params, $defaults);
247 $extends = explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaults['extends']);
248 unset($defaults['extends']);
249 foreach ($extends as $compId) $defaults['extends'][$compId] = 1;
250 }
251
252 return $defaults;
253 }
254
255 /**
256 * Process the form
257 *
258 * @param null
259 *
260 * @return void
261 * @access public
262 */
263 public function postProcess() {
264 // get the submitted form values.
265 $params = $this->controller->exportValues('Set');
266 $nameLength = CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'name');
267 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
268 $params['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $params, FALSE);
269
270 $compIds = array();
271 $extends = CRM_Utils_Array::value('extends', $params);
272 if (is_array($extends)) {
273 foreach ($extends as $compId => $selected) if ($selected) { $compIds[] = $compId; }
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 $url = CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=add&sid=' . $set->id);
291 CRM_Core_Session::setStatus(ts("Your Set '%1' has been added. You can add fields to this set now.",
292 array(1 => $set->title)
293 ), ts('Saved'), 'success');
294 $session = CRM_Core_Session::singleton();
295 $session->replaceUserContext($url);
296 }
297 }
298 }
299