Files
domokits/local/modules/Slide/Controller/BackController.php
2019-11-21 12:25:31 +01:00

291 lines
8.2 KiB
PHP

<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Slide\Controller;
use Slide\EventListeners\SlideCreateEvent;
use Slide\Model\SlideRel;
use Slide\Model\SlideRelQuery;
use Slide\Slide;
use Slide\EventListeners\SlideDeleteEvent;
use Slide\EventListeners\SlideEvent;
use Slide\EventListeners\SlideEvents;
use Slide\EventListeners\SlideUpdateEvent;
use Slide\Model\SlideItemQuery;
use Thelia\Controller\Admin\AbstractCrudController;
use Thelia\Core\Security\AccessManager;
/**
* Class BackController
* @package Slide\Controller
* @author Julien Chanséaume <jchanseaume@openstudio.fr>
*/
class BackController extends AbstractCrudController
{
protected $currentRouter = "router.slide";
public function __construct()
{
parent::__construct(
'slide',
'created_reverse',
'order',
null,
SlideEvents::SLIDE_CREATE,
SlideEvents::SLIDE_UPDATE,
SlideEvents::SLIDE_DELETE,
null, // No visibility toggle
null, // no position change
Slide::getModuleCode()
);
}
/**
* Return the creation form for this object
*/
protected function getCreationForm()
{
$form = $this->createForm('slide.create.form', 'form');
return $form;
}
/**
* Return the update form for this object
*/
protected function getUpdateForm()
{
$form = $this->createForm('slide.update.form', 'form');
return $form;
}
/**
* Hydrate the update form for this object, before passing it to the update template
*
* @param \Slide\Model\SlideItem $object
*/
protected function hydrateObjectForm($object)
{
// Prepare the data that will hydrate the form
$data = [
'id' => $object->getId(),
'content' => $object->getContent(),
'title' => $object->getTitle()
];
// Setup the object form
$form = $this->createForm('slide.update.form', 'form', $data);
return $form;
}
/**
* Creates the creation event with the provided form data
*
* @param unknown $formData
*/
protected function getCreationEvent($formData)
{
$event = $this->bindFormData(
new SlideCreateEvent(),
$formData
);
return $event;
}
/**
* Creates the update event with the provided form data
*
* @param unknown $formData
*/
protected function getUpdateEvent($formData)
{
$event = $this->bindFormData(
new SlideUpdateEvent(),
$formData
);
$event->setId($formData['id']);
$event->setContent($formData['content']);
return $event;
}
protected function bindFormData($event, $formData)
{
$event->setTitle($formData['title']);
return $event;
}
/**
* Creates the delete event with the provided form data
*/
protected function getDeleteEvent()
{
$event = new SlideDeleteEvent();
$event->setId($this->getRequest()->get('slide_id'));
return $event;
}
/**
* Return true if the event contains the object, e.g. the action has updated the object in the event.
*
* @param SlideEvent $event
*/
protected function eventContainsObject($event)
{
return null !== $event->getSlide();
}
/**
* Get the created object from an event.
*
* @param SlideEvent $event
*
* @return \Slide\Model\Slide
*/
protected function getObjectFromEvent($event)
{
return $event->getSlide();
}
/**
* Load an existing object from the database
*/
protected function getExistingObject()
{
$slide_id = $this->getRequest()->get('slide_id');
if (null === $slide_id) {
$slide_id = $this->getRequest()->attributes('slide_id');
}
return SlideItemQuery::create()->findPk($slide_id);
}
/**
* Returns the object label form the object event (name, title, etc.)
*
* @param \Slide\Model\SlideItem $object
*/
protected function getObjectLabel($object)
{
$object->getTitle();
}
/**
* Returns the object ID from the object
*
* @param \Slide\Model\SlideItem $object
*/
protected function getObjectId($object)
{
return $object->getId();
}
/**
* Render the main list template
*
* @param string $currentOrder , if any, null otherwise.
*/
protected function renderListTemplate($currentOrder)
{
return $this->render('slides', ['order' => $currentOrder]);
}
/**
* Render the edition template
*/
protected function renderEditionTemplate()
{
return $this->render(
'slide-edit',
[
'slide_id' => $this->getRequest()->get('slide_id')
]
);
}
/**
* Must return a RedirectResponse instance
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
protected function redirectToEditionTemplate()
{
return $this->generateRedirectFromRoute(
"admin.slide.slides.update",
[],
['slide_id' => $this->getRequest()->get('slide_id')]
);
}
/**
* Must return a RedirectResponse instance
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
protected function redirectToListTemplate()
{
return $this->generateRedirectFromRoute('admin.slide.slides.default');
}
public function savePositionAction($ref, $ref_id)
{
if (null !== $response = $this->checkAuth($this->resourceCode, $this->getModuleCode(), AccessManager::CREATE)) {
return $response;
}
$this->checkXmlHttpRequest();
$data = $this->getRequest()->request->all();
$message = [
"success" => true,
"message" => "ok"
];
SlideRelQuery::create()
->filterByRef($ref)
->filterByRefId($ref_id)
->delete();
foreach ($data as $slideId => $position) {
$slide = new SlideRel();
$slide
->setRef($ref)
->setRefId($ref_id)
->setSlideId($slideId)
->setPosition($position)
->save()
;
}
return $this->jsonResponse(json_encode($message));
}
}