INFRA-132 - Squiz.Scope.MethodScope.Missing
[civicrm-core.git] / CRM / Price / Page / Option.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
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 */
44class CRM_Price_Page_Option extends CRM_Core_Page {
45
96f50de2
CW
46 public $useLivePageJS = TRUE;
47
6a488035
TO
48 /**
49 * The field id of the option
50 *
51 * @var int
6a488035
TO
52 */
53 protected $_fid;
54
55 /**
56 * The field id of the option
57 *
58 * @var int
6a488035
TO
59 */
60 protected $_sid;
61
62 /**
63 * The price set is reserved or not
64 *
65 * @var boolean
6a488035 66 */
ba1dcfda 67 protected $_isSetReserved = FALSE;
366fe2a3 68
6a488035
TO
69 /**
70 * The action links that we need to display for the browse screen
71 *
72 * @var array
6a488035
TO
73 */
74 private static $_actionLinks;
75
76 /**
77 * Get the action links for this page.
78 *
79 * @param null
80 *
a6c01b45
CW
81 * @return array
82 * array of action links that we need to display for the browse screen
95ea96be 83 */
2da40d21 84 public function &actionLinks() {
6a488035
TO
85 if (!isset(self::$_actionLinks)) {
86 self::$_actionLinks = array(
87 CRM_Core_Action::UPDATE => array(
88 'name' => ts('Edit Option'),
89 'url' => 'civicrm/admin/price/field/option',
90 'qs' => 'reset=1&action=update&oid=%%oid%%&fid=%%fid%%&sid=%%sid%%',
91 'title' => ts('Edit Price Option'),
92 ),
93 CRM_Core_Action::VIEW => array(
94 'name' => ts('View'),
95 'url' => 'civicrm/admin/price/field/option',
96 'qs' => 'action=view&oid=%%oid%%',
97 'title' => ts('View Price Option'),
98 ),
99 CRM_Core_Action::DISABLE => array(
100 'name' => ts('Disable'),
4d17a233 101 'ref' => 'crm-enable-disable',
6a488035
TO
102 'title' => ts('Disable Price Option'),
103 ),
104 CRM_Core_Action::ENABLE => array(
105 'name' => ts('Enable'),
4d17a233 106 'ref' => 'crm-enable-disable',
6a488035
TO
107 'title' => ts('Enable Price Option'),
108 ),
109 CRM_Core_Action::DELETE => array(
110 'name' => ts('Delete'),
111 'url' => 'civicrm/admin/price/field/option',
112 'qs' => 'action=delete&oid=%%oid%%',
113 'title' => ts('Disable Price Option'),
114 ),
115 );
116 }
117 return self::$_actionLinks;
118 }
119
120 /**
121 * Browse all price fields.
122 *
123 * @param null
124 *
125 * @return void
6a488035 126 */
00be9182 127 public function browse() {
6a488035 128 $customOption = array();
9da8dc8c 129 CRM_Price_BAO_PriceFieldValue::getValues($this->_fid, $customOption);
6a488035
TO
130 $config = CRM_Core_Config::singleton();
131 $financialType = CRM_Contribute_PseudoConstant::financialType();
a32709be 132 $taxRate = CRM_Core_PseudoConstant::getTaxRates();
03b412ae 133 // display taxTerm for priceFields
ba1dcfda 134 $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings');
03b412ae
PB
135 $taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings);
136 $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings);
a32709be 137 $getTaxDetails = FALSE;
6a488035
TO
138 foreach ($customOption as $id => $values) {
139 $action = array_sum(array_keys($this->actionLinks()));
a32709be
PB
140 // Adding the required fields in the array
141 if (isset($taxRate[$values['financial_type_id']])) {
142 $customOption[$id]['tax_rate'] = $taxRate[$values['financial_type_id']];
03b412ae 143 if ($invoicing && isset($customOption[$id]['tax_rate'])) {
a32709be
PB
144 $getTaxDetails = TRUE;
145 }
146 $taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($customOption[$id]['amount'], $customOption[$id]['tax_rate']);
147 $customOption[$id]['tax_amount'] = $taxAmount['tax_amount'];
148 }
9b873358 149 if (!empty($values['financial_type_id'])) {
6a488035
TO
150 $customOption[$id]['financial_type_id'] = $financialType[$values['financial_type_id']];
151 }
152 // update enable/disable links depending on price_field properties.
153 if ($this->_isSetReserved) {
154 $action -= CRM_Core_Action::UPDATE + CRM_Core_Action::DELETE + CRM_Core_Action::DISABLE + CRM_Core_Action::ENABLE;
155 }
156 else {
157 if ($values['is_active']) {
158 $action -= CRM_Core_Action::ENABLE;
159 }
160 else {
161 $action -= CRM_Core_Action::DISABLE;
162 }
163 }
a7488080 164 if (!empty($customOption[$id]['is_default'])) {
6a488035
TO
165 $customOption[$id]['is_default'] = '<img src="' . $config->resourceBase . 'i/check.gif" />';
166 }
167 else {
168 $customOption[$id]['is_default'] = '';
169 }
170 $customOption[$id]['order'] = $customOption[$id]['weight'];
171 $customOption[$id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), $action,
172 array(
173 'oid' => $id,
174 'fid' => $this->_fid,
175 'sid' => $this->_sid,
87dab4a4
AH
176 ),
177 ts('more'),
178 FALSE,
179 'priceFieldValue.row.actions',
180 'PriceFieldValue',
181 $id
6a488035
TO
182 );
183 }
184 // Add order changing widget to selector
185 $returnURL = CRM_Utils_System::url('civicrm/admin/price/field/option', "action=browse&reset=1&fid={$this->_fid}&sid={$this->_sid}");
186 $filter = "price_field_id = {$this->_fid}";
9da8dc8c 187 CRM_Utils_Weight::addOrder($customOption, 'CRM_Price_DAO_PriceFieldValue',
6a488035
TO
188 'id', $returnURL, $filter
189 );
190
03b412ae 191 $this->assign('taxTerm', $taxTerm);
a32709be 192 $this->assign('getTaxDetails', $getTaxDetails);
6a488035
TO
193 $this->assign('customOption', $customOption);
194 $this->assign('sid', $this->_sid);
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 );
211 $params = array();
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
TO
237 $this->assign('usedBy', $usedBy);
238 $comps = array(
239 "Event" => "civicrm_event",
240 "Contribution" => "civicrm_contribution_page",
241 );
242 $priceSetContexts = array();
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 *
258 * @param null
259 *
260 * @return void
6a488035 261 */
00be9182 262 public function run() {
6a488035
TO
263
264 // get the field id
265 $this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive',
266 $this, FALSE, 0
267 );
268 //get the price set id
269 if (!$this->_sid) {
270 $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive', $this);
271 }
272
273 if ($this->_sid) {
9da8dc8c 274 CRM_Price_BAO_PriceSet::checkPermission($this->_sid);
ba1dcfda 275 $this->_isSetReserved = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'is_reserved');
6a488035
TO
276 $this->assign('isReserved', $this->_isSetReserved);
277 }
278 //as url contain $sid so append breadcrumb dynamically.
353ffa53
TO
279 $breadcrumb = array(
280 array(
281 'title' => ts('Price Fields'),
6a488035 282 'url' => CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&sid=' . $this->_sid),
af9b09df 283 ),
353ffa53 284 );
6a488035
TO
285 CRM_Utils_System::appendBreadCrumb($breadcrumb);
286
287 if ($this->_fid) {
9da8dc8c 288 $fieldTitle = CRM_Price_BAO_PriceField::getTitle($this->_fid);
6a488035
TO
289 $this->assign('fid', $this->_fid);
290 $this->assign('fieldTitle', $fieldTitle);
291 CRM_Utils_System::setTitle(ts('%1 - Price Options', array(1 => $fieldTitle)));
292
9da8dc8c 293 $htmlType = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceField', $this->_fid, 'html_type');
6a488035
TO
294 $this->assign('addMoreFields', TRUE);
295 //for text price field only single option present
296 if ($htmlType == 'Text') {
297 $this->assign('addMoreFields', FALSE);
298 }
299 }
300
301 // get the requested action
302 $action = CRM_Utils_Request::retrieve('action', 'String',
303 // default to 'browse'
304 $this, FALSE, 'browse'
305 );
306
307 // assign vars to templates
308 $this->assign('action', $action);
309
310 $oid = CRM_Utils_Request::retrieve('oid', 'Positive',
311 $this, FALSE, 0
312 );
313 // what action to take ?
314 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD |
315 CRM_Core_Action::VIEW | CRM_Core_Action::DELETE
353ffa53
TO
316 ) && !$this->_isSetReserved
317 ) {
6a488035
TO
318 // no browse for edit/update/view
319 $this->edit($action);
320 }
321 else {
322 $this->browse();
323 }
324 // Call the parents run method
325 return parent::run();
326 }
96025800 327
6a488035 328}