Merge pull request #16541 from bhahumanists/subtype-issue-991
[civicrm-core.git] / CRM / Price / Page / Set.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
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 */
28 class CRM_Price_Page_Set extends CRM_Core_Page {
29
30 /**
31 * The action links that we need to display for the browse screen.
32 *
33 * @var array
34 */
35 private static $_actionLinks;
36
37 /**
38 * Get the action links for this page.
39 *
40 * @return array
41 * array of action links that we need to display for the browse screen
42 */
43 public function &actionLinks() {
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?');
49 self::$_actionLinks = [
50 CRM_Core_Action::BROWSE => [
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'),
55 ],
56 CRM_Core_Action::PREVIEW => [
57 'name' => ts('Preview'),
58 'url' => 'civicrm/admin/price',
59 'qs' => 'action=preview&reset=1&sid=%%sid%%',
60 'title' => ts('Preview Price Set'),
61 ],
62 CRM_Core_Action::UPDATE => [
63 'name' => ts('Settings'),
64 'url' => 'civicrm/admin/price',
65 'qs' => 'action=update&reset=1&sid=%%sid%%',
66 'title' => ts('Edit Price Set'),
67 ],
68 CRM_Core_Action::DISABLE => [
69 'name' => ts('Disable'),
70 'ref' => 'crm-enable-disable',
71 'title' => ts('Disable Price Set'),
72 ],
73 CRM_Core_Action::ENABLE => [
74 'name' => ts('Enable'),
75 'ref' => 'crm-enable-disable',
76 'title' => ts('Enable Price Set'),
77 ],
78 CRM_Core_Action::DELETE => [
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 . '\');"',
84 ],
85 CRM_Core_Action::COPY => [
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 . '\');"',
91 ],
92 ];
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 *
104 * @return void
105 */
106 public function run() {
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) {
120 CRM_Price_BAO_PriceSet::checkPermission($sid);
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) {
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)) {
137 $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($sid);
138
139 if (empty($usedBy)) {
140 // prompt to delete
141 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin/price', 'action=browse'));
142 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_DeleteSet', 'Delete Price Set', NULL);
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);
152 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceSet::getTitle($sid));
153 $this->assign('usedBy', $usedBy);
154
155 $comps = [
156 'Event' => 'civicrm_event',
157 'Contribution' => 'civicrm_contribution_page',
158 'EventTemplate' => 'civicrm_event_template',
159 ];
160 $priceSetContexts = [];
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 /**
178 * Edit price set.
179 *
180 * @param int $sid
181 * Price set id.
182 * @param string $action
183 * The action to be invoked.
184 *
185 * @return void
186 */
187 public function edit($sid, $action) {
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 /**
201 * Preview price set.
202 *
203 * @param int $sid
204 * Price set id.
205 *
206 * @return void
207 */
208 public function preview($sid) {
209 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Preview', ts('Preview Price Set'), NULL);
210 $session = CRM_Core_Session::singleton();
211 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
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 /**
225 * Browse all price sets.
226 *
227 * @param string $action
228 * The action to be invoked.
229 *
230 * @return void
231 */
232 public function browse($action = NULL) {
233 // get all price sets
234 $priceSet = [];
235 $comps = [
236 'CiviEvent' => ts('Event'),
237 'CiviContribute' => ts('Contribution'),
238 'CiviMember' => ts('Membership'),
239 ];
240
241 $dao = new CRM_Price_DAO_PriceSet();
242 if (CRM_Price_BAO_PriceSet::eventPriceSetDomainID()) {
243 $dao->domain_id = CRM_Core_Config::domainID();
244 }
245 $dao->is_quick_config = 0;
246 $dao->find();
247 while ($dao->fetch()) {
248 $priceSet[$dao->id] = [];
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 );
254 $extends = [];
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 }
261 $priceSet[$dao->id]['extends'] = implode(', ', $extends);
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) {
281 $actionLinks[CRM_Core_Action::BROWSE]['name'] = ts('View Price Fields');
282 }
283 $priceSet[$dao->id]['action'] = CRM_Core_Action::formLink($actionLinks, $action,
284 ['sid' => $dao->id],
285 ts('more'),
286 FALSE,
287 'priceSet.row.actions',
288 'PriceSet',
289 $dao->id
290 );
291 }
292 $this->assign('rows', $priceSet);
293 }
294
295 /**
296 * make a copy of a price set, including
297 * all the fields in the page
298 *
299 * @return void
300 */
301 public function copy() {
302 $id = CRM_Utils_Request::retrieve('sid', 'Positive',
303 $this, TRUE, 0, 'GET'
304 );
305
306 CRM_Price_BAO_PriceSet::copy($id);
307
308 CRM_Utils_System::redirect(CRM_Utils_System::url(CRM_Utils_System::currentPath(), 'reset=1'));
309 }
310
311 }