Source of file BookmarksPageControllerExtension.php
Size: 7,893 Bytes - Last Modified: 2017-06-29T01:34:59+00:00
/var/www/docs.ssmods.com/process/src/code/extensions/BookmarksPageControllerExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 | <?php class BookmarksPageControllerExtension extends DataExtension { /** * @var array */ private static $allowed_actions = [ 'addremovebookmark' ]; /** * Helper to get all the {@link GlobalBookmark}s that have been added to * the CMS. Excludes any bookmarks that have an ExcludeGroup that the * current user is a part of. * * @return DataList */ public function getGlobalBookmarks() { $bookmarks = GlobalBookmark::get(); $member = Member::currentUser(); // if there is a logged in user check if his group has been excluded if ($member) { $memberGroupIds = $member->Groups()->column('ID'); // check if current user belongs to any groups if ($bookmarks->count() && count($memberGroupIds)) { foreach ($bookmarks as $bookmark) { $excludedGroups = $bookmark->ExcludeGroups(); // check if there any excluded groups for this bookmark if ($excludedGroups->count()) { $excludedGroupIds = $excludedGroups->column('ID'); // filter out this bookmark if user is in one of the excluded groups if (count(array_intersect($excludedGroupIds, $memberGroupIds))) { $bookmarks = $bookmarks->exclude('ID', $bookmark->ID); } } } } } return $bookmarks; } /** * Helper to check if the current page has been bookmarked by the current * user. * * @return bool */ public function IsBookmarked() { $pageId = $this->owner->data()->ID; $member = Member::currentUser(); if (!$member) { return false; } $bookmark = $member ->Bookmarks() ->filter('SiteTreeID', $pageId) ->first(); return ($bookmark && $bookmark->exists()); } /** * Adding bookmark link for current user if the bookmark has not already * been saved. If bookmark exists, bookmark will be removed. * * @param \SS_HTTPRequest $request * * @throws \Exception * @return string */ public function addremovebookmark(SS_HTTPRequest $request) { // ensure this is an ajax reqeust if (!$request->isAjax()) { return $this->owner->httpError(403); } $ID = (int)$request->postVar('ID'); try { $member = Member::currentUser(); if (($ID > 0) && $member) { // set default type $type = 'SiteTree'; // Allow a custom method to define type of BookmarkLink to save based on $request data if (method_exists($this->owner, 'getBookmarkType')) { $customType = $this->owner->getBookmarkType($request); $type = ($customType) ? $customType : $type; } // get filters based on type $filters = $this->getBookmarkFilterOptions($request, $type, $ID); if (count($filters) > 0) { $memberID = $member->ID; // add default filters $filters['Type'] = $type; $filters['BookmarkMemberID'] = $memberID; // Check if the bookmark already exists for this user $bookmark = BookmarkLink::get()->filter($filters)->first(); // If bookmark exists delete it, Otherwise add a new bookmark if ($bookmark) { $bookmark->delete(); } else { $this->createBookmarkFromData($request, $type, $ID, $memberID); } } else { throw new Exception('No filters found!'); } return $this->successResponse(); } } catch (Exeception $e) { SS_Log::log( sprintf( 'Error in add bookmark . %s', $e->getMessage() ), SS_Log::ERR ); } // log to push to raygun if it gets here SS_Log::log( 'Error in add bookmark - Invalid Member or ID!', SS_Log::ERR ); return $this->errorResponse(); } /** * Send back an error response. * * @return SS_HTTPResponse */ private function errorResponse() { $response = $this->owner->getResponse(); $response->setStatusCode(403); $response->addHeader('Content-Type', 'application/json'); return $response; } /** * Send back a successful response. Also pass back any custom data in the * body if required. * * @return SS_HTTPResponse */ private function successResponse() { $response = $this->owner->getResponse(); $response->setStatusCode(200); $response->addHeader('Content-Type', 'application/json'); // provide hook for custom data to be passed back if needed if (method_exists($this->owner, 'updateBookmarkSuccessResponse')) { $data = []; $extraData = $this->owner->updateBookmarkSuccessResponse($data); // add the extra data to the body of the response $response->setBody(json_encode($extraData)); } return $response; } /** * @param \SS_HTTPRequest $request * @param string $type * @param integer $ID * * @return array */ private function getBookmarkFilterOptions(SS_HTTPRequest $request, $type, $ID) { switch ($type) { case 'SiteTree': $siteTreeExists = $this->getSiteTree($ID); if ($siteTreeExists) { return [ 'SiteTreeID' => $ID ]; } return []; break; case 'URL': return [ 'URL' => Convert::raw2sql($request->getHeader('Referer')), ]; break; default: return []; break; } } /** * @param \SS_HTTPRequest $request * @param string $type * @param integer $ID * @param integer $memberID * * @return void */ private function createBookmarkFromData(SS_HTTPRequest $request, $type, $ID, $memberID) { $bookmarkLink = new BookmarkLink(); switch ($type) { case 'URL': $url = Convert::raw2sql($request->getHeader('Referer')); $bookmarkLink->Title = $this->getURLTitle($url); $bookmarkLink->URL = $url; break; case 'SiteTree': default: $siteTree = $this->getSiteTree($ID); if ($siteTree) { $bookmarkLink->Title = $siteTree->Title; $bookmarkLink->SiteTreeID = $ID; } break; } $bookmarkLink->BookmarkMemberID = $memberID; $bookmarkLink->Type = $type; $bookmarkLink->write(); } /** * @param integer $ID * * @return \DataObject */ private function getSiteTree($ID) { return SiteTree::get()->filter('ID', (int)$ID)->first(); } /** * @param string $url * * @return string */ private function getURLTitle($url) { $title = ''; if (method_exists($this->owner, 'updateURLTitle')) { $title = $this->owner->updateURLTitle($url); } return $title; } } |