Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-07-23-17-12-38
[civicrm-core.git] / CRM / Price / Page / Set.php
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 */
44 class 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_PriceSet' . '\',\'' . '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_PriceSet' . '\',\'' . '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_PriceSet::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_PriceSet::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_PriceSet::getTitle($sid));
179 $this->assign('usedBy', $usedBy);
180
181 $comps = array(
182 'Event' => 'civicrm_event',
183 'Contribution' => 'civicrm_contribution_page',
184 'EventTemplate' => 'civicrm_event_template'
185 );
186 $priceSetContexts = array();
187 foreach ($comps as $name => $table) {
188 if (array_key_exists($table, $usedBy)) {
189 $priceSetContexts[] = $name;
190 }
191 }
192 $this->assign('contexts', $priceSetContexts);
193 }
194 }
195
196 // finally browse the price sets
197 $this->browse();
198 }
199 // parent run
200 return parent::run();
201 }
202
203 /**
204 * edit price set
205 *
206 * @param int $id price set id
207 * @param string $action the action to be invoked
208 *
209 * @return void
210 * @access public
211 */
212 function edit($sid, $action) {
213 // create a simple controller for editing price sets
214 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Set', ts('Price Set'), $action);
215
216 // set the userContext stack
217 $session = CRM_Core_Session::singleton();
218 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price', 'action=browse'));
219 $controller->set('sid', $sid);
220 $controller->setEmbedded(TRUE);
221 $controller->process();
222 $controller->run();
223 }
224
225 /**
226 * Preview price set
227 *
228 * @param int $id price set id
229 *
230 * @return void
231 * @access public
232 */
233 function preview($sid) {
234 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Preview', ts('Preview Price Set'), NULL);
235 $session = CRM_Core_Session::singleton();
236 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
237 if ($context == 'field') {
238 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field', "action=browse&sid={$sid}"));
239 }
240 else {
241 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price', 'action=browse'));
242 }
243 $controller->set('groupId', $sid);
244 $controller->setEmbedded(TRUE);
245 $controller->process();
246 $controller->run();
247 }
248
249 /**
250 * Browse all price sets
251 *
252 * @param string $action the action to be invoked
253 *
254 * @return void
255 * @access public
256 */
257 function browse($action = NULL) {
258 // get all price sets
259 $priceSet = array();
260 $comps = array('CiviEvent' => ts('Event'),
261 'CiviContribute' => ts('Contribution'),
262 'CiviMember' => ts('Membership'),
263 );
264
265 $dao = new CRM_Price_DAO_PriceSet();
266 if (CRM_Price_BAO_PriceSet::eventPriceSetDomainID()) {
267 $dao->domain_id = CRM_Core_Config::domainID();
268 }
269 $dao->is_quick_config = 0;
270 $dao->find();
271 while ($dao->fetch()) {
272 $priceSet[$dao->id] = array();
273 CRM_Core_DAO::storeValues($dao, $priceSet[$dao->id]);
274
275 $compIds = explode(CRM_Core_DAO::VALUE_SEPARATOR,
276 CRM_Utils_Array::value('extends', $priceSet[$dao->id])
277 );
278 $extends = array();
279 foreach ($compIds as $compId) $extends[] = $comps[CRM_Core_Component::getComponentName($compId)];
280 $priceSet[$dao->id]['extends'] = implode(', ', $extends);
281
282 // form all action links
283 $action = array_sum(array_keys($this->actionLinks()));
284
285 // update enable/disable links depending on price_set properties.
286 if ($dao->is_reserved) {
287 $action -= CRM_Core_Action::UPDATE + CRM_Core_Action::DISABLE + CRM_Core_Action::ENABLE + CRM_Core_Action::DELETE + CRM_Core_Action::COPY;
288 }
289 else {
290 if ($dao->is_active) {
291 $action -= CRM_Core_Action::ENABLE;
292 }
293 else {
294 $action -= CRM_Core_Action::DISABLE;
295 }
296 }
297 $actionLinks = self::actionLinks();
298 //CRM-10117
299 if ($dao->is_reserved) {
300 $actionLinks[CRM_Core_Action::BROWSE]['name'] = 'View Price Fields';
301 }
302 $priceSet[$dao->id]['action'] = CRM_Core_Action::formLink($actionLinks, $action,
303 array('sid' => $dao->id)
304 );
305 }
306 $this->assign('rows', $priceSet);
307 }
308
309 /**
310 * This function is to make a copy of a price set, including
311 * all the fields in the page
312 *
313 * @return void
314 * @access public
315 */
316 function copy() {
317 $id = CRM_Utils_Request::retrieve('sid', 'Positive',
318 $this, TRUE, 0, 'GET'
319 );
320
321 CRM_Price_BAO_PriceSet::copy($id);
322
323 CRM_Utils_System::redirect(CRM_Utils_System::url(CRM_Utils_System::currentPath(), 'reset=1'));
324 }
325 }
326