copyright and version fixes
[civicrm-core.git] / CRM / Price / Page / Set.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
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 *
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'),
4d17a233 87 'ref' => 'crm-enable-disable',
6a488035
TO
88 'title' => ts('Disable Price Set'),
89 ),
90 CRM_Core_Action::ENABLE => array(
91 'name' => ts('Enable'),
4d17a233 92 'ref' => 'crm-enable-disable',
6a488035
TO
93 'title' => ts('Enable Price Set'),
94 ),
95 CRM_Core_Action::DELETE => array(
96 'name' => ts('Delete'),
97 'url' => 'civicrm/admin/price',
98 'qs' => 'action=delete&reset=1&sid=%%sid%%',
99 'title' => ts('Delete Price Set'),
100 'extra' => 'onclick = "return confirm(\'' . $deleteExtra . '\');"',
101 ),
102 CRM_Core_Action::COPY => array(
103 'name' => ts('Copy Price Set'),
104 'url' => CRM_Utils_System::currentPath(),
105 'qs' => 'action=copy&sid=%%sid%%',
106 'title' => ts('Make a Copy of Price Set'),
107 'extra' => 'onclick = "return confirm(\'' . $copyExtra . '\');"',
108 ),
109 );
110 }
111 return self::$_actionLinks;
112 }
113
114 /**
115 * Run the page.
116 *
117 * This method is called after the page is created. It checks for the
118 * type of action and executes that action.
119 * Finally it calls the parent's run method.
120 *
121 * @param null
122 *
123 * @return void
124 * @access public
125 *
126 */
127 function run() {
128 // get the requested action
129 $action = CRM_Utils_Request::retrieve('action', 'String',
130 // default to 'browse'
131 $this, FALSE, 'browse'
132 );
133
134 // assign vars to templates
135 $this->assign('action', $action);
136 $sid = CRM_Utils_Request::retrieve('sid', 'Positive',
137 $this, FALSE, 0
138 );
139
140 if ($sid) {
9da8dc8c 141 CRM_Price_BAO_PriceSet::checkPermission($sid);
6a488035
TO
142 }
143 // what action to take ?
144 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) {
145 $this->edit($sid, $action);
146 }
147 elseif ($action & CRM_Core_Action::PREVIEW) {
148 $this->preview($sid);
149 }
150 elseif ($action & CRM_Core_Action::COPY) {
151 $session = CRM_Core_Session::singleton();
152 CRM_Core_Session::setStatus(ts('A copy of the price set has been created'), ts('Saved'), 'success');
153 $this->copy();
154 }
155 else {
156
157 // if action is delete do the needful.
158 if ($action & (CRM_Core_Action::DELETE)) {
9da8dc8c 159 $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($sid);
6a488035
TO
160
161 if (empty($usedBy)) {
162 // prompt to delete
163 $session = CRM_Core_Session::singleton();
164 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price', 'action=browse'));
165 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_DeleteSet', 'Delete Price Set', NULL);
166 // $id = CRM_Utils_Request::retrieve('sid', 'Positive', $this, false, 0);
167 $controller->set('sid', $sid);
168 $controller->setEmbedded(TRUE);
169 $controller->process();
170 $controller->run();
171 }
172 else {
173 // add breadcrumb
174 $url = CRM_Utils_System::url('civicrm/admin/price', 'reset=1');
175 CRM_Utils_System::appendBreadCrumb(ts('Price Sets'), $url);
9da8dc8c 176 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceSet::getTitle($sid));
6a488035
TO
177 $this->assign('usedBy', $usedBy);
178
179 $comps = array(
180 'Event' => 'civicrm_event',
181 'Contribution' => 'civicrm_contribution_page',
c34e4bb4 182 'EventTemplate' => 'civicrm_event_template'
6a488035
TO
183 );
184 $priceSetContexts = array();
185 foreach ($comps as $name => $table) {
186 if (array_key_exists($table, $usedBy)) {
187 $priceSetContexts[] = $name;
188 }
189 }
190 $this->assign('contexts', $priceSetContexts);
191 }
192 }
193
194 // finally browse the price sets
195 $this->browse();
196 }
197 // parent run
198 return parent::run();
199 }
200
201 /**
202 * edit price set
203 *
204 * @param int $id price set id
205 * @param string $action the action to be invoked
206 *
207 * @return void
208 * @access public
209 */
210 function edit($sid, $action) {
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 *
226 * @param int $id price set id
227 *
228 * @return void
229 * @access public
230 */
231 function preview($sid) {
232 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Preview', ts('Preview Price Set'), NULL);
233 $session = CRM_Core_Session::singleton();
234 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
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 *
250 * @param string $action the action to be invoked
251 *
252 * @return void
253 * @access public
254 */
255 function browse($action = NULL) {
256 // get all price sets
257 $priceSet = array();
258 $comps = array('CiviEvent' => ts('Event'),
259 'CiviContribute' => ts('Contribution'),
260 'CiviMember' => ts('Membership'),
261 );
262
9da8dc8c 263 $dao = new CRM_Price_DAO_PriceSet();
264 if (CRM_Price_BAO_PriceSet::eventPriceSetDomainID()) {
6a488035
TO
265 $dao->domain_id = CRM_Core_Config::domainID();
266 }
267 $dao->is_quick_config = 0;
268 $dao->find();
269 while ($dao->fetch()) {
270 $priceSet[$dao->id] = array();
271 CRM_Core_DAO::storeValues($dao, $priceSet[$dao->id]);
272
273 $compIds = explode(CRM_Core_DAO::VALUE_SEPARATOR,
274 CRM_Utils_Array::value('extends', $priceSet[$dao->id])
275 );
276 $extends = array();
5e71e542 277 //CRM-10225
278 foreach ($compIds as $compId) {
279 if (!empty($comps[CRM_Core_Component::getComponentName($compId)])) {
280 $extends[] = $comps[CRM_Core_Component::getComponentName($compId)];
281 }
282 }
283 $priceSet[$dao->id]['extends'] = implode(', ', $extends);
6a488035
TO
284
285 // form all action links
286 $action = array_sum(array_keys($this->actionLinks()));
287
288 // update enable/disable links depending on price_set properties.
289 if ($dao->is_reserved) {
290 $action -= CRM_Core_Action::UPDATE + CRM_Core_Action::DISABLE + CRM_Core_Action::ENABLE + CRM_Core_Action::DELETE + CRM_Core_Action::COPY;
291 }
292 else {
293 if ($dao->is_active) {
294 $action -= CRM_Core_Action::ENABLE;
295 }
296 else {
297 $action -= CRM_Core_Action::DISABLE;
298 }
299 }
300 $actionLinks = self::actionLinks();
301 //CRM-10117
302 if ($dao->is_reserved) {
303 $actionLinks[CRM_Core_Action::BROWSE]['name'] = 'View Price Fields';
304 }
305 $priceSet[$dao->id]['action'] = CRM_Core_Action::formLink($actionLinks, $action,
87dab4a4
AH
306 array('sid' => $dao->id),
307 ts('more'),
308 FALSE,
309 'priceSet.row.actions',
310 'PriceSet',
311 $dao->id
6a488035
TO
312 );
313 }
314 $this->assign('rows', $priceSet);
315 }
316
317 /**
318 * This function is to make a copy of a price set, including
319 * all the fields in the page
320 *
321 * @return void
322 * @access public
323 */
324 function copy() {
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}
334