Skip to content
This repository has been archived by the owner on Oct 20, 2023. It is now read-only.

Commit

Permalink
implement i18n categories controller and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
skie committed Dec 3, 2010
1 parent 730cda3 commit add5d2c
Show file tree
Hide file tree
Showing 2 changed files with 399 additions and 0 deletions.
193 changes: 193 additions & 0 deletions controllers/i18n_categories_controller.php
@@ -0,0 +1,193 @@
<?php
/**
* Copyright 2010, Cake Development Corporation (http://cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010, Cake Development Corporation (http://cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

App::import('Model', 'Categories.I18nCategory');

/**
* Categories controller
*
* @package categories
* @subpackage categories.controllers
*/
class I18nCategoriesController extends CategoriesAppController {

/**
* Name
*
* @var string
*/
public $name = 'I18nCategories';

/**
* Models
*
* @var array
*/
public $uses = array('Categories.I18nCategory');

/**
* Helpers
*
* @var array
*/
public $helpers = array('Html', 'Form');

/**
* beforeFilter callback
*
* @return void
*/
public function beforeFilter() {
parent::beforeFilter();
$this->Category = $this->I18nCategory;
$this->Auth->allow('view', 'index');
$this->set('modelName', $this->modelClass);
}

/**
* Index for category.
*
*/
public function index() {
$this->Category->recursive = 0;
$this->set('categories', $this->paginate());
}

/**
* View for category.
*
* @param string $slug, category slug
*/
public function view($slug = null) {
try {
$category = $this->Category->view($slug);
} catch (OutOfBoundsException $e) {
$this->Session->setFlash($e->getMessage());
$this->redirect(array('action' => 'index'));
}
$this->set(compact('category'));
}

/**
* Admin index for category.
*
*/
public function admin_index() {
$this->Category->recursive = 0;
$this->set('categories', $this->paginate());
}

/**
* Admin index
*
*/
public function admin_tree() {
$this->Category->recursive = 0;
$this->helpers[] = 'Utils.Tree';
$this->set('categories', $this->Category->find('all', array('order' => $this->Category->alias . '.lft')));
}

/**
* Admin view for category.
*
* @param string $slug, category slug
*/
public function admin_view($slug = null) {
try {
$category = $this->Category->view($slug);
} catch (OutOfBoundsException $e) {
$this->Session->setFlash($e->getMessage());
$this->redirect(array('action' => 'index'));
}
$this->set(compact('category'));
}

/**
* Admin add for category.
*
*/
public function admin_add($category_id = null) {
try {
$result = $this->Category->add($this->Auth->user('id'), $this->data);
if ($result === true) {
$this->Session->setFlash(__d('categories', 'The category has been saved', true));
$this->redirect(array('action' => 'index'));
}
} catch (OutOfBoundsException $e) {
$this->Session->setFlash($e->getMessage());
} catch (Exception $e) {
$this->Session->setFlash($e->getMessage());
$this->redirect(array('action' => 'index'));
}
if (!empty($this->data) && !empty($category_id)) {
$this->data[$this->Category->alias]['category_id'] = $category_id;
}
$categories = $this->Category->find('list');
$users = $this->Category->User->find('list');
$this->set(compact('categories', 'users'));
}

/**
* Admin edit for category.
*
* @param string $id, category id
*/
public function admin_edit($id = null) {
try {
$actualLanguages = Configure::read('Config.languages');
if (defined('DEFAULT_LANGUAGE')) {
$actualLanguages[] = DEFAULT_LANGUAGE;
}
App::import('Lib', 'Utils.Languages');
$Languages = new Languages();
$languages = $Languages->lists('locale');
// debug($actualLanguages);
// debug($languages);
$this->set(compact('languages', 'actualLanguages'));
$result = $this->Category->edit($id, null, $this->data);
if ($result === true) {
$this->Session->setFlash(__d('categories', 'Category saved', true));
$this->redirect(array('action' => 'view', $this->Category->data[$this->Category->alias]['slug']));

} else {
$this->data = $result;
}
} catch (OutOfBoundsException $e) {
$this->Session->setFlash($e->getMessage());
$this->redirect('/');
}
$categories = $this->Category->find('list');
$users = $this->Category->User->find('list');
$this->set(compact('categories', 'users'));

}

/**
* Admin delete for category.
*
* @param string $id, category id
*/
public function admin_delete($id = null) {
try {
$result = $this->Category->validateAndDelete($id, $this->Auth->user('id'), $this->data);
if ($result === true) {
$this->Session->setFlash(__d('categories', 'Category deleted', true));
$this->redirect(array('action' => 'index'));
}
} catch (Exception $e) {
$this->Session->setFlash($e->getMessage());
$this->redirect(array('action' => 'index'));
}
if (!empty($this->Category->data['category'])) {
$this->set('category', $this->Category->data['category']);
}
}
}

0 comments on commit add5d2c

Please sign in to comment.