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