Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-02-03-14-48-25
[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 '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 'extra' => 'onclick = "return confirm(\'' . $deleteExtra . '\');"',
110 ),
111 );
112 }
113 return self::$_actionLinks;
114 }
115
116 /**
117 * Browse all price set fields.
118 *
119 * @param null
120 *
121 * @return void
122 * @access public
123 */
124 function browse() {
125 CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js');
126 $priceField = array();
127 $priceFieldBAO = new CRM_Price_BAO_PriceField();
128
129 // fkey is sid
130 $priceFieldBAO->price_set_id = $this->_sid;
131 $priceFieldBAO->orderBy('weight, label');
132 $priceFieldBAO->find();
133
134 while ($priceFieldBAO->fetch()) {
135 $priceField[$priceFieldBAO->id] = array();
136 CRM_Core_DAO::storeValues($priceFieldBAO, $priceField[$priceFieldBAO->id]);
137
138 // get price if it's a text field
139 if ($priceFieldBAO->html_type == 'Text') {
140 $optionValues = array();
141 $params = array('price_field_id' => $priceFieldBAO->id);
142
143 CRM_Price_BAO_PriceFieldValue::retrieve($params, $optionValues);
144
145 $priceField[$priceFieldBAO->id]['price'] = CRM_Utils_Array::value('amount', $optionValues);
146 }
147
148 $action = array_sum(array_keys($this->actionLinks()));
149
150 if ($this->_isSetReserved) {
151 $action -= CRM_Core_Action::UPDATE + CRM_Core_Action::DELETE + CRM_Core_Action::ENABLE + CRM_Core_Action::DISABLE;
152 }
153 else {
154 if ($priceFieldBAO->is_active) {
155 $action -= CRM_Core_Action::ENABLE;
156 }
157 else {
158 $action -= CRM_Core_Action::DISABLE;
159 }
160 }
161
162 if ($priceFieldBAO->active_on == '0000-00-00 00:00:00') {
163 $priceField[$priceFieldBAO->id]['active_on'] = '';
164 }
165
166 if ($priceFieldBAO->expire_on == '0000-00-00 00:00:00') {
167 $priceField[$priceFieldBAO->id]['expire_on'] = '';
168 }
169
170 // need to translate html types from the db
171 $htmlTypes = CRM_Price_BAO_PriceField::htmlTypes();
172 $priceField[$priceFieldBAO->id]['html_type'] = $htmlTypes[$priceField[$priceFieldBAO->id]['html_type']];
173 $priceField[$priceFieldBAO->id]['order'] = $priceField[$priceFieldBAO->id]['weight'];
174 $priceField[$priceFieldBAO->id]['action'] = CRM_Core_Action::formLink(
175 self::actionLinks(),
176 $action,
177 array(
178 'fid' => $priceFieldBAO->id,
179 'sid' => $this->_sid,
180 ),
181 ts('more'),
182 FALSE,
183 'priceField.row.actions',
184 'PriceField',
185 $priceFieldBAO->id
186 );
187 }
188
189 $returnURL = CRM_Utils_System::url('civicrm/admin/price/field', "reset=1&action=browse&sid={$this->_sid}");
190 $filter = "price_set_id = {$this->_sid}";
191 CRM_Utils_Weight::addOrder($priceField, 'CRM_Price_DAO_PriceField',
192 'id', $returnURL, $filter
193 );
194 $this->assign('priceField', $priceField);
195 }
196
197 /**
198 * edit price data.
199 *
200 * editing would involved modifying existing fields + adding data to new fields.
201 *
202 * @param string $action the action to be invoked
203
204 *
205 * @return void
206 * @access public
207 */
208 function edit($action) {
209 // create a simple controller for editing price data
210 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Field', ts('Price Field'), $action);
211
212 // set the userContext stack
213 $session = CRM_Core_Session::singleton();
214 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid));
215
216 $controller->set('sid', $this->_sid);
217 $controller->setEmbedded(TRUE);
218 $controller->process();
219 $controller->run();
220 }
221
222 /**
223 * Run the page.
224 *
225 * This method is called after the page is created. It checks for the
226 * type of action and executes that action.
227 *
228 * @param null
229 *
230 * @return void
231 * @access public
232 */
233 function run() {
234
235 // get the group id
236 $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive',
237 $this
238 );
239 $fid = CRM_Utils_Request::retrieve('fid', 'Positive',
240 $this, FALSE, 0
241 );
242 $action = CRM_Utils_Request::retrieve('action', 'String',
243 // default to 'browse'
244 $this, FALSE, 'browse'
245 );
246
247 if ($this->_sid) {
248 $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($this->_sid);
249 $this->assign('usedBy', $usedBy);
250 $this->_isSetReserved= CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'is_reserved');
251 $this->assign('isReserved', $this->_isSetReserved);
252
253 CRM_Price_BAO_PriceSet::checkPermission($this->_sid);
254 $comps = array(
255 'Event' => 'civicrm_event',
256 'Contribution' => 'civicrm_contribution_page',
257 'EventTemplate' => 'civicrm_event_template'
258 );
259 $priceSetContexts = array();
260 foreach ($comps as $name => $table) {
261 if (array_key_exists($table, $usedBy)) {
262 $priceSetContexts[] = $name;
263 }
264 }
265 $this->assign('contexts', $priceSetContexts);
266 }
267
268 if ($action & (CRM_Core_Action::DELETE) && !$this->_isSetReserved) {
269 if (empty($usedBy)) {
270 // prompt to delete
271 $session = CRM_Core_Session::singleton();
272 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid));
273 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_DeleteField', 'Delete Price Field', '');
274 $controller->set('fid', $fid);
275 $controller->setEmbedded(TRUE);
276 $controller->process();
277 $controller->run();
278 }
279 else {
280 // add breadcrumb
281 $url = CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1');
282 CRM_Utils_System::appendBreadCrumb(ts('Price'),
283 $url
284 );
285 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceField::getTitle($fid));
286 }
287 }
288
289 if ($this->_sid) {
290 $groupTitle = CRM_Price_BAO_PriceSet::getTitle($this->_sid);
291 $this->assign('sid', $this->_sid);
292 $this->assign('groupTitle', $groupTitle);
293 CRM_Utils_System::setTitle(ts('%1 - Price Fields', array(1 => $groupTitle)));
294 }
295
296 // assign vars to templates
297 $this->assign('action', $action);
298
299 // what action to take ?
300 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD) && !$this->_isSetReserved) {
301 // no browse for edit/update/view
302 $this->edit($action);
303 }
304 elseif ($action & CRM_Core_Action::PREVIEW) {
305 $this->preview($fid);
306 }
307 else {
308 $this->browse();
309 }
310
311 // Call the parents run method
312 return parent::run();
313 }
314
315 /**
316 * Preview price field
317 *
318 * @param int $id price field id
319 *
320 * @return void
321 * @access public
322 */
323 function preview($fid) {
324 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Preview', ts('Preview Form Field'), CRM_Core_Action::PREVIEW);
325 $session = CRM_Core_Session::singleton();
326 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid));
327 $controller->set('fieldId', $fid);
328 $controller->set('groupId', $this->_sid);
329 $controller->setEmbedded(TRUE);
330 $controller->process();
331 $controller->run();
332 }
333 }
334