Merge pull request #2061 from civicrm/4.3
[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 $components = array('CiviEvent' => array('title' => ts('Event'),
138 'extend' => CRM_Core_Component::getComponentID('CiviEvent'),
139 'tables' => array(
140 'civicrm_event',
141 'civicrm_participant',
142 ),
143 ),
144 'CiviContribute' => array('title' => ts('Contribution'),
145 'extend' => CRM_Core_Component::getComponentID('CiviContribute'),
146 'tables' => array(
147 'civicrm_contribution',
148 'civicrm_contribution_page',
149 ),
150 ),
151 'CiviMember' => array('title' => ts('Membership'),
152 'extend' => CRM_Core_Component::getComponentID('CiviMember'),
153 'tables' => array(
154 'civicrm_membership',
155 'civicrm_contribution_page',
156 ),
157 ),
158 );
159 foreach ($components as $compName => $compValues) {
160 // take only enabled components.
161 if (!in_array($compName, $config->enableComponents)) {
162 continue;
163 }
164 $option = $this->createElement('checkbox', $compValues['extend'], NULL, $compValues['title']);
165
166 //if price set is used than freeze it.
167 if (!empty($priceSetUsedTables)) {
168 foreach ($compValues['tables'] as $table) {
169
170 if (in_array($table, $priceSetUsedTables)) {
171 $option->freeze();
172 break;
173 }
174 }
175 }
176 $extends[] = $option;
177 }
178
179 if (CRM_Utils_System::isNull($extends)) {
180 $this->assign('extends', FALSE);
181 }
182 else {
183 $this->assign('extends', TRUE);
184 }
185
186 $this->addGroup($extends, 'extends', ts('Used For'), '&nbsp;', TRUE);
187
188 $this->addRule('extends', ts('%1 is a required field.', array(1 => ts('Used For'))), 'required');
189
190 // financial type
191 $financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType();
192
193 $this->add('select', 'financial_type_id',
194 ts('Default Financial Type'),
195 array('' => ts('- select -')) + $financialType, 'required'
196 );
197
198 // help text
199 $this->add('textarea', 'help_pre', ts('Pre-form Help'),
200 CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'help_pre')
201 );
202 $this->add('textarea', 'help_post', ts('Post-form Help'),
203 CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'help_post')
204 );
205
206 // is this set active ?
207 $this->addElement('checkbox', 'is_active', ts('Is this Price Set active?'));
208
209 $this->addButtons(array(
210 array(
211 'type' => 'next',
212 'name' => ts('Save'),
213 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
214 'isDefault' => TRUE,
215 ),
216 array(
217 'type' => 'cancel',
218 'name' => ts('Cancel'),
219 ),
220 )
221 );
222
223 $this->addFormRule(array('CRM_Price_Form_Set', 'formRule'));
224
225 // views are implemented as frozen form
226 if ($this->_action & CRM_Core_Action::VIEW) {
227 $this->freeze();
228 //$this->addElement('button', 'done', ts('Done'), array('onclick' => "location.href='civicrm/admin/price?reset=1&action=browse'"));
229 }
230 }
231
232 /**
233 * This function sets the default values for the form. Note that in edit/view mode
234 * the default values are retrieved from the database
235 *
236 * @param null
237 *
238 * @return array array of default values
239 * @access public
240 */
241 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) $defaults['extends'][$compId] = 1;
249 }
250
251 return $defaults;
252 }
253
254 /**
255 * Process the form
256 *
257 * @param null
258 *
259 * @return void
260 * @access public
261 */
262 public function postProcess() {
263 // get the submitted form values.
264 $params = $this->controller->exportValues('Set');
265 $nameLength = CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'name');
266 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
267 $params['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $params, FALSE);
268
269 $compIds = array();
270 $extends = CRM_Utils_Array::value('extends', $params);
271 if (is_array($extends)) {
272 foreach ($extends as $compId => $selected) if ($selected) { $compIds[] = $compId; }
273 }
274 $params['extends'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $compIds);
275
276 if ($this->_action & CRM_Core_Action::UPDATE) {
277 $params['id'] = $this->_sid;
278 }
279 else {
280 $params['name'] = CRM_Utils_String::titleToVar($params['title'],
281 CRM_Utils_Array::value('maxlength', $nameLength));
282 }
283
284 $set = CRM_Price_BAO_PriceSet::create($params);
285 if ($this->_action & CRM_Core_Action::UPDATE) {
286 CRM_Core_Session::setStatus(ts('The Set \'%1\' has been saved.', array(1 => $set->title)), ts('Saved'), 'success');
287 }
288 else {
289 $url = CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=add&sid=' . $set->id);
290 CRM_Core_Session::setStatus(ts("Your Set '%1' has been added. You can add fields to this set now.",
291 array(1 => $set->title)
292 ), ts('Saved'), 'success');
293 $session = CRM_Core_Session::singleton();
294 $session->replaceUserContext($url);
295 }
296 }
297 }
298