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