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 Price Fields.
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_Field
extends CRM_Core_Page
{
28 public $useLivePageJS = TRUE;
31 * The price set group id of the field.
38 * The action links that we need to display for the browse screen.
42 private static $_actionLinks;
45 * The price set is reserved or not.
49 protected $_isSetReserved = FALSE;
52 * Get the action links for this page.
55 * array of action links that we need to display for the browse screen
57 public static function &actionLinks() {
58 if (!isset(self
::$_actionLinks)) {
59 self
::$_actionLinks = [
60 CRM_Core_Action
::UPDATE
=> [
61 'name' => ts('Edit Price Field'),
62 'url' => 'civicrm/admin/price/field',
63 'qs' => 'action=update&reset=1&sid=%%sid%%&fid=%%fid%%',
64 'title' => ts('Edit Price'),
66 CRM_Core_Action
::PREVIEW
=> [
67 'name' => ts('Preview Field'),
68 'url' => 'civicrm/admin/price/field',
69 'qs' => 'action=preview&reset=1&sid=%%sid%%&fid=%%fid%%',
70 'title' => ts('Preview Price'),
72 CRM_Core_Action
::DISABLE
=> [
73 'name' => ts('Disable'),
74 'ref' => 'crm-enable-disable',
75 'title' => ts('Disable Price'),
77 CRM_Core_Action
::ENABLE
=> [
78 'name' => ts('Enable'),
79 'ref' => 'crm-enable-disable',
80 'title' => ts('Enable Price'),
82 CRM_Core_Action
::DELETE
=> [
83 'name' => ts('Delete'),
84 'url' => 'civicrm/admin/price/field',
85 'qs' => 'action=delete&reset=1&sid=%%sid%%&fid=%%fid%%',
86 'title' => ts('Delete Price'),
90 return self
::$_actionLinks;
94 * Browse all price set fields.
96 public function browse() {
97 $resourceManager = CRM_Core_Resources
::singleton();
98 if (!empty($_GET['new']) && $resourceManager->ajaxPopupsEnabled
) {
99 $resourceManager->addScriptFile('civicrm', 'js/crm.addNew.js', 999, 'html-header');
103 $priceFieldBAO = new CRM_Price_BAO_PriceField();
106 $priceFieldBAO->price_set_id
= $this->_sid
;
107 $priceFieldBAO->orderBy('weight, label');
108 $priceFieldBAO->find();
110 // display taxTerm for priceFields
111 $invoiceSettings = Civi
::settings()->get('contribution_invoice_settings');
112 $taxTerm = Civi
::settings()->get('tax_term');
113 $invoicing = $invoiceSettings['invoicing'] ??
NULL;
114 $getTaxDetails = FALSE;
115 $taxRate = CRM_Core_PseudoConstant
::getTaxRates();
116 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes);
117 while ($priceFieldBAO->fetch()) {
118 $priceField[$priceFieldBAO->id
] = [];
119 CRM_Core_DAO
::storeValues($priceFieldBAO, $priceField[$priceFieldBAO->id
]);
121 // get price if it's a text field
122 if ($priceFieldBAO->html_type
== 'Text') {
124 $params = ['price_field_id' => $priceFieldBAO->id
];
126 CRM_Price_BAO_PriceFieldValue
::retrieve($params, $optionValues);
127 $priceField[$priceFieldBAO->id
]['price'] = $optionValues['amount'] ??
NULL;
128 $financialTypeId = $optionValues['financial_type_id'];
129 if ($invoicing && isset($taxRate[$financialTypeId])) {
130 $priceField[$priceFieldBAO->id
]['tax_rate'] = $taxRate[$financialTypeId];
131 $getTaxDetails = TRUE;
133 if (isset($priceField[$priceFieldBAO->id
]['tax_rate'])) {
134 $taxAmount = CRM_Contribute_BAO_Contribution_Utils
::calculateTaxAmount($priceField[$priceFieldBAO->id
]['price'], $priceField[$priceFieldBAO->id
]['tax_rate']);
135 $priceField[$priceFieldBAO->id
]['tax_amount'] = $taxAmount['tax_amount'];
139 $action = array_sum(array_keys(self
::actionLinks()));
141 if ($this->_isSetReserved
) {
142 $action -= CRM_Core_Action
::UPDATE + CRM_Core_Action
::DELETE + CRM_Core_Action
::ENABLE + CRM_Core_Action
::DISABLE
;
145 if ($priceFieldBAO->is_active
) {
146 $action -= CRM_Core_Action
::ENABLE
;
149 $action -= CRM_Core_Action
::DISABLE
;
153 if ($priceFieldBAO->active_on
== '0000-00-00 00:00:00') {
154 $priceField[$priceFieldBAO->id
]['active_on'] = '';
157 if ($priceFieldBAO->expire_on
== '0000-00-00 00:00:00') {
158 $priceField[$priceFieldBAO->id
]['expire_on'] = '';
161 // need to translate html types from the db
162 $htmlTypes = CRM_Price_BAO_PriceField
::htmlTypes();
163 $priceField[$priceFieldBAO->id
]['html_type_display'] = $htmlTypes[$priceField[$priceFieldBAO->id
]['html_type']];
164 $priceField[$priceFieldBAO->id
]['order'] = $priceField[$priceFieldBAO->id
]['weight'];
165 $priceField[$priceFieldBAO->id
]['action'] = CRM_Core_Action
::formLink(
169 'fid' => $priceFieldBAO->id
,
170 'sid' => $this->_sid
,
174 'priceField.row.actions',
178 $this->assign('taxTerm', $taxTerm);
179 $this->assign('getTaxDetails', $getTaxDetails);
182 $returnURL = CRM_Utils_System
::url('civicrm/admin/price/field', "reset=1&action=browse&sid={$this->_sid}");
183 $filter = "price_set_id = {$this->_sid}";
184 CRM_Utils_Weight
::addOrder($priceField, 'CRM_Price_DAO_PriceField',
185 'id', $returnURL, $filter
187 $this->assign('priceField', $priceField);
193 * editing would involved modifying existing fields + adding data to new fields.
195 * @param string $action
196 * The action to be invoked.
198 public function edit($action) {
199 // create a simple controller for editing price data
200 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Field', ts('Price Field'), $action);
202 // set the userContext stack
203 $session = CRM_Core_Session
::singleton();
204 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid
));
206 $controller->set('sid', $this->_sid
);
207 $controller->setEmbedded(TRUE);
208 $controller->process();
215 * This method is called after the page is created. It checks for the
216 * type of action and executes that action.
220 public function run() {
223 $this->_sid
= CRM_Utils_Request
::retrieve('sid', 'Positive',
226 $fid = CRM_Utils_Request
::retrieve('fid', 'Positive',
229 $action = CRM_Utils_Request
::retrieve('action', 'String',
230 // default to 'browse'
231 $this, FALSE, 'browse'
235 $usedBy = CRM_Price_BAO_PriceSet
::getUsedBy($this->_sid
);
236 $this->assign('usedBy', $usedBy);
237 $this->_isSetReserved
= CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid
, 'is_reserved');
238 $this->assign('isReserved', $this->_isSetReserved
);
240 CRM_Price_BAO_PriceSet
::checkPermission($this->_sid
);
242 'Event' => 'civicrm_event',
243 'Contribution' => 'civicrm_contribution_page',
244 'EventTemplate' => 'civicrm_event_template',
246 $priceSetContexts = [];
247 foreach ($comps as $name => $table) {
248 if (array_key_exists($table, $usedBy)) {
249 $priceSetContexts[] = $name;
252 $this->assign('contexts', $priceSetContexts);
255 if ($action & (CRM_Core_Action
::DELETE
) && !$this->_isSetReserved
) {
256 if (empty($usedBy)) {
258 $session = CRM_Core_Session
::singleton();
259 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid
));
260 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_DeleteField', 'Delete Price Field', '');
261 $controller->set('fid', $fid);
262 $controller->setEmbedded(TRUE);
263 $controller->process();
268 $url = CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1');
269 CRM_Utils_System
::appendBreadCrumb(ts('Price'),
272 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceField
::getTitle($fid));
276 if ($action & CRM_Core_Action
::DELETE
) {
277 CRM_Utils_System
::setTitle(ts('Delete Price Field'));
279 elseif ($this->_sid
) {
280 $groupTitle = CRM_Price_BAO_PriceSet
::getTitle($this->_sid
);
281 $this->assign('sid', $this->_sid
);
282 $this->assign('groupTitle', $groupTitle);
283 CRM_Utils_System
::setTitle(ts('%1 - Price Fields', [1 => $groupTitle]));
286 // assign vars to templates
287 $this->assign('action', $action);
289 // what action to take ?
290 if ($action & (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD
) && !$this->_isSetReserved
) {
291 // no browse for edit/update/view
292 $this->edit($action);
294 elseif ($action & CRM_Core_Action
::PREVIEW
) {
295 $this->preview($fid);
301 // Call the parents run method
302 return parent
::run();
306 * Preview price field.
310 * @internal param int $id price field id
314 public function preview($fid) {
315 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Preview', ts('Preview Form Field'), CRM_Core_Action
::PREVIEW
);
316 $session = CRM_Core_Session
::singleton();
317 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid
));
318 $controller->set('fieldId', $fid);
319 $controller->set('groupId', $this->_sid
);
320 $controller->setEmbedded(TRUE);
321 $controller->process();