commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / CRM / Price / Page / Option.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 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 $customOption = array();
125 CRM_Price_BAO_PriceFieldValue::getValues($this->_fid, $customOption);
126 $config = CRM_Core_Config::singleton();
127 $financialType = CRM_Contribute_PseudoConstant::financialType();
128 $taxRate = CRM_Core_PseudoConstant::getTaxRates();
129 // display taxTerm for priceFields
130 $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings');
131 $taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings);
132 $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings);
133 $getTaxDetails = FALSE;
134 foreach ($customOption as $id => $values) {
135 $action = array_sum(array_keys($this->actionLinks()));
136 // Adding the required fields in the array
137 if (isset($taxRate[$values['financial_type_id']])) {
138 $customOption[$id]['tax_rate'] = $taxRate[$values['financial_type_id']];
139 if ($invoicing && isset($customOption[$id]['tax_rate'])) {
140 $getTaxDetails = TRUE;
141 }
142 $taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($customOption[$id]['amount'], $customOption[$id]['tax_rate']);
143 $customOption[$id]['tax_amount'] = $taxAmount['tax_amount'];
144 }
145 if (!empty($values['financial_type_id'])) {
146 $customOption[$id]['financial_type_id'] = $financialType[$values['financial_type_id']];
147 }
148 // update enable/disable links depending on price_field properties.
149 if ($this->_isSetReserved) {
150 $action -= CRM_Core_Action::UPDATE + CRM_Core_Action::DELETE + CRM_Core_Action::DISABLE + CRM_Core_Action::ENABLE;
151 }
152 else {
153 if ($values['is_active']) {
154 $action -= CRM_Core_Action::ENABLE;
155 }
156 else {
157 $action -= CRM_Core_Action::DISABLE;
158 }
159 }
160 if (!empty($customOption[$id]['is_default'])) {
161 $customOption[$id]['is_default'] = '<img src="' . $config->resourceBase . 'i/check.gif" />';
162 }
163 else {
164 $customOption[$id]['is_default'] = '';
165 }
166 $customOption[$id]['order'] = $customOption[$id]['weight'];
167 $customOption[$id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), $action,
168 array(
169 'oid' => $id,
170 'fid' => $this->_fid,
171 'sid' => $this->_sid,
172 ),
173 ts('more'),
174 FALSE,
175 'priceFieldValue.row.actions',
176 'PriceFieldValue',
177 $id
178 );
179 }
180 // Add order changing widget to selector
181 $returnURL = CRM_Utils_System::url('civicrm/admin/price/field/option', "action=browse&reset=1&fid={$this->_fid}&sid={$this->_sid}");
182 $filter = "price_field_id = {$this->_fid}";
183 CRM_Utils_Weight::addOrder($customOption, 'CRM_Price_DAO_PriceFieldValue',
184 'id', $returnURL, $filter
185 );
186
187 $this->assign('taxTerm', $taxTerm);
188 $this->assign('getTaxDetails', $getTaxDetails);
189 $this->assign('customOption', $customOption);
190 $this->assign('sid', $this->_sid);
191 }
192
193 /**
194 * Edit custom Option.
195 *
196 * editing would involved modifying existing fields + adding data to new fields.
197 *
198 * @param string $action
199 * The action to be invoked.
200 *
201 * @return void
202 */
203 public function edit($action) {
204 $oid = CRM_Utils_Request::retrieve('oid', 'Positive',
205 $this, FALSE, 0
206 );
207 $params = array();
208 if ($oid) {
209 $params['oid'] = $oid;
210 $sid = CRM_Price_BAO_PriceSet::getSetId($params);
211
212 $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($sid);
213 }
214 // set the userContext stack
215 $session = CRM_Core_Session::singleton();
216
217 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field/option',
218 "reset=1&action=browse&fid={$this->_fid}&sid={$this->_sid}"
219 ));
220 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Option', ts('Price Field Option'), $action);
221 $controller->set('fid', $this->_fid);
222 $controller->setEmbedded(TRUE);
223 $controller->process();
224 $controller->run();
225
226 if ($action & CRM_Core_Action::DELETE) {
227 // add breadcrumb
228 $url = CRM_Utils_System::url('civicrm/admin/price/field/option', 'reset=1');
229 CRM_Utils_System::appendBreadCrumb(ts('Price Option'),
230 $url
231 );
232 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceFieldValue::getOptionLabel($oid));
233 $this->assign('usedBy', $usedBy);
234 $comps = array(
235 "Event" => "civicrm_event",
236 "Contribution" => "civicrm_contribution_page",
237 );
238 $priceSetContexts = array();
239 foreach ($comps as $name => $table) {
240 if (array_key_exists($table, $usedBy)) {
241 $priceSetContexts[] = $name;
242 }
243 }
244 $this->assign('contexts', $priceSetContexts);
245 }
246 }
247
248 /**
249 * Run the page.
250 *
251 * This method is called after the page is created. It checks for the
252 * type of action and executes that action.
253 *
254 * @return void
255 */
256 public function run() {
257 // get the field id
258 $this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive',
259 $this, FALSE, 0
260 );
261 //get the price set id
262 if (!$this->_sid) {
263 $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive', $this);
264 }
265
266 if ($this->_sid) {
267 CRM_Price_BAO_PriceSet::checkPermission($this->_sid);
268 $this->_isSetReserved = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'is_reserved');
269 $this->assign('isReserved', $this->_isSetReserved);
270 }
271 //as url contain $sid so append breadcrumb dynamically.
272 $breadcrumb = array(
273 array(
274 'title' => ts('Price Fields'),
275 'url' => CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&sid=' . $this->_sid),
276 ),
277 );
278 CRM_Utils_System::appendBreadCrumb($breadcrumb);
279
280 if ($this->_fid) {
281 $fieldTitle = CRM_Price_BAO_PriceField::getTitle($this->_fid);
282 $this->assign('fid', $this->_fid);
283 $this->assign('fieldTitle', $fieldTitle);
284 CRM_Utils_System::setTitle(ts('%1 - Price Options', array(1 => $fieldTitle)));
285
286 $htmlType = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceField', $this->_fid, 'html_type');
287 $this->assign('addMoreFields', TRUE);
288 //for text price field only single option present
289 if ($htmlType == 'Text') {
290 $this->assign('addMoreFields', FALSE);
291 }
292 }
293
294 // get the requested action
295 $action = CRM_Utils_Request::retrieve('action', 'String',
296 // default to 'browse'
297 $this, FALSE, 'browse'
298 );
299
300 // assign vars to templates
301 $this->assign('action', $action);
302
303 $oid = CRM_Utils_Request::retrieve('oid', 'Positive',
304 $this, FALSE, 0
305 );
306 // what action to take ?
307 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD |
308 CRM_Core_Action::VIEW | CRM_Core_Action::DELETE
309 ) && !$this->_isSetReserved
310 ) {
311 // no browse for edit/update/view
312 $this->edit($action);
313 }
314 else {
315 $this->browse();
316 }
317 // Call the parents run method
318 return parent::run();
319 }
320
321 }