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 $taxTerm = Civi
::settings()->get('tax_term');
112 $getTaxDetails = FALSE;
113 $taxRate = CRM_Core_PseudoConstant
::getTaxRates();
114 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes);
115 while ($priceFieldBAO->fetch()) {
116 $priceField[$priceFieldBAO->id
] = [];
117 CRM_Core_DAO
::storeValues($priceFieldBAO, $priceField[$priceFieldBAO->id
]);
119 // get price if it's a text field
120 if ($priceFieldBAO->html_type
== 'Text') {
122 $params = ['price_field_id' => $priceFieldBAO->id
];
124 CRM_Price_BAO_PriceFieldValue
::retrieve($params, $optionValues);
125 $priceField[$priceFieldBAO->id
]['price'] = $optionValues['amount'] ??
NULL;
126 $financialTypeId = $optionValues['financial_type_id'];
127 if (Civi
::settings()->get('invoicing') && isset($taxRate[$financialTypeId])) {
128 $priceField[$priceFieldBAO->id
]['tax_rate'] = $taxRate[$financialTypeId];
129 $getTaxDetails = TRUE;
131 if (isset($priceField[$priceFieldBAO->id
]['tax_rate'])) {
132 $taxAmount = CRM_Contribute_BAO_Contribution_Utils
::calculateTaxAmount($priceField[$priceFieldBAO->id
]['price'], $priceField[$priceFieldBAO->id
]['tax_rate']);
133 $priceField[$priceFieldBAO->id
]['tax_amount'] = $taxAmount['tax_amount'];
137 $action = array_sum(array_keys(self
::actionLinks()));
139 if ($this->_isSetReserved
) {
140 $action -= CRM_Core_Action
::UPDATE + CRM_Core_Action
::DELETE + CRM_Core_Action
::ENABLE + CRM_Core_Action
::DISABLE
;
143 if ($priceFieldBAO->is_active
) {
144 $action -= CRM_Core_Action
::ENABLE
;
147 $action -= CRM_Core_Action
::DISABLE
;
151 if (!isset($priceField[$priceFieldBAO->id
]['active_on']) ||
$priceFieldBAO->active_on
== '0000-00-00 00:00:00') {
152 $priceField[$priceFieldBAO->id
]['active_on'] = '';
155 if (!isset($priceField[$priceFieldBAO->id
]['expire_on']) ||
$priceFieldBAO->expire_on
== '0000-00-00 00:00:00') {
156 $priceField[$priceFieldBAO->id
]['expire_on'] = '';
159 // need to translate html types from the db
160 $htmlTypes = CRM_Price_BAO_PriceField
::htmlTypes();
161 $priceField[$priceFieldBAO->id
]['html_type_display'] = $htmlTypes[$priceField[$priceFieldBAO->id
]['html_type']];
162 $priceField[$priceFieldBAO->id
]['order'] = $priceField[$priceFieldBAO->id
]['weight'];
163 $priceField[$priceFieldBAO->id
]['action'] = CRM_Core_Action
::formLink(
167 'fid' => $priceFieldBAO->id
,
168 'sid' => $this->_sid
,
172 'priceField.row.actions',
176 $this->assign('taxTerm', $taxTerm);
177 $this->assign('getTaxDetails', $getTaxDetails);
180 $returnURL = CRM_Utils_System
::url('civicrm/admin/price/field', "reset=1&action=browse&sid={$this->_sid}");
181 $filter = "price_set_id = {$this->_sid}";
182 CRM_Utils_Weight
::addOrder($priceField, 'CRM_Price_DAO_PriceField',
183 'id', $returnURL, $filter
185 $this->assign('priceField', $priceField);
191 * editing would involved modifying existing fields + adding data to new fields.
193 * @param string $action
194 * The action to be invoked.
196 public function edit($action) {
197 // create a simple controller for editing price data
198 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Field', ts('Price Field'), $action);
200 // set the userContext stack
201 $session = CRM_Core_Session
::singleton();
202 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid
));
204 $controller->set('sid', $this->_sid
);
205 $controller->setEmbedded(TRUE);
206 $controller->process();
213 * This method is called after the page is created. It checks for the
214 * type of action and executes that action.
218 public function run() {
221 $this->_sid
= CRM_Utils_Request
::retrieve('sid', 'Positive',
224 $fid = CRM_Utils_Request
::retrieve('fid', 'Positive',
227 $action = CRM_Utils_Request
::retrieve('action', 'String',
228 // default to 'browse'
229 $this, FALSE, 'browse'
234 'civicrm_event' => FALSE,
235 'civicrm_event' => FALSE,
236 'civicrm_contribution_page' => FALSE,
238 $usedBy = CRM_Price_BAO_PriceSet
::getUsedBy($this->_sid
);
239 $this->assign('usedBy', array_merge($usedByDefaults, $usedBy));
240 $this->_isSetReserved
= CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid
, 'is_reserved');
241 $this->assign('isReserved', $this->_isSetReserved
);
243 CRM_Price_BAO_PriceSet
::checkPermission($this->_sid
);
245 'Event' => 'civicrm_event',
246 'Contribution' => 'civicrm_contribution_page',
247 'EventTemplate' => 'civicrm_event_template',
249 $priceSetContexts = [];
250 foreach ($comps as $name => $table) {
251 if (array_key_exists($table, $usedBy)) {
252 $priceSetContexts[] = $name;
255 $this->assign('contexts', $priceSetContexts);
258 if ($action & (CRM_Core_Action
::DELETE
) && !$this->_isSetReserved
) {
259 if (empty($usedBy)) {
261 $session = CRM_Core_Session
::singleton();
262 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid
));
263 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_DeleteField', 'Delete Price Field', '');
264 $controller->set('fid', $fid);
265 $controller->setEmbedded(TRUE);
266 $controller->process();
271 $url = CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1');
272 CRM_Utils_System
::appendBreadCrumb(ts('Price'),
275 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceField
::getTitle($fid));
279 if ($action & CRM_Core_Action
::DELETE
) {
280 CRM_Utils_System
::setTitle(ts('Delete Price Field'));
282 elseif ($this->_sid
) {
283 $groupTitle = CRM_Price_BAO_PriceSet
::getTitle($this->_sid
);
284 $this->assign('sid', $this->_sid
);
285 $this->assign('groupTitle', $groupTitle);
286 CRM_Utils_System
::setTitle(ts('%1 - Price Fields', [1 => $groupTitle]));
289 // assign vars to templates
290 $this->assign('action', $action);
292 // what action to take ?
293 if ($action & (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD
) && !$this->_isSetReserved
) {
294 // no browse for edit/update/view
295 $this->edit($action);
297 elseif ($action & CRM_Core_Action
::PREVIEW
) {
298 $this->preview($fid);
304 // Call the parents run method
305 return parent
::run();
309 * Preview price field.
313 * @internal param int $id price field id
317 public function preview($fid) {
318 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Preview', ts('Preview Form Field'), CRM_Core_Action
::PREVIEW
);
319 $session = CRM_Core_Session
::singleton();
320 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid
));
321 $controller->set('fieldId', $fid);
322 $controller->set('groupId', $this->_sid
);
323 $controller->setEmbedded(TRUE);
324 $controller->process();