Created
January 29, 2012 14:05
-
-
Save jasongrimes/1698969 to your computer and use it in GitHub Desktop.
Fork of zf2tutorial AlbumController, from https://github.com/akrabat/zf2-tutorial/blob/master/module/Album/src/Album/Controller/AlbumController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Album\Controller; | |
use Zend\Mvc\Controller\ActionController, | |
Zend\View\Model\ViewModel, | |
Album\Model\AlbumTable, | |
Album\Model\Album, | |
Album\Form\AlbumForm; | |
class AlbumController extends ActionController | |
{ | |
/** | |
* @var \Album\Model\AlbumTable | |
*/ | |
protected $albumTable; | |
public function indexAction() | |
{ | |
return new ViewModel(array( | |
'albums' => $this->getAlbumTable()->fetchAll(), | |
)); | |
} | |
public function addAction() | |
{ | |
$form = new AlbumForm(); | |
$form->get('submit')->setAttribute('label', 'Add'); | |
$request = $this->getRequest(); | |
if ($request->isPost()) { | |
$album = new Album(); | |
$form->setInputFilter($album->getInputFilter()); | |
$form->setData($request->post()); | |
if ($form->isValid()) { | |
$album->populate($form->getData()); | |
$this->getAlbumTable()->saveAlbum($album); | |
// Redirect to list of albums | |
return $this->redirect()->toRoute('album'); | |
} | |
} | |
return array('form' => $form); | |
} | |
public function editAction() | |
{ | |
$id = (int)$this->getEvent()->getRouteMatch()->getParam('id'); | |
if (!$id) { | |
return $this->redirect()->toRoute('album', array('action'=>'add')); | |
} | |
$album = $this->getAlbumTable()->getAlbum($id); | |
$form = new AlbumForm(); | |
$form->setBindOnValidate(false); | |
$form->bind($album); | |
$form->get('submit')->setAttribute('label', 'Edit'); | |
$request = $this->getRequest(); | |
if ($request->isPost()) { | |
$form->setData($request->post()); | |
if ($form->isValid()) { | |
$form->bindValues(); | |
$this->getAlbumTable()->saveAlbum($album); | |
// Redirect to list of albums | |
return $this->redirect()->toRoute('album'); | |
} | |
} | |
return array( | |
'id' => $id, | |
'form' => $form, | |
); | |
} | |
public function deleteAction() | |
{ | |
$id = (int)$this->getEvent()->getRouteMatch()->getParam('id'); | |
if (!$id) { | |
return $this->redirect()->toRoute('album'); | |
} | |
$request = $this->getRequest(); | |
if ($request->isPost()) { | |
$del = $request->post()->get('del', 'No'); | |
if ($del == 'Yes') { | |
$id = (int)$request->post()->get('id'); | |
$this->getAlbumTable()->deleteAlbum($id); | |
} | |
// Redirect to list of albums | |
return $this->redirect()->toRoute('default', array( | |
'controller' => 'album', | |
'action' => 'index', | |
)); | |
} | |
return array( | |
'id' => $id, | |
'album' => $this->getAlbumTable()->getAlbum($id) | |
); | |
} | |
public function setAlbumTable(AlbumTable $albumTable) | |
{ | |
$this->albumTable = $albumTable; | |
return $this; | |
} | |
public function getAlbumTable() | |
{ | |
if (!$this->albumTable) { | |
$sm = $this->getServiceLocator(); | |
$this->albumTable = $sm->get('album-table'); | |
} | |
return $this->albumTable; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment