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