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