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