Skip to content

Instantly share code, notes, and snippets.

@diego-mi
Created January 7, 2016 19:40
Show Gist options
  • Select an option

  • Save diego-mi/c8aa12ccdab96c5affeb to your computer and use it in GitHub Desktop.

Select an option

Save diego-mi/c8aa12ccdab96c5affeb to your computer and use it in GitHub Desktop.
<?php
namespace Base\Controller;
use RuntimeException;
use Zend\Form\Form;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Paginator\Paginator;
use Zend\Paginator\Adapter\ArrayAdapter;
abstract class AbstractController extends AbstractActionController
{
protected $em;
protected $entity;
protected $controller;
protected $route;
protected $strService;
protected $strForm;
abstract function __construct();
/**
* Lista Resultados
*
* @return array|ViewModel
*/
public function indexAction()
{
$list = $this->getEm()->getRepository($this->entity)->findAll();
$page = $this->params()->fromRoute('page');
$paginator = new Paginator(new ArrayAdapter($list));
$paginator->setCurrentPageNumber($page)
->setDefaultItemCountPerPage(10);
if ($this->flashMessenger()->hasSuccessMessages()) {
return new ViewModel(array(
'data' => $paginator,
'page' => $page,
'success' => $this->flashMessenger()->getSuccessMessages()
));
}
if ($this->flashMessenger()->hasErrorMessages()) {
return new ViewModel(array(
'data' => $paginator,
'page' => $page,
'error' => $this->flashMessenger()->getErrorMessages()
));
}
return new ViewModel(array('data' => $paginator, 'page' => $page));
}
/**
* Inserir Registro
*/
public function inserirAction()
{
if (is_string($this->form)) {
$form = new $this->form;
} else {
$form = $this->form;
}
$request = $this->getRequest();
if ($request->isPost()) {
$form->setData($request->getPost());
if ($form->isValid()) {
$service = $this->getServiceLocator()->get($this->service);
if ($service->save($request->getPost()->toArray())) {
$this->flashMessenger()->addSuccessMessage('Cadastrado com sucesso!');
} else {
$this->flashMessenger()->addErrorMessage('Não foi possivel cadastrar! Tente mais tarde.');
}
return $this->redirect()
->toRoute($this->route, array('controller' => $this->controller, 'action' => 'inserir'));
}
}
if ($this->flashMessenger()->hasSuccessMessages()) {
return new ViewModel(array(
'form' => $form,
'success' => $this->flashMessenger()->getSuccessMessages()
));
}
if ($this->flashMessenger()->hasErrorMessages()) {
return new ViewModel(array(
'form' => $form,
'error' => $this->flashMessenger()->getErrorMessages()
));
}
$this->flashMessenger()->clearMessages();
return new ViewModel(array('form' => $form));
}
/**
* Edita um registro
*/
public function editarAction()
{
$form = $this->getForm();
$objRequest = $this->getRequest();
$intIdFromRoute = $this->params()->fromRoute('id', 0);
// if (is_string($this->form))
// $form = new $this->form;
// else
// $form = $this->form;
//
// $request = $this->getRequest();
// $param = $this->params()->fromRoute('id', 0);
$intIdFromRoute = $this->params()->fromRoute('id', 0);
$service = $this->getService();
$entityForEdit = $service->getRepository($this->entity)->find($intIdFromRoute);
if ($entityForEdit) {
$form->setData($entityForEdit->toArray());
if (($objRequest->isPost()) && ($form->setData($objRequest->getPost())->isValid())) {
$arrDataForEdit = $form->getData();
$arrDataForEdit['id'] = (int)$intIdFromRoute;
if ($service->save($arrDataForEdit)) {
$this->flashMessenger()->addSuccessMessage('Atualizado com sucesso!');
} else {
$this->flashMessenger()->addErrorMessage('Não foi possivel atualizar! Tente mais tarde.');
}
return $this->redirect()->toRoute(
$this->route,
array(
'controller' => $this->controller,
'action' => 'editar',
'id' => $intIdFromRoute
)
);
}
} else {
$this->flashMessenger()->addInfoMessage('Registro não foi encontrado!');
return $this->redirect()->toRoute($this->route, array('controller' => $this->controller));
}
if ($this->flashMessenger()->hasSuccessMessages()) {
return new ViewModel(array(
'form' => $form,
'success' => $this->flashMessenger()->getSuccessMessages(),
'id' => $param
));
}
if ($this->flashMessenger()->hasErrorMessages()) {
return new ViewModel(array(
'form' => $form,
'error' => $this->flashMessenger()->getErrorMessages(),
'id' => $param
));
}
if ($this->flashMessenger()->hasInfoMessages()) {
return new ViewModel(array(
'form' => $form,
'warning' => $this->flashMessenger()->getInfoMessages(),
'id' => $param
));
}
$this->flashMessenger()->clearMessages();
return new ViewModel(array('form' => $form, 'id' => $param));
}
/**
* Exclui um registro
*
* @return \Zend\Http\Response
*/
public function excluirAction()
{
$service = $this->getServiceLocator()->get($this->service);
$id = $this->params()->fromRoute('id', 0);
if ($service->remove(array('id' => $id))) {
$this->flashMessenger()->addSuccessMessage('Resistro deletado com sucesso!');
} else {
$this->flashMessenger()->addErrorMessage('Não foi possivel deletar o registro!');
}
return $this->redirect()->toRoute($this->route, array('controller' => $this->controller));
}
/**
* Inicializar formulario
*
* @param string $strNamespaceForm Namespaces do formulario a inicializar
*
* @return \Zend\Form\Form
*/
public function getForm(String $strNamespaceForm)
{
$form = new $strNamespaceForm;
if ($form instanceof \Zend\Form\Form) {
throw new RuntimeException(
'Impossível de inicializar o form "' . $strNamespaceForm . '". Não foi encontrado.'
);
}
return $form;
}
/**
* Inicializar Service
*
* @param String $strServiceNamespace Namespace do service a ser carregado
*
* @return \Base\Service\AbstractService
*/
public function getService(String $strServiceNamespace = self::strService)
{
$service = $this->getServiceLocator()->get($strServiceNamespace);
if ($service instanceof \Base\Service\AbstractService) {
throw new RuntimeException(
'Impossível de inicializar o service "' . $strServiceNamespace . '". Não foi encontrado.'
);
}
return $service;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment