toutpratique/modules/advmanufacturer/classes/AdvManu.php

209 lines
5.8 KiB
PHP
Raw Permalink Normal View History

2016-06-08 17:36:29 +02:00
<?php
class AdvManu extends ObjectModel {
public $id_advmanu;
public $position;
public $active;
public $id_brand;
public $link;
public $title;
public $url;
public static $definition = array(
'table' => 'advmanufacturer',
'primary' => 'id_advmanu',
'multilang' => TRUE,
'fields' => array(
'id_advmanu' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
'position' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isInt'),
'id_brand' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
'link' => array('type' => self::TYPE_BOOL, 'validate' => 'isInt'),
// Lang fields
'title' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isGenericName', 'size' => 255),
'url' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isUrl', 'size' => 255),
)
);
public function __construct($id = NULL, $id_lang = NULL, $id_shop = NULL) {
parent::__construct($id, $id_lang, $id_shop);
$this->image_dir = _PS_IMG_DIR_ . 'manufacturer/';
}
public function add($null_values = false, $autodate = true)
{
$result = parent::add($null_values, $autodate);
Hook::exec('actionRefreshManufacturer');
return $result;
}
public function update($null_values = FALSE) {
$result = parent::update($null_values);
Hook::exec('actionRefreshManufacturer');
return $result;
}
public function delete($null_values = FALSE) {
$result = parent::delete($null_values);
Hook::exec('actionRefreshManufacturer');
return $result;
}
public static function getAllBrands()
{
$brands = Db::getInstance()->executeS('
SELECT m.`id_manufacturer` as id, m.`name`
FROM `' . _DB_PREFIX_ . 'manufacturer` m
');
return $brands;
}
public static function getSlides()
{
$context = Context::getContext();
$brands = Db::getInstance()->executeS('
SELECT *
FROM `'._DB_PREFIX_.'advmanufacturer` adv
JOIN `'._DB_PREFIX_.'advmanufacturer_lang` advl ON adv.`id_advmanu` = advl.`id_advmanu` AND id_lang = '. (int)$context->language->id . '
WHERE adv.`active` = 1
ORDER BY `position` ASC
');
foreach($brands as $key => $brand)
{
if(!empty($brand['id_brand']) && $brand['id_brand'] != 0)
{
$brandInfo = new Manufacturer($brand['id_brand'], Context::getContext()->language->id);
$brands[$key]['title'] = $brandInfo->name;
if($brand['link'])
{
$brands[$key]['url'] = Context::getContext()->link->getManufacturerLink($brandInfo->id);
}
}
if(file_exists(_PS_IMG_DIR_ . 'manufacturer/' . $brand['id_advmanu'] . '.jpg'))
{
$brands[$key]['img'] = 'manufacturer/' . $brand['id_advmanu'] . '.jpg';
}
elseif(file_exists(_PS_IMG_DIR_ . 'm/' . $brand['id_brand'] . '.jpg'))
{
$brands[$key]['img'] = 'm/' . $brand['id_brand'] . '.jpg';
}
else
{
unset($brands[$key]);
}
}
return $brands;
}
public function deleteImage($force_delete = false)
{
if (!$this->id) {
return false;
}
$imgToDelete = Tools::getValue('imgName', false);
if ($imgToDelete === false) {
return parent::deleteImage($force_delete = false);
}
if ($force_delete || !$this->hasMultishopEntries()) {
/* Deleting object images and thumbnails (cache) */
if ($this->image_dir) {
if (file_exists($this->image_dir.$this->id.'-'.$imgToDelete.'.'.$this->image_format)
&& !unlink($this->image_dir.$this->id.'-'.$imgToDelete.'.'.$this->image_format)) {
return false;
}
}
if (file_exists(_PS_TMP_IMG_DIR_.$this->def['table'].'_'.$this->id.'-'.$imgToDelete.'.'.$this->image_format)
&& !unlink(_PS_TMP_IMG_DIR_.$this->def['table'].'_'.$this->id.'-'.$imgToDelete.'.'.$this->image_format)) {
return false;
}
if (file_exists(_PS_TMP_IMG_DIR_.$this->def['table'].'_mini_'.$this->id.'-'.$imgToDelete.'.'.$this->image_format)
&& !unlink(_PS_TMP_IMG_DIR_.$this->def['table'].'_mini_'.$this->id.'-'.$imgToDelete.'.'.$this->image_format)) {
return false;
}
}
return true;
}
public function cleanPositions(){
return Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'advmanufacturer`
SET `position`= `position` - 1
WHERE `id_advmanu` = '.(int)$this->id_advmanu.'
AND `position` > '.(int)$this->position);
}
public function updatePosition($way, $position)
{
$sql = 'SELECT `position`, `id_advmanu`
FROM `'._DB_PREFIX_.'advmanufacturer`
ORDER BY `position` ASC';
if (!$res = Db::getInstance()->executeS($sql))
return false;
foreach ($res as $row)
if ((int)$row['id_advmanu'] == (int)$this->id_advmanu)
$moved_row = $row;
if (!isset($moved_row) || !isset($position))
return false;
// < and > statements rather than BETWEEN operator
// since BETWEEN is treated differently according to databases
$res = Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'advmanufacturer`
SET `position`= `position` '.($way ? '- 1' : '+ 1').'
WHERE `position`
'.($way
? '> '.(int)$moved_row['position'].' AND `position` <= '.(int)$position
: '< '.(int)$moved_row['position'].' AND `position` >= '.(int)$position)
)
&& Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'advmanufacturer`
SET `position` = '.(int)$position.'
WHERE `id_advmanu`='.(int)$moved_row['id_advmanu']
);
$this->refreshPositions();
Hook::exec('actionRefreshManufacturer');
return $res;
}
public function refreshPositions(){
$sql = 'SELECT `id_advmanu`
FROM `'._DB_PREFIX_.'advmanufacturer`
ORDER BY `position` ASC';
if (!$blocks = Db::getInstance()->executeS($sql))
return false;
$pos=0;
foreach ($blocks as $block) {
Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'advmanufacturer`
SET `position` = '.(int)$pos.'
WHERE `id_advmanu`='.(int)$block['id_advmanu']);
$pos++;
}
}
}