Files
hotel/modules/wkfooterpaymentblock/classes/WkFooterPaymentBlockInfo.php

130 lines
4.6 KiB
PHP

<?php
/**
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License version 3.0
* that is bundled with this package in the file LICENSE.md
* It is also available through the world-wide-web at this URL:
* https://opensource.org/license/osl-3-0-php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to support@qloapps.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this module to a newer
* versions in the future. If you wish to customize this module for your needs
* please refer to https://store.webkul.com/customisation-guidelines for more information.
*
* @author Webkul IN
* @copyright Since 2010 Webkul
* @license https://opensource.org/license/osl-3-0-php Open Software License version 3.0
*/
class WkFooterPaymentBlockInfo extends ObjectModel
{
public $name;
public $active;
public $position;
public $date_add;
public $date_upd;
public static $definition = array(
'table' => 'htl_footer_payment_block_info',
'primary' => 'id_payment_block',
'fields' => array(
'name' => array('type' => self::TYPE_STRING, 'validate' => 'isCatalogName'),
'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'position' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
));
public function __construct($id = null, $id_lang = null, $id_shop = null)
{
parent::__construct($id, $id_lang, $id_shop);
$this->image_dir = _PS_MODULE_DIR_.'wkfooterpaymentblock/views/img/payment_img/';
}
/**
* Deletes current payment block from the database
* @return bool `true` if delete was successful
*/
public function delete()
{
if (!parent::delete()
|| !$this->deleteImage(true)
|| !self::cleanPositions()
) {
return false;
}
return true;
}
public function getAllPaymentBlocks($active = null, $orderBy = '', $orderWay = 'ASC')
{
$sql = 'SELECT * FROM `'._DB_PREFIX_.'htl_footer_payment_block_info` WHERE 1';
if (!is_null($active)) {
$sql .= ' AND `active` = '.(int)$active;
}
if (Validate::isOrderBy($orderBy) && Validate::isOrderBy($orderWay)) {
$sql .= ' ORDER BY '.pSQL($orderBy).' '.pSQL($orderWay);
}
return Db::getInstance()->executeS($sql);
}
public static function getHigherPosition()
{
$position = DB::getInstance()->getValue(
'SELECT MAX(`position`) FROM `'._DB_PREFIX_.'htl_footer_payment_block_info`'
);
$result = (is_numeric($position)) ? $position : -1;
return $result + 1;
}
public function updatePosition($way, $position)
{
if (!$res = Db::getInstance()->executeS(
'SELECT hpb.`id_payment_block`, hpb.`position` FROM `'._DB_PREFIX_.'htl_footer_payment_block_info` hpb
WHERE hpb.`id_payment_block` = '.(int) $this->id.' ORDER BY `position` ASC'
)
) {
return false;
}
$movedPaymentBlock = false;
foreach ($res as $paymentBlock) {
if ((int)$paymentBlock['id_payment_block'] == (int)$this->id) {
$movedPaymentBlock = $paymentBlock;
}
}
if ($movedPaymentBlock === false) {
return false;
}
return (Db::getInstance()->execute(
'UPDATE `'._DB_PREFIX_.'htl_footer_payment_block_info` SET `position`= `position` '.($way ? '- 1' : '+ 1').
' WHERE `position`'.($way ? '> '.
(int)$movedPaymentBlock['position'].' AND `position` <= '.(int)$position : '< '
.(int)$movedPaymentBlock['position'].' AND `position` >= '.(int)$position)
) && Db::getInstance()->execute(
'UPDATE `'._DB_PREFIX_.'htl_footer_payment_block_info`
SET `position` = '.(int)$position.'
WHERE `id_payment_block`='.(int)$movedPaymentBlock['id_payment_block']
));
}
/**
* Reorder payment blocks position
* Call it after deleting a payment blocks.
* @return bool $return
*/
public static function cleanPositions()
{
Db::getInstance()->execute('SET @i = -1', false);
$sql = 'UPDATE `'._DB_PREFIX_.'htl_footer_payment_block_info` SET `position` = @i:=@i+1 ORDER BY `position` ASC';
return (bool) Db::getInstance()->execute($sql);
}
}