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