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