Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Page for displaying list of events | |
38 | */ | |
39 | class CRM_Event_Page_ManageEvent extends CRM_Core_Page { | |
40 | ||
41 | /** | |
42 | * The action links that we need to display for the browse screen | |
43 | * | |
44 | * @var array | |
45 | * @static | |
46 | */ | |
47 | static $_actionLinks = NULL; | |
48 | ||
49 | static $_links = NULL; | |
50 | ||
51 | protected $_pager = NULL; | |
52 | ||
53 | protected $_sortByCharacter; | |
54 | ||
55 | protected $_isTemplate = FALSE; | |
56 | ||
57 | /** | |
58 | * Get action Links | |
59 | * | |
60 | * @return array (reference) of action links | |
61 | */ | |
62 | function &links() { | |
63 | if (!(self::$_actionLinks)) { | |
64 | // helper variable for nicer formatting | |
65 | $copyExtra = ts('Are you sure you want to make a copy of this Event?'); | |
66 | $deleteExtra = ts('Are you sure you want to delete this Event?'); | |
67 | ||
68 | self::$_actionLinks = array( | |
69 | CRM_Core_Action::DISABLE => array( | |
70 | 'name' => ts('Disable'), | |
71 | 'extra' => 'onclick = "enableDisable( %%id%%,\'' . 'CRM_Event_BAO_Event' . '\',\'' . 'enable-disable' . '\' );"', | |
72 | 'ref' => 'disable-action', | |
73 | 'title' => ts('Disable Event'), | |
74 | ), | |
75 | CRM_Core_Action::ENABLE => array( | |
76 | 'name' => ts('Enable'), | |
77 | 'extra' => 'onclick = "enableDisable( %%id%%,\'' . 'CRM_Event_BAO_Event' . '\',\'' . 'disable-enable' . '\' );"', | |
78 | 'ref' => 'enable-action', | |
79 | 'title' => ts('Enable Event'), | |
80 | ), | |
81 | CRM_Core_Action::DELETE => array( | |
82 | 'name' => ts('Delete'), | |
83 | 'url' => CRM_Utils_System::currentPath(), | |
84 | 'qs' => 'action=delete&id=%%id%%', | |
85 | 'extra' => 'onclick = "return confirm(\'' . $deleteExtra . '\');"', | |
86 | 'title' => ts('Delete Event'), | |
87 | ), | |
88 | CRM_Core_Action::COPY => array( | |
89 | 'name' => ts('Copy'), | |
90 | 'url' => CRM_Utils_System::currentPath(), | |
91 | 'qs' => 'reset=1&action=copy&id=%%id%%', | |
92 | 'extra' => 'onclick = "return confirm(\'' . $copyExtra . '\');"', | |
93 | 'title' => ts('Copy Event'), | |
94 | ), | |
95 | ); | |
96 | } | |
97 | return self::$_actionLinks; | |
98 | } | |
99 | ||
100 | /** | |
101 | * Run the page. | |
102 | * | |
103 | * This method is called after the page is created. It checks for the | |
104 | * type of action and executes that action. | |
105 | * Finally it calls the parent's run method. | |
106 | * | |
107 | * @return void | |
108 | * @access public | |
109 | * | |
110 | */ | |
111 | function run() { | |
112 | // get the requested action | |
113 | $action = CRM_Utils_Request::retrieve('action', 'String', | |
114 | // default to 'browse' | |
115 | $this, FALSE, 'browse' | |
116 | ); | |
117 | ||
118 | // assign vars to templates | |
119 | $this->assign('action', $action); | |
120 | $id = CRM_Utils_Request::retrieve('id', 'Positive', | |
121 | $this, FALSE, 0, 'REQUEST' | |
122 | ); | |
123 | ||
124 | // figure out whether we’re handling an event or an event template | |
125 | if ($id) { | |
126 | $this->_isTemplate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $id, 'is_template'); | |
127 | } | |
128 | elseif ($action & CRM_Core_Action::ADD) { | |
129 | $this->_isTemplate = CRM_Utils_Request::retrieve('is_template', 'Boolean', $this); | |
130 | } | |
131 | ||
132 | if (!$this->_isTemplate && $id) { | |
133 | $breadCrumb = array(array('title' => ts('Manage Events'), | |
134 | 'url' => CRM_Utils_System::url(CRM_Utils_System::currentPath(), 'reset=1'), | |
135 | )); | |
136 | CRM_Utils_System::appendBreadCrumb($breadCrumb); | |
137 | } | |
138 | ||
139 | // what action to take ? | |
140 | if ($action & CRM_Core_Action::DELETE) { | |
141 | $session = CRM_Core_Session::singleton(); | |
142 | $session->pushUserContext(CRM_Utils_System::url(CRM_Utils_System::currentPath(), 'reset=1&action=browse')); | |
143 | $controller = new CRM_Core_Controller_Simple('CRM_Event_Form_ManageEvent_Delete', | |
144 | 'Delete Event', | |
145 | $action | |
146 | ); | |
147 | $controller->set('id', $id); | |
148 | $controller->process(); | |
149 | return $controller->run(); | |
150 | } | |
151 | elseif ($action & CRM_Core_Action::COPY) { | |
152 | $this->copy(); | |
153 | } | |
154 | ||
155 | // finally browse the custom groups | |
156 | $this->browse(); | |
157 | ||
158 | // parent run | |
159 | return parent::run(); | |
160 | } | |
161 | ||
162 | /** | |
163 | * browse all events | |
164 | * | |
165 | * @return void | |
166 | */ | |
167 | function browse() { | |
168 | $this->_sortByCharacter = CRM_Utils_Request::retrieve('sortByCharacter', | |
169 | 'String', | |
170 | $this | |
171 | ); | |
172 | $createdId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE, 0); | |
173 | if (strtolower($this->_sortByCharacter) == 'all' || | |
174 | !empty($_POST) | |
175 | ) { | |
176 | $this->_sortByCharacter = ''; | |
177 | $this->set('sortByCharacter', ''); | |
178 | } | |
179 | ||
180 | $this->_force = $this->_searchResult = NULL; | |
181 | ||
182 | $this->search(); | |
183 | ||
184 | $config = CRM_Core_Config::singleton(); | |
185 | ||
186 | $params = array(); | |
187 | $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', | |
188 | $this, FALSE | |
189 | ); | |
190 | $this->_searchResult = CRM_Utils_Request::retrieve('searchResult', 'Boolean', $this); | |
191 | ||
192 | $whereClause = $this->whereClause($params, FALSE, $this->_force); | |
193 | $this->pagerAToZ($whereClause, $params); | |
194 | ||
195 | $params = array(); | |
196 | $whereClause = $this->whereClause($params, TRUE, $this->_force); | |
197 | // because is_template != 1 would be to simple | |
198 | $whereClause .= ' AND (is_template = 0 OR is_template IS NULL)'; | |
199 | ||
200 | $this->pager($whereClause, $params); | |
201 | ||
202 | list($offset, $rowCount) = $this->_pager->getOffsetAndRowCount(); | |
203 | ||
204 | // get all custom groups sorted by weight | |
205 | $manageEvent = array(); | |
206 | ||
207 | $query = " | |
208 | SELECT * | |
209 | FROM civicrm_event | |
210 | WHERE $whereClause | |
211 | ORDER BY start_date desc | |
212 | LIMIT $offset, $rowCount"; | |
213 | ||
214 | $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Event_DAO_Event'); | |
215 | $permissions = CRM_Event_BAO_Event::checkPermission(); | |
216 | ||
217 | //get all campaigns. | |
218 | $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE); | |
219 | ||
220 | // get the list of active event pcps | |
221 | $eventPCPS = array(); | |
222 | ||
223 | $pcpDao = new CRM_PCP_DAO_PCPBlock; | |
224 | $pcpDao->entity_table = 'civicrm_event'; | |
225 | $pcpDao->find(); | |
226 | ||
227 | while ($pcpDao->fetch()) { | |
228 | $eventPCPS[$pcpDao->entity_id] = $pcpDao->entity_id; | |
229 | } | |
230 | ||
231 | while ($dao->fetch()) { | |
232 | if (in_array($dao->id, $permissions[CRM_Core_Permission::VIEW])) { | |
233 | $manageEvent[$dao->id] = array(); | |
234 | CRM_Core_DAO::storeValues($dao, $manageEvent[$dao->id]); | |
235 | ||
236 | // form all action links | |
237 | $action = array_sum(array_keys($this->links())); | |
238 | ||
239 | if ($dao->is_active) { | |
240 | $action -= CRM_Core_Action::ENABLE; | |
241 | } | |
242 | else { | |
243 | $action -= CRM_Core_Action::DISABLE; | |
244 | } | |
245 | ||
246 | if (!in_array($dao->id, $permissions[CRM_Core_Permission::DELETE])) { | |
247 | $action -= CRM_Core_Action::DELETE; | |
248 | } | |
249 | if (!in_array($dao->id, $permissions[CRM_Core_Permission::EDIT])) { | |
250 | $action -= CRM_Core_Action::UPDATE; | |
251 | } | |
252 | ||
253 | $manageEvent[$dao->id]['action'] = CRM_Core_Action::formLink(self::links(), | |
254 | $action, | |
255 | array('id' => $dao->id), | |
256 | ts('more'), | |
257 | TRUE | |
258 | ); | |
259 | ||
260 | $params = array( | |
261 | 'entity_id' => $dao->id, | |
262 | 'entity_table' => 'civicrm_event', | |
263 | 'is_active' => 1, | |
264 | ); | |
265 | ||
266 | $defaults['location'] = CRM_Core_BAO_Location::getValues($params, TRUE); | |
267 | ||
268 | $manageEvent[$dao->id]['friend'] = CRM_Friend_BAO_Friend::getValues($params); | |
269 | ||
270 | if (isset($defaults['location']['address'][1]['city'])) { | |
271 | $manageEvent[$dao->id]['city'] = $defaults['location']['address'][1]['city']; | |
272 | } | |
273 | if (isset($defaults['location']['address'][1]['state_province_id'])) { | |
274 | $manageEvent[$dao->id]['state_province'] = CRM_Core_PseudoConstant::stateProvince($defaults['location']['address'][1]['state_province_id']); | |
275 | } | |
276 | ||
277 | //show campaigns on selector. | |
278 | $manageEvent[$dao->id]['campaign'] = CRM_Utils_Array::value($dao->campaign_id, $allCampaigns); | |
279 | $manageEvent[$dao->id]['reminder'] = CRM_Core_BAO_ActionSchedule::isConfigured($dao->id, 3); | |
280 | ||
281 | $manageEvent[$dao->id]['is_pcp_enabled'] = CRM_Utils_Array::value($dao->id, $eventPCPS); | |
282 | } | |
283 | } | |
284 | $this->assign('rows', $manageEvent); | |
285 | ||
286 | $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1'); | |
287 | $statusTypesPending = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 0'); | |
288 | $findParticipants['statusCounted'] = implode(', ', array_values($statusTypes)); | |
289 | $findParticipants['statusNotCounted'] = implode(', ', array_values($statusTypesPending)); | |
290 | $this->assign('findParticipants', $findParticipants); | |
291 | ||
292 | // check if we're in shopping cart mode for events | |
293 | $enableCart = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::EVENT_PREFERENCES_NAME, | |
294 | 'enable_cart' | |
295 | ); | |
296 | $this->assign('eventCartEnabled', $enableCart); | |
297 | } | |
298 | ||
299 | /** | |
300 | * This function is to make a copy of a Event, including | |
301 | * all the fields in the event wizard | |
302 | * | |
303 | * @return void | |
304 | * @access public | |
305 | */ | |
306 | function copy() { | |
307 | $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE, 0, 'GET'); | |
308 | ||
309 | $urlString = 'civicrm/event/manage'; | |
310 | $copyEvent = CRM_Event_BAO_Event::copy($id); | |
311 | $urlParams = 'reset=1'; | |
312 | // Redirect to Copied Event Configuration | |
313 | if ($copyEvent->id) { | |
314 | $urlString = 'civicrm/event/manage/settings'; | |
315 | $urlParams .= '&action=update&id=' . $copyEvent->id; | |
316 | } | |
317 | ||
318 | return CRM_Utils_System::redirect(CRM_Utils_System::url($urlString, $urlParams)); | |
319 | } | |
320 | ||
321 | function search() { | |
322 | if (isset($this->_action) & | |
323 | (CRM_Core_Action::ADD | | |
324 | CRM_Core_Action::UPDATE | | |
325 | CRM_Core_Action::DELETE | |
326 | ) | |
327 | ) { | |
328 | return; | |
329 | } | |
330 | ||
331 | $form = new CRM_Core_Controller_Simple('CRM_Event_Form_SearchEvent', ts('Search Events'), CRM_Core_Action::ADD); | |
332 | $form->setEmbedded(TRUE); | |
333 | $form->setParent($this); | |
334 | $form->process(); | |
335 | $form->run(); | |
336 | } | |
337 | ||
338 | function whereClause(&$params, $sortBy = TRUE, $force) { | |
339 | $values = array(); | |
340 | $clauses = array(); | |
341 | $title = $this->get('title'); | |
342 | $createdId = $this->get('cid'); | |
343 | ||
344 | if ($createdId) { | |
345 | $clauses[] = "(created_id = {$createdId})"; | |
346 | } | |
347 | ||
348 | if ($title) { | |
349 | $clauses[] = "title LIKE %1"; | |
350 | if (strpos($title, '%') !== FALSE) { | |
351 | $params[1] = array(trim($title), 'String', FALSE); | |
352 | } | |
353 | else { | |
354 | $params[1] = array(trim($title), 'String', TRUE); | |
355 | } | |
356 | } | |
357 | ||
358 | $value = $this->get('event_type_id'); | |
359 | $val = array(); | |
360 | if ($value) { | |
361 | if (is_array($value)) { | |
362 | foreach ($value as $k => $v) { | |
363 | if ($v) { | |
364 | $val[$k] = $k; | |
365 | } | |
366 | } | |
367 | $type = implode(',', $val); | |
368 | } | |
369 | $clauses[] = "event_type_id IN ({$type})"; | |
370 | } | |
371 | ||
372 | $eventsByDates = $this->get('eventsByDates'); | |
373 | if ($this->_searchResult) { | |
374 | if ($eventsByDates) { | |
375 | ||
376 | $from = $this->get('start_date'); | |
377 | if (!CRM_Utils_System::isNull($from)) { | |
378 | $clauses[] = '( start_date >= %3 OR start_date IS NULL )'; | |
379 | $params[3] = array($from, 'String'); | |
380 | } | |
381 | ||
382 | $to = $this->get('end_date'); | |
383 | if (!CRM_Utils_System::isNull($to)) { | |
384 | $clauses[] = '( end_date <= %4 OR end_date IS NULL )'; | |
385 | $params[4] = array($to, 'String'); | |
386 | } | |
387 | } | |
388 | else { | |
389 | $curDate = date('YmdHis'); | |
390 | $clauses[5] = "(end_date >= {$curDate} OR end_date IS NULL)"; | |
391 | } | |
392 | } | |
393 | else { | |
394 | $curDate = date('YmdHis'); | |
395 | $clauses[] = "(end_date >= {$curDate} OR end_date IS NULL)"; | |
396 | } | |
397 | ||
398 | if ($sortBy && | |
399 | $this->_sortByCharacter !== NULL | |
400 | ) { | |
401 | $clauses[] = "title LIKE '" . strtolower(CRM_Core_DAO::escapeWildCardString($this->_sortByCharacter)) . "%'"; | |
402 | } | |
403 | ||
404 | $campainIds = $this->get('campaign_id'); | |
405 | if (!CRM_Utils_System::isNull($campainIds)) { | |
406 | if (!is_array($campainIds)) { | |
407 | $campaignIds = array($campaignIds); | |
408 | } | |
409 | $clauses[] = '( campaign_id IN ( ' . implode(' , ', array_values($campainIds)) . ' ) )'; | |
410 | } | |
411 | ||
412 | // dont do a the below assignment when doing a | |
413 | // AtoZ pager clause | |
414 | if ($sortBy) { | |
415 | if (count($clauses) > 1 || $eventsByDates) { | |
416 | $this->assign('isSearch', 1); | |
417 | } | |
418 | else { | |
419 | $this->assign('isSearch', 0); | |
420 | } | |
421 | } | |
422 | ||
423 | return !empty($clauses) ? implode(' AND ', $clauses) : '(1)'; | |
424 | } | |
425 | ||
426 | function pager($whereClause, $whereParams) { | |
427 | ||
428 | $params['status'] = ts('Event %%StatusMessage%%'); | |
429 | $params['csvString'] = NULL; | |
430 | $params['buttonTop'] = 'PagerTopButton'; | |
431 | $params['buttonBottom'] = 'PagerBottomButton'; | |
432 | $params['rowCount'] = $this->get(CRM_Utils_Pager::PAGE_ROWCOUNT); | |
433 | if (!$params['rowCount']) { | |
434 | $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT; | |
435 | } | |
436 | ||
437 | $query = " | |
438 | SELECT count(id) | |
439 | FROM civicrm_event | |
440 | WHERE $whereClause"; | |
441 | ||
442 | $params['total'] = CRM_Core_DAO::singleValueQuery($query, $whereParams); | |
443 | ||
444 | $this->_pager = new CRM_Utils_Pager($params); | |
445 | $this->assign_by_ref('pager', $this->_pager); | |
446 | } | |
447 | ||
448 | function pagerAtoZ($whereClause, $whereParams) { | |
449 | ||
450 | $query = " | |
451 | SELECT DISTINCT UPPER(LEFT(title, 1)) as sort_name | |
452 | FROM civicrm_event | |
453 | WHERE $whereClause | |
454 | ORDER BY LEFT(title, 1) | |
455 | "; | |
456 | $dao = CRM_Core_DAO::executeQuery($query, $whereParams); | |
457 | ||
458 | $aToZBar = CRM_Utils_PagerAToZ::getAToZBar($dao, $this->_sortByCharacter, TRUE); | |
459 | $this->assign('aToZ', $aToZBar); | |
460 | } | |
461 | } | |
462 |