Skip to content

Commit

Permalink
EZP-22654: Section create and edit
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickroger committed Oct 27, 2014
1 parent f6af3a9 commit 5745cd0
Show file tree
Hide file tree
Showing 16 changed files with 568 additions and 52 deletions.
16 changes: 15 additions & 1 deletion Controller/PjaxController.php
Expand Up @@ -10,9 +10,23 @@

use eZ\Publish\Core\MVC\Symfony\Security\User as CoreUser;
use eZ\Bundle\EzPublishCoreBundle\Controller;
use Symfony\Component\HttpFoundation\Response;

class PjaxController extends Controller
{
/**
* To be used when access is denied to a user
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function accessDeniedAction()
{
$response = new Response();
$response->setStatusCode( $this->getNoAccessStatusCode() );

return $response;
}

/**
* Returns the HTTP status code to use when the user does not have access to
* a resource so that the JS code can detect if the user needs to be
Expand All @@ -37,7 +51,7 @@ private function isAnonymous()
!$user
|| (
$user instanceof CoreUser
&& $user->id == $this->getConfigResolver()->getParameter( "anonymous_user_id" )
&& $user->getAPIUser()->id == $this->getConfigResolver()->getParameter( "anonymous_user_id" )
)
);
}
Expand Down
197 changes: 181 additions & 16 deletions Controller/SectionController.php
Expand Up @@ -8,21 +8,51 @@

namespace EzSystems\PlatformUIBundle\Controller;

use eZ\Publish\API\Repository\Exceptions\NotFoundException;
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
use Symfony\Component\HttpFoundation\Response;
use EzSystems\PlatformUIBundle\Controller\PjaxController;
use EzSystems\PlatformUIBundle\Entity\Section;
use eZ\Bundle\EzPublishCoreBundle\Controller;
use EzSystems\PlatformUIBundle\Form\Type\SectionType;
use Symfony\Component\HttpFoundation\Request;
use EzSystems\PlatformUIBundle\Helper\SectionHelperInterface;
use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Translation\TranslatorInterface;

class SectionController extends PjaxController
class SectionController extends Controller
{
/**
* @var EzSystems\PlatformUIBundle\Helper\SectionHelperInterface
* @var \EzSystems\PlatformUIBundle\Helper\SectionHelperInterface
*/
protected $sectionHelper;

public function __construct( SectionHelperInterface $sectionHelper )
/**
* @var \EzSystems\PlatformUIBundle\Form\Type\SectionType
*/
protected $sectionType;

/**
* @var \Symfony\Component\Routing\RouterInterface
*/
private $router;

/**
* @var \Symfony\Component\Translation\TranslatorInterface
*/
private $translator;

public function __construct(
SectionHelperInterface $sectionHelper,
SectionType $sectionType,
RouterInterface $router,
TranslatorInterface $translator
)
{
$this->sectionHelper = $sectionHelper;
$this->sectionType = $sectionType;
$this->router = $router;
$this->translator = $translator;
}

/**
Expand All @@ -32,33 +62,29 @@ public function __construct( SectionHelperInterface $sectionHelper )
*/
public function listAction()
{
$response = new Response();
try
{
return $this->render(
'eZPlatformUIBundle:Section:list.html.twig',
array(
'sectionInfoList' => $this->sectionHelper->getSectionList(),
'canCreate' => $this->sectionHelper->canCreate(),
),
$response
)
);
}
catch ( UnauthorizedException $e )
{
$response->setStatusCode( $this->getNoAccessStatusCode() );
return $this->forward( 'eZPlatformUIBundle:Pjax:accessDenied' );
}
return $response;
}

/**
* Renders the view of a section
* @param int $sectionId
* @param mixed $sectionId
* @return \Symfony\Component\HttpFoundation\Response
*/
public function viewAction( $sectionId )
{
$response = new Response();
try
{
$section = $this->sectionHelper->loadSection( $sectionId );
Expand All @@ -68,14 +94,153 @@ public function viewAction( $sectionId )
array(
'section' => $section,
'contentCount' => $contentCount,
),
$response
)
);
}
catch ( UnauthorizedException $e )
{
$response->setStatusCode( $this->getNoAccessStatusCode() );
return $this->forward( 'eZPlatformUIBundle:Pjax:accessDenied' );
}
return $response;
}

