Merge pull request #4857 from atif-shaikh/CRM-15203
[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 the action to be invoked
202 *
203 * @return void
204 */
205 public function edit($action) {
206 $oid = CRM_Utils_Request::retrieve('oid', 'Positive',
207 $this, FALSE, 0
208 );
209 $params = array();
210 if ($oid) {
211 $params['oid'] = $oid;
212 $sid = CRM_Price_BAO_PriceSet::getSetId($params);
213
214 $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($sid);
215 }
216 // set the userContext stack
217 $session = CRM_Core_Session::singleton();
218
219 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field/option',
220 "reset=1&action=browse&fid={$this->_fid}&sid={$this->_sid}"
221 ));
222 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Option', ts('Price Field Option'), $action);
223 $controller->set('fid', $this->_fid);
224 $controller->setEmbedded(TRUE);
225 $controller->process();
226 $controller->run();
227
228 if ($action & CRM_Core_Action::DELETE) {
229 // add breadcrumb
230 $url = CRM_Utils_System::url('civicrm/admin/price/field/option', 'reset=1');
231 CRM_Utils_System::appendBreadCrumb(ts('Price Option'),
232 $url
233 );
234 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceFieldValue::getOptionLabel($oid));
235 $this->assign('usedBy', $usedBy);
236 $comps = array(
237 "Event" => "civicrm_event",
238 "Contribution" => "civicrm_contribution_page",
239 );
240 $priceSetContexts = array();
241 foreach ($comps as $name => $table) {
242 if (array_key_exists($table, $usedBy)) {
243 $priceSetContexts[] = $name;
244 }
245 }
246 $this->assign('contexts', $priceSetContexts);
247 }
248 }
249
250 /**
251 * Run the page.
252 *
253 * This method is called after the page is created. It checks for the
254 * type of action and executes that action.
255 *
256 * @param null
257 *
258 * @return void
259 */
260 public function run() {
261
262 // get the field id
263 $this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive',
264 $this, FALSE, 0
265 );
266 //get the price set id
267 if (!$this->_sid) {
268 $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive', $this);
269 }
270
271 if ($this->_sid) {
272 CRM_Price_BAO_PriceSet::checkPermission($this->_sid);
273 $this->_isSetReserved= CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'is_reserved');
274 $this->assign('isReserved', $this->_isSetReserved);
275 }
276 //as url contain $sid so append breadcrumb dynamically.
277 $breadcrumb = array(array('title' => ts('Price Fields'),
278 'url' => CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&sid=' . $this->_sid),
279 ));
280 CRM_Utils_System::appendBreadCrumb($breadcrumb);
281
282 if ($this->_fid) {
283 $fieldTitle = CRM_Price_BAO_PriceField::getTitle($this->_fid);
284 $this->assign('fid', $this->_fid);
285 $this->assign('fieldTitle', $fieldTitle);
286 CRM_Utils_System::setTitle(ts('%1 - Price Options', array(1 => $fieldTitle)));
287
288 $htmlType = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceField', $this->_fid, 'html_type');
289 $this->assign('addMoreFields', TRUE);
290 //for text price field only single option present
291 if ($htmlType == 'Text') {
292 $this->assign('addMoreFields', FALSE);
293 }
294 }
295
296 // get the requested action
297 $action = CRM_Utils_Request::retrieve('action', 'String',
298 // default to 'browse'
299 $this, FALSE, 'browse'
300 );
301
302 // assign vars to templates
303 $this->assign('action', $action);
304
305 $oid = CRM_Utils_Request::retrieve('oid', 'Positive',
306 $this, FALSE, 0
307 );
308 // what action to take ?
309 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD |
310 CRM_Core_Action::VIEW | CRM_Core_Action::DELETE
311 ) && !$this->_isSetReserved) {
312 // no browse for edit/update/view
313 $this->edit($action);
314 }
315 else {
316 $this->browse();
317 }
318 // Call the parents run method
319 return parent::run();
320 }
321 }