Skip to content

Instantly share code, notes, and snippets.

@tworzenieweb
Created March 29, 2013 10:04
Show Gist options
  • Save tworzenieweb/5270001 to your computer and use it in GitHub Desktop.
Save tworzenieweb/5270001 to your computer and use it in GitHub Desktop.
how to create movable tree interface with sonata
<?php
/*
* This file is part of the Sonata package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Luxodo\Bundle\ProductionBundle\Controller;
class CategoryController extends Controller {
public function upAction($id) {
$em = $this->getDoctrine()->getEntityManager();
$repo = $em->getRepository('LuxodoProductionBundle:Category');
$category = $repo->findOneById($id);
if ($category->getParent()) {
$repo->moveUp($category);
}
return $this->redirect($this->getRequest()->headers->get('referer'));
}
public function downAction($id) {
$em = $this->getDoctrine()->getEntityManager();
$repo = $em->getRepository('LuxodoProductionBundle:Category');
$category = $repo->findOneById($id);
if ($category->getParent()) {
$repo->moveDown($category);
}
return $this->redirect($this->getRequest()->headers->get('referer'));
}
public function childrenAction($id) {
// the key used to lookup the template
$templateKey = 'edit';
if (false === $this->admin->isGranted('CREATE')) {
throw new AccessDeniedException();
}
$object = $this->admin->getNewInstance();
$em = $this->getDoctrine()->getEntityManager();
$repo = $em->getRepository('LuxodoProductionBundle:Category');
$category = $repo->findOneById($id);
if(!$category) {
$this->createNotFoundException("Parent category with id {$id} was not found");
}
$object->setParent($category);
$this->admin->setSubject($object);
/** @var $form \Symfony\Component\Form\Form */
$form = $this->admin->getForm();
$form->setData($object);
if ($this->get('request')->getMethod() == 'POST') {
$form->bind($this->get('request'));
$isFormValid = $form->isValid();
// persist if the form was valid and if in preview mode the preview was approved
if ($isFormValid && (!$this->isInPreviewMode() || $this->isPreviewApproved())) {
$this->admin->create($object);
if ($this->isXmlHttpRequest()) {
return $this->renderJson(array(
'result' => 'ok',
'objectId' => $this->admin->getNormalizedIdentifier($object)
));
}
$this->get('session')->setFlash('sonata_flash_success','flash_create_success');
// redirect to edit mode
return $this->redirectTo($object);
}
// show an error message if the form failed validation
if (!$isFormValid) {
if (!$this->isXmlHttpRequest()) {
$this->get('session')->setFlash('sonata_flash_error', 'flash_create_error');
}
} elseif ($this->isPreviewRequested()) {
// pick the preview template if the form was valid and preview was requested
$templateKey = 'preview';
}
}
$view = $form->createView();
// set the theme for the current Admin Form
$this->get('twig')->getExtension('form')->renderer->setTheme($view, $this->admin->getFormTheme());
return $this->render($this->admin->getTemplate($templateKey), array(
'action' => 'create',
'form' => $view,
'object' => $object,
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment