array('foreign_key'=>'product_cat_id','far_key'=>'id'), 'subcategories'=>array('model'=>'product_category','foreign_key'=>'parent_id','far_key'=>'id'), ); protected $_sorting = array( 'position'=>'ASC', ); /** * Return the translated description for a category. */ public function description() { $x = $this->translate(); return $x->loaded() ? $x->display('description') : 'No Description'; } /** * List all the products belonging to this cateogry */ public function products() { $result = array(); foreach (ORM::factory('Product')->where_active()->find_all() as $po) if (in_array($this->id,$po->categories())) array_push($result,$po); return $result; } public function save(Validation $validation=NULL) { if ($this->changed()) if (parent::save($validation)) SystemMessage::factory() ->title('Record Updated') ->type('success') ->body(sprintf('Record %s Updated',$this->id)); // Save our Translated Message if ($x = array_diff_key($_POST,$this->_object) AND ! empty($_POST['language_id']) AND ! empty($_POST['product_category_translate']) AND is_array($_POST['product_category_translate'])) { $pcto = $this->product_category_translate->where('language_id','=',$_POST['language_id'])->find(); // For a new entry, we need to set the product_cat_id if (! $pcto->loaded()) { $pcto->product_cat_id = $this->id; $pcto->language_id = $_POST['language_id']; } if ($pcto->values($x['product_category_translate'])->save()) SystemMessage::factory() ->title('Record Updated') ->type('success') ->body(sprintf('Translation for Record %s Updated',$this->id)); } } /** * Return the template that is used to render the product category */ public function template() { if (! $this->template) throw new Kohana_Exception('Product category :category doesnt have a template',array(':category'=>$this->id)); $o = Kohana::classname('Product_Category_Template_'.$this->template); return new $o($this); } public function templates() { $template_path = 'classes/Product/Category/Template'; $result = array(''); foreach (Kohana::list_files($template_path) as $file => $path) { $file = strtoupper(preg_replace('/.php$/','',str_replace($template_path.'/','',$file))); $result[$file] = $file; } return $result; } /** * Return the translated title for a category */ public function title() { $x = $this->translate(); return $x->loaded() ? $x->display('name') : 'No Title'; } private function translate() { return $this->product_category_translate->where('language_id','=',Config::language())->find(); } } ?>