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