Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Create a page for displaying Price Fields. | |
38 | * | |
39 | * Heart of this class is the run method which checks | |
40 | * for action type and then displays the appropriate | |
41 | * page. | |
42 | * | |
43 | */ | |
44 | class CRM_Price_Page_Field extends CRM_Core_Page { | |
45 | ||
96f50de2 CW |
46 | public $useLivePageJS = TRUE; |
47 | ||
6a488035 TO |
48 | /** |
49 | * The price set group id of the field | |
50 | * | |
51 | * @var int | |
6a488035 TO |
52 | */ |
53 | protected $_sid; | |
54 | ||
55 | /** | |
56 | * The action links that we need to display for the browse screen | |
57 | * | |
58 | * @var array | |
6a488035 TO |
59 | */ |
60 | private static $_actionLinks; | |
61 | ||
62 | /** | |
63 | * The price set is reserved or not | |
64 | * | |
65 | * @var boolean | |
6a488035 TO |
66 | */ |
67 | protected $_isSetReserved = false; | |
68 | ||
69 | /** | |
70 | * Get the action links for this page. | |
71 | * | |
72 | * @param null | |
73 | * | |
74 | * @return array array of action links that we need to display for the browse screen | |
6a488035 TO |
75 | */ function &actionLinks() { |
76 | if (!isset(self::$_actionLinks)) { | |
6a488035 TO |
77 | self::$_actionLinks = array( |
78 | CRM_Core_Action::UPDATE => array( | |
79 | 'name' => ts('Edit Price Field'), | |
80 | 'url' => 'civicrm/admin/price/field', | |
81 | 'qs' => 'action=update&reset=1&sid=%%sid%%&fid=%%fid%%', | |
82 | 'title' => ts('Edit Price'), | |
83 | ), | |
84 | CRM_Core_Action::PREVIEW => array( | |
85 | 'name' => ts('Preview Field'), | |
86 | 'url' => 'civicrm/admin/price/field', | |
87 | 'qs' => 'action=preview&reset=1&sid=%%sid%%&fid=%%fid%%', | |
88 | 'title' => ts('Preview Price'), | |
89 | ), | |
90 | CRM_Core_Action::DISABLE => array( | |
91 | 'name' => ts('Disable'), | |
4d17a233 | 92 | 'ref' => 'crm-enable-disable', |
6a488035 TO |
93 | 'title' => ts('Disable Price'), |
94 | ), | |
95 | CRM_Core_Action::ENABLE => array( | |
96 | 'name' => ts('Enable'), | |
4d17a233 | 97 | 'ref' => 'crm-enable-disable', |
6a488035 TO |
98 | 'title' => ts('Enable Price'), |
99 | ), | |
100 | CRM_Core_Action::DELETE => array( | |
101 | 'name' => ts('Delete'), | |
102 | 'url' => 'civicrm/admin/price/field', | |
103 | 'qs' => 'action=delete&reset=1&sid=%%sid%%&fid=%%fid%%', | |
104 | 'title' => ts('Delete Price'), | |
6a488035 TO |
105 | ), |
106 | ); | |
107 | } | |
108 | return self::$_actionLinks; | |
109 | } | |
110 | ||
111 | /** | |
112 | * Browse all price set fields. | |
113 | * | |
114 | * @param null | |
115 | * | |
116 | * @return void | |
6a488035 | 117 | */ |
00be9182 | 118 | public function browse() { |
6f231148 CW |
119 | $resourceManager = CRM_Core_Resources::singleton(); |
120 | if (!empty($_GET['new']) && $resourceManager->ajaxPopupsEnabled) { | |
96ed17aa | 121 | $resourceManager->addScriptFile('civicrm', 'js/crm.addNew.js', 999, 'html-header'); |
6f231148 CW |
122 | } |
123 | ||
6a488035 | 124 | $priceField = array(); |
9da8dc8c | 125 | $priceFieldBAO = new CRM_Price_BAO_PriceField(); |
6a488035 TO |
126 | |
127 | // fkey is sid | |
128 | $priceFieldBAO->price_set_id = $this->_sid; | |
129 | $priceFieldBAO->orderBy('weight, label'); | |
130 | $priceFieldBAO->find(); | |
44c8822b | 131 | |
03b412ae PB |
132 | // display taxTerm for priceFields |
133 | $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,'contribution_invoice_settings'); | |
134 | $taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings); | |
135 | $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings); | |
a32709be PB |
136 | $getTaxDetails = FALSE; |
137 | $taxRate = CRM_Core_PseudoConstant::getTaxRates(); | |
6a488035 TO |
138 | while ($priceFieldBAO->fetch()) { |
139 | $priceField[$priceFieldBAO->id] = array(); | |
140 | CRM_Core_DAO::storeValues($priceFieldBAO, $priceField[$priceFieldBAO->id]); | |
141 | ||
142 | // get price if it's a text field | |
143 | if ($priceFieldBAO->html_type == 'Text') { | |
144 | $optionValues = array(); | |
145 | $params = array('price_field_id' => $priceFieldBAO->id); | |
146 | ||
9da8dc8c | 147 | CRM_Price_BAO_PriceFieldValue::retrieve($params, $optionValues); |
6a488035 | 148 | |
a32709be | 149 | $financialTypeId = $optionValues['financial_type_id']; |
6a488035 | 150 | $priceField[$priceFieldBAO->id]['price'] = CRM_Utils_Array::value('amount', $optionValues); |
03b412ae | 151 | if ($invoicing && isset($taxRate[$financialTypeId])) { |
a32709be PB |
152 | $priceField[$priceFieldBAO->id]['tax_rate'] = $taxRate[$financialTypeId]; |
153 | $getTaxDetails = TRUE; | |
154 | } | |
155 | if (isset($priceField[$priceFieldBAO->id]['tax_rate'])) { | |
156 | $taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($priceField[$priceFieldBAO->id]['price'], $priceField[$priceFieldBAO->id]['tax_rate']); | |
157 | $priceField[$priceFieldBAO->id]['tax_amount'] = $taxAmount['tax_amount']; | |
158 | } | |
6a488035 | 159 | } |
44c8822b | 160 | |
6a488035 TO |
161 | $action = array_sum(array_keys($this->actionLinks())); |
162 | ||
163 | if ($this->_isSetReserved) { | |
164 | $action -= CRM_Core_Action::UPDATE + CRM_Core_Action::DELETE + CRM_Core_Action::ENABLE + CRM_Core_Action::DISABLE; | |
165 | } | |
166 | else { | |
167 | if ($priceFieldBAO->is_active) { | |
168 | $action -= CRM_Core_Action::ENABLE; | |
169 | } | |
170 | else { | |
171 | $action -= CRM_Core_Action::DISABLE; | |
172 | } | |
173 | } | |
174 | ||
175 | if ($priceFieldBAO->active_on == '0000-00-00 00:00:00') { | |
176 | $priceField[$priceFieldBAO->id]['active_on'] = ''; | |
177 | } | |
178 | ||
179 | if ($priceFieldBAO->expire_on == '0000-00-00 00:00:00') { | |
180 | $priceField[$priceFieldBAO->id]['expire_on'] = ''; | |
181 | } | |
182 | ||
183 | // need to translate html types from the db | |
9da8dc8c | 184 | $htmlTypes = CRM_Price_BAO_PriceField::htmlTypes(); |
79c04577 | 185 | $priceField[$priceFieldBAO->id]['html_type_display'] = $htmlTypes[$priceField[$priceFieldBAO->id]['html_type']]; |
6a488035 | 186 | $priceField[$priceFieldBAO->id]['order'] = $priceField[$priceFieldBAO->id]['weight']; |
87dab4a4 AH |
187 | $priceField[$priceFieldBAO->id]['action'] = CRM_Core_Action::formLink( |
188 | self::actionLinks(), | |
189 | $action, | |
6a488035 TO |
190 | array( |
191 | 'fid' => $priceFieldBAO->id, | |
192 | 'sid' => $this->_sid, | |
87dab4a4 AH |
193 | ), |
194 | ts('more'), | |
195 | FALSE, | |
196 | 'priceField.row.actions', | |
197 | 'PriceField', | |
198 | $priceFieldBAO->id | |
6a488035 | 199 | ); |
03b412ae | 200 | $this->assign('taxTerm', $taxTerm); |
a32709be | 201 | $this->assign('getTaxDetails', $getTaxDetails); |
6a488035 TO |
202 | } |
203 | ||
204 | $returnURL = CRM_Utils_System::url('civicrm/admin/price/field', "reset=1&action=browse&sid={$this->_sid}"); | |
205 | $filter = "price_set_id = {$this->_sid}"; | |
9da8dc8c | 206 | CRM_Utils_Weight::addOrder($priceField, 'CRM_Price_DAO_PriceField', |
6a488035 TO |
207 | 'id', $returnURL, $filter |
208 | ); | |
209 | $this->assign('priceField', $priceField); | |
210 | } | |
211 | ||
212 | /** | |
100fef9d | 213 | * Edit price data. |
6a488035 TO |
214 | * |
215 | * editing would involved modifying existing fields + adding data to new fields. | |
216 | * | |
217 | * @param string $action the action to be invoked | |
218 | ||
219 | * | |
220 | * @return void | |
6a488035 | 221 | */ |
00be9182 | 222 | public function edit($action) { |
6a488035 TO |
223 | // create a simple controller for editing price data |
224 | $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Field', ts('Price Field'), $action); | |
225 | ||
226 | // set the userContext stack | |
227 | $session = CRM_Core_Session::singleton(); | |
228 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid)); | |
229 | ||
230 | $controller->set('sid', $this->_sid); | |
231 | $controller->setEmbedded(TRUE); | |
232 | $controller->process(); | |
233 | $controller->run(); | |
234 | } | |
235 | ||
236 | /** | |
237 | * Run the page. | |
238 | * | |
239 | * This method is called after the page is created. It checks for the | |
240 | * type of action and executes that action. | |
241 | * | |
242 | * @param null | |
243 | * | |
244 | * @return void | |
6a488035 | 245 | */ |
00be9182 | 246 | public function run() { |
6a488035 TO |
247 | |
248 | // get the group id | |
249 | $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive', | |
250 | $this | |
251 | ); | |
252 | $fid = CRM_Utils_Request::retrieve('fid', 'Positive', | |
253 | $this, FALSE, 0 | |
254 | ); | |
255 | $action = CRM_Utils_Request::retrieve('action', 'String', | |
256 | // default to 'browse' | |
257 | $this, FALSE, 'browse' | |
258 | ); | |
259 | ||
260 | if ($this->_sid) { | |
9da8dc8c | 261 | $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($this->_sid); |
6a488035 | 262 | $this->assign('usedBy', $usedBy); |
9da8dc8c | 263 | $this->_isSetReserved= CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'is_reserved'); |
6a488035 | 264 | $this->assign('isReserved', $this->_isSetReserved); |
44c8822b | 265 | |
9da8dc8c | 266 | CRM_Price_BAO_PriceSet::checkPermission($this->_sid); |
6a488035 TO |
267 | $comps = array( |
268 | 'Event' => 'civicrm_event', | |
269 | 'Contribution' => 'civicrm_contribution_page', | |
c34e4bb4 | 270 | 'EventTemplate' => 'civicrm_event_template' |
6a488035 TO |
271 | ); |
272 | $priceSetContexts = array(); | |
273 | foreach ($comps as $name => $table) { | |
274 | if (array_key_exists($table, $usedBy)) { | |
275 | $priceSetContexts[] = $name; | |
276 | } | |
277 | } | |
278 | $this->assign('contexts', $priceSetContexts); | |
279 | } | |
280 | ||
281 | if ($action & (CRM_Core_Action::DELETE) && !$this->_isSetReserved) { | |
282 | if (empty($usedBy)) { | |
283 | // prompt to delete | |
284 | $session = CRM_Core_Session::singleton(); | |
285 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid)); | |
286 | $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_DeleteField', 'Delete Price Field', ''); | |
287 | $controller->set('fid', $fid); | |
288 | $controller->setEmbedded(TRUE); | |
289 | $controller->process(); | |
290 | $controller->run(); | |
291 | } | |
292 | else { | |
293 | // add breadcrumb | |
294 | $url = CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1'); | |
295 | CRM_Utils_System::appendBreadCrumb(ts('Price'), | |
296 | $url | |
297 | ); | |
9da8dc8c | 298 | $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceField::getTitle($fid)); |
6a488035 TO |
299 | } |
300 | } | |
301 | ||
e5e8ab99 CW |
302 | if ($action & CRM_Core_Action::DELETE) { |
303 | CRM_Utils_System::setTitle(ts('Delete Price Field')); | |
304 | } | |
305 | elseif ($this->_sid) { | |
9da8dc8c | 306 | $groupTitle = CRM_Price_BAO_PriceSet::getTitle($this->_sid); |
6a488035 TO |
307 | $this->assign('sid', $this->_sid); |
308 | $this->assign('groupTitle', $groupTitle); | |
309 | CRM_Utils_System::setTitle(ts('%1 - Price Fields', array(1 => $groupTitle))); | |
310 | } | |
311 | ||
312 | // assign vars to templates | |
313 | $this->assign('action', $action); | |
314 | ||
315 | // what action to take ? | |
316 | if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD) && !$this->_isSetReserved) { | |
317 | // no browse for edit/update/view | |
318 | $this->edit($action); | |
319 | } | |
320 | elseif ($action & CRM_Core_Action::PREVIEW) { | |
321 | $this->preview($fid); | |
322 | } | |
323 | else { | |
324 | $this->browse(); | |
325 | } | |
326 | ||
327 | // Call the parents run method | |
328 | return parent::run(); | |
329 | } | |
330 | ||
331 | /** | |
332 | * Preview price field | |
333 | * | |
100fef9d | 334 | * @param int $fid |
77b97be7 EM |
335 | * |
336 | * @internal param int $id price field id | |
6a488035 TO |
337 | * |
338 | * @return void | |
6a488035 | 339 | */ |
00be9182 | 340 | public function preview($fid) { |
6a488035 TO |
341 | $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Preview', ts('Preview Form Field'), CRM_Core_Action::PREVIEW); |
342 | $session = CRM_Core_Session::singleton(); | |
343 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid)); | |
344 | $controller->set('fieldId', $fid); | |
345 | $controller->set('groupId', $this->_sid); | |
346 | $controller->setEmbedded(TRUE); | |
347 | $controller->process(); | |
348 | $controller->run(); | |
349 | } | |
350 | } | |
351 |