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 Custom Options. | |
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_Option extends CRM_Core_Page { | |
27 | ||
96f50de2 CW |
28 | public $useLivePageJS = TRUE; |
29 | ||
6a488035 | 30 | /** |
fe482240 | 31 | * The field id of the option. |
6a488035 TO |
32 | * |
33 | * @var int | |
6a488035 TO |
34 | */ |
35 | protected $_fid; | |
36 | ||
37 | /** | |
fe482240 | 38 | * The field id of the option. |
6a488035 TO |
39 | * |
40 | * @var int | |
6a488035 TO |
41 | */ |
42 | protected $_sid; | |
43 | ||
44 | /** | |
fe482240 | 45 | * The price set is reserved or not. |
6a488035 | 46 | * |
b67daa72 | 47 | * @var bool |
6a488035 | 48 | */ |
ba1dcfda | 49 | protected $_isSetReserved = FALSE; |
366fe2a3 | 50 | |
6a488035 | 51 | /** |
fe482240 | 52 | * The action links that we need to display for the browse screen. |
6a488035 TO |
53 | * |
54 | * @var array | |
6a488035 TO |
55 | */ |
56 | private static $_actionLinks; | |
57 | ||
58 | /** | |
59 | * Get the action links for this page. | |
60 | * | |
a6c01b45 CW |
61 | * @return array |
62 | * array of action links that we need to display for the browse screen | |
95ea96be | 63 | */ |
d1424f8f | 64 | public static function &actionLinks() { |
6a488035 | 65 | if (!isset(self::$_actionLinks)) { |
be2fb01f CW |
66 | self::$_actionLinks = [ |
67 | CRM_Core_Action::UPDATE => [ | |
6a488035 TO |
68 | 'name' => ts('Edit Option'), |
69 | 'url' => 'civicrm/admin/price/field/option', | |
70 | 'qs' => 'reset=1&action=update&oid=%%oid%%&fid=%%fid%%&sid=%%sid%%', | |
71 | 'title' => ts('Edit Price Option'), | |
be2fb01f CW |
72 | ], |
73 | CRM_Core_Action::VIEW => [ | |
6a488035 TO |
74 | 'name' => ts('View'), |
75 | 'url' => 'civicrm/admin/price/field/option', | |
76 | 'qs' => 'action=view&oid=%%oid%%', | |
77 | 'title' => ts('View Price Option'), | |
be2fb01f CW |
78 | ], |
79 | CRM_Core_Action::DISABLE => [ | |
6a488035 | 80 | 'name' => ts('Disable'), |
4d17a233 | 81 | 'ref' => 'crm-enable-disable', |
6a488035 | 82 | 'title' => ts('Disable Price Option'), |
be2fb01f CW |
83 | ], |
84 | CRM_Core_Action::ENABLE => [ | |
6a488035 | 85 | 'name' => ts('Enable'), |
4d17a233 | 86 | 'ref' => 'crm-enable-disable', |
6a488035 | 87 | 'title' => ts('Enable Price Option'), |
be2fb01f CW |
88 | ], |
89 | CRM_Core_Action::DELETE => [ | |
6a488035 TO |
90 | 'name' => ts('Delete'), |
91 | 'url' => 'civicrm/admin/price/field/option', | |
92 | 'qs' => 'action=delete&oid=%%oid%%', | |
93 | 'title' => ts('Disable Price Option'), | |
be2fb01f CW |
94 | ], |
95 | ]; | |
6a488035 TO |
96 | } |
97 | return self::$_actionLinks; | |
98 | } | |
99 | ||
100 | /** | |
101 | * Browse all price fields. | |
102 | * | |
6a488035 | 103 | * @return void |
6a488035 | 104 | */ |
00be9182 | 105 | public function browse() { |
be2fb01f | 106 | $priceOptions = civicrm_api3('PriceFieldValue', 'get', [ |
971e129b | 107 | 'price_field_id' => $this->_fid, |
23dba589 SL |
108 | // Explicitly do not check permissions so we are not |
109 | // restricted by financial type, so we can change them. | |
971e129b SL |
110 | 'check_permissions' => FALSE, |
111 | 'options' => [ | |
112 | 'limit' => 0, | |
113 | 'sort' => ['weight', 'label'], | |
114 | ], | |
be2fb01f | 115 | ]); |
23dba589 | 116 | $customOption = $priceOptions['values']; |
a8e4ceb4 DG |
117 | |
118 | // CRM-15378 - check if these price options are in an Event price set | |
119 | $isEvent = FALSE; | |
120 | $extendComponentId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'extends', 'id'); | |
121 | $allComponents = explode(CRM_Core_DAO::VALUE_SEPARATOR, $extendComponentId); | |
122 | $eventComponentId = CRM_Core_Component::getComponentID('CiviEvent'); | |
123 | if (in_array($eventComponentId, $allComponents)) { | |
124 | $isEvent = TRUE; | |
125 | } | |
126 | ||
6a488035 | 127 | $config = CRM_Core_Config::singleton(); |
a32709be | 128 | $taxRate = CRM_Core_PseudoConstant::getTaxRates(); |
03b412ae | 129 | // display taxTerm for priceFields |
aaffa79f | 130 | $invoiceSettings = Civi::settings()->get('contribution_invoice_settings'); |
530f1e12 | 131 | $taxTerm = Civi::settings()->get('tax_term'); |
9c1bc317 | 132 | $invoicing = $invoiceSettings['invoicing'] ?? NULL; |
a32709be | 133 | $getTaxDetails = FALSE; |
6a488035 | 134 | foreach ($customOption as $id => $values) { |
d1424f8f | 135 | $action = array_sum(array_keys(self::actionLinks())); |
a32709be PB |
136 | // Adding the required fields in the array |
137 | if (isset($taxRate[$values['financial_type_id']])) { | |
cc3f313d MW |
138 | // Cast to float so trailing zero decimals are removed |
139 | $customOption[$id]['tax_rate'] = (float) $taxRate[$values['financial_type_id']]; | |
03b412ae | 140 | if ($invoicing && isset($customOption[$id]['tax_rate'])) { |
a32709be PB |
141 | $getTaxDetails = TRUE; |
142 | } | |
6fec0f2e | 143 | $taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($customOption[$id]['amount'], $customOption[$id]['tax_rate']); |
a32709be PB |
144 | $customOption[$id]['tax_amount'] = $taxAmount['tax_amount']; |
145 | } | |
1475d837 MW |
146 | if (!empty($values['financial_type_id'])) { |
147 | $customOption[$id]['financial_type_id'] = CRM_Contribute_PseudoConstant::financialType($values['financial_type_id']); | |
6a488035 TO |
148 | } |
149 | // update enable/disable links depending on price_field properties. | |
150 | if ($this->_isSetReserved) { | |
151 | $action -= CRM_Core_Action::UPDATE + CRM_Core_Action::DELETE + CRM_Core_Action::DISABLE + CRM_Core_Action::ENABLE; | |
152 | } | |
153 | else { | |
154 | if ($values['is_active']) { | |
155 | $action -= CRM_Core_Action::ENABLE; | |
156 | } | |
157 | else { | |
158 | $action -= CRM_Core_Action::DISABLE; | |
159 | } | |
160 | } | |
6a488035 TO |
161 | $customOption[$id]['order'] = $customOption[$id]['weight']; |
162 | $customOption[$id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), $action, | |
be2fb01f | 163 | [ |
6a488035 TO |
164 | 'oid' => $id, |
165 | 'fid' => $this->_fid, | |
166 | 'sid' => $this->_sid, | |
be2fb01f | 167 | ], |
87dab4a4 AH |
168 | ts('more'), |
169 | FALSE, | |
170 | 'priceFieldValue.row.actions', | |
171 | 'PriceFieldValue', | |
172 | $id | |
6a488035 TO |
173 | ); |
174 | } | |
175 | // Add order changing widget to selector | |
176 | $returnURL = CRM_Utils_System::url('civicrm/admin/price/field/option', "action=browse&reset=1&fid={$this->_fid}&sid={$this->_sid}"); | |
177 | $filter = "price_field_id = {$this->_fid}"; | |
9da8dc8c | 178 | CRM_Utils_Weight::addOrder($customOption, 'CRM_Price_DAO_PriceFieldValue', |
6a488035 TO |
179 | 'id', $returnURL, $filter |
180 | ); | |
181 | ||
03b412ae | 182 | $this->assign('taxTerm', $taxTerm); |
a32709be | 183 | $this->assign('getTaxDetails', $getTaxDetails); |
6a488035 TO |
184 | $this->assign('customOption', $customOption); |
185 | $this->assign('sid', $this->_sid); | |
a8e4ceb4 | 186 | $this->assign('isEvent', $isEvent); |
6a488035 TO |
187 | } |
188 | ||
189 | /** | |
100fef9d | 190 | * Edit custom Option. |
6a488035 TO |
191 | * |
192 | * editing would involved modifying existing fields + adding data to new fields. | |
193 | * | |
414c1420 TO |
194 | * @param string $action |
195 | * The action to be invoked. | |
6a488035 TO |
196 | * |
197 | * @return void | |
6a488035 | 198 | */ |
00be9182 | 199 | public function edit($action) { |
6a488035 TO |
200 | $oid = CRM_Utils_Request::retrieve('oid', 'Positive', |
201 | $this, FALSE, 0 | |
202 | ); | |
be2fb01f | 203 | $params = []; |
6a488035 TO |
204 | if ($oid) { |
205 | $params['oid'] = $oid; | |
9da8dc8c | 206 | $sid = CRM_Price_BAO_PriceSet::getSetId($params); |
6a488035 | 207 | |
9da8dc8c | 208 | $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($sid); |
6a488035 TO |
209 | } |
210 | // set the userContext stack | |
211 | $session = CRM_Core_Session::singleton(); | |
212 | ||
213 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field/option', | |
353ffa53 TO |
214 | "reset=1&action=browse&fid={$this->_fid}&sid={$this->_sid}" |
215 | )); | |
6a488035 TO |
216 | $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Option', ts('Price Field Option'), $action); |
217 | $controller->set('fid', $this->_fid); | |
218 | $controller->setEmbedded(TRUE); | |
219 | $controller->process(); | |
220 | $controller->run(); | |
221 | ||
6a488035 TO |
222 | if ($action & CRM_Core_Action::DELETE) { |
223 | // add breadcrumb | |
224 | $url = CRM_Utils_System::url('civicrm/admin/price/field/option', 'reset=1'); | |
225 | CRM_Utils_System::appendBreadCrumb(ts('Price Option'), | |
226 | $url | |
227 | ); | |
9da8dc8c | 228 | $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceFieldValue::getOptionLabel($oid)); |
6a488035 | 229 | $this->assign('usedBy', $usedBy); |
be2fb01f | 230 | $comps = [ |
6a488035 TO |
231 | "Event" => "civicrm_event", |
232 | "Contribution" => "civicrm_contribution_page", | |
be2fb01f CW |
233 | ]; |
234 | $priceSetContexts = []; | |
6a488035 TO |
235 | foreach ($comps as $name => $table) { |
236 | if (array_key_exists($table, $usedBy)) { | |
237 | $priceSetContexts[] = $name; | |
238 | } | |
239 | } | |
240 | $this->assign('contexts', $priceSetContexts); | |
241 | } | |
242 | } | |
243 | ||
244 | /** | |
245 | * Run the page. | |
246 | * | |
247 | * This method is called after the page is created. It checks for the | |
248 | * type of action and executes that action. | |
249 | * | |
6a488035 | 250 | * @return void |
6a488035 | 251 | */ |
00be9182 | 252 | public function run() { |
6a488035 TO |
253 | // get the field id |
254 | $this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive', | |
255 | $this, FALSE, 0 | |
256 | ); | |
257 | //get the price set id | |
258 | if (!$this->_sid) { | |
259 | $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive', $this); | |
260 | } | |
261 | ||
262 | if ($this->_sid) { | |
9da8dc8c | 263 | CRM_Price_BAO_PriceSet::checkPermission($this->_sid); |
ba1dcfda | 264 | $this->_isSetReserved = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'is_reserved'); |
6a488035 TO |
265 | $this->assign('isReserved', $this->_isSetReserved); |
266 | } | |
267 | //as url contain $sid so append breadcrumb dynamically. | |
be2fb01f CW |
268 | $breadcrumb = [ |
269 | [ | |
353ffa53 | 270 | 'title' => ts('Price Fields'), |
6a488035 | 271 | 'url' => CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&sid=' . $this->_sid), |
be2fb01f CW |
272 | ], |
273 | ]; | |
6a488035 TO |
274 | CRM_Utils_System::appendBreadCrumb($breadcrumb); |
275 | ||
276 | if ($this->_fid) { | |
9da8dc8c | 277 | $fieldTitle = CRM_Price_BAO_PriceField::getTitle($this->_fid); |
6a488035 TO |
278 | $this->assign('fid', $this->_fid); |
279 | $this->assign('fieldTitle', $fieldTitle); | |
be2fb01f | 280 | CRM_Utils_System::setTitle(ts('%1 - Price Options', [1 => $fieldTitle])); |
6a488035 | 281 | |
9da8dc8c | 282 | $htmlType = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceField', $this->_fid, 'html_type'); |
6a488035 TO |
283 | $this->assign('addMoreFields', TRUE); |
284 | //for text price field only single option present | |
285 | if ($htmlType == 'Text') { | |
286 | $this->assign('addMoreFields', FALSE); | |
287 | } | |
288 | } | |
289 | ||
290 | // get the requested action | |
291 | $action = CRM_Utils_Request::retrieve('action', 'String', | |
292 | // default to 'browse' | |
293 | $this, FALSE, 'browse' | |
294 | ); | |
295 | ||
296 | // assign vars to templates | |
297 | $this->assign('action', $action); | |
298 | ||
299 | $oid = CRM_Utils_Request::retrieve('oid', 'Positive', | |
300 | $this, FALSE, 0 | |
301 | ); | |
302 | // what action to take ? | |
303 | if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | | |
304 | CRM_Core_Action::VIEW | CRM_Core_Action::DELETE | |
353ffa53 TO |
305 | ) && !$this->_isSetReserved |
306 | ) { | |
6a488035 TO |
307 | // no browse for edit/update/view |
308 | $this->edit($action); | |
309 | } | |
310 | else { | |
311 | $this->browse(); | |
312 | } | |
313 | // Call the parents run method | |
314 | return parent::run(); | |
315 | } | |
96025800 | 316 | |
6a488035 | 317 | } |