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