Replace all instances of check.gif appearing in listings
[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 }
f4388b57 163 $customOption[$id]['is_default'] = CRM_Core_Page::crmIcon('fa-check', ts('Default'), !empty($customOption[$id]['is_default']));
6a488035
TO
164 $customOption[$id]['order'] = $customOption[$id]['weight'];
165 $customOption[$id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), $action,
be2fb01f 166 [
6a488035
TO
167 'oid' => $id,
168 'fid' => $this->_fid,
169 'sid' => $this->_sid,
be2fb01f 170 ],
87dab4a4
AH
171 ts('more'),
172 FALSE,
173 'priceFieldValue.row.actions',
174 'PriceFieldValue',
175 $id
6a488035
TO
176 );
177 }
178 // Add order changing widget to selector
179 $returnURL = CRM_Utils_System::url('civicrm/admin/price/field/option', "action=browse&reset=1&fid={$this->_fid}&sid={$this->_sid}");
180 $filter = "price_field_id = {$this->_fid}";
9da8dc8c 181 CRM_Utils_Weight::addOrder($customOption, 'CRM_Price_DAO_PriceFieldValue',
6a488035
TO
182 'id', $returnURL, $filter
183 );
184
03b412ae 185 $this->assign('taxTerm', $taxTerm);
a32709be 186 $this->assign('getTaxDetails', $getTaxDetails);
6a488035
TO
187 $this->assign('customOption', $customOption);
188 $this->assign('sid', $this->_sid);
a8e4ceb4 189 $this->assign('isEvent', $isEvent);
6a488035
TO
190 }
191
192 /**
100fef9d 193 * Edit custom Option.
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
TO
199 *
200 * @return void
6a488035 201 */
00be9182 202 public function edit($action) {
6a488035
TO
203 $oid = CRM_Utils_Request::retrieve('oid', 'Positive',
204 $this, FALSE, 0
205 );
be2fb01f 206 $params = [];
6a488035
TO
207 if ($oid) {
208 $params['oid'] = $oid;
9da8dc8c 209 $sid = CRM_Price_BAO_PriceSet::getSetId($params);
6a488035 210
9da8dc8c 211 $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($sid);
6a488035
TO
212 }
213 // set the userContext stack
214 $session = CRM_Core_Session::singleton();
215
216 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field/option',
353ffa53
TO
217 "reset=1&action=browse&fid={$this->_fid}&sid={$this->_sid}"
218 ));
6a488035
TO
219 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Option', ts('Price Field Option'), $action);
220 $controller->set('fid', $this->_fid);
221 $controller->setEmbedded(TRUE);
222 $controller->process();
223 $controller->run();
224
6a488035
TO
225 if ($action & CRM_Core_Action::DELETE) {
226 // add breadcrumb
227 $url = CRM_Utils_System::url('civicrm/admin/price/field/option', 'reset=1');
228 CRM_Utils_System::appendBreadCrumb(ts('Price Option'),
229 $url
230 );
9da8dc8c 231 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceFieldValue::getOptionLabel($oid));
6a488035 232 $this->assign('usedBy', $usedBy);
be2fb01f 233 $comps = [
6a488035
TO
234 "Event" => "civicrm_event",
235 "Contribution" => "civicrm_contribution_page",
be2fb01f
CW
236 ];
237 $priceSetContexts = [];
6a488035
TO
238 foreach ($comps as $name => $table) {
239 if (array_key_exists($table, $usedBy)) {
240 $priceSetContexts[] = $name;
241 }
242 }
243 $this->assign('contexts', $priceSetContexts);
244 }
245 }
246
247 /**
248 * Run the page.
249 *
250 * This method is called after the page is created. It checks for the
251 * type of action and executes that action.
252 *
6a488035 253 * @return void
6a488035 254 */
00be9182 255 public function run() {
6a488035
TO
256 // get the field id
257 $this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive',
258 $this, FALSE, 0
259 );
260 //get the price set id
261 if (!$this->_sid) {
262 $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive', $this);
263 }
264
265 if ($this->_sid) {
9da8dc8c 266 CRM_Price_BAO_PriceSet::checkPermission($this->_sid);
ba1dcfda 267 $this->_isSetReserved = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'is_reserved');
6a488035
TO
268 $this->assign('isReserved', $this->_isSetReserved);
269 }
270 //as url contain $sid so append breadcrumb dynamically.
be2fb01f
CW
271 $breadcrumb = [
272 [
353ffa53 273 'title' => ts('Price Fields'),
6a488035 274 'url' => CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&sid=' . $this->_sid),
be2fb01f
CW
275 ],
276 ];
6a488035
TO
277 CRM_Utils_System::appendBreadCrumb($breadcrumb);
278
279 if ($this->_fid) {
9da8dc8c 280 $fieldTitle = CRM_Price_BAO_PriceField::getTitle($this->_fid);
6a488035
TO
281 $this->assign('fid', $this->_fid);
282 $this->assign('fieldTitle', $fieldTitle);
be2fb01f 283 CRM_Utils_System::setTitle(ts('%1 - Price Options', [1 => $fieldTitle]));
6a488035 284
9da8dc8c 285 $htmlType = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceField', $this->_fid, 'html_type');
6a488035
TO
286 $this->assign('addMoreFields', TRUE);
287 //for text price field only single option present
288 if ($htmlType == 'Text') {
289 $this->assign('addMoreFields', FALSE);
290 }
291 }
292
293 // get the requested action
294 $action = CRM_Utils_Request::retrieve('action', 'String',
295 // default to 'browse'
296 $this, FALSE, 'browse'
297 );
298
299 // assign vars to templates
300 $this->assign('action', $action);
301
302 $oid = CRM_Utils_Request::retrieve('oid', 'Positive',
303 $this, FALSE, 0
304 );
305 // what action to take ?
306 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD |
307 CRM_Core_Action::VIEW | CRM_Core_Action::DELETE
353ffa53
TO
308 ) && !$this->_isSetReserved
309 ) {
6a488035
TO
310 // no browse for edit/update/view
311 $this->edit($action);
312 }
313 else {
314 $this->browse();
315 }
316 // Call the parents run method
317 return parent::run();
318 }
96025800 319
6a488035 320}