2010-11-29 22:41:08 +00:00
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class supports OSB listing products by category.
|
|
|
|
*
|
2013-03-19 22:35:19 +00:00
|
|
|
* @package Product
|
2010-11-29 22:41:08 +00:00
|
|
|
* @category Models
|
|
|
|
* @author Deon George
|
2013-03-19 22:35:19 +00:00
|
|
|
* @copyright (c) 2009-2013 Open Source Billing
|
2010-11-29 22:41:08 +00:00
|
|
|
* @license http://dev.osbill.net/license.html
|
|
|
|
*/
|
2012-12-10 21:48:30 +00:00
|
|
|
class Model_Product_Category extends ORM_OSB {
|
2010-11-29 22:41:08 +00:00
|
|
|
protected $_table_name = 'product_cat';
|
2013-04-26 01:42:09 +00:00
|
|
|
protected $_created_column = FALSE;
|
|
|
|
protected $_updated_column = FALSE;
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2013-04-26 01:42:09 +00:00
|
|
|
protected $_nullifempty = array(
|
|
|
|
'status',
|
|
|
|
'template',
|
2012-01-29 06:23:24 +00:00
|
|
|
);
|
|
|
|
|
2013-04-26 01:42:09 +00:00
|
|
|
protected $_has_many = array(
|
|
|
|
'product_category_translate'=>array('foreign_key'=>'product_cat_id','far_key'=>'id'),
|
|
|
|
'subcategories'=>array('model'=>'product_category','foreign_key'=>'parent_id','far_key'=>'id'),
|
2010-11-29 22:41:08 +00:00
|
|
|
);
|
2011-09-28 06:46:22 +00:00
|
|
|
|
2013-04-26 01:42:09 +00:00
|
|
|
/**
|
|
|
|
* Return the translated description for a category.
|
|
|
|
*/
|
2012-01-29 06:23:24 +00:00
|
|
|
public function description() {
|
2013-04-26 01:42:09 +00:00
|
|
|
$x = $this->translate();
|
2012-07-30 05:10:58 +00:00
|
|
|
|
2013-04-26 01:42:09 +00:00
|
|
|
return $x->loaded() ? $x->display('description') : 'No Description';
|
2012-01-29 06:23:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-11 02:33:57 +00:00
|
|
|
/**
|
|
|
|
* List all the products belonging to this cateogry
|
|
|
|
*/
|
|
|
|
public function products() {
|
2013-04-05 12:50:08 +00:00
|
|
|
$result = array();
|
2013-01-11 02:33:57 +00:00
|
|
|
|
|
|
|
foreach (ORM::factory('Product')->where_active()->find_all() as $po)
|
|
|
|
if (in_array($this->id,$po->categories()))
|
2013-04-05 12:50:08 +00:00
|
|
|
array_push($result,$po);
|
2013-01-11 02:33:57 +00:00
|
|
|
|
2013-04-05 12:50:08 +00:00
|
|
|
return $result;
|
2013-01-11 02:33:57 +00:00
|
|
|
}
|
|
|
|
|
2013-04-26 01:42:09 +00:00
|
|
|
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);
|
|
|
|
}
|
2011-09-28 06:46:22 +00:00
|
|
|
|
2013-04-26 01:42:09 +00:00
|
|
|
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;
|
|
|
|
}
|
2011-09-28 06:46:22 +00:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2013-04-26 01:42:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
?>
|