roykin/override/classes/Category.php
2017-06-14 11:41:31 +02:00

262 lines
8.4 KiB
PHP
Executable File

<?php
class Category extends CategoryCore
{
const CATEGORY_LEVEL_BRANDS = 3;
const CATEGORY_HOME = 2;
const CATEGORY_BRAND_ROYKIN = 83;
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_HOME;
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 getBrands($only_category_ids = null, $context = null)
{
if (!$context) {
$context = Context::getContext();
}
$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.`active` = 1
AND c.`level_depth` = '.(int)self::CATEGORY_LEVEL_BRANDS.'
AND cl.`id_lang` = '.$context->language->id.'
AND cs.`id_shop` = '.$context->shop->id.'
AND cl.`id_shop` = '.$context->shop->id;
if (is_array($only_category_ids) && !empty($only_category_ids)) {
$query .= ' AND c.`id_category` IN ('.implode(',', array_map('pSQL', $only_category_ids)).')';
}
$query .= ' ORDER BY c.`level_depth`, cs.`position` ASC';
$brands = array();
foreach(Db::getInstance()->executeS($query) as $r) {
$brands[$r['id_category']] = $r;
}
return $brands;
}
public static function getBrandIdsForCategoryIds(array $category_ids, $context = null)
{
if (!$context) {
$context = Context::getContext();
}
$all_categories = array();
foreach (Db::getInstance()->executeS('
SELECT DISTINCT c.`id_category`, c.`id_parent`, c.`level_depth`
FROM `'._DB_PREFIX_.'category` c
LEFT JOIN `'._DB_PREFIX_.'category_shop` cs ON cs.`id_category` = c.`id_category`
WHERE c.`level_depth` >= '.(int)self::CATEGORY_LEVEL_BRANDS.'
AND cs.`id_shop` = '.(int)$context->shop->id.'
ORDER BY c.`id_category` ASC
') as $r) {
$all_categories[$r['id_category']] = array($r['level_depth'], $r['id_parent']);
}
$brand_ids_for_category_ids = array();
foreach ($category_ids as $id_category) {
$found = false;
$id_current_category = $id_category;
while (!$found) {
if ($all_categories[$id_current_category][0] == self::CATEGORY_LEVEL_BRANDS) {
$brand_ids_for_category_ids[$id_category] = $id_current_category;
$found = true;
}
elseif (isset($all_categories[$id_current_category])){
$id_current_category = $all_categories[$id_current_category][1];
}
else {
$found = true;
}
}
}
return $brand_ids_for_category_ids;
}
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;
}
}