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