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