Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
ad37ac8e | 35 | * Form to process actions on Price Sets. |
6a488035 TO |
36 | */ |
37 | class CRM_Price_Form_Set extends CRM_Core_Form { | |
38 | ||
39 | /** | |
fe482240 | 40 | * The set id saved to the session for an update. |
6a488035 TO |
41 | * |
42 | * @var int | |
6a488035 TO |
43 | */ |
44 | protected $_sid; | |
45 | ||
46 | /** | |
fe482240 | 47 | * Set variables up before form is built. |
6a488035 TO |
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) { | |
9da8dc8c | 56 | $title = CRM_Price_BAO_PriceSet::getTitle($this->_sid); |
6a488035 TO |
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'); | |
353ffa53 TO |
67 | $breadCrumb = array( |
68 | array( | |
69 | 'title' => ts('Price Sets'), | |
6a488035 | 70 | 'url' => $url, |
bed98343 | 71 | ), |
353ffa53 | 72 | ); |
6a488035 TO |
73 | CRM_Utils_System::appendBreadCrumb($breadCrumb); |
74 | } | |
75 | ||
76 | /** | |
fe482240 | 77 | * Global form rule. |
6a488035 | 78 | * |
414c1420 TO |
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. | |
6a488035 | 85 | * |
72b3a70c CW |
86 | * @return bool|array |
87 | * true if no errors, else array of errors | |
6a488035 | 88 | */ |
00be9182 | 89 | public static function formRule($fields, $files, $options) { |
6a488035 TO |
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) { | |
5ab78070 | 103 | $errors['title'] = ts("Name cannot not start with a digit"); |
6a488035 TO |
104 | } |
105 | return empty($errors) ? TRUE : $errors; | |
106 | } | |
107 | ||
108 | /** | |
fe482240 | 109 | * Build the form object. |
6a488035 TO |
110 | */ |
111 | public function buildQuickForm() { | |
112 | $this->applyFilter('__ALL__', 'trim'); | |
113 | ||
114 | $this->assign('sid', $this->_sid); | |
115 | ||
116 | // title | |
9da8dc8c | 117 | $this->add('text', 'title', ts('Set Name'), CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'title'), TRUE); |
6a488035 | 118 | $this->addRule('title', ts('Name already exists in Database.'), |
9da8dc8c | 119 | 'objectExists', array('CRM_Price_DAO_PriceSet', $this->_sid, 'title') |
6a488035 TO |
120 | ); |
121 | ||
122 | $priceSetUsedTables = $extends = array(); | |
123 | if ($this->_action == CRM_Core_Action::UPDATE && $this->_sid) { | |
9da8dc8c | 124 | $priceSetUsedTables = CRM_Price_BAO_PriceSet::getUsedBy($this->_sid, 'table'); |
6a488035 TO |
125 | } |
126 | ||
353ffa53 | 127 | $config = CRM_Core_Config::singleton(); |
6a488035 | 128 | $showContribution = FALSE; |
6091117f | 129 | $enabledComponents = CRM_Core_Component::getEnabledComponents(); |
130 | ||
131 | foreach ($enabledComponents as $name => $compObj) { | |
132 | switch ($name) { | |
133 | case 'CiviEvent': | |
134 | $option = $this->createElement('checkbox', $compObj->componentID, NULL, ts('Event')); | |
135 | if (!empty($priceSetUsedTables)) { | |
136 | foreach (array('civicrm_event', 'civicrm_participant') as $table) { | |
137 | if (in_array($table, $priceSetUsedTables)) { | |
138 | $option->freeze(); | |
139 | break; | |
140 | } | |
141 | } | |
142 | } | |
143 | $extends[] = $option; | |
144 | break; | |
ba1dcfda | 145 | |
6091117f | 146 | case 'CiviContribute': |
147 | $option = $this->createElement('checkbox', $compObj->componentID, NULL, ts('Contribution')); | |
148 | if (!empty($priceSetUsedTables)) { | |
149 | foreach (array('civicrm_contribution', 'civicrm_contribution_page') as $table) { | |
150 | if (in_array($table, $priceSetUsedTables)) { | |
151 | $option->freeze(); | |
152 | break; | |
153 | } | |
154 | } | |
155 | } | |
156 | $extends[] = $option; | |
157 | break; | |
ba1dcfda | 158 | |
6091117f | 159 | case 'CiviMember': |
160 | $option = $this->createElement('checkbox', $compObj->componentID, NULL, ts('Membership')); | |
161 | if (!empty($priceSetUsedTables)) { | |
162 | foreach (array('civicrm_membership', 'civicrm_contribution_page') as $table) { | |
163 | if (in_array($table, $priceSetUsedTables)) { | |
164 | $option->freeze(); | |
165 | break; | |
166 | } | |
167 | } | |
6a488035 | 168 | } |
6091117f | 169 | $extends[] = $option; |
170 | break; | |
6a488035 | 171 | } |
6a488035 TO |
172 | } |
173 | ||
174 | if (CRM_Utils_System::isNull($extends)) { | |
175 | $this->assign('extends', FALSE); | |
176 | } | |
177 | else { | |
178 | $this->assign('extends', TRUE); | |
179 | } | |
180 | ||
181 | $this->addGroup($extends, 'extends', ts('Used For'), ' ', TRUE); | |
182 | ||
183 | $this->addRule('extends', ts('%1 is a required field.', array(1 => ts('Used For'))), 'required'); | |
366fe2a3 | 184 | |
6a488035 TO |
185 | // financial type |
186 | $financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType(); | |
366fe2a3 | 187 | |
53df1142 | 188 | foreach ($financialType as $finTypeId => $type) { |
40c655aa | 189 | if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() |
66af7c48 PN |
190 | && !CRM_Core_Permission::check('add contributions of type ' . $type) |
191 | ) { | |
53df1142 E |
192 | unset($financialType[$finTypeId]); |
193 | } | |
194 | } | |
195 | ||
6a488035 TO |
196 | $this->add('select', 'financial_type_id', |
197 | ts('Default Financial Type'), | |
353ffa53 | 198 | array('' => ts('- select -')) + $financialType, 'required' |
6a488035 TO |
199 | ); |
200 | ||
201 | // help text | |
202 | $this->add('textarea', 'help_pre', ts('Pre-form Help'), | |
9da8dc8c | 203 | CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'help_pre') |
6a488035 TO |
204 | ); |
205 | $this->add('textarea', 'help_post', ts('Post-form Help'), | |
9da8dc8c | 206 | CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'help_post') |
6a488035 TO |
207 | ); |
208 | ||
209 | // is this set active ? | |
210 | $this->addElement('checkbox', 'is_active', ts('Is this Price Set active?')); | |
211 | ||
212 | $this->addButtons(array( | |
c5c263ca AH |
213 | array( |
214 | 'type' => 'next', | |
215 | 'name' => ts('Save'), | |
216 | 'spacing' => ' ', | |
217 | 'isDefault' => TRUE, | |
218 | ), | |
219 | array( | |
220 | 'type' => 'cancel', | |
221 | 'name' => ts('Cancel'), | |
222 | ), | |
223 | )); | |
6a488035 TO |
224 | |
225 | $this->addFormRule(array('CRM_Price_Form_Set', 'formRule')); | |
226 | ||
227 | // views are implemented as frozen form | |
228 | if ($this->_action & CRM_Core_Action::VIEW) { | |
229 | $this->freeze(); | |
230 | //$this->addElement('button', 'done', ts('Done'), array('onclick' => "location.href='civicrm/admin/price?reset=1&action=browse'")); | |
231 | } | |
232 | } | |
233 | ||
234 | /** | |
ad37ac8e | 235 | * Set default values for the form. Note that in edit/view mode. |
236 | * | |
237 | * The default values are retrieved from the database. | |
6a488035 | 238 | * |
a6c01b45 CW |
239 | * @return array |
240 | * array of default values | |
6a488035 | 241 | */ |
00be9182 | 242 | public function setDefaultValues() { |
6a488035 TO |
243 | $defaults = array('is_active' => TRUE); |
244 | if ($this->_sid) { | |
245 | $params = array('id' => $this->_sid); | |
9da8dc8c | 246 | CRM_Price_BAO_PriceSet::retrieve($params, $defaults); |
6a488035 TO |
247 | $extends = explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaults['extends']); |
248 | unset($defaults['extends']); | |
9277d9e4 TO |
249 | foreach ($extends as $compId) { |
250 | $defaults['extends'][$compId] = 1; | |
ba1dcfda | 251 | } |
6a488035 TO |
252 | } |
253 | ||
254 | return $defaults; | |
255 | } | |
256 | ||
257 | /** | |
fe482240 | 258 | * Process the form. |
6a488035 TO |
259 | */ |
260 | public function postProcess() { | |
261 | // get the submitted form values. | |
262 | $params = $this->controller->exportValues('Set'); | |
9da8dc8c | 263 | $nameLength = CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'name'); |
6a488035 TO |
264 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); |
265 | $params['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $params, FALSE); | |
266 | ||
267 | $compIds = array(); | |
268 | $extends = CRM_Utils_Array::value('extends', $params); | |
269 | if (is_array($extends)) { | |
4f99ca55 TO |
270 | foreach ($extends as $compId => $selected) { |
271 | if ($selected) { | |
272 | $compIds[] = $compId; | |
273 | } | |
ba1dcfda | 274 | } |
6a488035 TO |
275 | } |
276 | $params['extends'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $compIds); | |
277 | ||
278 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
279 | $params['id'] = $this->_sid; | |
280 | } | |
0c86abc7 | 281 | else { |
282 | $params['name'] = CRM_Utils_String::titleToVar($params['title'], | |
283 | CRM_Utils_Array::value('maxlength', $nameLength)); | |
284 | } | |
6a488035 | 285 | |
9da8dc8c | 286 | $set = CRM_Price_BAO_PriceSet::create($params); |
6a488035 TO |
287 | if ($this->_action & CRM_Core_Action::UPDATE) { |
288 | CRM_Core_Session::setStatus(ts('The Set \'%1\' has been saved.', array(1 => $set->title)), ts('Saved'), 'success'); | |
289 | } | |
290 | else { | |
704f21c0 CW |
291 | // Jump directly to adding a field if popups are disabled |
292 | $action = CRM_Core_Resources::singleton()->ajaxPopupsEnabled ? 'browse' : 'add'; | |
353ffa53 | 293 | $url = CRM_Utils_System::url('civicrm/admin/price/field', array( |
c5c263ca AH |
294 | 'reset' => 1, |
295 | 'action' => $action, | |
296 | 'sid' => $set->id, | |
297 | 'new' => 1, | |
298 | )); | |
6a488035 | 299 | CRM_Core_Session::setStatus(ts("Your Set '%1' has been added. You can add fields to this set now.", |
353ffa53 TO |
300 | array(1 => $set->title) |
301 | ), ts('Saved'), 'success'); | |
6a488035 TO |
302 | $session = CRM_Core_Session::singleton(); |
303 | $session->replaceUserContext($url); | |
304 | } | |
305 | } | |
96025800 | 306 | |
6a488035 | 307 | } |