Created
August 23, 2010 21:11
-
-
Save cwage/546344 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
require_once(dirname(__FILE__).'/../lib/BaseSchoolGeneratorConfiguration.class.php'); | |
require_once(dirname(__FILE__).'/../lib/BaseSchoolGeneratorHelper.class.php'); | |
/** | |
* school actions. | |
* | |
* @package ##PROJECT_NAME## | |
* @subpackage school | |
* @author ##AUTHOR_NAME## | |
* @version SVN: $Id: actions.class.php 24171 2009-11-19 16:37:50Z Kris.Wallsmith $ | |
*/ | |
abstract class autoSchoolActions extends sfActions | |
{ | |
public function preExecute() | |
{ | |
$this->configuration = new schoolGeneratorConfiguration(); | |
if (!$this->getUser()->hasCredential($this->configuration->getCredentials($this->getActionName()))) | |
{ | |
$this->forward(sfConfig::get('sf_secure_module'), sfConfig::get('sf_secure_action')); | |
} | |
$this->dispatcher->notify(new sfEvent($this, 'admin.pre_execute', array('configuration' => $this->configuration))); | |
$this->helper = new schoolGeneratorHelper(); | |
} | |
public function executeIndex(sfWebRequest $request) | |
{ | |
// sorting | |
if ($request->getParameter('sort') && $this->isValidSortColumn($request->getParameter('sort'))) | |
{ | |
$this->setSort(array($request->getParameter('sort'), $request->getParameter('sort_type'))); | |
} | |
// pager | |
if ($request->getParameter('page')) | |
{ | |
$this->setPage($request->getParameter('page')); | |
} | |
$this->pager = $this->getPager(); | |
$this->sort = $this->getSort(); | |
} | |
public function executeFilter(sfWebRequest $request) | |
{ | |
$this->setPage(1); | |
if ($request->hasParameter('_reset')) | |
{ | |
$this->setFilters($this->configuration->getFilterDefaults()); | |
$this->redirect($this->getRedirect()); | |
} | |
$this->filters = $this->configuration->getFilterForm($this->getFilters()); | |
$this->filters->bind($request->getParameter($this->filters->getName())); | |
if ($this->filters->isValid()) | |
{ | |
$this->setFilters($this->filters->getValues()); | |
$this->redirect($this->getRedirect()); | |
} | |
$this->pager = $this->getPager(); | |
$this->sort = $this->getSort(); | |
$this->setTemplate($request->getParameter('for_action', 'index')); | |
} | |
protected function getRedirect() | |
{ | |
$redirectTo = $this->getRequest()->getParameter('for_action', 'index'); | |
return $this->helper->getRouteForAction($redirectTo); | |
} | |
public function executeNew(sfWebRequest $request) | |
{ | |
$this->form = $this->configuration->getForm(); | |
$this->school = $this->form->getObject(); | |
} | |
public function executeCreate(sfWebRequest $request) | |
{ | |
$this->form = $this->configuration->getForm(); | |
$this->school = $this->form->getObject(); | |
$this->processForm($request, $this->form); | |
$this->setTemplate('new'); | |
} | |
public function executeEdit(sfWebRequest $request) | |
{ | |
$this->school = $this->getRoute()->getObject(); | |
$this->form = $this->configuration->getForm($this->school); | |
} | |
public function executeUpdate(sfWebRequest $request) | |
{ | |
$this->school = $this->getRoute()->getObject(); | |
$this->form = $this->configuration->getForm($this->school); | |
$this->processForm($request, $this->form); | |
$this->setTemplate('edit'); | |
} | |
public function executeDelete(sfWebRequest $request) | |
{ | |
$request->checkCSRFProtection(); | |
$this->dispatcher->notify(new sfEvent($this, 'admin.delete_object', array('object' => $this->getRoute()->getObject()))); | |
if ($this->getRoute()->getObject()->delete()) | |
{ | |
$this->getUser()->setFlash('notice', 'The item was deleted successfully.'); | |
} | |
$this->redirect($this->helper->getRouteForAction('list')); | |
} | |
public function executeShow(sfWebRequest $request) | |
{ | |
$this->school = $this->getRoute()->getObject(); | |
} | |
public function executeBatch(sfWebRequest $request) | |
{ | |
$request->checkCSRFProtection(); | |
if (!$ids = $request->getParameter('ids')) | |
{ | |
$this->getUser()->setFlash('error', 'You must at least select one item.'); | |
$this->redirect($this->helper->getRouteForAction('list')); | |
} | |
if (!$action = $request->getParameter('batch_action')) | |
{ | |
$this->getUser()->setFlash('error', 'You must select an action to execute on the selected items.'); | |
$this->redirect($this->helper->getRouteForAction('list')); | |
} | |
if (!method_exists($this, $method = 'execute'.ucfirst($action))) | |
{ | |
throw new InvalidArgumentException(sprintf('You must create a "%s" method for action "%s"', $method, $action)); | |
} | |
if (!$this->getUser()->hasCredential($this->configuration->getCredentials($action))) | |
{ | |
$this->forward(sfConfig::get('sf_secure_module'), sfConfig::get('sf_secure_action')); | |
} | |
$validator = new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'School')); | |
try | |
{ | |
// validate ids | |
$ids = $validator->clean($ids); | |
// execute batch | |
$this->$method($request); | |
} | |
catch (sfValidatorError $e) | |
{ | |
$this->getUser()->setFlash('error', 'A problem occurs when deleting the selected items as some items do not exist anymore.'); | |
} | |
$this->redirect($this->helper->getRouteForAction('list')); | |
} | |
protected function executeBatchDelete(sfWebRequest $request) | |
{ | |
$ids = $request->getParameter('ids'); | |
$records = Doctrine_Query::create() | |
->from('School') | |
->whereIn('id', $ids) | |
->execute(); | |
foreach ($records as $record) | |
{ | |
$record->delete(); | |
} | |
$this->getUser()->setFlash('notice', 'The selected items have been deleted successfully.'); | |
$this->redirect($this->helper->getRouteForAction('list')); | |
} | |
protected function processForm(sfWebRequest $request, sfForm $form) | |
{ | |
$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName())); | |
if ($form->isValid()) | |
{ | |
$notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.'; | |
try { | |
$school = $form->save(); | |
} catch (Doctrine_Validator_Exception $e) { | |
$errorStack = $form->getObject()->getErrorStack(); | |
$message = get_class($form->getObject()) . ' has ' . count($errorStack) . " field" . (count($errorStack) > 1 ? 's' : null) . " with validation errors: "; | |
foreach ($errorStack as $field => $errors) { | |
$message .= "$field (" . implode(", ", $errors) . "), "; | |
} | |
$message = trim($message, ', '); | |
$this->getUser()->setFlash('error', $message); | |
return sfView::SUCCESS; | |
} | |
$this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $school))); | |
if ($request->hasParameter('_save_and_add')) | |
{ | |
$this->getUser()->setFlash('notice', $notice.' You can add another one below.'); | |
$this->redirect($this->helper->getRouteForAction('new')); | |
} | |
else | |
{ | |
$this->getUser()->setFlash('notice', $notice); | |
$this->redirect(array('sf_route' => $this->helper->getUrlForAction($this->helper->postSaveAction()), 'sf_subject' => $school)); | |
} | |
} | |
else | |
{ | |
$this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false); | |
} | |
} | |
protected function getFilters() | |
{ | |
$filters = $this->getUser()->getAttribute('school.filters', $this->configuration->getFilterDefaults(), 'admin_module'); | |
$this->helper->setFilters($filters); | |
return $filters; | |
} | |
protected function setFilters(array $filters) | |
{ | |
return $this->getUser()->setAttribute('school.filters', $filters, 'admin_module'); | |
} | |
protected function getPager() | |
{ | |
$pager = $this->configuration->getPager('School'); | |
$pager->setQuery($this->buildQuery()); | |
$pager->setPage($this->getPage()); | |
$pager->init(); | |
return $pager; | |
} | |
protected function setPage($page) | |
{ | |
$this->getUser()->setAttribute('school.page', $page, 'admin_module'); | |
} | |
protected function getPage() | |
{ | |
return $this->getUser()->getAttribute('school.page', 1, 'admin_module'); | |
} | |
protected function buildQuery() | |
{ | |
$tableMethod = $this->configuration->getTableMethod(); | |
if (null === $this->filters) | |
{ | |
$this->filters = $this->configuration->getFilterForm($this->getFilters()); | |
} | |
$this->filters->setTableMethod($tableMethod); | |
$query = $this->filters->buildQuery($this->getFilters()); | |
$this->addSortQuery($query); | |
$event = $this->dispatcher->filter(new sfEvent($this, 'admin.build_query'), $query); | |
$query = $event->getReturnValue(); | |
return $query; | |
} | |
protected function addSortQuery($query) | |
{ | |
if (array(null, null) == ($sort = $this->getSort())) | |
{ | |
return; | |
} | |
$query->addOrderBy($sort[0] . ' ' . $sort[1]); | |
} | |
protected function getSort() | |
{ | |
if (null !== $sort = $this->getUser()->getAttribute('school.sort', null, 'admin_module')) | |
{ | |
return $sort; | |
} | |
$this->setSort($this->configuration->getDefaultSort()); | |
return $this->getUser()->getAttribute('school.sort', null, 'admin_module'); | |
} | |
protected function setSort(array $sort) | |
{ | |
if (null !== $sort[0] && null === $sort[1]) | |
{ | |
$sort[1] = 'asc'; | |
} | |
$this->getUser()->setAttribute('school.sort', $sort, 'admin_module'); | |
} | |
protected function isValidSortColumn($column) | |
{ | |
return Doctrine::getTable('School')->hasColumn($column); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment