| 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 | * Page for displaying list of financial types |
| 20 | */ |
| 21 | class CRM_PCP_Page_PCP extends CRM_Core_Page_Basic { |
| 22 | |
| 23 | /** |
| 24 | * The action links that we need to display for the browse screen. |
| 25 | * |
| 26 | * @var array |
| 27 | */ |
| 28 | public static $_links = NULL; |
| 29 | |
| 30 | /** |
| 31 | * Get BAO Name. |
| 32 | * |
| 33 | * @return string |
| 34 | * Classname of BAO. |
| 35 | */ |
| 36 | public function getBAOName() { |
| 37 | return 'CRM_PCP_BAO_PCP'; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Get action Links. |
| 42 | * |
| 43 | * @return array |
| 44 | * (reference) of action links |
| 45 | */ |
| 46 | public function &links() { |
| 47 | if (!(self::$_links)) { |
| 48 | // helper variable for nicer formatting |
| 49 | $deleteExtra = ts('Are you sure you want to delete this Campaign Page ?'); |
| 50 | |
| 51 | self::$_links = [ |
| 52 | CRM_Core_Action::UPDATE => [ |
| 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'), |
| 57 | ], |
| 58 | CRM_Core_Action::RENEW => [ |
| 59 | 'name' => ts('Approve'), |
| 60 | 'url' => 'civicrm/admin/pcp', |
| 61 | 'qs' => 'action=renew&id=%%id%%', |
| 62 | 'title' => ts('Approve Personal Campaign Page'), |
| 63 | ], |
| 64 | CRM_Core_Action::REVERT => [ |
| 65 | 'name' => ts('Reject'), |
| 66 | 'url' => 'civicrm/admin/pcp', |
| 67 | 'qs' => 'action=revert&id=%%id%%', |
| 68 | 'title' => ts('Reject Personal Campaign Page'), |
| 69 | ], |
| 70 | CRM_Core_Action::DELETE => [ |
| 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'), |
| 76 | ], |
| 77 | CRM_Core_Action::ENABLE => [ |
| 78 | 'name' => ts('Enable'), |
| 79 | 'url' => 'civicrm/admin/pcp', |
| 80 | 'qs' => 'action=enable&id=%%id%%', |
| 81 | 'title' => ts('Enable'), |
| 82 | ], |
| 83 | CRM_Core_Action::DISABLE => [ |
| 84 | 'name' => ts('Disable'), |
| 85 | 'url' => 'civicrm/admin/pcp', |
| 86 | 'qs' => 'action=disable&id=%%id%%', |
| 87 | 'title' => ts('Disable'), |
| 88 | ], |
| 89 | ]; |
| 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 | * |
| 101 | * @return mixed|null |
| 102 | * @throws \CRM_Core_Exception |
| 103 | */ |
| 104 | public function run() { |
| 105 | $id = $this->getIdAndAction(); |
| 106 | |
| 107 | if ($this->_action & CRM_Core_Action::REVERT) { |
| 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 | } |
| 112 | elseif ($this->_action & CRM_Core_Action::RENEW) { |
| 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 | } |
| 117 | elseif ($this->_action & CRM_Core_Action::DELETE) { |
| 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 |
| 134 | CRM_Core_Page::run(); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Browse all custom data groups. |
| 139 | * |
| 140 | * @param int $action |
| 141 | * |
| 142 | * @throws \CRM_Core_Exception |
| 143 | */ |
| 144 | public function browse($action = NULL) { |
| 145 | CRM_Core_Resources::singleton()->addStyleFile('civicrm', 'css/searchForm.css', 1, 'html-header'); |
| 146 | |
| 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 | |
| 157 | $status = CRM_PCP_BAO_PCP::buildOptions('status_id', 'create'); |
| 158 | |
| 159 | $pcpSummary = $params = []; |
| 160 | $whereClause = NULL; |
| 161 | |
| 162 | if (!empty($_POST) || !empty($_GET['page_type'])) { |
| 163 | if (!empty($_POST['status_id'])) { |
| 164 | $whereClause = ' AND cp.status_id = %1'; |
| 165 | $params['1'] = [$_POST['status_id'], 'Integer']; |
| 166 | } |
| 167 | |
| 168 | if (!empty($_POST['page_type'])) { |
| 169 | $whereClause .= ' AND cp.page_type = %2'; |
| 170 | $params['2'] = [$_POST['page_type'], 'String']; |
| 171 | } |
| 172 | elseif (!empty($_GET['page_type'])) { |
| 173 | $whereClause .= ' AND cp.page_type = %2'; |
| 174 | $params['2'] = [$_GET['page_type'], 'String']; |
| 175 | } |
| 176 | |
| 177 | if (!empty($_POST['page_id'])) { |
| 178 | $whereClause .= ' AND cp.page_id = %4 AND cp.page_type = "contribute"'; |
| 179 | $params['4'] = [$_POST['page_id'], 'Integer']; |
| 180 | } |
| 181 | |
| 182 | if (!empty($_POST['event_id'])) { |
| 183 | $whereClause .= ' AND cp.page_id = %5 AND cp.page_type = "event"'; |
| 184 | $params['5'] = [$_POST['event_id'], 'Integer']; |
| 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 | |
| 197 | $approvedId = CRM_Core_PseudoConstant::getKey('CRM_PCP_BAO_PCP', 'status_id', 'Approved'); |
| 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, |
| 213 | // although if target is contribution page this might not be correct. fixme? dgg |
| 214 | $query = "SELECT id, title, start_date, end_date, registration_start_date, registration_end_date |
| 215 | FROM civicrm_event |
| 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 | |
| 225 | $params = $this->get('params') ? $this->get('params') : []; |
| 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 | |
| 288 | $pcpSummary[$pcp->id] = [ |
| 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, |
| 300 | ['id' => $pcp->id], ts('more'), FALSE, 'contributionpage.pcp.list', 'PCP', $pcp->id |
| 301 | ), |
| 302 | 'title' => $pcp->title, |
| 303 | 'class' => $class, |
| 304 | ]; |
| 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 | |
| 321 | public function search() { |
| 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 | /** |
| 334 | * Get name of edit form. |
| 335 | * |
| 336 | * @return string |
| 337 | * Classname of edit form. |
| 338 | */ |
| 339 | public function editForm() { |
| 340 | return 'CRM_PCP_Form_PCP'; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Get edit form name. |
| 345 | * |
| 346 | * @return string |
| 347 | * name of this page. |
| 348 | */ |
| 349 | public function editName() { |
| 350 | return ts('Personal Campaign Page'); |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Get user context. |
| 355 | * |
| 356 | * @param null $mode |
| 357 | * |
| 358 | * @return string |
| 359 | * user context. |
| 360 | */ |
| 361 | public function userContext($mode = NULL) { |
| 362 | return 'civicrm/admin/pcp'; |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * @TODO this function changed, debug this at runtime |
| 367 | * @param $whereClause |
| 368 | * @param array $whereParams |
| 369 | */ |
| 370 | public function pagerAtoZ($whereClause, $whereParams) { |
| 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 | } |
| 391 | |
| 392 | } |