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