toutpratique/modules/cmsps/classes/CmsPsCategory.php
2015-10-28 15:20:49 +01:00

208 lines
6.8 KiB
PHP

<?php
class CmsPsCategory extends ObjectModel {
public $id_category;
public $id_parent;
public $date_add;
public $date_upd;
public $active;
public $title;
public $description;
public $slug;
public $meta_title;
public $meta_desc;
public $id_category_store;
/**
* @see ObjectModel::$definition
*/
public static $definition = array(
'table' => 'cmsps_categories',
'primary' => 'id_category',
'multilang' => TRUE,
'multilang_shop' => TRUE,
'fields' => array(
'id_parent' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
'active' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
'id_category_store' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
// Lang fields
'title' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isGenericName', 'required' => TRUE, 'size' => 255),
'slug' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isGenericName'),
'description' => array('type' => self::TYPE_HTML, 'lang' => TRUE, 'validate' => 'isCleanHtml'),
'meta_title' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isGenericName'),
'meta_desc' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isGenericName'),
'date_add' => array('type' => self::TYPE_DATE),
'date_upd' => array('type' => self::TYPE_DATE)
),
);
public function __construct($id = NULL, $id_lang = NULL, $id_shop = NULL) {
parent::__construct($id, $id_lang, $id_shop);
$this->id_image = ($this->id && file_exists(_CMS_CAT_IMG_DIR_.(int)$this->id.'.jpg')) ? (int)$this->id : false;
$this->image_dir = _CMS_CAT_IMG_DIR_;
}
public static function getAllCategories($id_lang, $id_shop = NULL) {
$sql = '
SELECT c.*, lc.*
FROM `'._DB_PREFIX_.'cmsps_categories` c
LEFT JOIN `'._DB_PREFIX_.'cmsps_categories_lang` lc ON c.`id_category` = lc.`id_category`
LEFT JOIN `'._DB_PREFIX_.'cmsps_categories_shop` ls ON c.`id_category` = ls.`id_category`
WHERE lc.`id_lang` = '.(int)$id_lang;
if ($id_shop) {
$sql.= ' AND ls.`id_shop` = '.(int)$id_shop;
}
return Db::getInstance()->executeS($sql);
}
/**
* @param int $id_lang [description]
* @param int $id_shop [description]
* @return array $parents [only parent with one level child]
* -- Thinking to optimize this function to have all levels
*/
public static function getCategoriesTree($id_lang, $id_shop = NULL) {
$sql = '
SELECT c.*, lc.*
FROM `'._DB_PREFIX_.'cmsps_categories` c
LEFT JOIN `'._DB_PREFIX_.'cmsps_categories_lang` lc ON c.`id_category` = lc.`id_category`
LEFT JOIN `'._DB_PREFIX_.'cmsps_categories_shop` ls ON c.`id_category` = ls.`id_category`
WHERE lc.`id_lang` = '.(int)$id_lang.' AND c.`id_parent`=0';
if ($id_shop) {
$sql.= ' AND ls.`id_shop` = '.(int)$id_shop;
}
$childSql = '
SELECT c.`id_category` as parent, child.`id_category`, child.`title`
FROM `'._DB_PREFIX_.'cmsps_categories` c
LEFT JOIN `'._DB_PREFIX_.'cmsps_categories_lang` lc ON c.`id_category` = lc.`id_category`
RIGHT JOIN (
SELECT c1.*, lc1.`title` FROM `'._DB_PREFIX_.'cmsps_categories` c1
LEFT JOIN `'._DB_PREFIX_.'cmsps_categories_lang` lc1 ON c1.`id_category` = lc1.`id_category`
WHERE lc1.`id_lang` = '.(int)$id_lang;
if ($id_shop)
$childSql.= ' AND ls.`id_shop` = '.(int)$id_shop;
$childSql.= ') as child ON (child.`id_parent` = c.`id_category`)
WHERE lc.`id_lang` = '.(int)$id_lang;
if ($id_shop) {
$childSql.= ' AND ls.`id_shop` = '.(int)$id_shop;
}
$parents = Db::getInstance()->executeS($sql);
$children = Db::getInstance()->executeS($childSql);
foreach ($parents as &$cat) {
// $cat['children'] = array();
foreach ($children as $key => $value) {
if((int)$value['parent'] == (int)$cat['id_category'])
$cat['children'][] = $value;
}
}
// echo "<pre>";var_dump($parents[9]);echo "</pre>";die();
return $parents;
}
public static function getParentPossibility($id_lang, $id_shop = NULL, $id_category = NULL) {
$sql = '
SELECT c.*, lc.*
FROM `'._DB_PREFIX_.'cmsps_categories` c
LEFT JOIN `'._DB_PREFIX_.'cmsps_categories_lang` lc ON c.`id_category` = lc.`id_category`
LEFT JOIN `'._DB_PREFIX_.'cmsps_categories_shop` ls ON c.`id_category` = ls.`id_category`
WHERE lc.`id_lang` = '.(int)$id_lang;
if ($id_shop) {
$sql.= ' AND ls.`id_shop` = '.(int)$id_shop;
}
if ($id_category) {
$sql.= ' AND c.`id_category` != '.(int)$id_category
.' AND c.`id_parent` !='.(int)$id_category;
}
return Db::getInstance()->executeS($sql);
}
public function hasChildren() {
return (bool) Db::getInstance()->getValue('
SELECT c.`id_category`
FROM `'._DB_PREFIX_.'cmsps_categories` c
WHERE c.`id_parent` = '.(int)$this->id
);
}
public function hasPosts() {
return (bool) Db::getInstance()->getValue('
SELECT p.`id_post`
FROM `'._DB_PREFIX_.'cmsps_posts` p
WHERE p.`id_category` = '.(int)$this->id
);
}
public static function getTipsofDay($id_category, $id_lang)
{
$collection_post = new Collection('CmsPsPost', $id_lang);
$collection_post->where('id_category', '=', $id_category);
$collection_post->where('active', '=', 1);
$collection_post->where('trick', '=', 1);
$collection_post->orderBy('date_add', 'DESC');
$collection_post->setPageSize(1);
$tips_category = $collection_post->getFirst();
return $tips_category;
}
public function getArticles($id_lang, $limit = 10, $num_page, $total = FALSE, $order_by = 'date_add', $filter_word = '')
{
$collection_post = new Collection('CmsPsPost', $id_lang);
$collection_post->where('id_category', '=', $this->id_category);
$collection_post->where('active', '=', 1);
$collection_post->orderBy($order_by, 'DESC');
$collection_post->setPageSize($limit);
$collection_post->setPageNumber($num_page);
if ($filter_word) {
$collection_post->sqlWhere('title LIKE '.'"%'.$filter_word.'%"');
}
if ($total) {
return $collection_post->count();
}
$articles = $collection_post->getResults();
return CmsPsPost::injectsData($articles, TRUE);
}
public function getChildren($id_lang)
{
$collection_categories = new Collection('CmsPsCategory', $id_lang);
$collection_categories->where('id_parent', '=', $this->id_category);
$collection_categories->where('active', '=', 1);
return $collection_categories->getResults();
}
public static function getRootCategory($id_category) {
$category = new CmsPsCategory($id_category);
if (self::isRootCategory($category)) {
return $category;
} else {
$parent = new CmsPsCategory($category->id_parent);
return self::getRootCategory($parent->id_category);
}
}
public static function isRootCategory(CmsPsCategory $category) {
return (bool) ($category->id_parent == 0) ? true : false;
}
}