202 lines
5.3 KiB
PHP
Executable File
202 lines
5.3 KiB
PHP
Executable File
<?php
|
|
|
|
class CategoryFamilyCore extends ObjectModel {
|
|
|
|
public $id_category_family;
|
|
public $id_parent;
|
|
public $name;
|
|
|
|
static $parent_info = array();
|
|
|
|
protected $table = 'category_family';
|
|
protected $tables = array('category_family', 'category_family_lang');
|
|
protected $identifier = 'id_category_family';
|
|
|
|
public function __construct($id = NULL, $id_lang = NULL) {
|
|
if ($id) {
|
|
$result = $this->loadCategoryFamily($id, $id_lang);
|
|
if ($result) {
|
|
$this->id = $result['id_category_family'];
|
|
$this->id_category_family = $result['id_category_family'];
|
|
$this->id_parent = $result['id_parent'];
|
|
$this->name = $result['name'];
|
|
$this->id_lang = $id_lang;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function getParentName($data, $object) {
|
|
global $cookie;
|
|
$parent = new CategoryFamilyCore($data, $cookie->id_lang);
|
|
if ($parent->name) {
|
|
return $parent->name;
|
|
} else {
|
|
return '--';
|
|
}
|
|
}
|
|
|
|
public function loadCategoryFamily($id_category_family, $id_lang) {
|
|
$result = array();
|
|
$data = Db::getInstance()->getRow('
|
|
SELECT *
|
|
FROM `'._DB_PREFIX_.'category_family`
|
|
WHERE `id_category_family` = '.(int)$id_category_family
|
|
);
|
|
$result['id_category_family'] = $data['id_category_family'];
|
|
$result['id_parent'] = $data['id_parent'];
|
|
|
|
$category_i18n = Db::getInstance()->ExecuteS('
|
|
SELECT `name`, `id_lang`
|
|
FROM `'._DB_PREFIX_.'category_family_lang`
|
|
WHERE `id_category_family` = '.(int)$id_category_family .
|
|
(($id_lang) ? ' AND id_lang ='.(int)$id_lang: '' )
|
|
);
|
|
foreach($category_i18n as $c) {
|
|
if ($id_lang) {
|
|
$result['name'] = $c['name'];
|
|
} else{
|
|
$result['name'][$c['id_lang']] = $c['name'];
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function add() {
|
|
Db::getInstance()->Execute('
|
|
INSERT INTO `'._DB_PREFIX_.'category_family` VALUES (
|
|
DEFAULT,
|
|
"'.(int)$this->id_parent.'"
|
|
)
|
|
');
|
|
$this->id = Db::getInstance()->Insert_ID();
|
|
|
|
foreach(Language::getLanguages(FALSE) as $language) {
|
|
Db::getInstance()->Execute('
|
|
INSERT INTO `'._DB_PREFIX_.'category_family_lang` VALUES (
|
|
'.(int)$this->id.',
|
|
'.(int)$language['id_lang'].',
|
|
"'.pSQL($this->name[$language['id_lang']]).'"
|
|
)
|
|
');
|
|
}
|
|
return TRUE;
|
|
}
|
|
|
|
public function delete() {
|
|
parent::delete();
|
|
}
|
|
|
|
public static function getAll($id_lang, $id_parent = NULL) {
|
|
return Db::getInstance()->executeS('
|
|
SELECT l.`name`, l.`id_category_family`, c.`id_parent`
|
|
FROM `'._DB_PREFIX_.'category_family` c
|
|
LEFT JOIN `'._DB_PREFIX_.'category_family_lang` l ON (c.`id_category_family`=l.`id_category_family`)
|
|
WHERE l.id_lang = '.(int)$id_lang .
|
|
(!is_null($id_parent) ?' AND id_parent = '.(int)$id_parent : '')
|
|
.' ORDER BY `position`'
|
|
);
|
|
}
|
|
|
|
public static function getParentFamily($id_lang) {
|
|
return self::getAll($id_lang, 0);
|
|
}
|
|
|
|
public function getTranslationsFieldsChild() {
|
|
return TRUE;
|
|
}
|
|
|
|
|
|
public static function deleteAssociations($id_category) {
|
|
return Db::getInstance()->execute('
|
|
DELETE
|
|
FROM `'._DB_PREFIX_.'category_family_association`
|
|
WHERE `id_category` ='.(int)$id_category.'
|
|
');
|
|
}
|
|
|
|
public static function addAssociations($id_category, $family) {
|
|
self::deleteAssociations($id_category);
|
|
|
|
if (isset($family[2])) {
|
|
Db::getInstance()->execute('
|
|
INSERT INTO
|
|
`'._DB_PREFIX_.'category_family_association`
|
|
(`id_category_family`, `id_category`)
|
|
VALUES (
|
|
'.(int)$family[2].',
|
|
'.(int)$id_category.'
|
|
)
|
|
');
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
public static function loadAssociations($id_category, $id_lang) {
|
|
global $cookie, $smarty;
|
|
$category_family = array();
|
|
|
|
$family = Db::getInstance()->getValue('
|
|
SELECT `id_category_family`
|
|
FROM `'._DB_PREFIX_.'category_family_association`
|
|
WHERE `id_category` ='.(int)$id_category.'
|
|
');
|
|
|
|
if (empty($family)) {
|
|
$familles = self::getParentFamily($cookie->id_lang);
|
|
|
|
$smarty->assign(array(
|
|
'familles' => $familles
|
|
));
|
|
return $smarty->fetch(dirname(__FILE__).'/../family-add.tpl');
|
|
}
|
|
|
|
$category_family = self::sortAndLoadAssociation($family, $cookie->id_lang);
|
|
|
|
$smarty->assign(array(
|
|
'id_parent_association' => $id_parent_association,
|
|
'category_family' => $category_family
|
|
));
|
|
|
|
return $smarty->fetch(dirname(__FILE__).'/../family.tpl');
|
|
}
|
|
|
|
public static function getIdParentAssociation($families) {
|
|
return (int) Db::getInstance()->getValue('
|
|
SELECT `id_category_family`
|
|
FROM `'._DB_PREFIX_.'category_family`
|
|
WHERE `id_parent` = 0
|
|
AND `id_category_family` IN('.implode(',', $families).')
|
|
');
|
|
}
|
|
|
|
public static function sortAndLoadAssociation($family, $id_lang) {
|
|
$family_object = new CategoryFamilyCore($family);
|
|
|
|
$data = array();
|
|
$data[0]['id_select'] = $family_object->id_category_family;
|
|
$data[0]['data'] = self::getAll($id_lang, $family_object->id_parent);
|
|
|
|
$parent = self::loadReverse($family_object, $id_lang);
|
|
|
|
$load_family = array_merge($data, $parent);
|
|
return array_reverse($load_family);
|
|
}
|
|
|
|
public static function loadReverse($family_object, $id_lang, $index = 0) {
|
|
$data = array();
|
|
$familyParent = new CategoryFamilyCore($family_object->id_parent);
|
|
|
|
$parent_info[$index]['id_select'] = $familyParent->id_category_family;
|
|
$parent_info[$index]['data'] = self::getAll($id_lang, $familyParent->id_parent);
|
|
|
|
if ($familyParent->parent != 0) {
|
|
self::loadReverse($familyParent, $id_lang, $index+1);
|
|
}
|
|
|
|
return $parent_info;
|
|
}
|
|
|
|
}
|