Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
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 | ||
96f50de2 CW |
28 | public $useLivePageJS = TRUE; |
29 | ||
6a488035 | 30 | /** |
fe482240 | 31 | * The price set group id of the field. |
6a488035 TO |
32 | * |
33 | * @var int | |
6a488035 TO |
34 | */ |
35 | protected $_sid; | |
36 | ||
37 | /** | |
fe482240 | 38 | * The action links that we need to display for the browse screen. |
6a488035 TO |
39 | * |
40 | * @var array | |
6a488035 TO |
41 | */ |
42 | private static $_actionLinks; | |
43 | ||
44 | /** | |
fe482240 | 45 | * The price set is reserved or not. |
6a488035 | 46 | * |
b67daa72 | 47 | * @var bool |
6a488035 | 48 | */ |
ba1dcfda | 49 | protected $_isSetReserved = FALSE; |
6a488035 TO |
50 | |
51 | /** | |
52 | * Get the action links for this page. | |
53 | * | |
a6c01b45 CW |
54 | * @return array |
55 | * array of action links that we need to display for the browse screen | |
95ea96be | 56 | */ |
d1424f8f | 57 | public static function &actionLinks() { |
6a488035 | 58 | if (!isset(self::$_actionLinks)) { |
be2fb01f CW |
59 | self::$_actionLinks = [ |
60 | CRM_Core_Action::UPDATE => [ | |
6a488035 TO |
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'), | |
be2fb01f CW |
65 | ], |
66 | CRM_Core_Action::PREVIEW => [ | |
6a488035 TO |
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'), | |
be2fb01f CW |
71 | ], |
72 | CRM_Core_Action::DISABLE => [ | |
6a488035 | 73 | 'name' => ts('Disable'), |
4d17a233 | 74 | 'ref' => 'crm-enable-disable', |
6a488035 | 75 | 'title' => ts('Disable Price'), |
be2fb01f CW |
76 | ], |
77 | CRM_Core_Action::ENABLE => [ | |
6a488035 | 78 | 'name' => ts('Enable'), |
4d17a233 | 79 | 'ref' => 'crm-enable-disable', |
6a488035 | 80 | 'title' => ts('Enable Price'), |
be2fb01f CW |
81 | ], |
82 | CRM_Core_Action::DELETE => [ | |
6a488035 TO |
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'), | |
be2fb01f CW |
87 | ], |
88 | ]; | |
6a488035 TO |
89 | } |
90 | return self::$_actionLinks; | |
91 | } | |
92 | ||
93 | /** | |
94 | * Browse all price set fields. | |
6a488035 | 95 | */ |
00be9182 | 96 | public function browse() { |
6f231148 CW |
97 | $resourceManager = CRM_Core_Resources::singleton(); |
98 | if (!empty($_GET['new']) && $resourceManager->ajaxPopupsEnabled) { | |
96ed17aa | 99 | $resourceManager->addScriptFile('civicrm', 'js/crm.addNew.js', 999, 'html-header'); |
6f231148 CW |
100 | } |
101 | ||
be2fb01f | 102 | $priceField = []; |
9da8dc8c | 103 | $priceFieldBAO = new CRM_Price_BAO_PriceField(); |
6a488035 TO |
104 | |
105 | // fkey is sid | |
106 | $priceFieldBAO->price_set_id = $this->_sid; | |
107 | $priceFieldBAO->orderBy('weight, label'); | |
108 | $priceFieldBAO->find(); | |
44c8822b | 109 | |
03b412ae | 110 | // display taxTerm for priceFields |
aaffa79f | 111 | $invoiceSettings = Civi::settings()->get('contribution_invoice_settings'); |
530f1e12 | 112 | $taxTerm = Civi::settings()->get('tax_term'); |
9c1bc317 | 113 | $invoicing = $invoiceSettings['invoicing'] ?? NULL; |
a32709be PB |
114 | $getTaxDetails = FALSE; |
115 | $taxRate = CRM_Core_PseudoConstant::getTaxRates(); | |
e3ca0d21 | 116 | CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes); |
6a488035 | 117 | while ($priceFieldBAO->fetch()) { |
be2fb01f | 118 | $priceField[$priceFieldBAO->id] = []; |
6a488035 TO |
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') { | |
be2fb01f CW |
123 | $optionValues = []; |
124 | $params = ['price_field_id' => $priceFieldBAO->id]; | |
6a488035 | 125 | |
9da8dc8c | 126 | CRM_Price_BAO_PriceFieldValue::retrieve($params, $optionValues); |
9c1bc317 | 127 | $priceField[$priceFieldBAO->id]['price'] = $optionValues['amount'] ?? NULL; |
1019b2fe | 128 | $financialTypeId = $optionValues['financial_type_id']; |
03b412ae | 129 | if ($invoicing && isset($taxRate[$financialTypeId])) { |
a32709be PB |
130 | $priceField[$priceFieldBAO->id]['tax_rate'] = $taxRate[$financialTypeId]; |
131 | $getTaxDetails = TRUE; | |
132 | } | |
133 | if (isset($priceField[$priceFieldBAO->id]['tax_rate'])) { | |
6fec0f2e | 134 | $taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($priceField[$priceFieldBAO->id]['price'], $priceField[$priceFieldBAO->id]['tax_rate']); |
a32709be PB |
135 | $priceField[$priceFieldBAO->id]['tax_amount'] = $taxAmount['tax_amount']; |
136 | } | |
6a488035 | 137 | } |
44c8822b | 138 | |
d1424f8f | 139 | $action = array_sum(array_keys(self::actionLinks())); |
6a488035 TO |
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 | |
9da8dc8c | 162 | $htmlTypes = CRM_Price_BAO_PriceField::htmlTypes(); |
79c04577 | 163 | $priceField[$priceFieldBAO->id]['html_type_display'] = $htmlTypes[$priceField[$priceFieldBAO->id]['html_type']]; |
6a488035 | 164 | $priceField[$priceFieldBAO->id]['order'] = $priceField[$priceFieldBAO->id]['weight']; |
87dab4a4 AH |
165 | $priceField[$priceFieldBAO->id]['action'] = CRM_Core_Action::formLink( |
166 | self::actionLinks(), | |
167 | $action, | |
be2fb01f | 168 | [ |
6a488035 TO |
169 | 'fid' => $priceFieldBAO->id, |
170 | 'sid' => $this->_sid, | |
be2fb01f | 171 | ], |
87dab4a4 AH |
172 | ts('more'), |
173 | FALSE, | |
174 | 'priceField.row.actions', | |
175 | 'PriceField', | |
176 | $priceFieldBAO->id | |
6a488035 | 177 | ); |
03b412ae | 178 | $this->assign('taxTerm', $taxTerm); |
a32709be | 179 | $this->assign('getTaxDetails', $getTaxDetails); |
6a488035 TO |
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}"; | |
9da8dc8c | 184 | CRM_Utils_Weight::addOrder($priceField, 'CRM_Price_DAO_PriceField', |
6a488035 TO |
185 | 'id', $returnURL, $filter |
186 | ); | |
187 | $this->assign('priceField', $priceField); | |
188 | } | |
189 | ||
190 | /** | |
100fef9d | 191 | * Edit price data. |
6a488035 TO |
192 | * |
193 | * editing would involved modifying existing fields + adding data to new fields. | |
194 | * | |
414c1420 TO |
195 | * @param string $action |
196 | * The action to be invoked. | |
6a488035 | 197 | */ |
00be9182 | 198 | public function edit($action) { |
6a488035 TO |
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 | * | |
6a488035 | 218 | * @return void |
6a488035 | 219 | */ |
00be9182 | 220 | public function run() { |
6a488035 TO |
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) { | |
9da8dc8c | 235 | $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($this->_sid); |
6a488035 | 236 | $this->assign('usedBy', $usedBy); |
ba1dcfda | 237 | $this->_isSetReserved = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'is_reserved'); |
6a488035 | 238 | $this->assign('isReserved', $this->_isSetReserved); |
44c8822b | 239 | |
9da8dc8c | 240 | CRM_Price_BAO_PriceSet::checkPermission($this->_sid); |
be2fb01f | 241 | $comps = [ |
6a488035 TO |
242 | 'Event' => 'civicrm_event', |
243 | 'Contribution' => 'civicrm_contribution_page', | |
21dfd5f5 | 244 | 'EventTemplate' => 'civicrm_event_template', |
be2fb01f CW |
245 | ]; |
246 | $priceSetContexts = []; | |
6a488035 TO |
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 | ); | |
9da8dc8c | 272 | $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceField::getTitle($fid)); |
6a488035 TO |
273 | } |
274 | } | |
275 | ||
e5e8ab99 CW |
276 | if ($action & CRM_Core_Action::DELETE) { |
277 | CRM_Utils_System::setTitle(ts('Delete Price Field')); | |
278 | } | |
279 | elseif ($this->_sid) { | |
9da8dc8c | 280 | $groupTitle = CRM_Price_BAO_PriceSet::getTitle($this->_sid); |
6a488035 TO |
281 | $this->assign('sid', $this->_sid); |
282 | $this->assign('groupTitle', $groupTitle); | |
be2fb01f | 283 | CRM_Utils_System::setTitle(ts('%1 - Price Fields', [1 => $groupTitle])); |
6a488035 TO |
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 | /** | |
fe482240 | 306 | * Preview price field. |
6a488035 | 307 | * |
100fef9d | 308 | * @param int $fid |
77b97be7 EM |
309 | * |
310 | * @internal param int $id price field id | |
6a488035 TO |
311 | * |
312 | * @return void | |
6a488035 | 313 | */ |
00be9182 | 314 | public function preview($fid) { |
6a488035 TO |
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 | } | |
96025800 | 324 | |
6a488035 | 325 | } |