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