<?php
App::uses('AppController', 'Controller');
/**
* Categories Controller
*
* @property Category $Category
* @property PaginatorComponent $Paginator
* @property SessionComponent $Session
* @property FlashComponent $Flash
*/
class CategoriesController extends AppController {
/**
* Components
*
* @var array
*/
public $components = array('Paginator', 'Session', 'Flash', /*'Auth' => array('authorize' => 'Controller')*/);
public function beforeFilter()
{
parent::beforeFilter();
/*if ($this->Auth->user('id')) {
$this->Auth->allow();
} else {
$this->Auth->allow('index');
}*/
//$this->Auth->deny('admin_ajax');
}
public function admin_add() {
if ($this->request->is('post')) {
$this->Category->create();
if ($this->Category->save($this->request->data)) {
$this->Flash->success(__('The Category has been saved.'));
return $this->redirect(array('action' => 'add'));
} else {
$this->Flash->error(__('The Category could not be saved. Please, try again.'));
}
}
$this->Category->recursive = 0;
$this->set('categories', $this->Paginator->paginate());
}
public function admin_edit($id = null) {
if (!$this->Category->exists($id)) {
throw new NotFoundException(__('Invalid Category'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->Category->save($this->request->data)) {
$this->Flash->success(__('The category has been saved.'));
return $this->redirect(array('action' => 'add'));
} else {
$this->Flash->error(__('The category could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id), 'recursive' => -1);
$this->request->data = $this->Category->find('first', $options);
}
$this->Category->recursive = 0;
$this->set('categories', $this->Paginator->paginate());
}
public function admin_delete($id = null) {
$this->Category->id = $id;
if (!$this->Category->exists()) {
throw new NotFoundException(__('Invalid Category'));
}
$this->request->allowMethod('post', 'delete');
$is_exists = $this->Category->Applicant->find('count', array('conditions' => array('category_id' => $id)));
if (!$is_exists && $this->Category->delete()) {
$this->Flash->success(__('The Category has been deleted.'));
} else {
$this->Flash->error(__('The Category could not be deleted. It is in use.'));
}
return $this->redirect(array('action' => 'add'));
}
}