Created
May 14, 2010 21:06
-
-
Save paulchubatyy/401672 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 | |
class Model_Category extends Model_Abstract { | |
protected static $_path; | |
public static function initialize(Jelly_Meta $meta) | |
{ | |
$meta->table('categories') | |
->name_key('title') | |
->sorting(array('materialized_path' => 'asc')) | |
->fields(array( | |
'id' => new Field_Primary, | |
'title' => new Field_String(array( | |
'rules' => array( | |
'not_empty' => array(TRUE), | |
), | |
)), | |
'level' => new Field_Integer(array( | |
'rules' => array( | |
'not_empty' => array(TRUE), | |
), | |
)), | |
'materialized_path' => new Field_String(array( | |
'unique' => TRUE, | |
'rules' => array( | |
'not_empty' => array(TRUE), | |
), | |
)), | |
'parent' => new Field_BelongsTo(array( | |
'foreign' => 'category.id', | |
'column' => 'parent_id', | |
)), | |
'children' => new Field_HasMany(array( | |
'foreign' => 'category.parent_id', | |
)), | |
'questions' => new Field_HasMany(array( | |
'foreign' => 'question', | |
)), | |
'created_at' => new Field_Timestamp(array( | |
'auto_now_create' => TRUE, | |
'format' => 'Y-m-d H:i:s', | |
)), | |
'updated_at' => new Field_Timestamp(array( | |
'auto_now_update' => TRUE, | |
'format' => 'Y-m-d H:i:s', | |
)), | |
)); | |
} | |
public function save($key = NULL) | |
{ | |
// getting the level of the category | |
if (empty($this->parent) == FALSE) { | |
$this->level = $this->parent->level + 1; | |
} else { | |
$this->level = 0; | |
} | |
// setting new materialized path | |
$this->set_materialized_path($this->get_materialized_path_array()); | |
parent::save($key); | |
// flushing cache | |
$this->_cache->delete('admin.cat.tree'); | |
$this->_cache->delete('cat.tree'); | |
} | |
/** | |
* Retrieves array of titles of parent categories | |
*/ | |
public function get_materialized_path_array(array $path = array()) | |
{ | |
$path[] = urlencode($this->title); | |
if ($this->parent->id != NULL) { | |
$path = $this->parent->get_materialized_path_array($path); | |
} | |
return $path; | |
} | |
protected function set_materialized_path(array $array) | |
{ | |
// the argument is a reverse array | |
$this->materialized_path = implode('/', array_reverse($array)); | |
} | |
/* | |
* Categories from root to current | |
* | |
* @return array | |
*/ | |
public function get_path() | |
{ | |
$category = $this; | |
// creating categories path array | |
$categories = array($this); | |
// while we reach the root category | |
while ($category->parent->id != NULL) { | |
// we add category parent to array | |
$category = $category->parent; | |
$categories[] = $category; | |
} | |
// reverting the array so that root category went first | |
return array_reverse($categories); | |
} | |
/* | |
* Delete the category and all the other stuff that concerns it. | |
* | |
* @param int category id to delete | |
*/ | |
public function delete($key = NULL) | |
{ | |
// subcategories | |
Jelly::delete('category')->subs($this)->execute(); | |
// and this | |
parent::delete($key); | |
// flushing the cache | |
$this->_cache->delete_all(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment