roykin/modules/advconstructor/classes/AdvConstructorHook.php
2017-06-14 11:41:31 +02:00

94 lines
3.2 KiB
PHP
Executable File

<?php
class AdvConstructorHook extends ObjectModel
{
public $id_advconstructor_hook;
public $id_advconstructor;
public $id_hook;
public $position = 0;
public static $definition = array(
'table' => 'advconstructor_hook',
'primary' => 'id_advconstructor_hook',
'fields' => array(
'id_advconstructor' => array(
'type' => ObjectModel :: TYPE_INT
),
'id_hook' => array(
'type' => ObjectModel :: TYPE_INT
),
'position' => array(
'type' => ObjectModel :: TYPE_INT
)
)
);
public function updatePosition($way, $position)
{
$sql = 'SELECT `position`, `id_advconstructor_hook`
FROM `'._DB_PREFIX_.'advconstructor_hook`
WHERE `id_hook` = '.(int)$this->id_hook.'
ORDER BY `position` ASC';
$res = Db::getInstance()->executeS($sql);
if (!$res)
return false;
foreach ($res as $row)
if ((int)$row['id_advconstructor_hook'] == (int)$this->id)
$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_.'advconstructor_hook`
SET `position`= `position` '.($way ? '- 1' : '+ 1').'
WHERE `id_hook` = '.(int)$this->id_hook.'
AND `position`
'.($way
? '> '.(int)$moved_row['position'].' AND `position` <= '.(int)$position
: '< '.(int)$moved_row['position'].' AND `position` >= '.(int)$position)
)
&& Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'advconstructor_hook`
SET `position` = '.(int)$position.'
WHERE `id_advconstructor_hook`='.(int)$moved_row['id_advconstructor_hook']
);
$this->refreshPositions();
return $res;
}
public function refreshPositions(){
$sql = 'SELECT `id_advconstructor_hook`
FROM `'._DB_PREFIX_.'advconstructor_hook`
WHERE `id_hook` = '.(int)$this->id_hook.'
ORDER BY `position` ASC';
if (!$blocks = Db::getInstance()->executeS($sql))
return false;
$pos=0;
foreach ($blocks as $block) {
Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'advconstructor_hook`
SET `position` = '.(int)$pos.'
WHERE `id_advconstructor_hook`='.(int)$block['id_advconstructor_hook']);
$pos++;
}
}
public static function associationHookExist( $id_advconstructor, $id_hook )
{
$sql = 'SELECT `id_advconstructor_hook`
FROM `'._DB_PREFIX_.'advconstructor_hook`
WHERE `id_hook` = '.(int) $id_hook.'
AND `id_advconstructor` = '.(int) $id_advconstructor;
if (!$id = Db::getInstance()->getRow($sql)) {
return false;
} else {
return $id['id_advconstructor_hook'];
}
}
}