Merge pull request #22538 from masetto/pdfletter
[civicrm-core.git] / CRM / Price / Page / Option.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 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
28 public $useLivePageJS = TRUE;
29
30 /**
31 * The field id of the option.
32 *
33 * @var int
34 */
35 protected $_fid;
36
37 /**
38 * The field id of the option.
39 *
40 * @var int
41 */
42 protected $_sid;
43
44 /**
45 * The price set is reserved or not.
46 *
47 * @var bool
48 */
49 protected $_isSetReserved = FALSE;
50
51 /**
52 * The action links that we need to display for the browse screen.
53 *
54 * @var array
55 */
56 private static $_actionLinks;
57
58 /**
59 * Get the action links for this page.
60 *
61 * @return array
62 * array of action links that we need to display for the browse screen
63 */
64 public static function &actionLinks() {
65 if (!isset(self::$_actionLinks)) {
66 self::$_actionLinks = [
67 CRM_Core_Action::UPDATE => [
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'),
72 ],
73 CRM_Core_Action::VIEW => [
74 'name' => ts('View'),
75 'url' => 'civicrm/admin/price/field/option',
76 'qs' => 'action=view&oid=%%oid%%',
77 'title' => ts('View Price Option'),
78 ],
79 CRM_Core_Action::DISABLE => [
80 'name' => ts('Disable'),
81 'ref' => 'crm-enable-disable',
82 'title' => ts('Disable Price Option'),
83 ],
84 CRM_Core_Action::ENABLE => [
85 'name' => ts('Enable'),
86 'ref' => 'crm-enable-disable',
87 'title' => ts('Enable Price Option'),
88 ],
89 CRM_Core_Action::DELETE => [
90 'name' => ts('Delete'),
91 'url' => 'civicrm/admin/price/field/option',
92 'qs' => 'action=delete&oid=%%oid%%',
93 'title' => ts('Disable Price Option'),
94 ],
95 ];
96 }
97 return self::$_actionLinks;
98 }
99
100 /**
101 * Browse all price fields.
102 *
103 * @return void
104 */
105 public function browse() {
106 $priceOptions = civicrm_api3('PriceFieldValue', 'get', [
107 'price_field_id' => $this->_fid,
108 // Explicitly do not check permissions so we are not
109 // restricted by financial type, so we can change them.
110 'check_permissions' => FALSE,
111 'options' => [
112 'limit' => 0,
113 'sort' => ['weight', 'label'],
114 ],
115 ]);
116 $customOption = $priceOptions['values'];
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
127 $config = CRM_Core_Config::singleton();
128 $taxRate = CRM_Core_PseudoConstant::getTaxRates();
129 // display taxTerm for priceFields
130 $invoiceSettings = Civi::settings()->get('contribution_invoice_settings');
131 $taxTerm = Civi::settings()->get('tax_term');
132 $invoicing = $invoiceSettings['invoicing'] ?? NULL;
133 $getTaxDetails = FALSE;
134 foreach ($customOption as $id => $values) {
135 $action = array_sum(array_keys(self::actionLinks()));
136 // Adding the required fields in the array
137 if (isset($taxRate[$values['financial_type_id']])) {
138 // Cast to float so trailing zero decimals are removed
139 $customOption[$id]['tax_rate'] = (float) $taxRate[$values['financial_type_id']];
140 if ($invoicing && isset($customOption[$id]['tax_rate'])) {
141 $getTaxDetails = TRUE;
142 }
143 $taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($customOption[$id]['amount'], $customOption[$id]['tax_rate']);
144 $customOption[$id]['tax_amount'] = $taxAmount['tax_amount'];
145 }
146 if (!empty($values['financial_type_id'])) {
147 $customOption[$id]['financial_type_id'] = CRM_Contribute_PseudoConstant::financialType($values['financial_type_id']);
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 }
161 $customOption[$id]['order'] = $customOption[$id]['weight'];
162 $customOption[$id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), $action,
163 [
164 'oid' => $id,
165 'fid' => $this->_fid,
166 'sid' => $this->_sid,
167 ],
168 ts('more'),
169 FALSE,
170 'priceFieldValue.row.actions',
171 'PriceFieldValue',
172 $id
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}";
178 CRM_Utils_Weight::addOrder($customOption, 'CRM_Price_DAO_PriceFieldValue',
179 'id', $returnURL, $filter
180 );
181
182 $this->assign('taxTerm', $taxTerm);
183 $this->assign('getTaxDetails', $getTaxDetails);
184 $this->assign('customOption', $customOption);
185 $this->assign('sid', $this->_sid);
186 $this->assign('isEvent', $isEvent);
187 }
188
189 /**
190 * Edit custom Option.
191 *
192 * editing would involved modifying existing fields + adding data to new fields.
193 *
194 * @param string $action
195 * The action to be invoked.
196 *
197 * @return void
198 */
199 public function edit($action) {
200 $oid = CRM_Utils_Request::retrieve('oid', 'Positive',
201 $this, FALSE, 0
202 );
203 $params = [];
204 if ($oid) {
205 $params['oid'] = $oid;
206 $sid = CRM_Price_BAO_PriceSet::getSetId($params);
207
208 $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($sid);
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',
214 "reset=1&action=browse&fid={$this->_fid}&sid={$this->_sid}"
215 ));
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
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 );
228 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceFieldValue::getOptionLabel($oid));
229 $this->assign('usedBy', $usedBy);
230 $comps = [
231 "Event" => "civicrm_event",
232 "Contribution" => "civicrm_contribution_page",
233 ];
234 $priceSetContexts = [];
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 *
250 * @return void
251 */
252 public function run() {
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) {
263 CRM_Price_BAO_PriceSet::checkPermission($this->_sid);
264 $this->_isSetReserved = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'is_reserved');
265 $this->assign('isReserved', $this->_isSetReserved);
266 }
267 //as url contain $sid so append breadcrumb dynamically.
268 $breadcrumb = [
269 [
270 'title' => ts('Price Fields'),
271 'url' => CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&sid=' . $this->_sid),
272 ],
273 ];
274 CRM_Utils_System::appendBreadCrumb($breadcrumb);
275
276 if ($this->_fid) {
277 $fieldTitle = CRM_Price_BAO_PriceField::getTitle($this->_fid);
278 $this->assign('fid', $this->_fid);
279 $this->assign('fieldTitle', $fieldTitle);
280 CRM_Utils_System::setTitle(ts('%1 - Price Options', [1 => $fieldTitle]));
281
282 $htmlType = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceField', $this->_fid, 'html_type');
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
305 ) && !$this->_isSetReserved
306 ) {
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 }
316
317 }