Merge pull request #17118 from lcdservices/dev-core-1722
[civicrm-core.git] / CRM / Price / Page / Option.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 Custom Options.
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_Option extends CRM_Core_Page {
29
96f50de2
CW
30 public $useLivePageJS = TRUE;
31
6a488035 32 /**
fe482240 33 * The field id of the option.
6a488035
TO
34 *
35 * @var int
6a488035
TO
36 */
37 protected $_fid;
38
39 /**
fe482240 40 * The field id of the option.
6a488035
TO
41 *
42 * @var int
6a488035
TO
43 */
44 protected $_sid;
45
46 /**
fe482240 47 * The price set is reserved or not.
6a488035 48 *
b67daa72 49 * @var bool
6a488035 50 */
ba1dcfda 51 protected $_isSetReserved = FALSE;
366fe2a3 52
6a488035 53 /**
fe482240 54 * The action links that we need to display for the browse screen.
6a488035
TO
55 *
56 * @var array
6a488035
TO
57 */
58 private static $_actionLinks;
59
60 /**
61 * Get the action links for this page.
62 *
a6c01b45
CW
63 * @return array
64 * array of action links that we need to display for the browse screen
95ea96be 65 */
d1424f8f 66 public static function &actionLinks() {
6a488035 67 if (!isset(self::$_actionLinks)) {
be2fb01f
CW
68 self::$_actionLinks = [
69 CRM_Core_Action::UPDATE => [
6a488035
TO
70 'name' => ts('Edit Option'),
71 'url' => 'civicrm/admin/price/field/option',
72 'qs' => 'reset=1&action=update&oid=%%oid%%&fid=%%fid%%&sid=%%sid%%',
73 'title' => ts('Edit Price Option'),
be2fb01f
CW
74 ],
75 CRM_Core_Action::VIEW => [
6a488035
TO
76 'name' => ts('View'),
77 'url' => 'civicrm/admin/price/field/option',
78 'qs' => 'action=view&oid=%%oid%%',
79 'title' => ts('View Price Option'),
be2fb01f
CW
80 ],
81 CRM_Core_Action::DISABLE => [
6a488035 82 'name' => ts('Disable'),
4d17a233 83 'ref' => 'crm-enable-disable',
6a488035 84 'title' => ts('Disable Price Option'),
be2fb01f
CW
85 ],
86 CRM_Core_Action::ENABLE => [
6a488035 87 'name' => ts('Enable'),
4d17a233 88 'ref' => 'crm-enable-disable',
6a488035 89 'title' => ts('Enable Price Option'),
be2fb01f
CW
90 ],
91 CRM_Core_Action::DELETE => [
6a488035
TO
92 'name' => ts('Delete'),
93 'url' => 'civicrm/admin/price/field/option',
94 'qs' => 'action=delete&oid=%%oid%%',
95 'title' => ts('Disable Price Option'),
be2fb01f
CW
96 ],
97 ];
6a488035
TO
98 }
99 return self::$_actionLinks;
100 }
101
102 /**
103 * Browse all price fields.
104 *
6a488035 105 * @return void
6a488035 106 */
00be9182 107 public function browse() {
be2fb01f 108 $priceOptions = civicrm_api3('PriceFieldValue', 'get', [
971e129b 109 'price_field_id' => $this->_fid,
23dba589
SL
110 // Explicitly do not check permissions so we are not
111 // restricted by financial type, so we can change them.
971e129b
SL
112 'check_permissions' => FALSE,
113 'options' => [
114 'limit' => 0,
115 'sort' => ['weight', 'label'],
116 ],
be2fb01f 117 ]);
23dba589 118 $customOption = $priceOptions['values'];
a8e4ceb4
DG
119
120 // CRM-15378 - check if these price options are in an Event price set
121 $isEvent = FALSE;
122 $extendComponentId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'extends', 'id');
123 $allComponents = explode(CRM_Core_DAO::VALUE_SEPARATOR, $extendComponentId);
124 $eventComponentId = CRM_Core_Component::getComponentID('CiviEvent');
125 if (in_array($eventComponentId, $allComponents)) {
126 $isEvent = TRUE;
127 }
128
6a488035 129 $config = CRM_Core_Config::singleton();
a32709be 130 $taxRate = CRM_Core_PseudoConstant::getTaxRates();
03b412ae 131 // display taxTerm for priceFields
aaffa79f 132 $invoiceSettings = Civi::settings()->get('contribution_invoice_settings');
530f1e12 133 $taxTerm = Civi::settings()->get('tax_term');
9c1bc317 134 $invoicing = $invoiceSettings['invoicing'] ?? NULL;
a32709be 135 $getTaxDetails = FALSE;
6a488035 136 foreach ($customOption as $id => $values) {
d1424f8f 137 $action = array_sum(array_keys(self::actionLinks()));
a32709be
PB
138 // Adding the required fields in the array
139 if (isset($taxRate[$values['financial_type_id']])) {
cc3f313d
MW
140 // Cast to float so trailing zero decimals are removed
141 $customOption[$id]['tax_rate'] = (float) $taxRate[$values['financial_type_id']];
03b412ae 142 if ($invoicing && isset($customOption[$id]['tax_rate'])) {
a32709be
PB
143 $getTaxDetails = TRUE;
144 }
6fec0f2e 145 $taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($customOption[$id]['amount'], $customOption[$id]['tax_rate']);
a32709be
PB
146 $customOption[$id]['tax_amount'] = $taxAmount['tax_amount'];
147 }
1475d837
MW
148 if (!empty($values['financial_type_id'])) {
149 $customOption[$id]['financial_type_id'] = CRM_Contribute_PseudoConstant::financialType($values['financial_type_id']);
6a488035
TO
150 }
151 // update enable/disable links depending on price_field properties.
152 if ($this->_isSetReserved) {
153 $action -= CRM_Core_Action::UPDATE + CRM_Core_Action::DELETE + CRM_Core_Action::DISABLE + CRM_Core_Action::ENABLE;
154 }
155 else {
156 if ($values['is_active']) {
157 $action -= CRM_Core_Action::ENABLE;
158 }
159 else {
160 $action -= CRM_Core_Action::DISABLE;
161 }
162 }
6a488035
TO
163 $customOption[$id]['order'] = $customOption[$id]['weight'];
164 $customOption[$id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), $action,
be2fb01f 165 [
6a488035
TO
166 'oid' => $id,
167 'fid' => $this->_fid,
168 'sid' => $this->_sid,
be2fb01f 169 ],
87dab4a4
AH
170 ts('more'),
171 FALSE,
172 'priceFieldValue.row.actions',
173 'PriceFieldValue',
174 $id
6a488035
TO
175 );
176 }
177 // Add order changing widget to selector
178 $returnURL = CRM_Utils_System::url('civicrm/admin/price/field/option', "action=browse&reset=1&fid={$this->_fid}&sid={$this->_sid}");
179 $filter = "price_field_id = {$this->_fid}";
9da8dc8c 180 CRM_Utils_Weight::addOrder($customOption, 'CRM_Price_DAO_PriceFieldValue',
6a488035
TO
181 'id', $returnURL, $filter
182 );
183
03b412ae 184 $this->assign('taxTerm', $taxTerm);
a32709be 185 $this->assign('getTaxDetails', $getTaxDetails);
6a488035
TO
186 $this->assign('customOption', $customOption);
187 $this->assign('sid', $this->_sid);
a8e4ceb4 188 $this->assign('isEvent', $isEvent);
6a488035
TO
189 }
190
191 /**
100fef9d 192 * Edit custom Option.
6a488035
TO
193 *
194 * editing would involved modifying existing fields + adding data to new fields.
195 *
414c1420
TO
196 * @param string $action
197 * The action to be invoked.
6a488035
TO
198 *
199 * @return void
6a488035 200 */
00be9182 201 public function edit($action) {
6a488035
TO
202 $oid = CRM_Utils_Request::retrieve('oid', 'Positive',
203 $this, FALSE, 0
204 );
be2fb01f 205 $params = [];
6a488035
TO
206 if ($oid) {
207 $params['oid'] = $oid;
9da8dc8c 208 $sid = CRM_Price_BAO_PriceSet::getSetId($params);
6a488035 209
9da8dc8c 210 $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($sid);
6a488035
TO
211 }
212 // set the userContext stack
213 $session = CRM_Core_Session::singleton();
214
215 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field/option',
353ffa53
TO
216 "reset=1&action=browse&fid={$this->_fid}&sid={$this->_sid}"
217 ));
6a488035
TO
218 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Option', ts('Price Field Option'), $action);
219 $controller->set('fid', $this->_fid);
220 $controller->setEmbedded(TRUE);
221 $controller->process();
222 $controller->run();
223
6a488035
TO
224 if ($action & CRM_Core_Action::DELETE) {
225 // add breadcrumb
226 $url = CRM_Utils_System::url('civicrm/admin/price/field/option', 'reset=1');
227 CRM_Utils_System::appendBreadCrumb(ts('Price Option'),
228 $url
229 );
9da8dc8c 230 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceFieldValue::getOptionLabel($oid));
6a488035 231 $this->assign('usedBy', $usedBy);
be2fb01f 232 $comps = [
6a488035
TO
233 "Event" => "civicrm_event",
234 "Contribution" => "civicrm_contribution_page",
be2fb01f
CW
235 ];
236 $priceSetContexts = [];
6a488035
TO
237 foreach ($comps as $name => $table) {
238 if (array_key_exists($table, $usedBy)) {
239 $priceSetContexts[] = $name;
240 }
241 }
242 $this->assign('contexts', $priceSetContexts);
243 }
244 }
245
246 /**
247 * Run the page.
248 *
249 * This method is called after the page is created. It checks for the
250 * type of action and executes that action.
251 *
6a488035 252 * @return void
6a488035 253 */
00be9182 254 public function run() {
6a488035
TO
255 // get the field id
256 $this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive',
257 $this, FALSE, 0
258 );
259 //get the price set id
260 if (!$this->_sid) {
261 $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive', $this);
262 }
263
264 if ($this->_sid) {
9da8dc8c 265 CRM_Price_BAO_PriceSet::checkPermission($this->_sid);
ba1dcfda 266 $this->_isSetReserved = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'is_reserved');
6a488035
TO
267 $this->assign('isReserved', $this->_isSetReserved);
268 }
269 //as url contain $sid so append breadcrumb dynamically.
be2fb01f
CW
270 $breadcrumb = [
271 [
353ffa53 272 'title' => ts('Price Fields'),
6a488035 273 'url' => CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&sid=' . $this->_sid),
be2fb01f
CW
274 ],
275 ];
6a488035
TO
276 CRM_Utils_System::appendBreadCrumb($breadcrumb);
277
278 if ($this->_fid) {
9da8dc8c 279 $fieldTitle = CRM_Price_BAO_PriceField::getTitle($this->_fid);
6a488035
TO
280 $this->assign('fid', $this->_fid);
281 $this->assign('fieldTitle', $fieldTitle);
be2fb01f 282 CRM_Utils_System::setTitle(ts('%1 - Price Options', [1 => $fieldTitle]));
6a488035 283
9da8dc8c 284 $htmlType = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceField', $this->_fid, 'html_type');
6a488035
TO
285 $this->assign('addMoreFields', TRUE);
286 //for text price field only single option present
287 if ($htmlType == 'Text') {
288 $this->assign('addMoreFields', FALSE);
289 }
290 }
291
292 // get the requested action
293 $action = CRM_Utils_Request::retrieve('action', 'String',
294 // default to 'browse'
295 $this, FALSE, 'browse'
296 );
297
298 // assign vars to templates
299 $this->assign('action', $action);
300
301 $oid = CRM_Utils_Request::retrieve('oid', 'Positive',
302 $this, FALSE, 0
303 );
304 // what action to take ?
305 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD |
306 CRM_Core_Action::VIEW | CRM_Core_Action::DELETE
353ffa53
TO
307 ) && !$this->_isSetReserved
308 ) {
6a488035
TO
309 // no browse for edit/update/view
310 $this->edit($action);
311 }
312 else {
313 $this->browse();
314 }
315 // Call the parents run method
316 return parent::run();
317 }
96025800 318
6a488035 319}