Zwischenstand

This commit is contained in:
Sven Wappler
2021-08-20 13:33:13 +02:00
parent ce6b9e38dc
commit 508d3d2759
32 changed files with 2807 additions and 602 deletions

View File

@@ -0,0 +1,41 @@
<?php
namespace WapplerSystems\BookmarksLikesRatings\Controller;
use TYPO3\CMS\Frontend\Exception;
use WapplerSystems\BookmarksLikesRatings\Domain\Repository\BookmarkRepository;
use WapplerSystems\BookmarksLikesRatings\Domain\Repository\LikeRepository;
class AbstractController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
/** @var BookmarkRepository */
protected $bookmarkRepository;
/**
* @param \WapplerSystems\BookmarksLikesRatings\Domain\Repository\BookmarkRepository $bookmarkRepository
*/
public function injectBookmarkRepository(BookmarkRepository $bookmarkRepository) {
$this->bookmarkRepository = $bookmarkRepository;
}
/** @var LikeRepository */
protected $likeRepository;
/**
* @param \WapplerSystems\BookmarksLikesRatings\Domain\Repository\LikeRepository $bookmarkRepository
*/
public function injectLikeRepository(LikeRepository $likeRepository) {
$this->likeRepository = $likeRepository;
}
protected function getCurrentUser() : array {
if (!$GLOBALS['TSFE']->fe_user) {
throw new Exception('no access');
}
return $GLOBALS['TSFE']->fe_user->user;
}
}

View File

@@ -0,0 +1,132 @@
<?php
namespace WapplerSystems\BookmarksLikesRatings\Controller;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Utility\DebugUtility;
use TYPO3\CMS\Extbase\Mvc\View\JsonView;
use WapplerSystems\BookmarksLikesRatings\Domain\Model\Bookmark;
use WapplerSystems\BookmarksLikesRatings\Domain\Model\Bookmarks;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Plugin controller
*/
class BookmarkController extends AbstractController
{
public function initializeStatusAction()
{
$this->defaultViewObjectName = JsonView::class;
}
/**
* @param int $objectUid
* @param string $tablename
* @throws \TYPO3\CMS\Frontend\Exception
*/
public function statusAction(int $objectUid,string $tablename) {
$user = $this->getCurrentUser();
$this->view->setVariablesToRender(['status']);
$this->view->assignMultiple([
'status' => ['status' => $this->bookmarkRepository->countByUserTablenameObjectUid($user['uid'],$tablename,$objectUid) === 0? 'false':'true']
]);
}
public function initializeToggleAction()
{
$this->defaultViewObjectName = JsonView::class;
}
/**
* @param int $objectUid
* @param string $tablename
* @throws \TYPO3\CMS\Frontend\Exception
*/
public function toggleAction(int $objectUid, string $tablename)
{
$user = $this->getCurrentUser();
$this->view->setVariablesToRender(['status']);
if ($this->bookmarkRepository->countByUserTablenameObjectUid($user['uid'], $tablename, $objectUid) === 0) {
$bookmark = new Bookmark();
$bookmark->setUser($user['uid']);
$bookmark->setTablename($tablename);
$bookmark->setObjectUid($objectUid);
$this->bookmarkRepository->add($bookmark);
$this->view->assignMultiple([
'status' => ['status' => 'true']
]);
} else {
$this->bookmarkRepository->removeByUserTablenameObjectUid($user['uid'], $tablename, $objectUid);
$this->view->assignMultiple([
'status' => ['status' => 'false']
]);
}
}
/**
* @param int $objectUid
* @param string $tablename
* @throws \TYPO3\CMS\Frontend\Exception
*/
public function deleteAction(int $objectUid, string $tablename)
{
$user = $this->getCurrentUser();
$this->bookmarkRepository->removeByUserTablenameObjectUid($user['uid'], $tablename, $objectUid);
$this->forward('personalList');
}
/**
*/
public function personalListAction()
{
$user = $this->getCurrentUser();
$bookmarks = [];
$objs = $this->bookmarkRepository->findByUser($user['uid']);
/** @var Bookmark $obj */
foreach ($objs as $obj) {
$bookmark = [];
$bookmark['tablename'] = $obj->getTablename();
$bookmark['object_uid'] = $obj->getObjectUid();
if ($obj->getTablename() === 'pages') {
$page = BackendUtility::getRecord('pages',$obj->getObjectUid(),'title');
if ($page) {
$bookmark['title'] = $page['title'];
$bookmark['url'] = $this->uriBuilder->reset()->setTargetPageUid($obj->getObjectUid())->buildFrontendUri();
}
$bookmarks[] = $bookmark;
}
}
$this->view->assignMultiple([
'bookmarks' => $bookmarks
]);
}
}

View File

@@ -1,109 +0,0 @@
<?php
/*
* This file is part of the package buepro/bookmark_pages.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/
namespace WapplerSystems\BookmarksLikesRatings\Controller;
use WapplerSystems\BookmarksLikesRatings\Model\Bookmark;
use WapplerSystems\BookmarksLikesRatings\Model\Bookmarks;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
/**
* Plugin controller
*/
class BookmarksController extends ActionController
{
/**
* display bookmarks list
*/
public function indexAction()
{
$bookmark = Bookmark::createFromCurrent();
$this->view->assignMultiple([
'bookmark' => $bookmark->toArray()
]);
}
/**
* Adds the current page as bookmark and renders/returns updated list as html
*
* This is meant to be called by ajax (typoscript_rendering)
*
* @param array $localBookmarks
*/
public function bookmarkAction($localBookmarks = [])
{
// use the parameter directly and ignore chash because url is submitted by JS
$url = GeneralUtility::_GP('url');
$url = $url ? $url : null;
$bookmark = Bookmark::createFromCurrent($url);
$bookmarks = new Bookmarks();
$bookmarks->merge($localBookmarks);
$bookmarks->addBookmark($bookmark);
$bookmarks->persist();
$this->updateAndSendList($bookmarks);
}
/**
* Remove a bookmark from list and renders/returns updated list as html
*
* This is meant to be called by ajax (typoscript_rendering)
*
* @param string $id
* @param array $localBookmarks
*/
public function deleteAction($id = '', $localBookmarks = [])
{
$bookmarks = new Bookmarks();
$bookmarks->merge($localBookmarks);
if ($id) {
$bookmarks->removeBookmark($id);
$bookmarks->persist();
}
$this->updateAndSendList($bookmarks);
}
/**
* Action to get bookmark list
*
* @param array $localBookmarks
*/
public function listEntriesAction($localBookmarks = [])
{
$bookmarks = new Bookmarks();
$bookmarks->merge($localBookmarks);
$this->updateAndSendList($bookmarks);
}
/**
* This is for ajax requests
*
* @param Bookmarks $bookmarks
*/
public function updateAndSendList(Bookmarks $bookmarks)
{
// check if we bookmarked the current page
$bookmark = Bookmark::createFromCurrent();
$isBookmarked = $bookmarks->bookmarkExists($bookmark);
// build the ajax response data
$response = [
'isBookmarked' => $isBookmarked,
'bookmarks' => $bookmarks->getBookmarksForLocalStorage()
];
header('Content-Type: application/json');
echo json_encode($response);
die();
}
}

File diff suppressed because it is too large Load Diff