3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 * Create a page for displaying Custom Options.
23 * Heart of this class is the run method which checks
24 * for action type and then displays the appropriate
28 class CRM_Price_Page_Option
extends CRM_Core_Page
{
30 public $useLivePageJS = TRUE;
33 * The field id of the option.
40 * The field id of the option.
47 * The price set is reserved or not.
51 protected $_isSetReserved = FALSE;
54 * The action links that we need to display for the browse screen.
58 private static $_actionLinks;
61 * Get the action links for this page.
64 * array of action links that we need to display for the browse screen
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'),
75 CRM_Core_Action
::VIEW
=> [
77 'url' => 'civicrm/admin/price/field/option',
78 'qs' => 'action=view&oid=%%oid%%',
79 'title' => ts('View Price Option'),
81 CRM_Core_Action
::DISABLE
=> [
82 'name' => ts('Disable'),
83 'ref' => 'crm-enable-disable',
84 'title' => ts('Disable Price Option'),
86 CRM_Core_Action
::ENABLE
=> [
87 'name' => ts('Enable'),
88 'ref' => 'crm-enable-disable',
89 'title' => ts('Enable Price Option'),
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'),
99 return self
::$_actionLinks;
103 * Browse all price fields.
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,
115 'sort' => ['weight', 'label'],
118 $customOption = $priceOptions['values'];
120 // CRM-15378 - check if these price options are in an Event price set
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)) {
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;
145 $taxAmount = CRM_Contribute_BAO_Contribution_Utils
::calculateTaxAmount($customOption[$id]['amount'], $customOption[$id]['tax_rate'], TRUE);
146 $customOption[$id]['tax_amount'] = $taxAmount['tax_amount'];
148 if (!empty($values['financial_type_id'])) {
149 $customOption[$id]['financial_type_id'] = CRM_Contribute_PseudoConstant
::financialType($values['financial_type_id']);
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
;
156 if ($values['is_active']) {
157 $action -= CRM_Core_Action
::ENABLE
;
160 $action -= CRM_Core_Action
::DISABLE
;
163 if (!empty($customOption[$id]['is_default'])) {
164 $customOption[$id]['is_default'] = '<img src="' . $config->resourceBase
. 'i/check.gif" />';
167 $customOption[$id]['is_default'] = '';
169 $customOption[$id]['order'] = $customOption[$id]['weight'];
170 $customOption[$id]['action'] = CRM_Core_Action
::formLink(self
::actionLinks(), $action,
173 'fid' => $this->_fid
,
174 'sid' => $this->_sid
,
178 'priceFieldValue.row.actions',
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
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);
198 * Edit custom Option.
200 * editing would involved modifying existing fields + adding data to new fields.
202 * @param string $action
203 * The action to be invoked.
207 public function edit($action) {
208 $oid = CRM_Utils_Request
::retrieve('oid', 'Positive',
213 $params['oid'] = $oid;
214 $sid = CRM_Price_BAO_PriceSet
::getSetId($params);
216 $usedBy = CRM_Price_BAO_PriceSet
::getUsedBy($sid);
218 // set the userContext stack
219 $session = CRM_Core_Session
::singleton();
221 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin/price/field/option',
222 "reset=1&action=browse&fid={$this->_fid}&sid={$this->_sid}"
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();
230 if ($action & CRM_Core_Action
::DELETE
) {
232 $url = CRM_Utils_System
::url('civicrm/admin/price/field/option', 'reset=1');
233 CRM_Utils_System
::appendBreadCrumb(ts('Price Option'),
236 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceFieldValue
::getOptionLabel($oid));
237 $this->assign('usedBy', $usedBy);
239 "Event" => "civicrm_event",
240 "Contribution" => "civicrm_contribution_page",
242 $priceSetContexts = [];
243 foreach ($comps as $name => $table) {
244 if (array_key_exists($table, $usedBy)) {
245 $priceSetContexts[] = $name;
248 $this->assign('contexts', $priceSetContexts);
255 * This method is called after the page is created. It checks for the
256 * type of action and executes that action.
260 public function run() {
262 $this->_fid
= CRM_Utils_Request
::retrieve('fid', 'Positive',
265 //get the price set id
267 $this->_sid
= CRM_Utils_Request
::retrieve('sid', 'Positive', $this);
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
);
275 //as url contain $sid so append breadcrumb dynamically.
278 'title' => ts('Price Fields'),
279 'url' => CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&sid=' . $this->_sid
),
282 CRM_Utils_System
::appendBreadCrumb($breadcrumb);
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]));
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);
298 // get the requested action
299 $action = CRM_Utils_Request
::retrieve('action', 'String',
300 // default to 'browse'
301 $this, FALSE, 'browse'
304 // assign vars to templates
305 $this->assign('action', $action);
307 $oid = CRM_Utils_Request
::retrieve('oid', 'Positive',
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
315 // no browse for edit/update/view
316 $this->edit($action);
321 // Call the parents run method
322 return parent
::run();