INFRA-132 - Move stray comments into docblocks
[civicrm-core.git] / CRM / PCP / Page / PCP.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
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 * Page for displaying list of financial types
38 */
39class CRM_PCP_Page_PCP extends CRM_Core_Page_Basic {
40
41 /**
42 * The action links that we need to display for the browse screen
43 *
44 * @var array
45 * @static
46 */
47 static $_links = NULL;
48
49 /**
50 * Get BAO Name
51 *
a6c01b45
CW
52 * @return string
53 * Classname of BAO.
6a488035 54 */
00be9182 55 public function getBAOName() {
6a488035
TO
56 return 'CRM_PCP_BAO_PCP';
57 }
58
59 /**
60 * Get action Links
61 *
a6c01b45
CW
62 * @return array
63 * (reference) of action links
6a488035 64 */
00be9182 65 public function &links() {
6a488035
TO
66 if (!(self::$_links)) {
67 // helper variable for nicer formatting
68 $deleteExtra = ts('Are you sure you want to delete this Campaign Page ?');
69
70 self::$_links = array(
71 CRM_Core_Action::UPDATE => array(
72 'name' => ts('Edit'),
73 'url' => 'civicrm/pcp/info',
74 'qs' => 'action=update&reset=1&id=%%id%%&context=dashboard',
75 'title' => ts('Edit Personal Campaign Page'),
76 ),
77 CRM_Core_Action::RENEW => array(
78 'name' => ts('Approve'),
79 'url' => 'civicrm/admin/pcp',
80 'qs' => 'action=renew&id=%%id%%',
81 'title' => ts('Approve Personal Campaign Page'),
82 ),
83 CRM_Core_Action::REVERT => array(
84 'name' => ts('Reject'),
85 'url' => 'civicrm/admin/pcp',
86 'qs' => 'action=revert&id=%%id%%',
87 'title' => ts('Reject Personal Campaign Page'),
88 ),
89 CRM_Core_Action::DELETE => array(
90 'name' => ts('Delete'),
91 'url' => 'civicrm/admin/pcp',
92 'qs' => 'action=delete&id=%%id%%',
93 'extra' => 'onclick = "return confirm(\'' . $deleteExtra . '\');"',
94 'title' => ts('Delete Personal Campaign Page'),
95 ),
e049d911 96 CRM_Core_Action::ENABLE => array(
353ffa53 97 'name' => ts('Enable'),
6a488035
TO
98 'url' => 'civicrm/admin/pcp',
99 'qs' => 'action=enable&id=%%id%%',
100 'title' => ts('Enable'),
101 ),
e049d911 102 CRM_Core_Action::DISABLE => array(
353ffa53 103 'name' => ts('Disable'),
6a488035
TO
104 'url' => 'civicrm/admin/pcp',
105 'qs' => 'action=disable&id=%%id%%',
106 'title' => ts('Disable'),
107 ),
108 );
109 }
110 return self::$_links;
111 }
112
113 /**
114 * Run the page.
115 *
116 * This method is called after the page is created. It checks for the
117 * type of action and executes that action.
118 * Finally it calls the parent's run method.
119 *
120 * @param
121 *
122 * @return void
6a488035 123 */
00be9182 124 public function run() {
6a488035
TO
125 // get the requested action
126 $action = CRM_Utils_Request::retrieve('action', 'String',
127 $this, FALSE,
128 'browse'
129 );
130 if ($action & CRM_Core_Action::REVERT) {
131 $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE);
132 CRM_PCP_BAO_PCP::setIsActive($id, 0);
133 $session = CRM_Core_Session::singleton();
134 $session->pushUserContext(CRM_Utils_System::url(CRM_Utils_System::currentPath(), 'reset=1'));
135 }
136 elseif ($action & CRM_Core_Action::RENEW) {
137 $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE);
138 CRM_PCP_BAO_PCP::setIsActive($id, 1);
139 $session = CRM_Core_Session::singleton();
140 $session->pushUserContext(CRM_Utils_System::url(CRM_Utils_System::currentPath(), 'reset=1'));
141 }
142 elseif ($action & CRM_Core_Action::DELETE) {
143 $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE);
144 $session = CRM_Core_Session::singleton();
145 $session->pushUserContext(CRM_Utils_System::url(CRM_Utils_System::currentPath(), 'reset=1&action=browse'));
146 $controller = new CRM_Core_Controller_Simple('CRM_PCP_Form_PCP',
147 'Personal Campaign Page',
148 CRM_Core_Action::DELETE
149 );
150 //$this->setContext( $id, $action );
151 $controller->set('id', $id);
152 $controller->process();
153 return $controller->run();
154 }
155
156 // finally browse
157 $this->browse();
158
159 // parent run
160 parent::run();
161 }
162
163 /**
164 * Browse all custom data groups.
165 *
166 *
da6b46f4
EM
167 * @param null $action
168 *
6a488035 169 * @return void
6a488035
TO
170 * @static
171 */
00be9182 172 public function browse($action = NULL) {
6a488035
TO
173 $this->_sortByCharacter = CRM_Utils_Request::retrieve('sortByCharacter',
174 'String',
175 $this
176 );
177 if ($this->_sortByCharacter == 1 ||
178 !empty($_POST)
179 ) {
180 $this->_sortByCharacter = '';
181 }
182
01dc0aff 183 $status = CRM_PCP_BAO_PCP::buildOptions('status_id', 'create');
6a488035
TO
184
185 $pcpSummary = $params = array();
186 $whereClause = NULL;
187
188 if (!empty($_POST) || !empty($_GET['page_type'])) {
a7488080 189 if (!empty($_POST['status_id'])) {
6a488035
TO
190 $whereClause = ' AND cp.status_id = %1';
191 $params['1'] = array($_POST['status_id'], 'Integer');
192 }
193
a7488080 194 if (!empty($_POST['page_type'])) {
6a488035
TO
195 $whereClause .= ' AND cp.page_type = %2';
196 $params['2'] = array($_POST['page_type'], 'String');
197 }
a7488080 198 elseif (!empty($_GET['page_type'])) {
6a488035
TO
199 $whereClause .= ' AND cp.page_type = %2';
200 $params['2'] = array($_GET['page_type'], 'String');
201 }
202
a7488080 203 if (!empty($_POST['page_id'])) {
6a488035
TO
204 $whereClause .= ' AND cp.page_id = %4 AND cp.page_type = "contribute"';
205 $params['4'] = array($_POST['page_id'], 'Integer');
206 }
207
a7488080 208 if (!empty($_POST['event_id'])) {
6a488035
TO
209 $whereClause .= ' AND cp.page_id = %5 AND cp.page_type = "event"';
210 $params['5'] = array($_POST['event_id'], 'Integer');
211 }
212
213 if ($whereClause) {
214 $this->set('whereClause', $whereClause);
215 $this->set('params', $params);
216 }
217 else {
218 $this->set('whereClause', NULL);
219 $this->set('params', NULL);
220 }
221 }
222
223 $approvedId = CRM_Core_OptionGroup::getValue('pcp_status', 'Approved', 'name');
224
225 //check for delete CRM-4418
226 $allowToDelete = CRM_Core_Permission::check('delete in CiviContribute');
227
228 // get all contribution pages
229 $query = "SELECT id, title, start_date, end_date FROM civicrm_contribution_page WHERE (1)";
230 $cpages = CRM_Core_DAO::executeQuery($query);
231 while ($cpages->fetch()) {
232 $pages['contribute'][$cpages->id]['id'] = $cpages->id;
233 $pages['contribute'][$cpages->id]['title'] = $cpages->title;
234 $pages['contribute'][$cpages->id]['start_date'] = $cpages->start_date;
235 $pages['contribute'][$cpages->id]['end_date'] = $cpages->end_date;
236 }
237
238 // get all event pages. pcp campaign start and end dates for event related pcp's use the online registration start and end dates,
239 // altho if target is contribution page this might not be correct. fixme? dgg
366fe2a3 240 $query = "SELECT id, title, start_date, end_date, registration_start_date, registration_end_date
241 FROM civicrm_event
6a488035
TO
242 WHERE is_template IS NULL OR is_template != 1";
243 $epages = CRM_Core_DAO::executeQuery($query);
244 while ($epages->fetch()) {
245 $pages['event'][$epages->id]['id'] = $epages->id;
246 $pages['event'][$epages->id]['title'] = $epages->title;
247 $pages['event'][$epages->id]['start_date'] = $epages->registration_start_date;
248 $pages['event'][$epages->id]['end_date'] = $epages->registration_end_date;
249 }
250
251 $params = $this->get('params') ? $this->get('params') : array();
252
253 $title = '1';
254 if ($this->_sortByCharacter !== NULL) {
255 $clauses[] = "cp.title LIKE '" . strtolower(CRM_Core_DAO::escapeWildCardString($this->_sortByCharacter)) . "%'";
256 }
257
258 $query = "
259 SELECT cp.id, cp.contact_id , cp.status_id, cp.title, cp.is_active, cp.page_type, cp.page_id
260 FROM civicrm_pcp cp
261 WHERE $title" . $this->get('whereClause') . " ORDER BY cp.status_id";
262
263 $pcp = CRM_Core_DAO::executeQuery($query, $params);
264 while ($pcp->fetch()) {
265 $action = array_sum(array_keys($this->links()));
266 $contact = CRM_Contact_BAO_Contact::getDisplayAndImage($pcp->contact_id);
267
268 $class = '';
269
270 if ($pcp->status_id != $approvedId || $pcp->is_active != 1) {
271 $class = 'disabled';
272 }
273
274 switch ($pcp->status_id) {
275 case 2:
276 $action -= CRM_Core_Action::RENEW;
277 break;
278
279 case 3:
280 $action -= CRM_Core_Action::REVERT;
281 break;
282 }
283
284 switch ($pcp->is_active) {
285 case 1:
286 $action -= CRM_Core_Action::ENABLE;
287 break;
288
289 case 0:
290 $action -= CRM_Core_Action::DISABLE;
291 break;
292 }
293
294 if (!$allowToDelete) {
295 $action -= CRM_Core_Action::DELETE;
296 }
297
298 $page_type = $pcp->page_type;
299 $page_id = (int) $pcp->page_id;
300 if ($pages[$page_type][$page_id]['title'] == '' || $pages[$page_type][$page_id]['title'] == NULL) {
301 $title = '(no title found for ' . $page_type . ' id ' . $page_id . ')';
302 }
303 else {
304 $title = $pages[$page_type][$page_id]['title'];
305 }
306
307 if ($pcp->page_type == 'contribute') {
308 $pageUrl = CRM_Utils_System::url('civicrm/' . $page_type . '/transact', 'reset=1&id=' . $pcp->page_id);
309 }
310 else {
311 $pageUrl = CRM_Utils_System::url('civicrm/' . $page_type . '/register', 'reset=1&id=' . $pcp->page_id);
312 }
313
314 $pcpSummary[$pcp->id] = array(
315 'id' => $pcp->id,
316 'start_date' => $pages[$page_type][$page_id]['start_date'],
317 'end_date' => $pages[$page_type][$page_id]['end_date'],
318 'supporter' => $contact['0'],
319 'supporter_id' => $pcp->contact_id,
320 'status_id' => $status[$pcp->status_id],
321 'page_id' => $page_id,
322 'page_title' => $title,
323 'page_url' => $pageUrl,
324 'page_type' => $page_type,
325 'action' => CRM_Core_Action::formLink(self::links(), $action,
87dab4a4 326 array('id' => $pcp->id), ts('more'), FALSE, 'contributionpage.pcp.list', 'PCP', $pcp->id
6a488035
TO
327 ),
328 'title' => $pcp->title,
329 'class' => $class,
330 );
331 }
332
333 $this->search();
334 $this->pagerAToZ($this->get('whereClause'), $params);
335
336 $this->assign('rows', $pcpSummary);
337
338 // Let template know if user has run a search or not
339 if ($this->get('whereClause')) {
340 $this->assign('isSearch', 1);
341 }
342 else {
343 $this->assign('isSearch', 0);
344 }
345 }
346
00be9182 347 public function search() {
6a488035
TO
348
349 if ($this->_action & CRM_Core_Action::DELETE) {
350 return;
351 }
352
353 $form = new CRM_Core_Controller_Simple('CRM_PCP_Form_PCP', ts('Search Campaign Pages'), CRM_Core_Action::ADD);
354 $form->setEmbedded(TRUE);
355 $form->setParent($this);
356 $form->process();
357 $form->run();
358 }
359
360 /**
361 * Get name of edit form
362 *
a6c01b45
CW
363 * @return string
364 * Classname of edit form.
6a488035 365 */
00be9182 366 public function editForm() {
6a488035
TO
367 return 'CRM_PCP_Form_PCP';
368 }
369
370 /**
371 * Get edit form name
372 *
a6c01b45
CW
373 * @return string
374 * name of this page.
6a488035 375 */
00be9182 376 public function editName() {
6a488035
TO
377 return ts('Personal Campaign Page');
378 }
379
380 /**
381 * Get user context.
382 *
77b97be7
EM
383 * @param null $mode
384 *
a6c01b45
CW
385 * @return string
386 * user context.
6a488035 387 */
00be9182 388 public function userContext($mode = NULL) {
6a488035
TO
389 return 'civicrm/admin/pcp';
390 }
391
e0ef6999 392 /**
4f1f1f2a 393 * @TODO this function changed, debug this at runtime
e0ef6999 394 * @param $whereClause
100fef9d 395 * @param array $whereParams
e0ef6999 396 */
00be9182 397 public function pagerAtoZ($whereClause, $whereParams) {
6a488035
TO
398 $where = '';
399 if ($whereClause) {
400 if (strpos($whereClause, ' AND') == 0) {
401 $whereClause = substr($whereClause, 4);
402 }
403 $where = 'WHERE ' . $whereClause;
404 }
405
406 $query = "
407 SELECT UPPER(LEFT(cp.title, 1)) as sort_name
408 FROM civicrm_pcp cp
409 " . $where . "
410 ORDER BY LEFT(cp.title, 1);
411 ";
412
413 $dao = CRM_Core_DAO::executeQuery($query, $whereParams);
414
415 $aToZBar = CRM_Utils_PagerAToZ::getAToZBar($dao, $this->_sortByCharacter, TRUE);
416 $this->assign('aToZ', $aToZBar);
417 }
418}