Merge pull request #4860 from totten/master-ext-cache
[civicrm-core.git] / CRM / Price / Page / Option.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 * @param null
80 *
81 * @return array array of action links that we need to display for the browse screen
82 */
83 function &actionLinks() {
84 if (!isset(self::$_actionLinks)) {
85 self::$_actionLinks = array(
86 CRM_Core_Action::UPDATE => array(
87 'name' => ts('Edit Option'),
88 'url' => 'civicrm/admin/price/field/option',
89 'qs' => 'reset=1&action=update&oid=%%oid%%&fid=%%fid%%&sid=%%sid%%',
90 'title' => ts('Edit Price Option'),
91 ),
92 CRM_Core_Action::VIEW => array(
93 'name' => ts('View'),
94 'url' => 'civicrm/admin/price/field/option',
95 'qs' => 'action=view&oid=%%oid%%',
96 'title' => ts('View Price Option'),
97 ),
98 CRM_Core_Action::DISABLE => array(
99 'name' => ts('Disable'),
100 'ref' => 'crm-enable-disable',
101 'title' => ts('Disable Price Option'),
102 ),
103 CRM_Core_Action::ENABLE => array(
104 'name' => ts('Enable'),
105 'ref' => 'crm-enable-disable',
106 'title' => ts('Enable Price Option'),
107 ),
108 CRM_Core_Action::DELETE => array(
109 'name' => ts('Delete'),
110 'url' => 'civicrm/admin/price/field/option',
111 'qs' => 'action=delete&oid=%%oid%%',
112 'title' => ts('Disable Price Option'),
113 ),
114 );
115 }
116 return self::$_actionLinks;
117 }
118
119 /**
120 * Browse all price fields.
121 *
122 * @param null
123 *
124 * @return void
125 */
126 public function browse() {
127 $customOption = array();
128 CRM_Price_BAO_PriceFieldValue::getValues($this->_fid, $customOption);
129 $config = CRM_Core_Config::singleton();
130 $financialType = CRM_Contribute_PseudoConstant::financialType();
131 $taxRate = CRM_Core_PseudoConstant::getTaxRates();
132 // display taxTerm for priceFields
133 $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings');
134 $taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings);
135 $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings);
136 $getTaxDetails = FALSE;
137 foreach ($customOption as $id => $values) {
138 $action = array_sum(array_keys($this->actionLinks()));
139 // Adding the required fields in the array
140 if (isset($taxRate[$values['financial_type_id']])) {
141 $customOption[$id]['tax_rate'] = $taxRate[$values['financial_type_id']];
142 if ($invoicing && isset($customOption[$id]['tax_rate'])) {
143 $getTaxDetails = TRUE;
144 }
145 $taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($customOption[$id]['amount'], $customOption[$id]['tax_rate']);
146 $customOption[$id]['tax_amount'] = $taxAmount['tax_amount'];
147 }
148 if (!empty($values['financial_type_id'])) {
149 $customOption[$id]['financial_type_id'] = $financialType[$values['financial_type_id']];
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 }
163 if (!empty($customOption[$id]['is_default'])) {
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,
171 array(
172 'oid' => $id,
173 'fid' => $this->_fid,
174 'sid' => $this->_sid,
175 ),
176 ts('more'),
177 FALSE,
178 'priceFieldValue.row.actions',
179 'PriceFieldValue',
180 $id
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}";
186 CRM_Utils_Weight::addOrder($customOption, 'CRM_Price_DAO_PriceFieldValue',
187 'id', $returnURL, $filter
188 );
189
190 $this->assign('taxTerm', $taxTerm);
191 $this->assign('getTaxDetails', $getTaxDetails);
192 $this->assign('customOption', $customOption);
193 $this->assign('sid', $this->_sid);
194 }
195
196 /**
197 * Edit custom Option.
198 *
199 * editing would involved modifying existing fields + adding data to new fields.
200 *
201 * @param string $action
202 * The action to be invoked.
203 *
204 * @return void
205 */
206 public function edit($action) {
207 $oid = CRM_Utils_Request::retrieve('oid', 'Positive',
208 $this, FALSE, 0
209 );
210 $params = array();
211 if ($oid) {
212 $params['oid'] = $oid;
213 $sid = CRM_Price_BAO_PriceSet::getSetId($params);
214
215 $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($sid);
216 }
217 // set the userContext stack
218 $session = CRM_Core_Session::singleton();
219
220 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field/option',
221 "reset=1&action=browse&fid={$this->_fid}&sid={$this->_sid}"
222 ));
223 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Option', ts('Price Field Option'), $action);
224 $controller->set('fid', $this->_fid);
225 $controller->setEmbedded(TRUE);
226 $controller->process();
227 $controller->run();
228
229 if ($action & CRM_Core_Action::DELETE) {
230 // add breadcrumb
231 $url = CRM_Utils_System::url('civicrm/admin/price/field/option', 'reset=1');
232 CRM_Utils_System::appendBreadCrumb(ts('Price Option'),
233 $url
234 );
235 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceFieldValue::getOptionLabel($oid));
236 $this->assign('usedBy', $usedBy);
237 $comps = array(
238 "Event" => "civicrm_event",
239 "Contribution" => "civicrm_contribution_page",
240 );
241 $priceSetContexts = array();
242 foreach ($comps as $name => $table) {
243 if (array_key_exists($table, $usedBy)) {
244 $priceSetContexts[] = $name;
245 }
246 }
247 $this->assign('contexts', $priceSetContexts);
248 }
249 }
250
251 /**
252 * Run the page.
253 *
254 * This method is called after the page is created. It checks for the
255 * type of action and executes that action.
256 *
257 * @param null
258 *
259 * @return void
260 */
261 public function run() {
262
263 // get the field id
264 $this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive',
265 $this, FALSE, 0
266 );
267 //get the price set id
268 if (!$this->_sid) {
269 $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive', $this);
270 }
271
272 if ($this->_sid) {
273 CRM_Price_BAO_PriceSet::checkPermission($this->_sid);
274 $this->_isSetReserved = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'is_reserved');
275 $this->assign('isReserved', $this->_isSetReserved);
276 }
277 //as url contain $sid so append breadcrumb dynamically.
278 $breadcrumb = array(array(
279 'title' => ts('Price Fields'),
280 'url' => CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&sid=' . $this->_sid),
281 ));
282 CRM_Utils_System::appendBreadCrumb($breadcrumb);
283
284 if ($this->_fid) {
285 $fieldTitle = CRM_Price_BAO_PriceField::getTitle($this->_fid);
286 $this->assign('fid', $this->_fid);
287 $this->assign('fieldTitle', $fieldTitle);
288 CRM_Utils_System::setTitle(ts('%1 - Price Options', array(1 => $fieldTitle)));
289
290 $htmlType = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceField', $this->_fid, 'html_type');
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
313 ) && !$this->_isSetReserved) {
314 // no browse for edit/update/view
315 $this->edit($action);
316 }
317 else {
318 $this->browse();
319 }
320 // Call the parents run method
321 return parent::run();
322 }
323 }