Merge pull request #22508 from twomice/event_66_allow_dupliate_price_field_label
[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 */
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 /**
29 * The action links that we need to display for the browse screen.
30 *
31 * @var array
32 */
33 private static $_actionLinks;
34
35 /**
36 * Get the action links for this page.
37 *
38 * @return array
39 * array of action links that we need to display for the browse screen
40 */
41 public function &actionLinks() {
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?');
47 self::$_actionLinks = [
48 CRM_Core_Action::BROWSE => [
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'),
53 ],
54 CRM_Core_Action::PREVIEW => [
55 'name' => ts('Preview'),
56 'url' => 'civicrm/admin/price',
57 'qs' => 'action=preview&reset=1&sid=%%sid%%',
58 'title' => ts('Preview Price Set'),
59 ],
60 CRM_Core_Action::UPDATE => [
61 'name' => ts('Settings'),
62 'url' => 'civicrm/admin/price',
63 'qs' => 'action=update&reset=1&sid=%%sid%%',
64 'title' => ts('Edit Price Set'),
65 ],
66 CRM_Core_Action::DISABLE => [
67 'name' => ts('Disable'),
68 'ref' => 'crm-enable-disable',
69 'title' => ts('Disable Price Set'),
70 ],
71 CRM_Core_Action::ENABLE => [
72 'name' => ts('Enable'),
73 'ref' => 'crm-enable-disable',
74 'title' => ts('Enable Price Set'),
75 ],
76 CRM_Core_Action::DELETE => [
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 . '\');"',
82 ],
83 CRM_Core_Action::COPY => [
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 . '\');"',
89 ],
90 ];
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 *
102 * @return void
103 */
104 public function run() {
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) {
118 CRM_Price_BAO_PriceSet::checkPermission($sid);
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) {
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)) {
135 $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($sid);
136
137 if (empty($usedBy)) {
138 // prompt to delete
139 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin/price', 'action=browse'));
140 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_DeleteSet', 'Delete Price Set', NULL);
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);
150 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceSet::getTitle($sid));
151 $this->assign('usedBy', $usedBy);
152
153 $comps = [
154 'Event' => 'civicrm_event',
155 'Contribution' => 'civicrm_contribution_page',
156 'EventTemplate' => 'civicrm_event_template',
157 ];
158 $priceSetContexts = [];
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 /**
176 * Edit price set.
177 *
178 * @param int $sid
179 * Price set id.
180 * @param string $action
181 * The action to be invoked.
182 *
183 * @return void
184 */
185 public function edit($sid, $action) {
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 /**
199 * Preview price set.
200 *
201 * @param int $sid
202 * Price set id.
203 *
204 * @return void
205 */
206 public function preview($sid) {
207 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Preview', ts('Preview Price Set'), NULL);
208 $session = CRM_Core_Session::singleton();
209 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
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 /**
223 * Browse all price sets.
224 *
225 * @param string $action
226 * The action to be invoked.
227 *
228 * @return void
229 */
230 public function browse($action = NULL) {
231 // get all price sets
232 $priceSet = [];
233 $comps = [
234 'CiviEvent' => ts('Event'),
235 'CiviContribute' => ts('Contribution'),
236 'CiviMember' => ts('Membership'),
237 ];
238
239 $dao = new CRM_Price_DAO_PriceSet();
240 if (CRM_Price_BAO_PriceSet::eventPriceSetDomainID()) {
241 $dao->domain_id = CRM_Core_Config::domainID();
242 }
243 $dao->is_quick_config = 0;
244 $dao->find();
245 while ($dao->fetch()) {
246 $priceSet[$dao->id] = [];
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 );
252 $extends = [];
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 }
259 $priceSet[$dao->id]['extends'] = implode(', ', $extends);
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) {
279 $actionLinks[CRM_Core_Action::BROWSE]['name'] = ts('View Price Fields');
280 }
281 $priceSet[$dao->id]['action'] = CRM_Core_Action::formLink($actionLinks, $action,
282 ['sid' => $dao->id],
283 ts('more'),
284 FALSE,
285 'priceSet.row.actions',
286 'PriceSet',
287 $dao->id
288 );
289 }
290 $this->assign('rows', $priceSet);
291 }
292
293 /**
294 * make a copy of a price set, including
295 * all the fields in the page
296 *
297 * @return void
298 */
299 public function copy() {
300 $id = CRM_Utils_Request::retrieve('sid', 'Positive',
301 $this, TRUE, 0, 'GET'
302 );
303
304 CRM_Price_BAO_PriceSet::copy($id);
305
306 CRM_Utils_System::redirect(CRM_Utils_System::url(CRM_Utils_System::currentPath(), 'reset=1'));
307 }
308
309 }