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