Replace all instances of check.gif appearing in listings
[civicrm-core.git] / CRM / Price / Page / Set.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 * $Id$
17 *
18 */
19
20/**
21 * Create a page for displaying Price Sets.
22 *
23 * Heart of this class is the run method which checks
24 * for action type and then displays the appropriate
25 * page.
26 *
27 */
28class CRM_Price_Page_Set extends CRM_Core_Page {
29
30 /**
fe482240 31 * The action links that we need to display for the browse screen.
6a488035
TO
32 *
33 * @var array
34 */
35 private static $_actionLinks;
36
37 /**
38 * Get the action links for this page.
39 *
a6c01b45
CW
40 * @return array
41 * array of action links that we need to display for the browse screen
95ea96be 42 */
acb1052e 43 public function &actionLinks() {
6a488035
TO
44 // check if variable _actionsLinks is populated
45 if (!isset(self::$_actionLinks)) {
46 // helper variable for nicer formatting
47 $deleteExtra = ts('Are you sure you want to delete this price set?');
48 $copyExtra = ts('Are you sure you want to make a copy of this price set?');
be2fb01f
CW
49 self::$_actionLinks = [
50 CRM_Core_Action::BROWSE => [
6a488035
TO
51 'name' => ts('View and Edit Price Fields'),
52 'url' => 'civicrm/admin/price/field',
53 'qs' => 'reset=1&action=browse&sid=%%sid%%',
54 'title' => ts('View and Edit Price Fields'),
be2fb01f
CW
55 ],
56 CRM_Core_Action::PREVIEW => [
6a488035
TO
57 'name' => ts('Preview'),
58 'url' => 'civicrm/admin/price',
59 'qs' => 'action=preview&reset=1&sid=%%sid%%',
60 'title' => ts('Preview Price Set'),
be2fb01f
CW
61 ],
62 CRM_Core_Action::UPDATE => [
6a488035
TO
63 'name' => ts('Settings'),
64 'url' => 'civicrm/admin/price',
65 'qs' => 'action=update&reset=1&sid=%%sid%%',
66 'title' => ts('Edit Price Set'),
be2fb01f
CW
67 ],
68 CRM_Core_Action::DISABLE => [
6a488035 69 'name' => ts('Disable'),
4d17a233 70 'ref' => 'crm-enable-disable',
6a488035 71 'title' => ts('Disable Price Set'),
be2fb01f
CW
72 ],
73 CRM_Core_Action::ENABLE => [
6a488035 74 'name' => ts('Enable'),
4d17a233 75 'ref' => 'crm-enable-disable',
6a488035 76 'title' => ts('Enable Price Set'),
be2fb01f
CW
77 ],
78 CRM_Core_Action::DELETE => [
6a488035
TO
79 'name' => ts('Delete'),
80 'url' => 'civicrm/admin/price',
81 'qs' => 'action=delete&reset=1&sid=%%sid%%',
82 'title' => ts('Delete Price Set'),
83 'extra' => 'onclick = "return confirm(\'' . $deleteExtra . '\');"',
be2fb01f
CW
84 ],
85 CRM_Core_Action::COPY => [
6a488035
TO
86 'name' => ts('Copy Price Set'),
87 'url' => CRM_Utils_System::currentPath(),
88 'qs' => 'action=copy&sid=%%sid%%',
89 'title' => ts('Make a Copy of Price Set'),
90 'extra' => 'onclick = "return confirm(\'' . $copyExtra . '\');"',
be2fb01f
CW
91 ],
92 ];
6a488035
TO
93 }
94 return self::$_actionLinks;
95 }
96
97 /**
98 * Run the page.
99 *
100 * This method is called after the page is created. It checks for the
101 * type of action and executes that action.
102 * Finally it calls the parent's run method.
103 *
6a488035 104 * @return void
6a488035 105 */
00be9182 106 public function run() {
6a488035
TO
107 // get the requested action
108 $action = CRM_Utils_Request::retrieve('action', 'String',
109 // default to 'browse'
110 $this, FALSE, 'browse'
111 );
112
113 // assign vars to templates
114 $this->assign('action', $action);
115 $sid = CRM_Utils_Request::retrieve('sid', 'Positive',
116 $this, FALSE, 0
117 );
118
119 if ($sid) {
9da8dc8c 120 CRM_Price_BAO_PriceSet::checkPermission($sid);
6a488035
TO
121 }
122 // what action to take ?
123 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) {
124 $this->edit($sid, $action);
125 }
126 elseif ($action & CRM_Core_Action::PREVIEW) {
127 $this->preview($sid);
128 }
129 elseif ($action & CRM_Core_Action::COPY) {
6a488035
TO
130 CRM_Core_Session::setStatus(ts('A copy of the price set has been created'), ts('Saved'), 'success');
131 $this->copy();
132 }
133 else {
134
135 // if action is delete do the needful.
136 if ($action & (CRM_Core_Action::DELETE)) {
9da8dc8c 137 $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($sid);
6a488035
TO
138
139 if (empty($usedBy)) {
140 // prompt to delete
9d8f190f 141 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin/price', 'action=browse'));
6a488035 142 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_DeleteSet', 'Delete Price Set', NULL);
6a488035
TO
143 $controller->set('sid', $sid);
144 $controller->setEmbedded(TRUE);
145 $controller->process();
146 $controller->run();
147 }
148 else {
149 // add breadcrumb
150 $url = CRM_Utils_System::url('civicrm/admin/price', 'reset=1');
151 CRM_Utils_System::appendBreadCrumb(ts('Price Sets'), $url);
9da8dc8c 152 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceSet::getTitle($sid));
6a488035
TO
153 $this->assign('usedBy', $usedBy);
154
be2fb01f 155 $comps = [
6a488035
TO
156 'Event' => 'civicrm_event',
157 'Contribution' => 'civicrm_contribution_page',
21dfd5f5 158 'EventTemplate' => 'civicrm_event_template',
be2fb01f
CW
159 ];
160 $priceSetContexts = [];
6a488035
TO
161 foreach ($comps as $name => $table) {
162 if (array_key_exists($table, $usedBy)) {
163 $priceSetContexts[] = $name;
164 }
165 }
166 $this->assign('contexts', $priceSetContexts);
167 }
168 }
169
170 // finally browse the price sets
171 $this->browse();
172 }
173 // parent run
174 return parent::run();
175 }
176
177 /**
fe482240 178 * Edit price set.
6a488035 179 *
414c1420
TO
180 * @param int $sid
181 * Price set id.
182 * @param string $action
183 * The action to be invoked.
6a488035
TO
184 *
185 * @return void
6a488035 186 */
00be9182 187 public function edit($sid, $action) {
6a488035
TO
188 // create a simple controller for editing price sets
189 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Set', ts('Price Set'), $action);
190
191 // set the userContext stack
192 $session = CRM_Core_Session::singleton();
193 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price', 'action=browse'));
194 $controller->set('sid', $sid);
195 $controller->setEmbedded(TRUE);
196 $controller->process();
197 $controller->run();
198 }
199
200 /**
fe482240 201 * Preview price set.
6a488035 202 *
414c1420
TO
203 * @param int $sid
204 * Price set id.
6a488035
TO
205 *
206 * @return void
6a488035 207 */
00be9182 208 public function preview($sid) {
6a488035 209 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Preview', ts('Preview Price Set'), NULL);
353ffa53 210 $session = CRM_Core_Session::singleton();
edc80cda 211 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
6a488035
TO
212 if ($context == 'field') {
213 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field', "action=browse&sid={$sid}"));
214 }
215 else {
216 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price', 'action=browse'));
217 }
218 $controller->set('groupId', $sid);
219 $controller->setEmbedded(TRUE);
220 $controller->process();
221 $controller->run();
222 }
223
224 /**
fe482240 225 * Browse all price sets.
6a488035 226 *
414c1420
TO
227 * @param string $action
228 * The action to be invoked.
6a488035
TO
229 *
230 * @return void
6a488035 231 */
00be9182 232 public function browse($action = NULL) {
6a488035 233 // get all price sets
be2fb01f
CW
234 $priceSet = [];
235 $comps = [
353ffa53 236 'CiviEvent' => ts('Event'),
6a488035
TO
237 'CiviContribute' => ts('Contribution'),
238 'CiviMember' => ts('Membership'),
be2fb01f 239 ];
6a488035 240
9da8dc8c 241 $dao = new CRM_Price_DAO_PriceSet();
242 if (CRM_Price_BAO_PriceSet::eventPriceSetDomainID()) {
6a488035
TO
243 $dao->domain_id = CRM_Core_Config::domainID();
244 }
245 $dao->is_quick_config = 0;
246 $dao->find();
247 while ($dao->fetch()) {
be2fb01f 248 $priceSet[$dao->id] = [];
6a488035
TO
249 CRM_Core_DAO::storeValues($dao, $priceSet[$dao->id]);
250
251 $compIds = explode(CRM_Core_DAO::VALUE_SEPARATOR,
252 CRM_Utils_Array::value('extends', $priceSet[$dao->id])
253 );
be2fb01f 254 $extends = [];
5e71e542 255 //CRM-10225
256 foreach ($compIds as $compId) {
257 if (!empty($comps[CRM_Core_Component::getComponentName($compId)])) {
258 $extends[] = $comps[CRM_Core_Component::getComponentName($compId)];
259 }
260 }
ba1dcfda 261 $priceSet[$dao->id]['extends'] = implode(', ', $extends);
6a488035
TO
262
263 // form all action links
264 $action = array_sum(array_keys($this->actionLinks()));
265
266 // update enable/disable links depending on price_set properties.
267 if ($dao->is_reserved) {
268 $action -= CRM_Core_Action::UPDATE + CRM_Core_Action::DISABLE + CRM_Core_Action::ENABLE + CRM_Core_Action::DELETE + CRM_Core_Action::COPY;
269 }
270 else {
271 if ($dao->is_active) {
272 $action -= CRM_Core_Action::ENABLE;
273 }
274 else {
275 $action -= CRM_Core_Action::DISABLE;
276 }
277 }
278 $actionLinks = self::actionLinks();
279 //CRM-10117
280 if ($dao->is_reserved) {
e60e0c01 281 $actionLinks[CRM_Core_Action::BROWSE]['name'] = ts('View Price Fields');
6a488035
TO
282 }
283 $priceSet[$dao->id]['action'] = CRM_Core_Action::formLink($actionLinks, $action,
be2fb01f 284 ['sid' => $dao->id],
87dab4a4
AH
285 ts('more'),
286 FALSE,
287 'priceSet.row.actions',
288 'PriceSet',
289 $dao->id
6a488035
TO
290 );
291 }
292 $this->assign('rows', $priceSet);
293 }
294
295 /**
dc195289 296 * make a copy of a price set, including
6a488035
TO
297 * all the fields in the page
298 *
299 * @return void
6a488035 300 */
00be9182 301 public function copy() {
6a488035
TO
302 $id = CRM_Utils_Request::retrieve('sid', 'Positive',
303 $this, TRUE, 0, 'GET'
304 );
305
9da8dc8c 306 CRM_Price_BAO_PriceSet::copy($id);
6a488035
TO
307
308 CRM_Utils_System::redirect(CRM_Utils_System::url(CRM_Utils_System::currentPath(), 'reset=1'));
309 }
96025800 310
6a488035 311}