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