Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-01-05-23-28-33
[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(array('title' => ts('Price Sets'),
74 'url' => $url,
75 ));
76 CRM_Utils_System::appendBreadCrumb($breadCrumb);
77 }
78
79 /**
80 * Global form rule
81 *
82 * @param array $fields the input form values
83 * @param array $files the uploaded files if any
84 * @param array $options additional user data
85 *
86 * @return true if no errors, else array of errors
87 * @static
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 doesnot start with digit
99 $title = $fields['title'];
100 // gives the ascii value
101 $asciiValue = ord($title{0});
102 if ($asciiValue >= 48 && $asciiValue <= 57) {
103 $errors['title'] = ts("Name cannot not start with a digit");
104 }
105 return empty($errors) ? TRUE : $errors;
106 }
107
108 /**
109 * Build the form object
110 *
111 * @param null
112 *
113 * @return void
114 */
115 public function buildQuickForm() {
116 $this->applyFilter('__ALL__', 'trim');
117
118 $this->assign('sid', $this->_sid);
119
120 // title
121 $this->add('text', 'title', ts('Set Name'), CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'title'), TRUE);
122 $this->addRule('title', ts('Name already exists in Database.'),
123 'objectExists', array('CRM_Price_DAO_PriceSet', $this->_sid, 'title')
124 );
125
126 $priceSetUsedTables = $extends = array();
127 if ($this->_action == CRM_Core_Action::UPDATE && $this->_sid) {
128 $priceSetUsedTables = CRM_Price_BAO_PriceSet::getUsedBy($this->_sid, 'table');
129 }
130
131 $config = CRM_Core_Config::singleton();
132 $showContribution = FALSE;
133 $enabledComponents = CRM_Core_Component::getEnabledComponents();
134
135 foreach ($enabledComponents as $name => $compObj) {
136 switch ($name) {
137 case 'CiviEvent':
138 $option = $this->createElement('checkbox', $compObj->componentID, NULL, ts('Event'));
139 if (!empty($priceSetUsedTables)) {
140 foreach (array('civicrm_event', 'civicrm_participant') as $table) {
141 if (in_array($table, $priceSetUsedTables)) {
142 $option->freeze();
143 break;
144 }
145 }
146 }
147 $extends[] = $option;
148 break;
149 case 'CiviContribute':
150 $option = $this->createElement('checkbox', $compObj->componentID, NULL, ts('Contribution'));
151 if (!empty($priceSetUsedTables)) {
152 foreach (array('civicrm_contribution', 'civicrm_contribution_page') as $table) {
153 if (in_array($table, $priceSetUsedTables)) {
154 $option->freeze();
155 break;
156 }
157 }
158 }
159 $extends[] = $option;
160 break;
161 case 'CiviMember':
162 $option = $this->createElement('checkbox', $compObj->componentID, NULL, ts('Membership'));
163 if (!empty($priceSetUsedTables)) {
164 foreach (array('civicrm_membership', 'civicrm_contribution_page') as $table) {
165 if (in_array($table, $priceSetUsedTables)) {
166 $option->freeze();
167 break;
168 }
169 }
170 }
171 $extends[] = $option;
172 break;
173 }
174 }
175
176 if (CRM_Utils_System::isNull($extends)) {
177 $this->assign('extends', FALSE);
178 }
179 else {
180 $this->assign('extends', TRUE);
181 }
182
183 $this->addGroup($extends, 'extends', ts('Used For'), '&nbsp;', TRUE);
184
185 $this->addRule('extends', ts('%1 is a required field.', array(1 => ts('Used For'))), 'required');
186
187 // financial type
188 $financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType();
189
190 $this->add('select', 'financial_type_id',
191 ts('Default Financial Type'),
192 array('' => ts('- select -')) + $financialType, 'required'
193 );
194
195 // help text
196 $this->add('textarea', 'help_pre', ts('Pre-form Help'),
197 CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'help_pre')
198 );
199 $this->add('textarea', 'help_post', ts('Post-form Help'),
200 CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'help_post')
201 );
202
203 // is this set active ?
204 $this->addElement('checkbox', 'is_active', ts('Is this Price Set active?'));
205
206 $this->addButtons(array(
207 array(
208 'type' => 'next',
209 'name' => ts('Save'),
210 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
211 'isDefault' => TRUE,
212 ),
213 array(
214 'type' => 'cancel',
215 'name' => ts('Cancel'),
216 ),
217 )
218 );
219
220 $this->addFormRule(array('CRM_Price_Form_Set', 'formRule'));
221
222 // views are implemented as frozen form
223 if ($this->_action & CRM_Core_Action::VIEW) {
224 $this->freeze();
225 //$this->addElement('button', 'done', ts('Done'), array('onclick' => "location.href='civicrm/admin/price?reset=1&action=browse'"));
226 }
227 }
228
229 /**
230 * Set default values for the form. Note that in edit/view mode
231 * the default values are retrieved from the database
232 *
233 * @param null
234 *
235 * @return array array of default values
236 */
237 public function setDefaultValues() {
238 $defaults = array('is_active' => TRUE);
239 if ($this->_sid) {
240 $params = array('id' => $this->_sid);
241 CRM_Price_BAO_PriceSet::retrieve($params, $defaults);
242 $extends = explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaults['extends']);
243 unset($defaults['extends']);
244 foreach ($extends as $compId) $defaults['extends'][$compId] = 1;
245 }
246
247 return $defaults;
248 }
249
250 /**
251 * Process the form
252 *
253 * @param null
254 *
255 * @return void
256 */
257 public function postProcess() {
258 // get the submitted form values.
259 $params = $this->controller->exportValues('Set');
260 $nameLength = CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'name');
261 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
262 $params['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $params, FALSE);
263
264 $compIds = array();
265 $extends = CRM_Utils_Array::value('extends', $params);
266 if (is_array($extends)) {
267 foreach ($extends as $compId => $selected) if ($selected) { $compIds[] = $compId; }
268 }
269 $params['extends'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $compIds);
270
271 if ($this->_action & CRM_Core_Action::UPDATE) {
272 $params['id'] = $this->_sid;
273 }
274 else {
275 $params['name'] = CRM_Utils_String::titleToVar($params['title'],
276 CRM_Utils_Array::value('maxlength', $nameLength));
277 }
278
279 $set = CRM_Price_BAO_PriceSet::create($params);
280 if ($this->_action & CRM_Core_Action::UPDATE) {
281 CRM_Core_Session::setStatus(ts('The Set \'%1\' has been saved.', array(1 => $set->title)), ts('Saved'), 'success');
282 }
283 else {
284 // Jump directly to adding a field if popups are disabled
285 $action = CRM_Core_Resources::singleton()->ajaxPopupsEnabled ? 'browse' : 'add';
286 $url = CRM_Utils_System::url('civicrm/admin/price/field', array('reset' => 1, 'action' => $action, 'sid' => $set->id, 'new' => 1));
287 CRM_Core_Session::setStatus(ts("Your Set '%1' has been added. You can add fields to this set now.",
288 array(1 => $set->title)
289 ), ts('Saved'), 'success');
290 $session = CRM_Core_Session::singleton();
291 $session->replaceUserContext($url);
292 }
293 }
294 }