diff options
author | Tim Almdal <tnalmdal@shaw.ca> | 2008-11-26 16:48:00 +0000 |
---|---|---|
committer | Tim Almdal <tnalmdal@shaw.ca> | 2008-11-26 16:48:00 +0000 |
commit | af9a27216d25bcb390413c7848e26d3cd5e1b15f (patch) | |
tree | e67cc6b6c015dc5ce75b5d80eeb8648d64ac6d0d /modules/tag/models | |
parent | dfd02815125af60d7b1791646b441ba41c4e438a (diff) |
Modify the tag model to behave like a virtual album. There are two outstanding issues that i still have to resolve. The first being there is no thumbnail for the root directory, so it doesn't look quite right. And secondly, the bread crumb shows the dynamic tag album as hot having a parent. I wanted it to be the root directory, but i will overcome :-)
Diffstat (limited to 'modules/tag/models')
-rw-r--r-- | modules/tag/models/tag.php | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/modules/tag/models/tag.php b/modules/tag/models/tag.php index 0bf85843..b25afc58 100644 --- a/modules/tag/models/tag.php +++ b/modules/tag/models/tag.php @@ -20,6 +20,74 @@ class Tag_Model extends ORM { protected $has_and_belongs_to_many = array("items"); + protected $_children = array(); + var $rules = array( "name" => "required|length[4,32]"); + + /** + * Emulate the album method charactistics so that the tag looks like an album to the framework. + */ + public function __call($function, $args) { + if ($function == "children") { + return $this->_get_tag_children($args[0], $args[1]); + } else if ($function == "children_count") { + return $this->_get_children_count(); + } else if ($function == "parents") { + return ORM::factory("item", 1); + } else { + return parent::__call($function, $args); + } + } + + /** + * Emulate the album property charactistics so that the tag looks like an album to the framework. + */ + public function __get($property) { + if ($property == "title" || $property == "title_edit" || $property == "name_edit") { + return $this->name; + } else if ($property == "description_edit") { + return ""; + } else if ($property == "owner") { + return null; + } else { + return parent::__get($property); + } + } + + /** + * Get the item children. This code was borrowed from the ORM::__get($column) method and modified + * to allow for the specification of the limit and offset. + * @param int $limit + * @param int $offset + * @return ORM_Iterator + */ + private function _get_tag_children($limit, $offset) { + // Load the child model + $model = ORM::factory(inflector::singular("items")); + + // Load JOIN info + $join_table = $model->join_table($this->table_name); + $join_col1 = $model->foreign_key(NULL, $join_table); + $join_col2 = $model->foreign_key(TRUE); + + // one<>alias:many relationship + return $model + ->join($join_table, $join_col1, $join_col2) + ->where($this->foreign_key(NULL, $join_table), $this->object[$this->primary_key]) + ->find_all($limit, $offset); + } + + /** + * Get the total number of children. + * @return int + */ + private function _get_children_count() { + return Database::instance() + ->select("COUNT(*) AS count") + ->from("items_tags") + ->where("tag_id", $this->id) + ->get() + ->current()->count; + } }
\ No newline at end of file |