Merge pull request #10823 from mattwire/CRM-21031_financialtypedisplay
[civicrm-core.git] / CRM / Price / Page / Option.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2017
32 * $Id$
33 *
34 */
35
36 /**
37 * Create a page for displaying Custom Options.
38 *
39 * Heart of this class is the run method which checks
40 * for action type and then displays the appropriate
41 * page.
42 *
43 */
44 class CRM_Price_Page_Option extends CRM_Core_Page {
45
46 public $useLivePageJS = TRUE;
47
48 /**
49 * The field id of the option.
50 *
51 * @var int
52 */
53 protected $_fid;
54
55 /**
56 * The field id of the option.
57 *
58 * @var int
59 */
60 protected $_sid;
61
62 /**
63 * The price set is reserved or not.
64 *
65 * @var boolean
66 */
67 protected $_isSetReserved = FALSE;
68
69 /**
70 * The action links that we need to display for the browse screen.
71 *
72 * @var array
73 */
74 private static $_actionLinks;
75
76 /**
77 * Get the action links for this page.
78 *
79 * @return array
80 * array of action links that we need to display for the browse screen
81 */
82 public static function &actionLinks() {
83 if (!isset(self::$_actionLinks)) {
84 self::$_actionLinks = array(
85 CRM_Core_Action::UPDATE => array(
86 'name' => ts('Edit Option'),
87 'url' => 'civicrm/admin/price/field/option',
88 'qs' => 'reset=1&action=update&oid=%%oid%%&fid=%%fid%%&sid=%%sid%%',
89 'title' => ts('Edit Price Option'),
90 ),
91 CRM_Core_Action::VIEW => array(
92 'name' => ts('View'),
93 'url' => 'civicrm/admin/price/field/option',
94 'qs' => 'action=view&oid=%%oid%%',
95 'title' => ts('View Price Option'),
96 ),
97 CRM_Core_Action::DISABLE => array(
98 'name' => ts('Disable'),
99 'ref' => 'crm-enable-disable',
100 'title' => ts('Disable Price Option'),
101 ),
102 CRM_Core_Action::ENABLE => array(
103 'name' => ts('Enable'),
104 'ref' => 'crm-enable-disable',
105 'title' => ts('Enable Price Option'),
106 ),
107 CRM_Core_Action::DELETE => array(
108 'name' => ts('Delete'),
109 'url' => 'civicrm/admin/price/field/option',
110 'qs' => 'action=delete&oid=%%oid%%',
111 'title' => ts('Disable Price Option'),
112 ),
113 );
114 }
115 return self::$_actionLinks;
116 }
117
118 /**
119 * Browse all price fields.
120 *
121 * @return void
122 */
123 public function browse() {
124 $priceOptions = civicrm_api3('PriceFieldValue', 'get', array(
125 'price_field_id' => $this->_fid,
126 // Explicitly do not check permissions so we are not
127 // restricted by financial type, so we can change them.
128 'check_permissions' => FALSE,
129 'options' => array(
130 'limit' => 0,
131 'sort' => array('weight', 'label'),
132 ),
133 ));
134 $customOption = $priceOptions['values'];
135
136 // CRM-15378 - check if these price options are in an Event price set
137 $isEvent = FALSE;
138 $extendComponentId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'extends', 'id');
139 $allComponents = explode(CRM_Core_DAO::VALUE_SEPARATOR, $extendComponentId);
140 $eventComponentId = CRM_Core_Component::getComponentID('CiviEvent');
141 if (in_array($eventComponentId, $allComponents)) {
142 $isEvent = TRUE;
143 }
144
145 $config = CRM_Core_Config::singleton();
146 $taxRate = CRM_Core_PseudoConstant::getTaxRates();
147 // display taxTerm for priceFields
148 $invoiceSettings = Civi::settings()->get('contribution_invoice_settings');
149 $taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings);
150 $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings);
151 $getTaxDetails = FALSE;
152 foreach ($customOption as $id => $values) {
153 $action = array_sum(array_keys(self::actionLinks()));
154 // Adding the required fields in the array
155 if (isset($taxRate[$values['financial_type_id']])) {
156 $customOption[$id]['tax_rate'] = $taxRate[$values['financial_type_id']];
157 if ($invoicing && isset($customOption[$id]['tax_rate'])) {
158 $getTaxDetails = TRUE;
159 }
160 $taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($customOption[$id]['amount'], $customOption[$id]['tax_rate']);
161 $customOption[$id]['tax_amount'] = $taxAmount['tax_amount'];
162 }
163 if (!empty($values['financial_type_id'])) {
164 $customOption[$id]['financial_type_id'] = CRM_Contribute_PseudoConstant::financialType($values['financial_type_id']);
165 }
166 // update enable/disable links depending on price_field properties.
167 if ($this->_isSetReserved) {
168 $action -= CRM_Core_Action::UPDATE + CRM_Core_Action::DELETE + CRM_Core_Action::DISABLE + CRM_Core_Action::ENABLE;
169 }
170 else {
171 if ($values['is_active']) {
172 $action -= CRM_Core_Action::ENABLE;
173 }
174 else {
175 $action -= CRM_Core_Action::DISABLE;
176 }
177 }
178 if (!empty($customOption[$id]['is_default'])) {
179 $customOption[$id]['is_default'] = '<img src="' . $config->resourceBase . 'i/check.gif" />';
180 }
181 else {
182 $customOption[$id]['is_default'] = '';
183 }
184 $customOption[$id]['order'] = $customOption[$id]['weight'];
185 $customOption[$id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), $action,
186 array(
187 'oid' => $id,
188 'fid' => $this->_fid,
189 'sid' => $this->_sid,
190 ),
191 ts('more'),
192 FALSE,
193 'priceFieldValue.row.actions',
194 'PriceFieldValue',
195 $id
196 );
197 }
198 // Add order changing widget to selector
199 $returnURL = CRM_Utils_System::url('civicrm/admin/price/field/option', "action=browse&reset=1&fid={$this->_fid}&sid={$this->_sid}");
200 $filter = "price_field_id = {$this->_fid}";
201 CRM_Utils_Weight::addOrder($customOption, 'CRM_Price_DAO_PriceFieldValue',
202 'id', $returnURL, $filter
203 );
204
205 $this->assign('taxTerm', $taxTerm);
206 $this->assign('getTaxDetails', $getTaxDetails);
207 $this->assign('customOption', $customOption);
208 $this->assign('sid', $this->_sid);
209 $this->assign('isEvent', $isEvent);
210 }
211
212 /**
213 * Edit custom Option.
214 *
215 * editing would involved modifying existing fields + adding data to new fields.
216 *
217 * @param string $action
218 * The action to be invoked.
219 *
220 * @return void
221 */
222 public function edit($action) {
223 $oid = CRM_Utils_Request::retrieve('oid', 'Positive',
224 $this, FALSE, 0
225 );
226 $params = array();
227 if ($oid) {
228 $params['oid'] = $oid;
229 $sid = CRM_Price_BAO_PriceSet::getSetId($params);
230
231 $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($sid);
232 }
233 // set the userContext stack
234 $session = CRM_Core_Session::singleton();
235
236 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field/option',
237 "reset=1&action=browse&fid={$this->_fid}&sid={$this->_sid}"
238 ));
239 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Option', ts('Price Field Option'), $action);
240 $controller->set('fid', $this->_fid);
241 $controller->setEmbedded(TRUE);
242 $controller->process();
243 $controller->run();
244
245 if ($action & CRM_Core_Action::DELETE) {
246 // add breadcrumb
247 $url = CRM_Utils_System::url('civicrm/admin/price/field/option', 'reset=1');
248 CRM_Utils_System::appendBreadCrumb(ts('Price Option'),
249 $url
250 );
251 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceFieldValue::getOptionLabel($oid));
252 $this->assign('usedBy', $usedBy);
253 $comps = array(
254 "Event" => "civicrm_event",
255 "Contribution" => "civicrm_contribution_page",
256 );
257 $priceSetContexts = array();
258 foreach ($comps as $name => $table) {
259 if (array_key_exists($table, $usedBy)) {
260 $priceSetContexts[] = $name;
261 }
262 }
263 $this->assign('contexts', $priceSetContexts);
264 }
265 }
266
267 /**
268 * Run the page.
269 *
270 * This method is called after the page is created. It checks for the
271 * type of action and executes that action.
272 *
273 * @return void
274 */
275 public function run() {
276 // get the field id
277 $this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive',
278 $this, FALSE, 0
279 );
280 //get the price set id
281 if (!$this->_sid) {
282 $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive', $this);
283 }
284
285 if ($this->_sid) {
286 CRM_Price_BAO_PriceSet::checkPermission($this->_sid);
287 $this->_isSetReserved = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'is_reserved');
288 $this->assign('isReserved', $this->_isSetReserved);
289 }
290 //as url contain $sid so append breadcrumb dynamically.
291 $breadcrumb = array(
292 array(
293 'title' => ts('Price Fields'),
294 'url' => CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&sid=' . $this->_sid),
295 ),
296 );
297 CRM_Utils_System::appendBreadCrumb($breadcrumb);
298
299 if ($this->_fid) {
300 $fieldTitle = CRM_Price_BAO_PriceField::getTitle($this->_fid);
301 $this->assign('fid', $this->_fid);
302 $this->assign('fieldTitle', $fieldTitle);
303 CRM_Utils_System::setTitle(ts('%1 - Price Options', array(1 => $fieldTitle)));
304
305 $htmlType = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceField', $this->_fid, 'html_type');
306 $this->assign('addMoreFields', TRUE);
307 //for text price field only single option present
308 if ($htmlType == 'Text') {
309 $this->assign('addMoreFields', FALSE);
310 }
311 }
312
313 // get the requested action
314 $action = CRM_Utils_Request::retrieve('action', 'String',
315 // default to 'browse'
316 $this, FALSE, 'browse'
317 );
318
319 // assign vars to templates
320 $this->assign('action', $action);
321
322 $oid = CRM_Utils_Request::retrieve('oid', 'Positive',
323 $this, FALSE, 0
324 );
325 // what action to take ?
326 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD |
327 CRM_Core_Action::VIEW | CRM_Core_Action::DELETE
328 ) && !$this->_isSetReserved
329 ) {
330 // no browse for edit/update/view
331 $this->edit($action);
332 }
333 else {
334 $this->browse();
335 }
336 // Call the parents run method
337 return parent::run();
338 }
339
340 }