roykin/override/classes/Category.php

187 lines
5.7 KiB
PHP

<?php
class Category extends CategoryCore
{
const CATEGORY_HOME = 2;
const CATEGORY_BRAND_ROYKIN = 59;
const CATEGORY_RANGE_COLLECTION = 3;
const CATEGORY_RANGE_ORIGINAL = 8;
const CATEGORY_RANGE_SAVEUR = 12;
const CATEGORY_RANGE_SAVEUR_TABACS = 27;
private static $default_sub_category_ids = array();
public $images = array('logos' => '', 'bkg' => '');
public function __construct($id_category = null, $id_lang = null, $id_shop = null)
{
ObjectModel::__construct($id_category, $id_lang, $id_shop);
$this->image_dir = _PS_CAT_IMG_DIR_;
if ($this->id) {
$path = 'logos/'.$this->id;
if (file_exists($this->image_dir.$path.'.jpg')) {
$this->images['logos'] = $path.'.jpg';
}
elseif (file_exists($this->image_dir.$path.'.png')) {
$this->images['logos'] = $path.'.png';
}
$path = 'bkg/'.$this->id;
if (file_exists($this->image_dir.$path.'.jpg')) {
$this->images['bkg'] = $path.'.jpg';
}
elseif (file_exists($this->image_dir.$path.'.png')) {
$this->images['bkg'] = $path.'.png';
}
}
}
public static function getDefault($context = null)
{
return self::CATEGORY_BRAND_ROYKIN;
}
public static function getDefaultSubCategory($id_category, $on_notfound_use_firstsubcategory = false, $context = null)
{
if (empty(self::$default_sub_category_ids)) {
self::$default_sub_category_ids = array(
self::CATEGORY_HOME => self::CATEGORY_BRAND_ROYKIN,
self::CATEGORY_BRAND_ROYKIN => self::CATEGORY_RANGE_ORIGINAL
);
}
if (isset(self::$default_sub_category_ids[$id_category])) {
return self::$default_sub_category_ids[$id_category];
}
if ($on_notfound_use_firstsubcategory) {
return self::getFirstSubCategory($id_category, $context);
}
return -1;
}
public static function getFirstSubCategory($id_category, $context = null)
{
if(!$context) {
$context = Context::getContext();
}
$id_sub_category = Db::getInstance()->getValue(
'SELECT c.`id_category`
FROM `'._DB_PREFIX_.'category` c
LEFT JOIN `'._DB_PREFIX_.'category_shop` cs ON cs.`id_category` = c.`id_category`
WHERE c.`id_parent` = '.(int)$id_category.'
AND c.`active` = 1
AND cs.`id_shop` = '.(int)$context->shop->id.'
ORDER BY cs.`position` ASC'
);
if (empty($id_sub_category)) {
$id_sub_category = -1;
}
return $id_sub_category;
}
public static function getCategoriesTree($id_parent, $currentDepth = 0, $depth = 3, $context = null)
{
if(!$context)
{
$context = Context::getContext();
}
if($currentDepth >= $depth)
{
return array();
}
$query = '
SELECT DISTINCT c.`id_category`, cl.`name`
FROM `'._DB_PREFIX_.'category` c
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON cl.`id_category` = c.`id_category`
LEFT JOIN `'._DB_PREFIX_.'category_shop` cs ON cs.`id_category` = c.`id_category`
WHERE c.`id_parent` = '.$id_parent.'
AND c.`active` = 1
AND cl.`id_lang` = '.$context->language->id.'
AND cs.`id_shop` = '.$context->shop->id.'
AND cl.`id_shop` = '.$context->shop->id.'
ORDER BY cs.`position` ASC
';
$categories = Db::getInstance()->executeS($query);
if(!$categories)
{
return array();
}
foreach($categories as $key => $category)
{
$categories[$key]['link'] = $context->link->getCategoryLink($category['id_category']);
$categories[$key]['children'] = self::getCategoriesTree($category['id_category'], $currentDepth+1, $depth, $context);
if($categories[$key]['children'])
{
foreach($categories[$key]['children'] as $idx => $childrenCategory)
{
$categories[$key]['children'][$idx]['link'] = $context->link->getCategoryLink($childrenCategory['id_category']);
}
}
}
return $categories;
}
public function getImageLogo()
{
if (empty($this->images['logos'])) {
return '';
}
return _THEME_CAT_DIR_.$this->images['logos'];
}
public function getImageBackground()
{
if (empty($this->images['bkg'])) {
return '';
}
return _THEME_CAT_DIR_.$this->images['bkg'];
}
public function deleteSpecificImage($folder, $thumb_image_type)
{
if (!$this->id) {
return false;
}
if (preg_match('/[a-z]+/i', $folder)!==1) {
return false;
}
if (!empty($this->images[$folder])) {
if (file_exists($this->image_dir.$this->images[$folder]) && !unlink($this->image_dir.$this->images[$folder])) {
return false;
}
}
if (file_exists(_PS_TMP_IMG_DIR_.$this->getThumbnailFilename($folder, $thumb_image_type))
&& !unlink(_PS_TMP_IMG_DIR_.$this->getThumbnailFilename($folder, $thumb_image_type))) {
return false;
}
return true;
}
public function getThumbnailFilename($folder, $thumb_image_type)
{
$thumb = self::$definition['table'].'_'.strtr($folder, array('/' => '', '.'=>'')).'_'.(int)$this->id.'.'.strtr($thumb_image_type, array('/' => '', '.'=>''));
return $thumb;
}
}