Skip to content

Instantly share code, notes, and snippets.

@slywalker
Created December 10, 2011 02:24
Show Gist options
  • Save slywalker/1454324 to your computer and use it in GitHub Desktop.
Save slywalker/1454324 to your computer and use it in GitHub Desktop.
CakePHP2.0 BaseActionComponent
<?php
App::uses('Component', 'Controller');
App::uses('Inflector', 'Utility');
class BaseActionComponent extends Component {
public $components = array('Session');
public $Controller;
public $Model;
public $request;
public $names = array();
public $messages = array();
public function initialize($controller) {
$this->Controller = $controller;
$this->Model = $controller->{$controller->modelClass};
$this->request = $controller->request;
$this->names = array(
'controllerName' => $this->Controller->name,
'currentModelName' => $this->Model->name,
'pluralName' => Inflector::variable(Inflector::pluralize($this->Model->name)),
'singularName' => Inflector::variable($this->Model->name),
'singularHumanName' => Inflector::humanize(Inflector::underscore(Inflector::singularize($this->Controller->name))),
'pluralHumanName' => Inflector::humanize(Inflector::underscore($this->Controller->name)),
'displayField' => $this->Model->displayField,
'primaryKey' => $this->Model->primaryKey,
);
extract($this->names);
$this->messages = array(
'invalid' => __('Invalid %s', __($singularHumanName)),
'saveTrue' => __('The %s has been saved', __($singularHumanName)),
'saveFalse' => __('The %s could not be saved. Please, try again.', __($singularHumanName)),
'deteleTrue' => __('%s deleted', __($singularHumanName)),
'deleteFalse' => __('%s was not deleted', __($singularHumanName))
);
}
public function index() {
extract($this->names);
$results = $this->Controller->paginate($currentModelName);
$this->Controller->set($pluralName, $results);
return $results;
}
public function view($id = null) {
extract($this->names);
$this->Model->id = $id;
if (!$this->Model->exists()) {
throw new NotFoundException($this->messages['invalid']);
}
$result = $this->Model->read(null, $id);
$this->Controller->set($singularName, $result);
return $result;
}
public function add($redirect = array('action' => 'index')) {
if ($this->request->is('post')) {
$this->Model->create();
if ($this->Model->save($this->request->data)) {
$this->Session->setFlash($this->messages['saveTrue'], 'default', array('class' => 'success'));
$this->Controller->redirect($redirect);
} else {
$this->Session->setFlash($this->messages['saveFalse'], 'default', array('class' => 'error'));
}
}
}
public function edit($id = null, $redirect = array('action' => 'index')) {
$this->Model->id = $id;
if (!$this->Model->exists()) {
throw new NotFoundException($this->messages['invalid']);
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->Model->save($this->request->data)) {
$this->Session->setFlash($this->messages['saveTrue'], 'default', array('class' => 'success'));
$this->Controller->redirect($redirect);
} else {
$this->Session->setFlash($this->messages['saveFalse'], 'default', array('class' => 'error'));
}
} else {
$this->request->data = $this->Model->read(null, $id);
}
}
public function delete($id = null, $redirect = array('action' => 'index')) {
$redirect = $this->Controller->referer($redirect);
if (!empty($this->request->params['named']['redirect'])) {
$redirect = array('action' => $this->request->params['named']['redirect']);
}
if (!$this->request->is('post')) {
throw new MethodNotAllowedException();
}
$this->Model->id = $id;
if (!$this->Model->exists()) {
throw new NotFoundException($this->messages['invalid']);
}
if ($this->Model->delete()) {
$this->Session->setFlash($this->messages['deteleTrue'], 'default', array('class' => 'success'));
$this->Controller->redirect($redirect);
}
$this->Session->setFlash($this->messages['deleteFalse'], 'default', array('class' => 'error'));
$this->Controller->redirect($redirect);
}
public function moveUp($id = null, $redirect = array('action' => 'index')) {
if (!$this->request->is('post')) {
throw new MethodNotAllowedException();
}
$this->Model->id = $id;
if (!$this->Model->exists()) {
throw new NotFoundException(__('Invalid id'));
}
if ($this->Model->moveUp($id)) {
$this->Session->setFlash(__('Moved up successfully'), 'default', array('class' => 'success'));
$this->Controller->redirect($this->Controller->referer(array('action' => 'index')));
}
$this->Session->setFlash(__('Could not move up'), 'default', array('class' => 'error'));
$this->Controller->redirect($this->Controller->referer($redirect));
}
public function moveDown($id = null, $redirect = array('action' => 'index')) {
if (!$this->request->is('post')) {
throw new MethodNotAllowedException();
}
$this->Model->id = $id;
if (!$this->Model->exists()) {
throw new NotFoundException(__('Invalid id'));
}
if ($this->Model->moveDown($id)) {
$this->Session->setFlash(__('Moved down successfully'), 'default', array('class' => 'success'));
$this->Controller->redirect($this->Controller->referer(array('action' => 'index')));
}
$this->Session->setFlash(__('Could not move down'), 'default', array('class' => 'error'));
$this->Controller->redirect($this->Controller->referer($redirect));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment