111 lines
4.1 KiB
PHP
111 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace Websmid\CustomHtml\Model;
|
|
|
|
use ObjectModel;
|
|
use Db;
|
|
|
|
class CustomHtmlSlide extends ObjectModel
|
|
{
|
|
/** @var int */
|
|
public $id;
|
|
|
|
/** @var int */
|
|
public $id_ws_customhtml;
|
|
|
|
/** @var string */
|
|
public $title = '';
|
|
|
|
/** @var string */
|
|
public $description = '';
|
|
|
|
/** @var string */
|
|
public $extra_description = '';
|
|
|
|
/** @var string */
|
|
public $link = '';
|
|
|
|
/** @var string */
|
|
public $link_text = '';
|
|
|
|
/** @var string */
|
|
public $image = '';
|
|
|
|
/** @var int */
|
|
public $ordering = 0;
|
|
|
|
/** @var string */
|
|
public $date_add;
|
|
|
|
/** @var string */
|
|
public $date_upd;
|
|
|
|
public static $definition = [
|
|
'table' => 'ws_customhtml_slide',
|
|
'primary' => 'id_ws_customhtml_slide',
|
|
'multilang' => true,
|
|
'fields' => [
|
|
'id_ws_customhtml' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true],
|
|
'title' => ['type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => false, 'size' => 256, 'lang' => true],
|
|
'description' => ['type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => false, 'size' => 1024, 'lang' => true],
|
|
'extra_description' => ['type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => false, 'size' => 1024, 'lang' => true],
|
|
'link' => ['type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => false, 'size' => 512, 'lang' => true],
|
|
'link_text' => ['type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => false, 'size' => 128, 'lang' => true],
|
|
'image' => ['type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => false, 'size' => 255],
|
|
'ordering' => ['type' => self::TYPE_INT, 'validate' => 'isInt'],
|
|
'date_add' => ['type' => self::TYPE_DATE, 'validate' => 'isDate'],
|
|
'date_upd' => ['type' => self::TYPE_DATE, 'validate' => 'isDate'],
|
|
],
|
|
];
|
|
|
|
/**
|
|
* Admin use — loads every language into lang fields as [id_lang => value] arrays.
|
|
*/
|
|
public static function getByBlockId(int $idBlock): array
|
|
{
|
|
$ids = Db::getInstance()->executeS(
|
|
'SELECT `id_ws_customhtml_slide` FROM `' . _DB_PREFIX_ . 'ws_customhtml_slide`
|
|
WHERE `id_ws_customhtml` = ' . (int) $idBlock . '
|
|
ORDER BY `ordering` ASC'
|
|
);
|
|
|
|
return array_map(static function (array $row) {
|
|
$slide = new self((int) $row['id_ws_customhtml_slide']);
|
|
|
|
return [
|
|
'id_ws_customhtml_slide' => (int) $slide->id,
|
|
'title' => $slide->title,
|
|
'description' => $slide->description,
|
|
'extra_description' => $slide->extra_description,
|
|
'link' => $slide->link,
|
|
'link_text' => $slide->link_text,
|
|
'image' => $slide->image,
|
|
'ordering' => (int) $slide->ordering,
|
|
];
|
|
}, $ids ?: []);
|
|
}
|
|
|
|
/**
|
|
* Front-end use — resolves lang fields to plain strings for one language.
|
|
*/
|
|
public static function getByBlockIdForLang(int $idBlock, int $idLang): array
|
|
{
|
|
$result = Db::getInstance()->executeS(
|
|
'SELECT wcs.`id_ws_customhtml_slide`, wcsl.`title`, wcsl.`description`, wcsl.`extra_description`,
|
|
wcsl.`link`, wcsl.`link_text`, wcs.`image`, wcs.`ordering`
|
|
FROM `' . _DB_PREFIX_ . 'ws_customhtml_slide` wcs
|
|
LEFT JOIN `' . _DB_PREFIX_ . 'ws_customhtml_slide_lang` wcsl
|
|
ON wcsl.`id_ws_customhtml_slide` = wcs.`id_ws_customhtml_slide` AND wcsl.`id_lang` = ' . (int) $idLang . '
|
|
WHERE wcs.`id_ws_customhtml` = ' . (int) $idBlock . '
|
|
ORDER BY wcs.`ordering` ASC'
|
|
);
|
|
|
|
return $result ?: [];
|
|
}
|
|
|
|
public static function deleteByBlockId(int $idBlock): void
|
|
{
|
|
Db::getInstance()->delete('ws_customhtml_slide', '`id_ws_customhtml` = ' . (int) $idBlock);
|
|
}
|
|
}
|