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
19 * Create a page for displaying Custom Options.
21 * Heart of this class is the run method which checks
22 * for action type and then displays the appropriate
26 class CRM_Price_Page_Option
extends CRM_Core_Page
{
28 public $useLivePageJS = TRUE;
31 * The field id of the option.
38 * The field id of the option.
45 * The price set is reserved or not.
49 protected $_isSetReserved = FALSE;
52 * The action links that we need to display for the browse screen.
56 private static $_actionLinks;
59 * Get the action links for this page.
62 * array of action links that we need to display for the browse screen
64 public static function &actionLinks() {
65 if (!isset(self
::$_actionLinks)) {
66 self
::$_actionLinks = [
67 CRM_Core_Action
::UPDATE
=> [
68 'name' => ts('Edit Option'),
69 'url' => 'civicrm/admin/price/field/option',
70 'qs' => 'reset=1&action=update&oid=%%oid%%&fid=%%fid%%&sid=%%sid%%',
71 'title' => ts('Edit Price Option'),
73 CRM_Core_Action
::VIEW
=> [
75 'url' => 'civicrm/admin/price/field/option',
76 'qs' => 'action=view&oid=%%oid%%',
77 'title' => ts('View Price Option'),
79 CRM_Core_Action
::DISABLE
=> [
80 'name' => ts('Disable'),
81 'ref' => 'crm-enable-disable',
82 'title' => ts('Disable Price Option'),
84 CRM_Core_Action
::ENABLE
=> [
85 'name' => ts('Enable'),
86 'ref' => 'crm-enable-disable',
87 'title' => ts('Enable Price Option'),
89 CRM_Core_Action
::DELETE
=> [
90 'name' => ts('Delete'),
91 'url' => 'civicrm/admin/price/field/option',
92 'qs' => 'action=delete&oid=%%oid%%',
93 'title' => ts('Disable Price Option'),
97 return self
::$_actionLinks;
101 * Browse all price fields.
105 public function browse() {
106 $priceOptions = civicrm_api3('PriceFieldValue', 'get', [
107 'price_field_id' => $this->_fid
,
108 // Explicitly do not check permissions so we are not
109 // restricted by financial type, so we can change them.
110 'check_permissions' => FALSE,
113 'sort' => ['weight', 'label'],
116 $customOption = $priceOptions['values'];
118 // CRM-15378 - check if these price options are in an Event price set
120 $extendComponentId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid
, 'extends', 'id');
121 $allComponents = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $extendComponentId);
122 $eventComponentId = CRM_Core_Component
::getComponentID('CiviEvent');
123 if (in_array($eventComponentId, $allComponents)) {
127 $config = CRM_Core_Config
::singleton();
128 $taxRate = CRM_Core_PseudoConstant
::getTaxRates();
129 // display taxTerm for priceFields
130 $invoiceSettings = Civi
::settings()->get('contribution_invoice_settings');
131 $taxTerm = Civi
::settings()->get('tax_term');
132 $invoicing = $invoiceSettings['invoicing'] ??
NULL;
133 $getTaxDetails = FALSE;
134 foreach ($customOption as $id => $values) {
135 $action = array_sum(array_keys(self
::actionLinks()));
136 // Adding the required fields in the array
137 if (isset($taxRate[$values['financial_type_id']])) {
138 // Cast to float so trailing zero decimals are removed
139 $customOption[$id]['tax_rate'] = (float) $taxRate[$values['financial_type_id']];
140 if ($invoicing && isset($customOption[$id]['tax_rate'])) {
141 $getTaxDetails = TRUE;
143 $taxAmount = CRM_Contribute_BAO_Contribution_Utils
::calculateTaxAmount($customOption[$id]['amount'], $customOption[$id]['tax_rate']);
144 $customOption[$id]['tax_amount'] = $taxAmount['tax_amount'];
146 if (!empty($values['financial_type_id'])) {
147 $customOption[$id]['financial_type_id'] = CRM_Contribute_PseudoConstant
::financialType($values['financial_type_id']);
149 // update enable/disable links depending on price_field properties.
150 if ($this->_isSetReserved
) {
151 $action -= CRM_Core_Action
::UPDATE + CRM_Core_Action
::DELETE + CRM_Core_Action
::DISABLE + CRM_Core_Action
::ENABLE
;
154 if ($values['is_active']) {
155 $action -= CRM_Core_Action
::ENABLE
;
158 $action -= CRM_Core_Action
::DISABLE
;
161 $customOption[$id]['order'] = $customOption[$id]['weight'];
162 $customOption[$id]['action'] = CRM_Core_Action
::formLink(self
::actionLinks(), $action,
165 'fid' => $this->_fid
,
166 'sid' => $this->_sid
,
170 'priceFieldValue.row.actions',
175 // Add order changing widget to selector
176 $returnURL = CRM_Utils_System
::url('civicrm/admin/price/field/option', "action=browse&reset=1&fid={$this->_fid}&sid={$this->_sid}");
177 $filter = "price_field_id = {$this->_fid}";
178 CRM_Utils_Weight
::addOrder($customOption, 'CRM_Price_DAO_PriceFieldValue',
179 'id', $returnURL, $filter
182 $this->assign('taxTerm', $taxTerm);
183 $this->assign('getTaxDetails', $getTaxDetails);
184 $this->assign('customOption', $customOption);
185 $this->assign('sid', $this->_sid
);
186 $this->assign('isEvent', $isEvent);
190 * Edit custom Option.
192 * editing would involved modifying existing fields + adding data to new fields.
194 * @param string $action
195 * The action to be invoked.
199 public function edit($action) {
200 $oid = CRM_Utils_Request
::retrieve('oid', 'Positive',
205 $params['oid'] = $oid;
206 $sid = CRM_Price_BAO_PriceSet
::getSetId($params);
208 $usedBy = CRM_Price_BAO_PriceSet
::getUsedBy($sid);
210 // set the userContext stack
211 $session = CRM_Core_Session
::singleton();
213 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin/price/field/option',
214 "reset=1&action=browse&fid={$this->_fid}&sid={$this->_sid}"
216 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Option', ts('Price Field Option'), $action);
217 $controller->set('fid', $this->_fid
);
218 $controller->setEmbedded(TRUE);
219 $controller->process();
222 if ($action & CRM_Core_Action
::DELETE
) {
224 $url = CRM_Utils_System
::url('civicrm/admin/price/field/option', 'reset=1');
225 CRM_Utils_System
::appendBreadCrumb(ts('Price Option'),
228 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceFieldValue
::getOptionLabel($oid));
229 $this->assign('usedBy', $usedBy);
231 "Event" => "civicrm_event",
232 "Contribution" => "civicrm_contribution_page",
234 $priceSetContexts = [];
235 foreach ($comps as $name => $table) {
236 if (array_key_exists($table, $usedBy)) {
237 $priceSetContexts[] = $name;
240 $this->assign('contexts', $priceSetContexts);
247 * This method is called after the page is created. It checks for the
248 * type of action and executes that action.
252 public function run() {
254 $this->_fid
= CRM_Utils_Request
::retrieve('fid', 'Positive',
257 //get the price set id
259 $this->_sid
= CRM_Utils_Request
::retrieve('sid', 'Positive', $this);
263 CRM_Price_BAO_PriceSet
::checkPermission($this->_sid
);
264 $this->_isSetReserved
= CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid
, 'is_reserved');
265 $this->assign('isReserved', $this->_isSetReserved
);
267 //as url contain $sid so append breadcrumb dynamically.
270 'title' => ts('Price Fields'),
271 'url' => CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&sid=' . $this->_sid
),
274 CRM_Utils_System
::appendBreadCrumb($breadcrumb);
277 $fieldTitle = CRM_Price_BAO_PriceField
::getTitle($this->_fid
);
278 $this->assign('fid', $this->_fid
);
279 $this->assign('fieldTitle', $fieldTitle);
280 CRM_Utils_System
::setTitle(ts('%1 - Price Options', [1 => $fieldTitle]));
282 $htmlType = CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceField', $this->_fid
, 'html_type');
283 $this->assign('addMoreFields', TRUE);
284 //for text price field only single option present
285 if ($htmlType == 'Text') {
286 $this->assign('addMoreFields', FALSE);
290 // get the requested action
291 $action = CRM_Utils_Request
::retrieve('action', 'String',
292 // default to 'browse'
293 $this, FALSE, 'browse'
296 // assign vars to templates
297 $this->assign('action', $action);
299 $oid = CRM_Utils_Request
::retrieve('oid', 'Positive',
302 // what action to take ?
303 if ($action & (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD |
304 CRM_Core_Action
::VIEW | CRM_Core_Action
::DELETE
305 ) && !$this->_isSetReserved
307 // no browse for edit/update/view
308 $this->edit($action);
313 // Call the parents run method
314 return parent
::run();