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