Merge pull request #14455 from civicrm/5.14
[civicrm-core.git] / CRM / Price / Page / Set.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
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?');
be2fb01f
CW
65 self::$_actionLinks = [
66 CRM_Core_Action::BROWSE => [
6a488035
TO
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'),
be2fb01f
CW
71 ],
72 CRM_Core_Action::PREVIEW => [
6a488035
TO
73 'name' => ts('Preview'),
74 'url' => 'civicrm/admin/price',
75 'qs' => 'action=preview&reset=1&sid=%%sid%%',
76 'title' => ts('Preview Price Set'),
be2fb01f
CW
77 ],
78 CRM_Core_Action::UPDATE => [
6a488035
TO
79 'name' => ts('Settings'),
80 'url' => 'civicrm/admin/price',
81 'qs' => 'action=update&reset=1&sid=%%sid%%',
82 'title' => ts('Edit Price Set'),
be2fb01f
CW
83 ],
84 CRM_Core_Action::DISABLE => [
6a488035 85 'name' => ts('Disable'),
4d17a233 86 'ref' => 'crm-enable-disable',
6a488035 87 'title' => ts('Disable Price Set'),
be2fb01f
CW
88 ],
89 CRM_Core_Action::ENABLE => [
6a488035 90 'name' => ts('Enable'),
4d17a233 91 'ref' => 'crm-enable-disable',
6a488035 92 'title' => ts('Enable Price Set'),
be2fb01f
CW
93 ],
94 CRM_Core_Action::DELETE => [
6a488035
TO
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 . '\');"',
be2fb01f
CW
100 ],
101 CRM_Core_Action::COPY => [
6a488035
TO
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 . '\');"',
be2fb01f
CW
107 ],
108 ];
6a488035
TO
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) {
6a488035
TO
146 CRM_Core_Session::setStatus(ts('A copy of the price set has been created'), ts('Saved'), 'success');
147 $this->copy();
148 }
149 else {
150
151 // if action is delete do the needful.
152 if ($action & (CRM_Core_Action::DELETE)) {
9da8dc8c 153 $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($sid);
6a488035
TO
154
155 if (empty($usedBy)) {
156 // prompt to delete
9d8f190f 157 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin/price', 'action=browse'));
6a488035 158 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_DeleteSet', 'Delete Price Set', NULL);
6a488035
TO
159 $controller->set('sid', $sid);
160 $controller->setEmbedded(TRUE);
161 $controller->process();
162 $controller->run();
163 }
164 else {
165 // add breadcrumb
166 $url = CRM_Utils_System::url('civicrm/admin/price', 'reset=1');
167 CRM_Utils_System::appendBreadCrumb(ts('Price Sets'), $url);
9da8dc8c 168 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceSet::getTitle($sid));
6a488035
TO
169 $this->assign('usedBy', $usedBy);
170
be2fb01f 171 $comps = [
6a488035
TO
172 'Event' => 'civicrm_event',
173 'Contribution' => 'civicrm_contribution_page',
21dfd5f5 174 'EventTemplate' => 'civicrm_event_template',
be2fb01f
CW
175 ];
176 $priceSetContexts = [];
6a488035
TO
177 foreach ($comps as $name => $table) {
178 if (array_key_exists($table, $usedBy)) {
179 $priceSetContexts[] = $name;
180 }
181 }
182 $this->assign('contexts', $priceSetContexts);
183 }
184 }
185
186 // finally browse the price sets
187 $this->browse();
188 }
189 // parent run
190 return parent::run();
191 }
192
193 /**
fe482240 194 * Edit price set.
6a488035 195 *
414c1420
TO
196 * @param int $sid
197 * Price set id.
198 * @param string $action
199 * The action to be invoked.
6a488035
TO
200 *
201 * @return void
6a488035 202 */
00be9182 203 public function edit($sid, $action) {
6a488035
TO
204 // create a simple controller for editing price sets
205 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Set', ts('Price Set'), $action);
206
207 // set the userContext stack
208 $session = CRM_Core_Session::singleton();
209 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price', 'action=browse'));
210 $controller->set('sid', $sid);
211 $controller->setEmbedded(TRUE);
212 $controller->process();
213 $controller->run();
214 }
215
216 /**
fe482240 217 * Preview price set.
6a488035 218 *
414c1420
TO
219 * @param int $sid
220 * Price set id.
6a488035
TO
221 *
222 * @return void
6a488035 223 */
00be9182 224 public function preview($sid) {
6a488035 225 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Preview', ts('Preview Price Set'), NULL);
353ffa53 226 $session = CRM_Core_Session::singleton();
edc80cda 227 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
6a488035
TO
228 if ($context == 'field') {
229 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field', "action=browse&sid={$sid}"));
230 }
231 else {
232 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price', 'action=browse'));
233 }
234 $controller->set('groupId', $sid);
235 $controller->setEmbedded(TRUE);
236 $controller->process();
237 $controller->run();
238 }
239
240 /**
fe482240 241 * Browse all price sets.
6a488035 242 *
414c1420
TO
243 * @param string $action
244 * The action to be invoked.
6a488035
TO
245 *
246 * @return void
6a488035 247 */
00be9182 248 public function browse($action = NULL) {
6a488035 249 // get all price sets
be2fb01f
CW
250 $priceSet = [];
251 $comps = [
353ffa53 252 'CiviEvent' => ts('Event'),
6a488035
TO
253 'CiviContribute' => ts('Contribution'),
254 'CiviMember' => ts('Membership'),
be2fb01f 255 ];
6a488035 256
9da8dc8c 257 $dao = new CRM_Price_DAO_PriceSet();
258 if (CRM_Price_BAO_PriceSet::eventPriceSetDomainID()) {
6a488035
TO
259 $dao->domain_id = CRM_Core_Config::domainID();
260 }
261 $dao->is_quick_config = 0;
262 $dao->find();
263 while ($dao->fetch()) {
be2fb01f 264 $priceSet[$dao->id] = [];
6a488035
TO
265 CRM_Core_DAO::storeValues($dao, $priceSet[$dao->id]);
266
267 $compIds = explode(CRM_Core_DAO::VALUE_SEPARATOR,
268 CRM_Utils_Array::value('extends', $priceSet[$dao->id])
269 );
be2fb01f 270 $extends = [];
5e71e542 271 //CRM-10225
272 foreach ($compIds as $compId) {
273 if (!empty($comps[CRM_Core_Component::getComponentName($compId)])) {
274 $extends[] = $comps[CRM_Core_Component::getComponentName($compId)];
275 }
276 }
ba1dcfda 277 $priceSet[$dao->id]['extends'] = implode(', ', $extends);
6a488035
TO
278
279 // form all action links
280 $action = array_sum(array_keys($this->actionLinks()));
281
282 // update enable/disable links depending on price_set properties.
283 if ($dao->is_reserved) {
284 $action -= CRM_Core_Action::UPDATE + CRM_Core_Action::DISABLE + CRM_Core_Action::ENABLE + CRM_Core_Action::DELETE + CRM_Core_Action::COPY;
285 }
286 else {
287 if ($dao->is_active) {
288 $action -= CRM_Core_Action::ENABLE;
289 }
290 else {
291 $action -= CRM_Core_Action::DISABLE;
292 }
293 }
294 $actionLinks = self::actionLinks();
295 //CRM-10117
296 if ($dao->is_reserved) {
e60e0c01 297 $actionLinks[CRM_Core_Action::BROWSE]['name'] = ts('View Price Fields');
6a488035
TO
298 }
299 $priceSet[$dao->id]['action'] = CRM_Core_Action::formLink($actionLinks, $action,
be2fb01f 300 ['sid' => $dao->id],
87dab4a4
AH
301 ts('more'),
302 FALSE,
303 'priceSet.row.actions',
304 'PriceSet',
305 $dao->id
6a488035
TO
306 );
307 }
308 $this->assign('rows', $priceSet);
309 }
310
311 /**
dc195289 312 * make a copy of a price set, including
6a488035
TO
313 * all the fields in the page
314 *
315 * @return void
6a488035 316 */
00be9182 317 public function copy() {
6a488035
TO
318 $id = CRM_Utils_Request::retrieve('sid', 'Positive',
319 $this, TRUE, 0, 'GET'
320 );
321
9da8dc8c 322 CRM_Price_BAO_PriceSet::copy($id);
6a488035
TO
323
324 CRM_Utils_System::redirect(CRM_Utils_System::url(CRM_Utils_System::currentPath(), 'reset=1'));
325 }
96025800 326
6a488035 327}