fix run-on formatting
[civicrm-core.git] / CRM / Price / Page / Field.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 Price Fields.
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_Field extends CRM_Core_Page {
45
46 public $useLivePageJS = TRUE;
47
48 /**
49 * The price set group id of the field
50 *
51 * @var int
52 */
53 protected $_sid;
54
55 /**
56 * The action links that we need to display for the browse screen
57 *
58 * @var array
59 */
60 private static $_actionLinks;
61
62 /**
63 * The price set is reserved or not
64 *
65 * @var boolean
66 */
67 protected $_isSetReserved = false;
68
69 /**
70 * Get the action links for this page.
71 *
72 * @param null
73 *
74 * @return array array of action links that we need to display for the browse screen
75 */
76 function &actionLinks() {
77 if (!isset(self::$_actionLinks)) {
78 self::$_actionLinks = array(
79 CRM_Core_Action::UPDATE => array(
80 'name' => ts('Edit Price Field'),
81 'url' => 'civicrm/admin/price/field',
82 'qs' => 'action=update&reset=1&sid=%%sid%%&fid=%%fid%%',
83 'title' => ts('Edit Price'),
84 ),
85 CRM_Core_Action::PREVIEW => array(
86 'name' => ts('Preview Field'),
87 'url' => 'civicrm/admin/price/field',
88 'qs' => 'action=preview&reset=1&sid=%%sid%%&fid=%%fid%%',
89 'title' => ts('Preview Price'),
90 ),
91 CRM_Core_Action::DISABLE => array(
92 'name' => ts('Disable'),
93 'ref' => 'crm-enable-disable',
94 'title' => ts('Disable Price'),
95 ),
96 CRM_Core_Action::ENABLE => array(
97 'name' => ts('Enable'),
98 'ref' => 'crm-enable-disable',
99 'title' => ts('Enable Price'),
100 ),
101 CRM_Core_Action::DELETE => array(
102 'name' => ts('Delete'),
103 'url' => 'civicrm/admin/price/field',
104 'qs' => 'action=delete&reset=1&sid=%%sid%%&fid=%%fid%%',
105 'title' => ts('Delete Price'),
106 ),
107 );
108 }
109 return self::$_actionLinks;
110 }
111
112 /**
113 * Browse all price set fields.
114 *
115 * @param null
116 *
117 * @return void
118 */
119 public function browse() {
120 $resourceManager = CRM_Core_Resources::singleton();
121 if (!empty($_GET['new']) && $resourceManager->ajaxPopupsEnabled) {
122 $resourceManager->addScriptFile('civicrm', 'js/crm.addNew.js', 999, 'html-header');
123 }
124
125 $priceField = array();
126 $priceFieldBAO = new CRM_Price_BAO_PriceField();
127
128 // fkey is sid
129 $priceFieldBAO->price_set_id = $this->_sid;
130 $priceFieldBAO->orderBy('weight, label');
131 $priceFieldBAO->find();
132
133 // display taxTerm for priceFields
134 $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,'contribution_invoice_settings');
135 $taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings);
136 $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings);
137 $getTaxDetails = FALSE;
138 $taxRate = CRM_Core_PseudoConstant::getTaxRates();
139 while ($priceFieldBAO->fetch()) {
140 $priceField[$priceFieldBAO->id] = array();
141 CRM_Core_DAO::storeValues($priceFieldBAO, $priceField[$priceFieldBAO->id]);
142
143 // get price if it's a text field
144 if ($priceFieldBAO->html_type == 'Text') {
145 $optionValues = array();
146 $params = array('price_field_id' => $priceFieldBAO->id);
147
148 CRM_Price_BAO_PriceFieldValue::retrieve($params, $optionValues);
149
150 $financialTypeId = $optionValues['financial_type_id'];
151 $priceField[$priceFieldBAO->id]['price'] = CRM_Utils_Array::value('amount', $optionValues);
152 if ($invoicing && isset($taxRate[$financialTypeId])) {
153 $priceField[$priceFieldBAO->id]['tax_rate'] = $taxRate[$financialTypeId];
154 $getTaxDetails = TRUE;
155 }
156 if (isset($priceField[$priceFieldBAO->id]['tax_rate'])) {
157 $taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($priceField[$priceFieldBAO->id]['price'], $priceField[$priceFieldBAO->id]['tax_rate']);
158 $priceField[$priceFieldBAO->id]['tax_amount'] = $taxAmount['tax_amount'];
159 }
160 }
161
162 $action = array_sum(array_keys($this->actionLinks()));
163
164 if ($this->_isSetReserved) {
165 $action -= CRM_Core_Action::UPDATE + CRM_Core_Action::DELETE + CRM_Core_Action::ENABLE + CRM_Core_Action::DISABLE;
166 }
167 else {
168 if ($priceFieldBAO->is_active) {
169 $action -= CRM_Core_Action::ENABLE;
170 }
171 else {
172 $action -= CRM_Core_Action::DISABLE;
173 }
174 }
175
176 if ($priceFieldBAO->active_on == '0000-00-00 00:00:00') {
177 $priceField[$priceFieldBAO->id]['active_on'] = '';
178 }
179
180 if ($priceFieldBAO->expire_on == '0000-00-00 00:00:00') {
181 $priceField[$priceFieldBAO->id]['expire_on'] = '';
182 }
183
184 // need to translate html types from the db
185 $htmlTypes = CRM_Price_BAO_PriceField::htmlTypes();
186 $priceField[$priceFieldBAO->id]['html_type_display'] = $htmlTypes[$priceField[$priceFieldBAO->id]['html_type']];
187 $priceField[$priceFieldBAO->id]['order'] = $priceField[$priceFieldBAO->id]['weight'];
188 $priceField[$priceFieldBAO->id]['action'] = CRM_Core_Action::formLink(
189 self::actionLinks(),
190 $action,
191 array(
192 'fid' => $priceFieldBAO->id,
193 'sid' => $this->_sid,
194 ),
195 ts('more'),
196 FALSE,
197 'priceField.row.actions',
198 'PriceField',
199 $priceFieldBAO->id
200 );
201 $this->assign('taxTerm', $taxTerm);
202 $this->assign('getTaxDetails', $getTaxDetails);
203 }
204
205 $returnURL = CRM_Utils_System::url('civicrm/admin/price/field', "reset=1&action=browse&sid={$this->_sid}");
206 $filter = "price_set_id = {$this->_sid}";
207 CRM_Utils_Weight::addOrder($priceField, 'CRM_Price_DAO_PriceField',
208 'id', $returnURL, $filter
209 );
210 $this->assign('priceField', $priceField);
211 }
212
213 /**
214 * Edit price data.
215 *
216 * editing would involved modifying existing fields + adding data to new fields.
217 *
218 * @param string $action the action to be invoked
219
220 *
221 * @return void
222 */
223 public function edit($action) {
224 // create a simple controller for editing price data
225 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Field', ts('Price Field'), $action);
226
227 // set the userContext stack
228 $session = CRM_Core_Session::singleton();
229 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid));
230
231 $controller->set('sid', $this->_sid);
232 $controller->setEmbedded(TRUE);
233 $controller->process();
234 $controller->run();
235 }
236
237 /**
238 * Run the page.
239 *
240 * This method is called after the page is created. It checks for the
241 * type of action and executes that action.
242 *
243 * @param null
244 *
245 * @return void
246 */
247 public function run() {
248
249 // get the group id
250 $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive',
251 $this
252 );
253 $fid = CRM_Utils_Request::retrieve('fid', 'Positive',
254 $this, FALSE, 0
255 );
256 $action = CRM_Utils_Request::retrieve('action', 'String',
257 // default to 'browse'
258 $this, FALSE, 'browse'
259 );
260
261 if ($this->_sid) {
262 $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($this->_sid);
263 $this->assign('usedBy', $usedBy);
264 $this->_isSetReserved= CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'is_reserved');
265 $this->assign('isReserved', $this->_isSetReserved);
266
267 CRM_Price_BAO_PriceSet::checkPermission($this->_sid);
268 $comps = array(
269 'Event' => 'civicrm_event',
270 'Contribution' => 'civicrm_contribution_page',
271 'EventTemplate' => 'civicrm_event_template'
272 );
273 $priceSetContexts = array();
274 foreach ($comps as $name => $table) {
275 if (array_key_exists($table, $usedBy)) {
276 $priceSetContexts[] = $name;
277 }
278 }
279 $this->assign('contexts', $priceSetContexts);
280 }
281
282 if ($action & (CRM_Core_Action::DELETE) && !$this->_isSetReserved) {
283 if (empty($usedBy)) {
284 // prompt to delete
285 $session = CRM_Core_Session::singleton();
286 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid));
287 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_DeleteField', 'Delete Price Field', '');
288 $controller->set('fid', $fid);
289 $controller->setEmbedded(TRUE);
290 $controller->process();
291 $controller->run();
292 }
293 else {
294 // add breadcrumb
295 $url = CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1');
296 CRM_Utils_System::appendBreadCrumb(ts('Price'),
297 $url
298 );
299 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceField::getTitle($fid));
300 }
301 }
302
303 if ($action & CRM_Core_Action::DELETE) {
304 CRM_Utils_System::setTitle(ts('Delete Price Field'));
305 }
306 elseif ($this->_sid) {
307 $groupTitle = CRM_Price_BAO_PriceSet::getTitle($this->_sid);
308 $this->assign('sid', $this->_sid);
309 $this->assign('groupTitle', $groupTitle);
310 CRM_Utils_System::setTitle(ts('%1 - Price Fields', array(1 => $groupTitle)));
311 }
312
313 // assign vars to templates
314 $this->assign('action', $action);
315
316 // what action to take ?
317 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD) && !$this->_isSetReserved) {
318 // no browse for edit/update/view
319 $this->edit($action);
320 }
321 elseif ($action & CRM_Core_Action::PREVIEW) {
322 $this->preview($fid);
323 }
324 else {
325 $this->browse();
326 }
327
328 // Call the parents run method
329 return parent::run();
330 }
331
332 /**
333 * Preview price field
334 *
335 * @param int $fid
336 *
337 * @internal param int $id price field id
338 *
339 * @return void
340 */
341 public function preview($fid) {
342 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Preview', ts('Preview Form Field'), CRM_Core_Action::PREVIEW);
343 $session = CRM_Core_Session::singleton();
344 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid));
345 $controller->set('fieldId', $fid);
346 $controller->set('groupId', $this->_sid);
347 $controller->setEmbedded(TRUE);
348 $controller->process();
349 $controller->run();
350 }
351 }