Skip to content

Commit

Permalink
EZP-22655: Remove sections
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickroger committed Nov 20, 2014
1 parent 3483981 commit 64ea426
Show file tree
Hide file tree
Showing 12 changed files with 285 additions and 42 deletions.
52 changes: 50 additions & 2 deletions Controller/SectionController.php
Expand Up @@ -12,6 +12,8 @@
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
use EzSystems\PlatformUIBundle\Entity\Section;
use eZ\Bundle\EzPublishCoreBundle\Controller;
use EzSystems\PlatformUIBundle\Entity\SectionList;
use EzSystems\PlatformUIBundle\Form\Type\SectionListType;
use EzSystems\PlatformUIBundle\Form\Type\SectionType;
use Symfony\Component\HttpFoundation\Request;
use EzSystems\PlatformUIBundle\Helper\SectionHelperInterface;
Expand All @@ -32,6 +34,11 @@ class SectionController extends Controller
*/
protected $sectionType;

/**
* @var \EzSystems\PlatformUIBundle\Form\Type\SectionListType
*/
protected $sectionListType;

/**
* @var \Symfony\Component\Routing\RouterInterface
*/
Expand All @@ -46,13 +53,15 @@ public function __construct(
SectionHelperInterface $sectionHelper,
SectionType $sectionType,
RouterInterface $router,
TranslatorInterface $translator
TranslatorInterface $translator,
SectionListType $sectionListType
)
{
$this->sectionHelper = $sectionHelper;
$this->sectionType = $sectionType;
$this->router = $router;
$this->translator = $translator;
$this->sectionListType = $sectionListType;
}

/**
Expand All @@ -67,8 +76,8 @@ public function listAction()
return $this->render(
'eZPlatformUIBundle:Section:list.html.twig',
array(
'sectionInfoList' => $this->sectionHelper->getSectionList(),
'canCreate' => $this->sectionHelper->canCreate(),
'form' => $this->generateDeleteForm( new SectionList() )->createView()
)
);
}
Expand All @@ -78,6 +87,45 @@ public function listAction()
}
}

/**
* Deletes sections
*
* @param Request $request
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function deleteAction( Request $request )
{
$sectionListToDelete = new SectionList();
$form = $this->generateDeleteForm( $sectionListToDelete );
$form->handleRequest( $request );

if ( $form->isValid() )
{
$this->sectionHelper->deleteSectionList( $sectionListToDelete );
}

return $this->redirect( $this->generateUrl( 'admin_sectionlist' ) );
}

/**
* Generate the form object used to delete sections
*
* @param \EzSystems\PlatformUIBundle\Entity\SectionList $sectionListToDelete sections to be populated/deleted
*
* @return \Symfony\Component\Form\Form
*/
private function generateDeleteForm( SectionList $sectionListToDelete )
{
return $this->createForm(
$this->sectionListType,
$sectionListToDelete,
array(
'action' => $this->router->generate( 'admin_sectiondelete' )
)
);
}

