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