Skip to content

Commit

Permalink
Merge pull request #2 from tomphp/feature/scud-controller
Browse files Browse the repository at this point in the history
Feature/scud controller
  • Loading branch information
Tom Oram committed Jul 5, 2013
2 parents c594349 + 180eea4 commit 34e859f
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 128 deletions.
128 changes: 2 additions & 126 deletions src/SclZfSingleEntityController/Controller/SingleEntityController.php
Expand Up @@ -4,16 +4,12 @@

use SclZfUtilities\Mapper\GenericMapperInterface;
use Zend\Mvc\Controller\AbstractActionController as ZendActionController;
use SclZfSingleEntityController\Exception\NoEntityException;
use SclZfSingleEntityController\Exception\InvalidArgumentException;

/**
* A generic controller which provides add, edit, delete & view actions for a
* single entity type.
* A generic controller which implements SingleEntityControllerInterface.
*
* @author Tom Oram
* @todo Create trait for SingleEntityControllerInterface implementation.
* @todo Add success/failure messages.
*/
class SingleEntityController extends ZendActionController implements
SingleEntityControllerInterface
Expand Down Expand Up @@ -66,11 +62,7 @@ class SingleEntityController extends ZendActionController implements
*
* @var array
*/
protected $entityRequiredActions = array(
'edit',
'view',
'delete',
);
protected $entityRequiredActions = array();

/**
* Set the mapper for the controller.
Expand Down Expand Up @@ -228,120 +220,4 @@ protected function redirectToRoute($route, $params = array())
{
return $this->redirect()->toRoute($route, $params);
}

/*
* Controller actions.
*/

/**
* Display a searchable list of entities.
*
* @return array
*/
public function indexAction()
{
return array(
$this->listVariable => $this->getSearchable(
$this->getMapper(),
'domainSI',
'findAll'
)
);
}

/**
* Display a form to add a new entity.
*
* @return array
*/
public function addAction()
{
$entity = $this->getMapper()->create();

$form = $this->getFormBuilder()->createForm($entity, 'Add');

//$form->remove('id')->getInputFilter()->remove('id');

if ($this->getFormBuilder()->save($entity, $form)) {
return $this->redirectToRoute($this->redirectRoute);
}

return array('form' => $form);
}

/**
* Display a form to edit an entity.
*
* @return array
* @throws NoEntityException When no entity is loaded.
*/
public function editAction()
{
$entity = $this->getEntity();

if (null == $entity) {
throw new NoEntityException(__METHOD__);
}

$form = $this->getFormBuilder()->createForm($entity, 'Edit');

if ($this->getFormBuilder()->save($entity, $form)) {
return $this->redirectToRoute($this->redirectRoute);
}

return array(
'id' => $entity->getId(), // @todo Enforce the existence of getId()
'form' => $form
);
}

/**
* Display an entity.
*
* @return array
* @throws NoEntityException When no entity is loaded.
*/
public function viewAction()
{
$entity = $this->getEntity();

if (null == $entity) {
throw new NoEntityException(__METHOD__);
}

return array($this->viewVariable => $entity);
}

/**
* Prompt the user to find if they want to delete an entity.
*
* @return array
* @throws NoEntityException When no entity is loaded.
* @todo Add checks that delete requests are valid.
*/
public function deleteAction()
{
$entity = $this->getEntity();

if (null == $entity) {
throw new NoEntityException(__METHOD__);
}

$request = $this->getRequest();

if ($request->isPost()) {
$del = $request->getPost()->get('del', 'No');

if ($del == 'Yes') {
$this->getMapper()->delete($entity);
}

return $this->redirectToRoute($this->redirectRoute);
}

return array(
'id' => $entity->getId(),
$this->viewVariable => $entity
);
}
}
@@ -0,0 +1,140 @@
<?php

namespace SclZfSingleEntityController\Controller;

use SclZfUtilities\Mapper\GenericMapperInterface;
use SclZfSingleEntityController\Exception\NoEntityException;

/**
* A generic controller which provides add, edit, delete & view actions for a
* single entity type.
*
* @author Tom Oram
* @todo Add success/failure messages.
*/
class SingleScudEntityController extends SingleEntityController implements
SingleEntityControllerInterface
{
/**
* A list of actions which require the entity to be pre-loaded.
*
* @var array
*/
protected $entityRequiredActions = array(
'edit',
'view',
'delete',
);

/**
* Display a searchable list of entities.
*
* @return array
*/
public function indexAction()
{
return array(
$this->listVariable => $this->getSearchable(
$this->getMapper(),
'domainSI',
'findAll'
)
);
}

/**
* Display a form to add a new entity.
*
* @return array
*/
public function addAction()
{
$entity = $this->getMapper()->create();

$form = $this->getFormBuilder()->createForm($entity, 'Add');

//$form->remove('id')->getInputFilter()->remove('id');

if ($this->getFormBuilder()->save($entity, $form)) {
return $this->redirectToRoute($this->redirectRoute);
}

return array('form' => $form);
}

/**
* Display a form to edit an entity.
*
* @return array
* @throws NoEntityException When no entity is loaded.
*/
public function editAction()
{
$entity = $this->getEntity();

if (null == $entity) {
throw new NoEntityException(__METHOD__);
}

$form = $this->getFormBuilder()->createForm($entity, 'Edit');

if ($this->getFormBuilder()->save($entity, $form)) {
return $this->redirectToRoute($this->redirectRoute);
}

return array(
'id' => $entity->getId(), // @todo Enforce the existence of getId()
'form' => $form
);
}

/**
* Display an entity.
*
* @return array
* @throws NoEntityException When no entity is loaded.
*/
public function viewAction()
{
$entity = $this->getEntity();

if (null == $entity) {
throw new NoEntityException(__METHOD__);
}

return array($this->viewVariable => $entity);
}

/**
* Prompt the user to find if they want to delete an entity.
*
* @return array
* @throws NoEntityException When no entity is loaded.
* @todo Add checks that delete requests are valid.
*/
public function deleteAction()
{
$entity = $this->getEntity();

if (null == $entity) {
throw new NoEntityException(__METHOD__);
}

$request = $this->getRequest();

if ($request->isPost()) {
$del = $request->getPost()->get('del', 'No');

if ($del == 'Yes') {
$this->getMapper()->delete($entity);
}

return $this->redirectToRoute($this->redirectRoute);
}

return array(
'id' => $entity->getId(),
$this->viewVariable => $entity
);
}
}
Expand Up @@ -76,7 +76,8 @@ public function testDetach() {
->with(
$this->equalTo('Zend\Mvc\Controller\AbstractActionController'),
$this->equalTo($callback)
);
)
->will($this->returnValue(true));

$this->listener->detachShared($events);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/SclZfSingleEntityControllerTests/ModuleTest.php
Expand Up @@ -101,7 +101,7 @@ public function testGetAutoloaderConfig()
/**
* Check the service manager config.
*
* @covers SclZfSingleEntityController\Module::getAutoloaderConfig
* @covers SclZfSingleEntityController\Module::getServiceConfig
*
* @return void
*/
Expand Down

0 comments on commit 34e859f

Please sign in to comment.