This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/modules/product/classes/Model/Product/Category.php

130 lines
3.2 KiB
PHP

<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class supports OSB listing products by category.
*
* @package Product
* @category Models
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Model_Product_Category extends ORM {
protected $_table_name = 'product_cat';
protected $_created_column = FALSE;
protected $_updated_column = FALSE;
protected $_nullifempty = array(
'status',
'template',
);
protected $_has_many = array(
'subcategories'=>array('model'=>'product_category','foreign_key'=>'parent_id','far_key'=>'id'),
'translate'=>array('model'=>'Product_Category_Translate','foreign_key'=>'product_cat_id','far_key'=>'id'),
);
protected $_sorting = array(
'position'=>'ASC',
);
private $_pto = NULL;
protected $_save_message = TRUE;
/** REQUIRED ABSTRACT METHODS **/
/** LOCAL METHODS **/
/**
* Return the translated langauge object
*/
private function _pto(Model_Language $lo) {
if (! $this->_pto) {
// First try the called langauge.
$pto = $this->translate->where('language_id','=',$lo->id)->find();
// Second try the called langauge.
if (! $pto->loaded())
$pto = $this->translate->where('language_id','=',Company::instance()->language()->id)->find();
$this->_pto = $pto;
}
return $this->_pto;
}
/**
* Return the translated description for a category.
*/
public function description() {
$pto = $this->_pto(Site::language());
return $pto->loaded() ? $pto->display('description') : 'No Description';
}
public function name($variable=NULL) {
$pto = $this->_pto(Site::language());
return $pto->loaded() ? $pto->display('name') : 'No Name';
}
/**
* 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->avail_category))
array_push($result,$po);
return $result;
}
public function save(Validation $validation=NULL) {
parent::save($validation);
// Save our Translated Message
if ($x = array_diff_key($_POST,$this->_object) AND Arr::get($_POST,'language_id') AND is_array(Arr::get($_POST,'translate'))) {
$to = $this->translate->where('language_id','=',Arr::get($_POST,'language_id'))->find();
// For a new entry, we need to set the product_cat_id
if (! $to->loaded()) {
$to->product_cat_id = $this->id;
$to->language_id = Arr::get($_POST,'language_id');
}
$to->values($x['translate'])->save();
}
return $this;
}
/**
* 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;
}
}
?>