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