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