<?php
namespace App\Controller\Dashboard\Websites;
use App\Controller\AbstractController;
use App\Controller\PaginationTrait;
use App\Doctrine\Repository\Content\LegacyProxySearch;
use App\Doctrine\Repository\Content\ObjectRepository;
use App\Doctrine\Repository\Content\ObjectSearch;
use App\Entity\Content\AbstractObject;
use App\Form\Forms\DummyForm;
use App\Form\Forms\Searching\LegacyProxySearchForm;
use App\Form\Forms\Searching\ObjectSearchForm;
use App\Model\Content\ObjectInterface;
use App\Service\Content\ContentManager;
use Cms\ModuleBundle\Service\ContentManager as LegacyContentManager;
use App\Service\Feed\FeedManager;
use Cms\ContainerBundle\Entity\Containers\GenericContainer;
use Cms\FrontendBundle\Service\ResolverManager;
use Cms\ModuleBundle\Controller\ContentController;
use Cms\ModuleBundle\Doctrine\ProxyRepository;
use Cms\ModuleBundle\Entity\Proxy;
use Cms\ModuleBundle\Service\ModuleManager;
use Doctrine\Common\Util\ClassUtils;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
/**
*
* @Route(
* "/websites/content/posts",
* )
*/
abstract class AbstractContentController extends AbstractController
{
use PaginationTrait;
protected const ENTRY_TYPE = null;
/**
*
*/
protected function listLogic(string $class, int $pagination = 0)
{
// check that class is legit
if ($class !== AbstractObject::class && ! is_subclass_of($class, AbstractObject::class)) {
throw new \LogicException();
}
// get sites
$sites = $this->getEntityManager()->getRepository(GenericContainer::class)->findAllByWithDomain();
// search form logic
$this->handleSearch(
$form = $this->createForm(
ObjectSearchForm::class,
$search = (new ObjectSearch())
->setVisibility(null)
->setAccount($this->getGlobalContext()->getEffectiveAccount())
)
);
// querying
$repo = $this->getEntityManager()->getRepository($class);
if ( ! $repo instanceof ObjectRepository) {
throw new \LogicException();
}
$items = $repo->findBySearch(
$search,
$this->getPageSize($search),
$this->getPageOffset($pagination, $search)
);
// determine if we are out of bounds on the pagination
if ($this->isPageOutOfBounds($items, $pagination, $search)) {
return $this->handlePageOutOfBounds($items, $search);
}
return $this->html([
'sites' => $sites,
'form' => $form->createView(),
'search' => $search,
'pagination' => $this->generatePagination(
$items,
$pagination,
$search
),
'items' => $items,
]);
}
/**
*
*/
protected function listLegacyLogic(string $class, int $pagination = 0)
{
// check that class is legit
if ( ! is_subclass_of($class, Proxy::class)) {
throw new \LogicException();
}
// get sites
$sites = $this->getEntityManager()->getRepository(GenericContainer::class)->findAllByWithDomain();
// search form logic
$this->handleSearch(
$form = $this->createForm(
LegacyProxySearchForm::class,
$search = (new LegacyProxySearch())
->setVisibility(null)
->setAccount($this->getGlobalContext()->getEffectiveAccount())
)
);
// querying
$repo = $this->getEntityManager()->getRepository($class);
if ( ! $repo instanceof ProxyRepository) {
throw new \LogicException();
}
$items = $repo->schoolnow(
$search,
$this->getPageSize($search),
$this->getPageOffset($pagination, $search)
);
// determine if we are out of bounds on the pagination
if ($this->isPageOutOfBounds($items, $pagination, $search)) {
return $this->handlePageOutOfBounds($items, $search);
}
return $this->html([
'sites' => $sites,
'form' => $form->createView(),
'search' => $search,
'pagination' => $this->generatePagination(
$items,
$pagination,
$search
),
'items' => $items,
]);
}
/**
*
*/
protected function createLogic(AbstractObject $object, string $form, string $jump)
{
// AUDIT
$this->denyAccessUnlessGranted(
[
sprintf('campussuite.cms.container.%s.manage', $object->getDepartment()->getType()),
'campussuite.cms.module.manage',
],
[$object, $object->getDepartment()],
);
// load department from object
if ( ! $object->getDepartment()) {
throw new \LogicException();
}
// find the school that is at play here, if any
$school = $this->getResolverManager()->getSchoolResolver()->resolveSchoolByDepartment(
$object->getDepartment()
);
// create the form
$form = $this->createForm(
$form,
$object,
[
'boosting' => ( ! empty($school)),
'boosting_locked' => ($school && $school->getDepartment() === $object->getDepartment()),
'pinning' => false,
]
);
// set boosting default
$form->get('boosted')->setData(
( ! $school) ? false : (($school->getDepartment() === $object->getDepartment())
? FeedManager::BOOSTING[ClassUtils::getClass($object)]
: true)
);
// set pinning default
$form->get('pinned')->setData(
( ! $school) ? false : (($school->getDepartment() === $object->getDepartment())
? FeedManager::PINNING[ClassUtils::getClass($object)]
: false)
);
// handle submission
if ($this->handleForm($form)) {
// set object as draft and save it
$this->getContentManager()->createObject(
$object->setVisibility(ObjectInterface::VISIBILITIES__UNPUBLISHED),
$form->get('boosted')->getData() ? FeedManager::FLAGS__BOOST__SET : FeedManager::FLAGS__BOOST__UNSET,
$form->get('pinned')->getData() ? FeedManager::FLAGS__PIN__SET : FeedManager::FLAGS__PIN__UNSET,
);
// determine if we are publishing and handle it if so
if ($form->get('action')->getData() === 'publish') {
$this->getContentManager()->publishObject(
$object,
$form->get('boosted')->getData() ? FeedManager::FLAGS__BOOST__SET : FeedManager::FLAGS__BOOST__UNSET,
$form->get('pinned')->getData() ? FeedManager::FLAGS__PIN__SET : FeedManager::FLAGS__PIN__UNSET,
);
}
$this->getLoggingService()->createLog($object);
return $this->jumpOrRedirectToRoute($jump);
}
return $this->html([
'department' => $object->getDepartment(),
'school' => $school,
'object' => $object,
'form' => $form->createView(),
]);
}
/**
*
*/
protected function updateLogic(AbstractObject $object, string $form, string $jump)
{
// AUDIT
$this->denyAccessUnlessGranted(
[
sprintf('campussuite.cms.container.%s.manage', $object->getDepartment()->getType()),
'campussuite.cms.module.manage',
],
[$object, $object->getDepartment()],
);
// find the school that is at play here, if any
$school = $this->getResolverManager()->getSchoolResolver()->resolveSchoolByDepartment(
$object->getDepartment()
);
// locate entry if any
$entry = $this->getFeedManager()->lookup($object);
if ( ! $entry) {
throw new \RuntimeException();
}
// create the form
$form = $this->createForm(
$form,
$object,
[
'boosting' => ( ! empty($school)),
'boosting_locked' => ($school && $school->getDepartment() === $object->getDepartment()),
'pinning' => false,
]
);
// set boosting default
if ($entry) {
$form->get('boosted')->setData($entry->isBoosted());
} else {
$form->get('boosted')->setData(
( ! $school) ? false : (($school->getDepartment() === $object->getDepartment())
? FeedManager::BOOSTING[ClassUtils::getClass($object)]
: false)
);
}
// set pinning default
if ($entry) {
$form->get('pinned')->setData($entry->isPinned());
} else {
$form->get('pinned')->setData(FeedManager::PINNING[ClassUtils::getClass($object)]);
//$form->get('pinned')->setData(false);
}
// handle submission
if ($this->handleForm($form)) {
// set object as draft and save it
$this->getContentManager()->updateObject(
$object,
$form->get('boosted')->getData() ? FeedManager::FLAGS__BOOST__SET : FeedManager::FLAGS__BOOST__UNSET,
$form->get('pinned')->getData() ? FeedManager::FLAGS__PIN__SET : FeedManager::FLAGS__PIN__UNSET,
);
// determine if we are publishing and handle it if so
if ($form->get('action')->getData() === 'publish') {
$this->getContentManager()->publishObject(
$object,
$form->get('boosted')->getData() ? FeedManager::FLAGS__BOOST__SET : FeedManager::FLAGS__BOOST__UNSET,
$form->get('pinned')->getData() ? FeedManager::FLAGS__PIN__SET : FeedManager::FLAGS__PIN__UNSET,
);
}
$this->getLoggingService()->createLog($object);
return $this->jumpOrRedirectToRoute($jump);
}
return $this->html([
'department' => $object->getDepartment(),
'school' => $school,
'object' => $object,
'entry' => $entry,
'form' => $form->createView(),
]);
}
/**
* @param Proxy $object
* @param string $jump
* @return RedirectResponse
*/
protected function updateLegacyLogic(Proxy $object, string $jump): RedirectResponse
{
return $this->redirectToRoute(
ContentController::ROUTES__MODIFY_CONFLICTS,
[
'container' => $object->getContainer()->getId(),
'module' => $this->getModuleManager()->getModuleConfigurationForEntity($object)->key(),
'proxy' => $object->getId(),
'redirectTo' => $jump,
]
);
}
/**
*
*/
protected function deleteLogic(AbstractObject $object, string $jump)
{
// make sure it is ajax
if ( ! $this->getRequest()->isXmlHttpRequest()) {
throw new NotFoundHttpException();
}
// AUDIT
$this->denyAccessUnlessGranted(
[
sprintf('campussuite.cms.container.%s.manage', $object->getDepartment()->getType()),
'campussuite.cms.module.manage',
],
[$object, $object->getDepartment()],
);
// create the form
$form = $this->createForm(DummyForm::class);
// handle submission
if ($this->handleForm($form)) {
// delete the object
$id = $object->getId();
$this->getContentManager()->deleteObject(
$object
);
$this->getLoggingService()->createLog($object, $id);
// send back redirect
return $this->jsonView([
'redirect' => true,
]);
}
return $this->ajax([
'object' => $object,
'form' => $form->createView(),
]);
}
/**
*
*/
protected function deleteLegacyLogic(Proxy $object, string $jump)
{
// make sure it is ajax
if ( ! $this->getRequest()->isXmlHttpRequest()) {
throw new NotFoundHttpException();
}
// create the form
$form = $this->createForm(DummyForm::class);
// handle submission
if ($this->handleForm($form)) {
// delete the object
$id = $object->getId();
$this->getLegacyContentManager()->proxyDelete(
$object
);
$this->getLoggingService()->createLog($object, $id);
// send back redirect
return $this->jsonView([
'redirect' => true,
]);
}
return $this->ajax([
'object' => $object,
'form' => $form->createView(),
]);
}
/**
* @return ResolverManager|object
*/
protected function getResolverManager(): ResolverManager
{
return $this->get(__METHOD__);
}
/**
* @return ContentManager|object
*/
protected function getContentManager(): ContentManager
{
return $this->get(__METHOD__);
}
protected function getLegacyContentManager(): LegacyContentManager
{
return $this->get(LegacyContentManager::class);
}
/**
* @return FeedManager|object
*/
protected function getFeedManager(): FeedManager
{
return $this->get(__METHOD__);
}
/**
* @return ModuleManager|object
*/
protected function getModuleManager(): ModuleManager
{
return $this->get(__METHOD__);
}
public static function getSubscribedServices(): array
{
return array_merge(
parent::getSubscribedServices(),
[
LegacyContentManager::class => '?'.LegacyContentManager::class,
],
);
}
}