send most action links thru hook_civicrm_links
[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'] = $htmlTypes[$priceField[$priceFieldBAO->id]['html_type']];
174 $priceField[$priceFieldBAO->id]['order'] = $priceField[$priceFieldBAO->id]['weight'];
175 $priceField[$priceFieldBAO->id]['action'] = CRM_Core_Action::formLink(
176 self::actionLinks(),
177 $action,
178 array(
179 'fid' => $priceFieldBAO->id,
180 'sid' => $this->_sid,
181 ),
182 ts('more'),
183 FALSE,
184 'priceField.row.actions',
185 'PriceField',
186 $priceFieldBAO->id
187 );
188 }
189
190 $returnURL = CRM_Utils_System::url('civicrm/admin/price/field', "reset=1&action=browse&sid={$this->_sid}");
191 $filter = "price_set_id = {$this->_sid}";
192 CRM_Utils_Weight::addOrder($priceField, 'CRM_Price_DAO_PriceField',
193 'id', $returnURL, $filter
194 );
195 $this->assign('priceField', $priceField);
196 }
197
198 /**
199 * edit price data.
200 *
201 * editing would involved modifying existing fields + adding data to new fields.
202 *
203 * @param string $action the action to be invoked
204
205 *
206 * @return void
207 * @access public
208 */
209 function edit($action) {
210 // create a simple controller for editing price data
211 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Field', ts('Price Field'), $action);
212
213 // set the userContext stack
214 $session = CRM_Core_Session::singleton();
215 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid));
216
217 $controller->set('sid', $this->_sid);
218 $controller->setEmbedded(TRUE);
219 $controller->process();
220 $controller->run();
221 }
222
223 /**
224 * Run the page.
225 *
226 * This method is called after the page is created. It checks for the
227 * type of action and executes that action.
228 *
229 * @param null
230 *
231 * @return void
232 * @access public
233 */
234 function run() {
235
236 // get the group id
237 $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive',
238 $this
239 );
240 $fid = CRM_Utils_Request::retrieve('fid', 'Positive',
241 $this, FALSE, 0
242 );
243 $action = CRM_Utils_Request::retrieve('action', 'String',
244 // default to 'browse'
245 $this, FALSE, 'browse'
246 );
247
248 if ($this->_sid) {
249 $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($this->_sid);
250 $this->assign('usedBy', $usedBy);
251 $this->_isSetReserved= CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'is_reserved');
252 $this->assign('isReserved', $this->_isSetReserved);
253
254 CRM_Price_BAO_PriceSet::checkPermission($this->_sid);
255 $comps = array(
256 'Event' => 'civicrm_event',
257 'Contribution' => 'civicrm_contribution_page',
258 'EventTemplate' => 'civicrm_event_template'
259 );
260 $priceSetContexts = array();
261 foreach ($comps as $name => $table) {
262 if (array_key_exists($table, $usedBy)) {
263 $priceSetContexts[] = $name;
264 }
265 }
266 $this->assign('contexts', $priceSetContexts);
267 }
268
269 if ($action & (CRM_Core_Action::DELETE) && !$this->_isSetReserved) {
270 if (empty($usedBy)) {
271 // prompt to delete
272 $session = CRM_Core_Session::singleton();
273 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid));
274 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_DeleteField', 'Delete Price Field', '');
275 $controller->set('fid', $fid);
276 $controller->setEmbedded(TRUE);
277 $controller->process();
278 $controller->run();
279 }
280 else {
281 // add breadcrumb
282 $url = CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1');
283 CRM_Utils_System::appendBreadCrumb(ts('Price'),
284 $url
285 );
286 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceField::getTitle($fid));
287 }
288 }
289
290 if ($this->_sid) {
291 $groupTitle = CRM_Price_BAO_PriceSet::getTitle($this->_sid);
292 $this->assign('sid', $this->_sid);
293 $this->assign('groupTitle', $groupTitle);
294 CRM_Utils_System::setTitle(ts('%1 - Price Fields', array(1 => $groupTitle)));
295 }
296
297 // assign vars to templates
298 $this->assign('action', $action);
299
300 // what action to take ?
301 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD) && !$this->_isSetReserved) {
302 // no browse for edit/update/view
303 $this->edit($action);
304 }
305 elseif ($action & CRM_Core_Action::PREVIEW) {
306 $this->preview($fid);
307 }
308 else {
309 $this->browse();
310 }
311
312 // Call the parents run method
313 return parent::run();
314 }
315
316 /**
317 * Preview price field
318 *
319 * @param int $id price field id
320 *
321 * @return void
322 * @access public
323 */
324 function preview($fid) {
325 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Preview', ts('Preview Form Field'), CRM_Core_Action::PREVIEW);
326 $session = CRM_Core_Session::singleton();
327 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid));
328 $controller->set('fieldId', $fid);
329 $controller->set('groupId', $this->_sid);
330 $controller->setEmbedded(TRUE);
331 $controller->process();
332 $controller->run();
333 }
334 }
335