Merge pull request #18654 from eileenmcnaughton/token1
[civicrm-core.git] / CRM / Price / Page / Field.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Create a page for displaying Price Fields.
20 *
21 * Heart of this class is the run method which checks
22 * for action type and then displays the appropriate
23 * page.
24 *
25 */
26 class CRM_Price_Page_Field extends CRM_Core_Page {
27
28 public $useLivePageJS = TRUE;
29
30 /**
31 * The price set group id of the field.
32 *
33 * @var int
34 */
35 protected $_sid;
36
37 /**
38 * The action links that we need to display for the browse screen.
39 *
40 * @var array
41 */
42 private static $_actionLinks;
43
44 /**
45 * The price set is reserved or not.
46 *
47 * @var bool
48 */
49 protected $_isSetReserved = FALSE;
50
51 /**
52 * Get the action links for this page.
53 *
54 * @return array
55 * array of action links that we need to display for the browse screen
56 */
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'),
65 ],
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'),
71 ],
72 CRM_Core_Action::DISABLE => [
73 'name' => ts('Disable'),
74 'ref' => 'crm-enable-disable',
75 'title' => ts('Disable Price'),
76 ],
77 CRM_Core_Action::ENABLE => [
78 'name' => ts('Enable'),
79 'ref' => 'crm-enable-disable',
80 'title' => ts('Enable Price'),
81 ],
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'),
87 ],
88 ];
89 }
90 return self::$_actionLinks;
91 }
92
93 /**
94 * Browse all price set fields.
95 */
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');
100 }
101
102 $priceField = [];
103 $priceFieldBAO = new CRM_Price_BAO_PriceField();
104
105 // fkey is sid
106 $priceFieldBAO->price_set_id = $this->_sid;
107 $priceFieldBAO->orderBy('weight, label');
108 $priceFieldBAO->find();
109
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]);
120
121 // get price if it's a text field
122 if ($priceFieldBAO->html_type == 'Text') {
123 $optionValues = [];
124 $params = ['price_field_id' => $priceFieldBAO->id];
125
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;
132 }
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'];
136 }
137 }
138
139 $action = array_sum(array_keys(self::actionLinks()));
140
141 if ($this->_isSetReserved) {
142 $action -= CRM_Core_Action::UPDATE + CRM_Core_Action::DELETE + CRM_Core_Action::ENABLE + CRM_Core_Action::DISABLE;
143 }
144 else {
145 if ($priceFieldBAO->is_active) {
146 $action -= CRM_Core_Action::ENABLE;
147 }
148 else {
149 $action -= CRM_Core_Action::DISABLE;
150 }
151 }
152
153 if ($priceFieldBAO->active_on == '0000-00-00 00:00:00') {
154 $priceField[$priceFieldBAO->id]['active_on'] = '';
155 }
156
157 if ($priceFieldBAO->expire_on == '0000-00-00 00:00:00') {
158 $priceField[$priceFieldBAO->id]['expire_on'] = '';
159 }
160
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(
166 self::actionLinks(),
167 $action,
168 [
169 'fid' => $priceFieldBAO->id,
170 'sid' => $this->_sid,
171 ],
172 ts('more'),
173 FALSE,
174 'priceField.row.actions',
175 'PriceField',
176 $priceFieldBAO->id
177 );
178 $this->assign('taxTerm', $taxTerm);
179 $this->assign('getTaxDetails', $getTaxDetails);
180 }
181
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
186 );
187 $this->assign('priceField', $priceField);
188 }
189
190 /**
191 * Edit price data.
192 *
193 * editing would involved modifying existing fields + adding data to new fields.
194 *
195 * @param string $action
196 * The action to be invoked.
197 */
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);
201
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));
205
206 $controller->set('sid', $this->_sid);
207 $controller->setEmbedded(TRUE);
208 $controller->process();
209 $controller->run();
210 }
211
212 /**
213 * Run the page.
214 *
215 * This method is called after the page is created. It checks for the
216 * type of action and executes that action.
217 *
218 * @return void
219 */
220 public function run() {
221
222 // get the group id
223 $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive',
224 $this
225 );
226 $fid = CRM_Utils_Request::retrieve('fid', 'Positive',
227 $this, FALSE, 0
228 );
229 $action = CRM_Utils_Request::retrieve('action', 'String',
230 // default to 'browse'
231 $this, FALSE, 'browse'
232 );
233
234 if ($this->_sid) {
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);
239
240 CRM_Price_BAO_PriceSet::checkPermission($this->_sid);
241 $comps = [
242 'Event' => 'civicrm_event',
243 'Contribution' => 'civicrm_contribution_page',
244 'EventTemplate' => 'civicrm_event_template',
245 ];
246 $priceSetContexts = [];
247 foreach ($comps as $name => $table) {
248 if (array_key_exists($table, $usedBy)) {
249 $priceSetContexts[] = $name;
250 }
251 }
252 $this->assign('contexts', $priceSetContexts);
253 }
254
255 if ($action & (CRM_Core_Action::DELETE) && !$this->_isSetReserved) {
256 if (empty($usedBy)) {
257 // prompt to delete
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();
264 $controller->run();
265 }
266 else {
267 // add breadcrumb
268 $url = CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1');
269 CRM_Utils_System::appendBreadCrumb(ts('Price'),
270 $url
271 );
272 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceField::getTitle($fid));
273 }
274 }
275
276 if ($action & CRM_Core_Action::DELETE) {
277 CRM_Utils_System::setTitle(ts('Delete Price Field'));
278 }
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]));
284 }
285
286 // assign vars to templates
287 $this->assign('action', $action);
288
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);
293 }
294 elseif ($action & CRM_Core_Action::PREVIEW) {
295 $this->preview($fid);
296 }
297 else {
298 $this->browse();
299 }
300
301 // Call the parents run method
302 return parent::run();
303 }
304
305 /**
306 * Preview price field.
307 *
308 * @param int $fid
309 *
310 * @internal param int $id price field id
311 *
312 * @return void
313 */
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();
322 $controller->run();
323 }
324
325 }