/**
* Displays the create form and processes it once submitted.
*
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function createAction( Request $request)
{
$section = new Section();

$form = $this->createForm(
$this->sectionType,
$section,
array(
'action' => $this->router->generate( 'admin_sectioncreate' ),
)
);

$form->handleRequest( $request );

if ( $form->isValid() )
{
try
{
$newSection = $this->sectionHelper->createSection( $section );

return $this->redirect(
$this->generateUrl(
'admin_sectionview',
array( 'sectionId' => $newSection->id )
)
);
}
catch ( UnauthorizedException $e )
{
return $this->forward( 'eZPlatformUIBundle:Pjax:accessDenied' );
}
catch ( InvalidArgumentException $e )
{
$this->addAlreadyExistErrorMessage();
}
}

return $this->render(
'eZPlatformUIBundle:Section:create.html.twig',
array( 'form' => $form->createView() )
);
}

/**
* Displays the edit form and processes it once submitted.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @param mixed $sectionId
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function editAction( Request $request, $sectionId )
{
try
{
$sectionToUpdate = $this->sectionHelper->loadSection( $sectionId );
}
catch ( UnauthorizedException $e )
{
return $this->forward( 'eZPlatformUIBundle:Pjax:accessDenied' );
}
catch ( NotFoundException $e )
{
$response = new Response();
$response->setStatusCode( 404 );

return $this->render(
'eZPlatformUIBundle:Section:not_found.html.twig',
array( 'sectionId' => $sectionId ),
$response
);
}

// Loading API data
$section = new Section();
$section->identifier = $sectionToUpdate->identifier;
$section->name = $sectionToUpdate->name;

$form = $this->createForm(
$this->sectionType,
$section,
array(
'action' => $this->router->generate(
'admin_sectionedit', array( 'sectionId' => $sectionId )
)
)
);

$form->handleRequest( $request );

if ( $form->isValid() )
{
try
{
$updatedSection = $this->sectionHelper->updateSection( $sectionToUpdate, $section );

return $this->redirect(
$this->generateUrl(
'admin_sectionview',
array( 'sectionId' => $updatedSection->id )
)
);
}
catch ( UnauthorizedException $e )
{
return $this->forward( 'eZPlatformUIBundle:Pjax:accessDenied' );
}
catch ( InvalidArgumentException $e )
{
$this->addAlreadyExistErrorMessage();
}
}

return $this->render(
'eZPlatformUIBundle:Section:edit.html.twig',
array( 'form' => $form->createView() )
);
}

/**
* Adds a "Section already exists" message to the flashbag.
*/
private function addAlreadyExistErrorMessage()
{
$this->get( 'session' )->getFlashBag()->add(
'error',
$this->translator->trans(
'section.error.id_already_exist',
array(),
'section'
)
);
}
}
13 changes: 6 additions & 7 deletions Controller/SystemInfoController.php
Expand Up @@ -10,11 +10,11 @@

use eZ\Publish\Core\MVC\Symfony\Security\Authorization\Attribute as AuthorizationAttribute;
use Symfony\Component\HttpFoundation\Response;
use eZ\Bundle\EzPublishCoreBundle\Controller;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use EzSystems\PlatformUIBundle\Controller\PjaxController;
use EzSystems\PlatformUIBundle\Helper\SystemInfoHelperInterface;

class SystemInfoController extends PjaxController
class SystemInfoController extends Controller
{
/**
* @var \EzSystems\PlatformUIBundle\Helper\SystemInfoHelperInterface
Expand All @@ -33,26 +33,25 @@ public function __construct( SystemInfoHelperInterface $systemInfoHelper )
*/
public function infoAction()
{
$response = new Response();
if ( !$this->hasAccess() )
{
$response->setStatusCode( $this->getNoAccessStatusCode() );
return $response;
return $this->forward( 'eZPlatformUIBundle:Pjax:accessDenied' );
}

return $this->render(
'eZPlatformUIBundle:SystemInfo:info.html.twig',
array(
'ezplatformInfo' => $this->systemInfoHelper->getEzPlatformInfo(),
'systemInfo' => $this->systemInfoHelper->getSystemInfo(),
),
$response
)
);
}

/**
* Renders a PHP info page
*
* @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function phpinfoAction()
Expand Down
63 changes: 63 additions & 0 deletions Entity/EnrichedSection.php
@@ -0,0 +1,63 @@
<?php
/**
* File containing the EnrichedSection class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
*/

namespace EzSystems\PlatformUIBundle\Entity;

use eZ\Publish\API\Repository\Values\Content\Section;

/**
* Class EnrichedSection
*
* This class is a container for a Section and extra information
*
* @package EzSystems\PlatformUIBundle\Entity
*/
class EnrichedSection
{
/**
* @var \eZ\Publish\API\Repository\Values\Content\Section
*/
public $section;

/**
* @var int
*/
public $contentCount;

/**
* @var bool
*/
public $canEdit;

/**
* @var bool
*/
public $canDelete;

/**
* @var bool
*/
public $canAssign;

/**
* @param \eZ\Publish\API\Repository\Values\Content\Section $section
* @param int $contentCount
* @param bool $canEdit
* @param bool $canDelete
* @param bool $canAssign
*/
public function __construct( Section $section, $contentCount, $canEdit, $canDelete, $canAssign )
{
$this->section = $section;
$this->contentCount = $contentCount;
$this->canEdit = $canEdit;
$this->canDelete = $canDelete;
$this->canAssign = $canAssign;
}
}

0 comments on commit 5745cd0

Please sign in to comment.