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 Price Fields.
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_Field
extends CRM_Core_Page
{
30 public $useLivePageJS = TRUE;
33 * The price set group id of the field.
40 * The action links that we need to display for the browse screen.
44 private static $_actionLinks;
47 * The price set is reserved or not.
51 protected $_isSetReserved = FALSE;
54 * Get the action links for this page.
57 * array of action links that we need to display for the browse screen
59 public static function &actionLinks() {
60 if (!isset(self
::$_actionLinks)) {
61 self
::$_actionLinks = [
62 CRM_Core_Action
::UPDATE
=> [
63 'name' => ts('Edit Price Field'),
64 'url' => 'civicrm/admin/price/field',
65 'qs' => 'action=update&reset=1&sid=%%sid%%&fid=%%fid%%',
66 'title' => ts('Edit Price'),
68 CRM_Core_Action
::PREVIEW
=> [
69 'name' => ts('Preview Field'),
70 'url' => 'civicrm/admin/price/field',
71 'qs' => 'action=preview&reset=1&sid=%%sid%%&fid=%%fid%%',
72 'title' => ts('Preview Price'),
74 CRM_Core_Action
::DISABLE
=> [
75 'name' => ts('Disable'),
76 'ref' => 'crm-enable-disable',
77 'title' => ts('Disable Price'),
79 CRM_Core_Action
::ENABLE
=> [
80 'name' => ts('Enable'),
81 'ref' => 'crm-enable-disable',
82 'title' => ts('Enable Price'),
84 CRM_Core_Action
::DELETE
=> [
85 'name' => ts('Delete'),
86 'url' => 'civicrm/admin/price/field',
87 'qs' => 'action=delete&reset=1&sid=%%sid%%&fid=%%fid%%',
88 'title' => ts('Delete Price'),
92 return self
::$_actionLinks;
96 * Browse all price set fields.
98 public function browse() {
99 $resourceManager = CRM_Core_Resources
::singleton();
100 if (!empty($_GET['new']) && $resourceManager->ajaxPopupsEnabled
) {
101 $resourceManager->addScriptFile('civicrm', 'js/crm.addNew.js', 999, 'html-header');
105 $priceFieldBAO = new CRM_Price_BAO_PriceField();
108 $priceFieldBAO->price_set_id
= $this->_sid
;
109 $priceFieldBAO->orderBy('weight, label');
110 $priceFieldBAO->find();
112 // display taxTerm for priceFields
113 $invoiceSettings = Civi
::settings()->get('contribution_invoice_settings');
114 $taxTerm = Civi
::settings()->get('tax_term');
115 $invoicing = $invoiceSettings['invoicing'] ??
NULL;
116 $getTaxDetails = FALSE;
117 $taxRate = CRM_Core_PseudoConstant
::getTaxRates();
118 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes);
119 while ($priceFieldBAO->fetch()) {
120 $priceField[$priceFieldBAO->id
] = [];
121 CRM_Core_DAO
::storeValues($priceFieldBAO, $priceField[$priceFieldBAO->id
]);
123 // get price if it's a text field
124 if ($priceFieldBAO->html_type
== 'Text') {
126 $params = ['price_field_id' => $priceFieldBAO->id
];
128 CRM_Price_BAO_PriceFieldValue
::retrieve($params, $optionValues);
129 $priceField[$priceFieldBAO->id
]['price'] = $optionValues['amount'] ??
NULL;
130 $financialTypeId = $optionValues['financial_type_id'];
131 if ($invoicing && isset($taxRate[$financialTypeId])) {
132 $priceField[$priceFieldBAO->id
]['tax_rate'] = $taxRate[$financialTypeId];
133 $getTaxDetails = TRUE;
135 if (isset($priceField[$priceFieldBAO->id
]['tax_rate'])) {
136 $taxAmount = CRM_Contribute_BAO_Contribution_Utils
::calculateTaxAmount($priceField[$priceFieldBAO->id
]['price'], $priceField[$priceFieldBAO->id
]['tax_rate']);
137 $priceField[$priceFieldBAO->id
]['tax_amount'] = $taxAmount['tax_amount'];
141 $action = array_sum(array_keys(self
::actionLinks()));
143 if ($this->_isSetReserved
) {
144 $action -= CRM_Core_Action
::UPDATE + CRM_Core_Action
::DELETE + CRM_Core_Action
::ENABLE + CRM_Core_Action
::DISABLE
;
147 if ($priceFieldBAO->is_active
) {
148 $action -= CRM_Core_Action
::ENABLE
;
151 $action -= CRM_Core_Action
::DISABLE
;
155 if ($priceFieldBAO->active_on
== '0000-00-00 00:00:00') {
156 $priceField[$priceFieldBAO->id
]['active_on'] = '';
159 if ($priceFieldBAO->expire_on
== '0000-00-00 00:00:00') {
160 $priceField[$priceFieldBAO->id
]['expire_on'] = '';
163 // need to translate html types from the db
164 $htmlTypes = CRM_Price_BAO_PriceField
::htmlTypes();
165 $priceField[$priceFieldBAO->id
]['html_type_display'] = $htmlTypes[$priceField[$priceFieldBAO->id
]['html_type']];
166 $priceField[$priceFieldBAO->id
]['order'] = $priceField[$priceFieldBAO->id
]['weight'];
167 $priceField[$priceFieldBAO->id
]['action'] = CRM_Core_Action
::formLink(
171 'fid' => $priceFieldBAO->id
,
172 'sid' => $this->_sid
,
176 'priceField.row.actions',
180 $this->assign('taxTerm', $taxTerm);
181 $this->assign('getTaxDetails', $getTaxDetails);
184 $returnURL = CRM_Utils_System
::url('civicrm/admin/price/field', "reset=1&action=browse&sid={$this->_sid}");
185 $filter = "price_set_id = {$this->_sid}";
186 CRM_Utils_Weight
::addOrder($priceField, 'CRM_Price_DAO_PriceField',
187 'id', $returnURL, $filter
189 $this->assign('priceField', $priceField);
195 * editing would involved modifying existing fields + adding data to new fields.
197 * @param string $action
198 * The action to be invoked.
200 public function edit($action) {
201 // create a simple controller for editing price data
202 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Field', ts('Price Field'), $action);
204 // set the userContext stack
205 $session = CRM_Core_Session
::singleton();
206 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid
));
208 $controller->set('sid', $this->_sid
);
209 $controller->setEmbedded(TRUE);
210 $controller->process();
217 * This method is called after the page is created. It checks for the
218 * type of action and executes that action.
222 public function run() {
225 $this->_sid
= CRM_Utils_Request
::retrieve('sid', 'Positive',
228 $fid = CRM_Utils_Request
::retrieve('fid', 'Positive',
231 $action = CRM_Utils_Request
::retrieve('action', 'String',
232 // default to 'browse'
233 $this, FALSE, 'browse'
237 $usedBy = CRM_Price_BAO_PriceSet
::getUsedBy($this->_sid
);
238 $this->assign('usedBy', $usedBy);
239 $this->_isSetReserved
= CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid
, 'is_reserved');
240 $this->assign('isReserved', $this->_isSetReserved
);
242 CRM_Price_BAO_PriceSet
::checkPermission($this->_sid
);
244 'Event' => 'civicrm_event',
245 'Contribution' => 'civicrm_contribution_page',
246 'EventTemplate' => 'civicrm_event_template',
248 $priceSetContexts = [];
249 foreach ($comps as $name => $table) {
250 if (array_key_exists($table, $usedBy)) {
251 $priceSetContexts[] = $name;
254 $this->assign('contexts', $priceSetContexts);
257 if ($action & (CRM_Core_Action
::DELETE
) && !$this->_isSetReserved
) {
258 if (empty($usedBy)) {
260 $session = CRM_Core_Session
::singleton();
261 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid
));
262 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_DeleteField', 'Delete Price Field', '');
263 $controller->set('fid', $fid);
264 $controller->setEmbedded(TRUE);
265 $controller->process();
270 $url = CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1');
271 CRM_Utils_System
::appendBreadCrumb(ts('Price'),
274 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceField
::getTitle($fid));
278 if ($action & CRM_Core_Action
::DELETE
) {
279 CRM_Utils_System
::setTitle(ts('Delete Price Field'));
281 elseif ($this->_sid
) {
282 $groupTitle = CRM_Price_BAO_PriceSet
::getTitle($this->_sid
);
283 $this->assign('sid', $this->_sid
);
284 $this->assign('groupTitle', $groupTitle);
285 CRM_Utils_System
::setTitle(ts('%1 - Price Fields', [1 => $groupTitle]));
288 // assign vars to templates
289 $this->assign('action', $action);
291 // what action to take ?
292 if ($action & (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD
) && !$this->_isSetReserved
) {
293 // no browse for edit/update/view
294 $this->edit($action);
296 elseif ($action & CRM_Core_Action
::PREVIEW
) {
297 $this->preview($fid);
303 // Call the parents run method
304 return parent
::run();
308 * Preview price field.
312 * @internal param int $id price field id
316 public function preview($fid) {
317 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Preview', ts('Preview Form Field'), CRM_Core_Action
::PREVIEW
);
318 $session = CRM_Core_Session
::singleton();
319 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid
));
320 $controller->set('fieldId', $fid);
321 $controller->set('groupId', $this->_sid
);
322 $controller->setEmbedded(TRUE);
323 $controller->process();