Skip to content

Instantly share code, notes, and snippets.

@mariuskubilius
Created December 28, 2011 22:13
Show Gist options
  • Save mariuskubilius/1530063 to your computer and use it in GitHub Desktop.
Save mariuskubilius/1530063 to your computer and use it in GitHub Desktop.
Lithium Model and Controller for taxonomies
<?php
namespace app\controllers;
use \app\models\Taxonomies;
use MongoId;
class CategoriesController extends \lithium\action\Controller {
/**
* @var Protected $_type - protected variable which define type of taxonomy,
* Can be omited if there is no need for additional types of taxonomy.
*/
protected $_type = 'categories';
/**
* Index method of category controller, queries using `Taxonomies::getTaxonomies`
* @TODO remove breadcrumbs from Taxonomies model
*/
public function admin_index() {
$results = FALSE;
$results = Taxonomies::getTaxonomies($this->_type, $this->request->id);
$breadcrumbs = Taxonomies::breadcrumbs($this->request->id, $this->_type);
return compact('results', 'breadcrumbs');
}
public function admin_add() {
var_dump($this->request->id);
$results = FALSE;
if ($this->request->data){
$results = Taxonomies::create($this->request->data);
if(Taxonomies::addTaxonomies($results, $this->_type, $this->request->id)) return $this->redirect(array('action' => 'index', 'id'=>$this->request->id));
}
return(compact('results'));
}
public function admin_edit() {
$results = false;
if(!$this->request->id) return $this->redirect($this->request->refferer);
$conditions = array('_id' => $this->request->id, 'type' => $this->_type);
$results = Taxonomies::first(compact('conditions'));
var_dump($results->data());
if ($this->request->data){
if($results->save($this->request->data)) {
Taxonomies::updateCategories($results);
$this->redirect(array('action' => 'index', 'id'=>$results->parent['_id']));
}
}
return(compact('results'));
}
/**
* Checks whether current Taxonomy has child Taxonomies and if it has no Taxonomies deletes them
*/
public function admin_delete() {
$results = Taxonomies::first($this->request->id);
$hasChildren = Taxonomies::getAncestors($this->request->id, $this->_type, array('operation'=>'count'));
if($this->request->data && !$hasChildren){
if($results->delete()) $this->redirect(array('action'=>'index'));
}
return compact('results');
}
}
?>
<?php
namespace app\models;
use MongoId;
class Taxonomies extends \app\extensions\data\Model {
/**
* Function to return all categories of current parent or root categories if parent is empty
* @param Mixed $type string or NULL the type of category
* @param Mixed $parent string or NULL category of parent
* @param Array $options an array of options
* - queryBy String an field by which it should be queried can be slug or _id
* @return Object
*/
public static function getTaxonomies($type = null, $parent = null , $options = array()){
$options += array(
'queryBy' => '_id',
);
if($options['queryBy']=='_id') $parent = isset($parent)?new MongoId($parent):NULL;
$conditions = array( 'parent.'.$options['queryBy'] => $parent, 'type' => $type);
$results = static::all(compact('conditions'));
return $results;
}
/**
* Method used to get hierarchy of taxonomies, returns new parent if any and hierarchy.
* @param String $parent parent of taxonomy
* @param String $type type of taxonomy
* @return Object
*/
public static function hierarchy($parent, $type){
$conditions = array ('_id' => $parent, 'type' => $type);
$fields = array('_id', 'title', 'parent', 'slug', 'ancestors');
$hierarchy = static::first(compact('conditions', 'fields'));
if(!$hierarchy) return NULL;
$results->parent = array(
'_id' => $hierarchy->_id,
'title' => $hierarchy->title,
'slug' => $hierarchy->slug,
);
$results->ancestors = $hierarchy->ancestors;
$results->ancestors[] = $results->parent;
return $results;
}
/**
* Manages saving of taxonomies
* @param Object $entity a data object to be added.
* @param Mixed $type type of taxonomy
* @param Mixed $id id of taxonomy to which taxonomy will be added
* @return Boolean
*/
public static function addTaxonomies($entity, $type=null, $id = null ){
$entity->type = $type;
$entity->slug = Taxonomies::slug($entity);
if($id){
$hierarchy = Taxonomies::hierarchy($id, $type);
$entity->parent = $hierarchy->parent;
$entity->ancestors = $hierarchy->ancestors;
}
return $entity->save();
}
/**
* Returns all ancestors of current category
* @param String $id
* @param String $type
* @param Array an array of option
* - operation type of query first, all, count refer to model documentation
* @return object
*/
public static function getAncestors($id = NULL, $type = NULL, $options = array()) {
if(!$id) return FALSE;
$options += array(
'operation'=>'all'
);
$conditions = array('ancestors._id'=>new MongoId($id));
return Taxonomies::$options['operation'](compact('conditions'));
}
/**
* @TODO Remove breadcrumbs from taxonomies model as it does not belong here. It's shared
* functionality and should not be here.
*/
public static function breadcrumbs($current, $type, $options = array(NULL)){
$options += array(
'homeCaption' => 'Home',
'queryBy' => '_id',
);
$breadcrumbs = NULL;
$conditions = array($options['queryBy'] => $current, 'type' => $type);
$results = static::first(compact('conditions'));
$breadcrumbs[] = array(
'_id' => NULL,
'title' => $options['homeCaption'],
'slug' => NULL,
);
if ($results) {
if($results->ancestors){
$breadcrumbs = array_merge($breadcrumbs, $results->ancestors->data());
}
$breadcrumbs[] = array(
'_id' => $results->_id,
'title' => $results->title,
'slug' => $results->slug,
'current' => true,
);
}
return $breadcrumbs;
}
/**
* Handles update of Taxonomies in hierarchy
* @param Object entity an entity of changed Taxonomy
* @return Boolean not realy sure about that
*/
public static function updateCategories($entity){
$parentData = array('parent.title'=> $entity->title, 'parent.slug'=>$entity->slug);
$parentConditions = array('parent._id'=>$entity->_id, 'type'=>$entity->type);
$ancestorsData = array('ancestors.$.title'=> $entity->title, 'ancestors.$.slug'=>$entity->slug);
$ancestorsConditions = array('ancestors._id'=>$entity->_id, 'type'=>$entity->type);
static::update($parentData, $parentConditions);
static::update($ancestorsData, $ancestorsConditions);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment