Avoid PHP notices from smarty on pricefield table
[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 $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]);
118
119 // get price if it's a text field
120 if ($priceFieldBAO->html_type == 'Text') {
121 $optionValues = [];
122 $params = ['price_field_id' => $priceFieldBAO->id];
123
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;
130 }
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'];
134 }
135 }
136
137 $action = array_sum(array_keys(self::actionLinks()));
138
139 if ($this->_isSetReserved) {
140 $action -= CRM_Core_Action::UPDATE + CRM_Core_Action::DELETE + CRM_Core_Action::ENABLE + CRM_Core_Action::DISABLE;
141 }
142 else {
143 if ($priceFieldBAO->is_active) {
144 $action -= CRM_Core_Action::ENABLE;
145 }
146 else {
147 $action -= CRM_Core_Action::DISABLE;
148 }
149 }
150
151 if (!isset($priceField[$priceFieldBAO->id]['active_on']) || $priceFieldBAO->active_on == '0000-00-00 00:00:00') {
152 $priceField[$priceFieldBAO->id]['active_on'] = '';
153 }
154
155 if (!isset($priceField[$priceFieldBAO->id]['expire_on']) || $priceFieldBAO->expire_on == '0000-00-00 00:00:00') {
156 $priceField[$priceFieldBAO->id]['expire_on'] = '';
157 }
158
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(
164 self::actionLinks(),
165 $action,
166 [
167 'fid' => $priceFieldBAO->id,
168 'sid' => $this->_sid,
169 ],
170 ts('more'),
171 FALSE,
172 'priceField.row.actions',
173 'PriceField',
174 $priceFieldBAO->id
175 );
176 $this->assign('taxTerm', $taxTerm);
177 $this->assign('getTaxDetails', $getTaxDetails);
178 }
179
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
184 );
185 $this->assign('priceField', $priceField);
186 }
187
188 /**
189 * Edit price data.
190 *
191 * editing would involved modifying existing fields + adding data to new fields.
192 *
193 * @param string $action
194 * The action to be invoked.
195 */
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);
199
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));
203
204 $controller->set('sid', $this->_sid);
205 $controller->setEmbedded(TRUE);
206 $controller->process();
207 $controller->run();
208 }
209
210 /**
211 * Run the page.
212 *
213 * This method is called after the page is created. It checks for the
214 * type of action and executes that action.
215 *
216 * @return void
217 */
218 public function run() {
219
220 // get the group id
221 $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive',
222 $this
223 );
224 $fid = CRM_Utils_Request::retrieve('fid', 'Positive',
225 $this, FALSE, 0
226 );
227 $action = CRM_Utils_Request::retrieve('action', 'String',
228 // default to 'browse'
229 $this, FALSE, 'browse'
230 );
231
232 if ($this->_sid) {
233 $usedByDefaults = [
234 'civicrm_event' => FALSE,
235 'civicrm_event' => FALSE,
236 'civicrm_contribution_page' => FALSE,
237 ];
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);
242
243 CRM_Price_BAO_PriceSet::checkPermission($this->_sid);
244 $comps = [
245 'Event' => 'civicrm_event',
246 'Contribution' => 'civicrm_contribution_page',
247 'EventTemplate' => 'civicrm_event_template',
248 ];
249 $priceSetContexts = [];
250 foreach ($comps as $name => $table) {
251 if (array_key_exists($table, $usedBy)) {
252 $priceSetContexts[] = $name;
253 }
254 }
255 $this->assign('contexts', $priceSetContexts);
256 }
257
258 if ($action & (CRM_Core_Action::DELETE) && !$this->_isSetReserved) {
259 if (empty($usedBy)) {
260 // prompt to delete
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();
267 $controller->run();
268 }
269 else {
270 // add breadcrumb
271 $url = CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1');
272 CRM_Utils_System::appendBreadCrumb(ts('Price'),
273 $url
274 );
275 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceField::getTitle($fid));
276 }
277 }
278
279 if ($action & CRM_Core_Action::DELETE) {
280 CRM_Utils_System::setTitle(ts('Delete Price Field'));
281 }
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]));
287 }
288
289 // assign vars to templates
290 $this->assign('action', $action);
291
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);
296 }
297 elseif ($action & CRM_Core_Action::PREVIEW) {
298 $this->preview($fid);
299 }
300 else {
301 $this->browse();
302 }
303
304 // Call the parents run method
305 return parent::run();
306 }
307
308 /**
309 * Preview price field.
310 *
311 * @param int $fid
312 *
313 * @internal param int $id price field id
314 *
315 * @return void
316 */
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();
325 $controller->run();
326 }
327
328 }