toutpratique/modules/cmsps/classes/CmsPsCategory.php
2015-07-09 12:56:40 +02:00

112 lines
3.5 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;
/**
* @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'),
// Lang fields
'title' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isGenericName', 'required' => TRUE, 'size' => 40),
'slug' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isGenericName'),
'description' => array('type' => self::TYPE_STRING, '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);
}
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 function deleteGroups() {
return Db::getInstance()->execute('
DELETE FROM `'._DB_PREFIX_.'cmsps_categories_group`
WHERE `id_category` = ' . (int)$this->id
);
}
public function getGroups() {
return Db::getInstance()->executeS('
SELECT `id_group`
FROM `'._DB_PREFIX_.'cmsps_categories_group`
WHERE `id_category` = ' . (int)$this->id
);
}
}