Merge pull request #220 from kurund/CRM-12155
[civicrm-core.git] / CRM / Price / Page / Set.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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 Sets.
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 */
44class CRM_Price_Page_Set extends CRM_Core_Page {
45
46 /**
47 * The action links that we need to display for the browse screen
48 *
49 * @var array
50 */
51 private static $_actionLinks;
52
53 /**
54 * Get the action links for this page.
55 *
56 * @param null
57 *
58 * @return array array of action links that we need to display for the browse screen
59 * @access public
60 */ function &actionLinks() {
61 // check if variable _actionsLinks is populated
62 if (!isset(self::$_actionLinks)) {
63 // helper variable for nicer formatting
64 $deleteExtra = ts('Are you sure you want to delete this price set?');
65 $copyExtra = ts('Are you sure you want to make a copy of this price set?');
66 self::$_actionLinks = array(
67 CRM_Core_Action::BROWSE => array(
68 'name' => ts('View and Edit Price Fields'),
69 'url' => 'civicrm/admin/price/field',
70 'qs' => 'reset=1&action=browse&sid=%%sid%%',
71 'title' => ts('View and Edit Price Fields'),
72 ),
73 CRM_Core_Action::PREVIEW => array(
74 'name' => ts('Preview'),
75 'url' => 'civicrm/admin/price',
76 'qs' => 'action=preview&reset=1&sid=%%sid%%',
77 'title' => ts('Preview Price Set'),
78 ),
79 CRM_Core_Action::UPDATE => array(
80 'name' => ts('Settings'),
81 'url' => 'civicrm/admin/price',
82 'qs' => 'action=update&reset=1&sid=%%sid%%',
83 'title' => ts('Edit Price Set'),
84 ),
85 CRM_Core_Action::DISABLE => array(
86 'name' => ts('Disable'),
87 'extra' => 'onclick = "enableDisable( %%sid%%,\'' . 'CRM_Price_BAO_Set' . '\',\'' . 'enable-disable' . '\' );"',
88 'ref' => 'disable-action',
89 'title' => ts('Disable Price Set'),
90 ),
91 CRM_Core_Action::ENABLE => array(
92 'name' => ts('Enable'),
93 'extra' => 'onclick = "enableDisable( %%sid%%,\'' . 'CRM_Price_BAO_Set' . '\',\'' . 'disable-enable' . '\' );"',
94 'ref' => 'enable-action',
95 'title' => ts('Enable Price Set'),
96 ),
97 CRM_Core_Action::DELETE => array(
98 'name' => ts('Delete'),
99 'url' => 'civicrm/admin/price',
100 'qs' => 'action=delete&reset=1&sid=%%sid%%',
101 'title' => ts('Delete Price Set'),
102 'extra' => 'onclick = "return confirm(\'' . $deleteExtra . '\');"',
103 ),
104 CRM_Core_Action::COPY => array(
105 'name' => ts('Copy Price Set'),
106 'url' => CRM_Utils_System::currentPath(),
107 'qs' => 'action=copy&sid=%%sid%%',
108 'title' => ts('Make a Copy of Price Set'),
109 'extra' => 'onclick = "return confirm(\'' . $copyExtra . '\');"',
110 ),
111 );
112 }
113 return self::$_actionLinks;
114 }
115
116 /**
117 * Run the page.
118 *
119 * This method is called after the page is created. It checks for the
120 * type of action and executes that action.
121 * Finally it calls the parent's run method.
122 *
123 * @param null
124 *
125 * @return void
126 * @access public
127 *
128 */
129 function run() {
130 // get the requested action
131 $action = CRM_Utils_Request::retrieve('action', 'String',
132 // default to 'browse'
133 $this, FALSE, 'browse'
134 );
135
136 // assign vars to templates
137 $this->assign('action', $action);
138 $sid = CRM_Utils_Request::retrieve('sid', 'Positive',
139 $this, FALSE, 0
140 );
141
142 if ($sid) {
143 CRM_Price_BAO_Set::checkPermission($sid);
144 }
145 // what action to take ?
146 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) {
147 $this->edit($sid, $action);
148 }
149 elseif ($action & CRM_Core_Action::PREVIEW) {
150 $this->preview($sid);
151 }
152 elseif ($action & CRM_Core_Action::COPY) {
153 $session = CRM_Core_Session::singleton();
154 CRM_Core_Session::setStatus(ts('A copy of the price set has been created'), ts('Saved'), 'success');
155 $this->copy();
156 }
157 else {
158
159 // if action is delete do the needful.
160 if ($action & (CRM_Core_Action::DELETE)) {
161 $usedBy = CRM_Price_BAO_Set::getUsedBy($sid);
162
163 if (empty($usedBy)) {
164 // prompt to delete
165 $session = CRM_Core_Session::singleton();
166 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price', 'action=browse'));
167 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_DeleteSet', 'Delete Price Set', NULL);
168 // $id = CRM_Utils_Request::retrieve('sid', 'Positive', $this, false, 0);
169 $controller->set('sid', $sid);
170 $controller->setEmbedded(TRUE);
171 $controller->process();
172 $controller->run();
173 }
174 else {
175 // add breadcrumb
176 $url = CRM_Utils_System::url('civicrm/admin/price', 'reset=1');
177 CRM_Utils_System::appendBreadCrumb(ts('Price Sets'), $url);
178 $this->assign('usedPriceSetTitle', CRM_Price_BAO_Set::getTitle($sid));
179 $this->assign('usedBy', $usedBy);
180
181 $comps = array(
182 'Event' => 'civicrm_event',
183 'Contribution' => 'civicrm_contribution_page',
184 );
185 $priceSetContexts = array();
186 foreach ($comps as $name => $table) {
187 if (array_key_exists($table, $usedBy)) {
188 $priceSetContexts[] = $name;
189 }
190 }
191 $this->assign('contexts', $priceSetContexts);
192 }
193 }
194
195 // finally browse the price sets
196 $this->browse();
197 }
198 // parent run
199 return parent::run();
200 }
201
202 /**
203 * edit price set
204 *
205 * @param int $id price set id
206 * @param string $action the action to be invoked
207 *
208 * @return void
209 * @access public
210 */
211 function edit($sid, $action) {
212 // create a simple controller for editing price sets
213 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Set', ts('Price Set'), $action);
214
215 // set the userContext stack
216 $session = CRM_Core_Session::singleton();
217 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price', 'action=browse'));
218 $controller->set('sid', $sid);
219 $controller->setEmbedded(TRUE);
220 $controller->process();
221 $controller->run();
222 }
223
224 /**
225 * Preview price set
226 *
227 * @param int $id price set id
228 *
229 * @return void
230 * @access public
231 */
232 function preview($sid) {
233 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Preview', ts('Preview Price Set'), NULL);
234 $session = CRM_Core_Session::singleton();
235 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
236 if ($context == 'field') {
237 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field', "action=browse&sid={$sid}"));
238 }
239 else {
240 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price', 'action=browse'));
241 }
242 $controller->set('groupId', $sid);
243 $controller->setEmbedded(TRUE);
244 $controller->process();
245 $controller->run();
246 }
247
248 /**
249 * Browse all price sets
250 *
251 * @param string $action the action to be invoked
252 *
253 * @return void
254 * @access public
255 */
256 function browse($action = NULL) {
257 // get all price sets
258 $priceSet = array();
259 $comps = array('CiviEvent' => ts('Event'),
260 'CiviContribute' => ts('Contribution'),
261 'CiviMember' => ts('Membership'),
262 );
263
264 $dao = new CRM_Price_DAO_Set();
265 if (CRM_Price_BAO_Set::eventPriceSetDomainID()) {
266 $dao->domain_id = CRM_Core_Config::domainID();
267 }
268 $dao->is_quick_config = 0;
269 $dao->find();
270 while ($dao->fetch()) {
271 $priceSet[$dao->id] = array();
272 CRM_Core_DAO::storeValues($dao, $priceSet[$dao->id]);
273
274 $compIds = explode(CRM_Core_DAO::VALUE_SEPARATOR,
275 CRM_Utils_Array::value('extends', $priceSet[$dao->id])
276 );
277 $extends = array();
278 foreach ($compIds as $compId) $extends[] = $comps[CRM_Core_Component::getComponentName($compId)];
279 $priceSet[$dao->id]['extends'] = implode(', ', $extends);
280
281 // form all action links
282 $action = array_sum(array_keys($this->actionLinks()));
283
284 // update enable/disable links depending on price_set properties.
285 if ($dao->is_reserved) {
286 $action -= CRM_Core_Action::UPDATE + CRM_Core_Action::DISABLE + CRM_Core_Action::ENABLE + CRM_Core_Action::DELETE + CRM_Core_Action::COPY;
287 }
288 else {
289 if ($dao->is_active) {
290 $action -= CRM_Core_Action::ENABLE;
291 }
292 else {
293 $action -= CRM_Core_Action::DISABLE;
294 }
295 }
296 $actionLinks = self::actionLinks();
297 //CRM-10117
298 if ($dao->is_reserved) {
299 $actionLinks[CRM_Core_Action::BROWSE]['name'] = 'View Price Fields';
300 }
301 $priceSet[$dao->id]['action'] = CRM_Core_Action::formLink($actionLinks, $action,
302 array('sid' => $dao->id)
303 );
304 }
305 $this->assign('rows', $priceSet);
306 }
307
308 /**
309 * This function is to make a copy of a price set, including
310 * all the fields in the page
311 *
312 * @return void
313 * @access public
314 */
315 function copy() {
316 $id = CRM_Utils_Request::retrieve('sid', 'Positive',
317 $this, TRUE, 0, 'GET'
318 );
319
320 CRM_Price_BAO_Set::copy($id);
321
322 CRM_Utils_System::redirect(CRM_Utils_System::url(CRM_Utils_System::currentPath(), 'reset=1'));
323 }
324}
325