/**
* Renders the view of a section
* @param mixed $sectionId
Expand Down
24 changes: 24 additions & 0 deletions Entity/SectionList.php
@@ -0,0 +1,24 @@
<?php
/**
* File containing the SectionList 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;

/**
* Class SectionList
*
* Entity class used by the section list form
*
*/
class SectionList
{
/**
* @var array of sections ids
*/
public $ids = array();
}
19 changes: 11 additions & 8 deletions Entity/EnrichedSection.php → Entity/SectionListItem.php
@@ -1,6 +1,6 @@
<?php
/**
* File containing the EnrichedSection class.
* File containing the SectionListItem 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.
Expand All @@ -9,21 +9,22 @@

namespace EzSystems\PlatformUIBundle\Entity;

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

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

/**
* @var int
Expand Down Expand Up @@ -52,9 +53,11 @@ class EnrichedSection
* @param bool $canDelete
* @param bool $canAssign
*/
public function __construct( Section $section, $contentCount, $canEdit, $canDelete, $canAssign )
public function __construct( ApiSection $section, $contentCount, $canEdit, $canDelete, $canAssign )
{
$this->section = $section;
$this->id = $section->id;
$this->name = $section->name;
$this->identifier = $section->identifier;
$this->contentCount = $contentCount;
$this->canEdit = $canEdit;
$this->canDelete = $canDelete;
Expand Down
78 changes: 78 additions & 0 deletions Form/Type/SectionListType.php
@@ -0,0 +1,78 @@
<?php
/**
* File containing the SectionListType 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\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use EzSystems\PlatformUIBundle\Helper\SectionHelperInterface;
use Symfony\Component\Translation\TranslatorInterface;

/**
* Class SectionListType
*
* @package EzSystems\PlatformUIBundle\Form\Type
*/
class SectionListType extends AbstractType
{
/**
* @var \EzSystems\PlatformUIBundle\Helper\SectionHelperInterface
*/
protected $sectionHelper;

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

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

public function buildForm( FormBuilderInterface $builder, array $options )
{
$sectionList = $this->sectionHelper->getSectionList();

$submitLabel = $this->translator->trans(
'section.remove.selected',
array(),
'section'
);

$builder
->add(
'ids',
'choice',
array(
'choices' => array( $sectionList ),
'multiple' => true,
'expanded' => true
)
)
->add( 'delete', 'submit', array( 'label' => $submitLabel ) );
}

public function getName()
{
return 'sectionlist';
}

public function setDefaultOptions( OptionsResolverInterface $resolver )
{
$resolver->setDefaults(
array( 'data_class' => 'EzSystems\PlatformUIBundle\Entity\SectionList' )
);
}
}
19 changes: 18 additions & 1 deletion Form/Type/SectionType.php
Expand Up @@ -12,15 +12,32 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Translation\TranslatorInterface;

class SectionType extends AbstractType
{
/**
* @var \Symfony\Component\Translation\TranslatorInterface
*/
private $translator;

public function __construct( TranslatorInterface $translator )
{
$this->translator = $translator;
}

public function buildForm( FormBuilderInterface $builder, array $options )
{
$submitLabel = $this->translator->trans(
'section.create.submit',
array(),
'section'
);

$builder
->add( 'name', 'text' )
->add( 'identifier', 'text' )
->add( 'save', 'submit' );
->add( 'save', 'submit', array( 'label' => $submitLabel ) );
}

public function getName()
Expand Down
27 changes: 26 additions & 1 deletion Helper/SectionHelper.php
Expand Up @@ -11,6 +11,8 @@
use eZ\Publish\API\Repository\SectionService;
use eZ\Publish\Core\MVC\Symfony\Security\Authorization\Attribute as AuthorizationAttribute;
use EzSystems\PlatformUIBundle\Entity\EnrichedSection;
use EzSystems\PlatformUIBundle\Entity\SectionList;
use EzSystems\PlatformUIBundle\Entity\SectionListItem;
use Symfony\Component\Security\Core\SecurityContextInterface;
use eZ\Publish\API\Repository\Values\Content\Section;
use EzSystems\PlatformUIBundle\Entity\Section as SectionEntity;
Expand Down Expand Up @@ -44,7 +46,8 @@ public function getSectionList()
$list = array();
foreach ( $sections as $section )
{
$list[] = new EnrichedSection(
/** @var $section Section */
$list[$section->id] = new SectionListItem(
$section,
$this->sectionService->countAssignedContents( $section ),
$this->canUser( 'edit' ),
Expand Down Expand Up @@ -120,4 +123,26 @@ public function updateSection( Section $sectionToUpdate, SectionEntity $section)

return $this->sectionService->updateSection( $sectionToUpdate, $sectionUpdateStruct );
}

/**
* {@inheritDoc}
*/
public function deleteSectionList( SectionList $sectionList )
{
foreach ( $sectionList->ids as $sectionId )
{
$this->deleteSection( $sectionId );
}
}

/**
* Removes the section having a given section id
*
* @param mixed $sectionId to be deleted
*/
private function deleteSection( $sectionId )
{
$section = $this->sectionService->loadSection( $sectionId );
$this->sectionService->deleteSection( $section );
}
}
11 changes: 10 additions & 1 deletion Helper/SectionHelperInterface.php
Expand Up @@ -10,6 +10,7 @@

use eZ\Publish\API\Repository\Values\Content\Section;
use EzSystems\PlatformUIBundle\Entity\Section as SectionEntity;
use EzSystems\PlatformUIBundle\Entity\SectionList;

/**
* Interface SectionHelperInterface
Expand All @@ -23,7 +24,7 @@ interface SectionHelperInterface
/**
* Returns the section list
*
* @return \EzSystems\PlatformUIBundle\Entity\EnrichedSection[]
* @return array[] key is section id and value a \EzSystems\PlatformUIBundle\Entity\SectionListItem
*/
public function getSectionList();

Expand Down Expand Up @@ -82,4 +83,12 @@ public function createSection( SectionEntity $section );
*/
public function updateSection( Section $sectionToUpdate, SectionEntity $section );

/**
* Removes sections
*
* @param \EzSystems\PlatformUIBundle\Entity\SectionList $sectionList sections to be deleted
*
*/
public function deleteSectionList( SectionList $sectionList );

}
7 changes: 6 additions & 1 deletion Resources/config/routing.yml
Expand Up @@ -58,4 +58,9 @@ admin_sectionedit:
defaults:
_controller: ezsystems.platformui.controller.section:editAction
requirements:
sectionId: \d+
sectionId: \d+

admin_sectiondelete:
path: /pjax/section/delete
defaults:
_controller: ezsystems.platformui.controller.section:deleteAction
13 changes: 12 additions & 1 deletion Resources/config/services.yml
Expand Up @@ -7,6 +7,7 @@ parameters:
ezsystems.platformui.controller.section.class: EzSystems\PlatformUIBundle\Controller\SectionController
ezsystems.platformui.controller.template.class: EzSystems\PlatformUIBundle\Controller\TemplateController
ezsystems.platformui.form.type.section.class: EzSystems\PlatformUIBundle\Form\Type\SectionType
ezsystems.platformui.form.type.section_list.class: EzSystems\PlatformUIBundle\Form\Type\SectionListType
ezsystems.platformui.controller.pjax.class: EzSystems\PlatformUIBundle\Controller\PjaxController

services:
Expand Down Expand Up @@ -50,7 +51,7 @@ services:
- @ezsystems.platformui.form.type.section
- @router
- @translator
- @session
- @ezsystems.platformui.form.type.section_list
parent: ezpublish.controller.base

ezsystems.platformui.helper.section:
Expand All @@ -63,3 +64,13 @@ services:
class: %ezsystems.platformui.form.type.section.class%
tags:
- { name: form.section, alias: section }
arguments:
- @translator

ezsystems.platformui.form.type.section_list:
class: %ezsystems.platformui.form.type.section_list.class%
tags:
- { name: form.section, alias: section_list }
arguments:
- @ezsystems.platformui.helper.section
- @translator

0 comments on commit 64ea426

Please sign in to comment.