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