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