Files
2025-04-01 00:38:54 +02:00

256 lines
10 KiB
PHP

<?php
class Sticker
{
public static $positions = array(
1 => 'Góra lewa',
2 => 'Góra środek',
3 => 'Góra prawa',
4 => 'Środek lewa',
5 => 'Środek',
6 => 'Środek prawa',
7 => 'Doł lewa',
8 => 'Dół środek',
9 => 'Dół prawa'
);
public static function addSticker($files, $priceOn = 0, $priceMin = 0, $priceMax = 0, $pricePos = 0, $qtyOn = 0, $qtyMin = 0, $qtyMax = 0, $qtyPos = 0, $manuOn = 0, $idManu = 0, $manuPos = 0, $posNew = 0, $showNew = 0)
{
if (is_array($files)) {
Db::getInstance()->insert('phstickers', array(
'price_on' => $priceOn,
'price_min' => $priceMin,
'price_max' => $priceMax,
'qty_on' => $qtyOn,
'qty_min' => $qtyMin,
'qty_max' => $qtyMax,
'manu_on' => $manuOn,
'position_price' => $pricePos,
'position_qty' => $qtyPos,
'position_manu' => $manuPos,
'id_manufacturer' => $idManu,
'position_new' => $posNew,
'show_new' => $showNew,
'date_add' => date('Y-m-d H:i:s')
));
$id_sticker = Db::getInstance()->Insert_ID();
foreach ($files as $key => $file) {
$id_lang = (int)$key;
$name = $file['name'];
$destination = __DIR__.'/../stickers/'.$key.'/'.$name;
if (!is_dir(__DIR__.'/../stickers/'.$id_lang)) {
if (!mkdir($concurrentDirectory = __DIR__ . '/../stickers/' . $id_lang) && !is_dir($concurrentDirectory)) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $concurrentDirectory));
}
}
if (move_uploaded_file($file['tmp_name'], $destination)) {
Db::getInstance()->insert('phstickers_lang', array(
'id_sticker' => $id_sticker,
'file_name' => $name,
'id_lang' => $id_lang
));
}
}
}
}
public static function editSticker($id_sticker, $files, $priceOn = 0, $priceMin = 0, $priceMax = 0, $pricePos = 0, $qtyOn = 0, $qtyMin = 0, $qtyMax = 0, $qtyPos = 0, $manuOn = 0, $idManu = 0, $manuPos = 0, $showNew = 0, $posNew = 0)
{
Db::getInstance()->update('phstickers', array(
'price_on' => $priceOn,
'price_min' => $priceMin,
'price_max' => $priceMax,
'qty_on' => $qtyOn,
'qty_min' => $qtyMin,
'qty_max' => $qtyMax,
'manu_on' => $manuOn,
'position_price' => $pricePos,
'position_qty' => $qtyPos,
'position_manu' => $manuPos,
'id_manufacturer' => $idManu,
'position_new' => $posNew,
'show_new' => $showNew,
), '`id_sticker` = '.(int)$id_sticker);
if (is_array($files)) {
foreach ($files as $key => $file) {
$id_lang = (int)$key;
$name = $file['name'];
$destination = __DIR__.'/../stickers/'.$key.'/'.$name;
$update = false;
$sticker = Sticker::getSticker((int)$id_sticker, (int)$id_lang);
if (isset($sticker['langs'][(int)$id_lang]) && !empty($sticker['langs'][(int)$id_lang])) {
$update = true;
}
if (!is_dir(__DIR__.'/../stickers/'.$id_lang)) {
if (!mkdir($concurrentDirectory = __DIR__ . '/../stickers/' . $id_lang) && !is_dir($concurrentDirectory)) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $concurrentDirectory));
}
}
if (move_uploaded_file($file['tmp_name'], $destination)) {
if ($update) {
Db::getInstance()->update('phstickers_lang', array(
'file_name' => $name
), '`id_sticker` = '.(int)$id_sticker.' AND `id_lang` = '.(int)$id_lang);
} else {
Db::getInstance()->insert('phstickers_lang', array(
'id_sticker' => $id_sticker,
'file_name' => $name,
'id_lang' => $id_lang
));
}
}
}
}
}
public static function deleteSticker($id_sticker)
{
$sticker = Sticker::getSticker((int)$id_sticker);
if (!empty($sticker)) {
foreach ($sticker['langs'] as $lang) {
$dir = __DIR__.'/../stickers/'.$lang['id_lang'].'/';
unlink($dir.$lang['file_name']);
Db::getInstance()->delete('phstickers_lang', 'id_sticker = '.(int)$id_sticker.' AND id_lang = '.(int)$lang['id_lang']);
}
Db::getInstance()->delete('phstickers', 'id_sticker = '.(int)$id_sticker);
return true;
}
return false;
}
public static function getSticker($id_sticker, $id_lang = false)
{
$sticker = DB::getInstance()->getRow('SELECT * FROM `'._DB_PREFIX_.'phstickers` WHERE `id_sticker` = '.(int)$id_sticker);
if (!empty($sticker)) {
$sticker_lang = DB::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'phstickers_lang` WHERE `id_sticker` = '.(int)$id_sticker.($id_lang ? ' AND `id_lang` = '.(int)$id_lang : ''));
if (!$id_lang) {
$langs = array();
if (!empty($sticker_lang)) {
foreach ($sticker_lang as $item) {
$langs[$item['id_lang']] = $item;
}
}
} else {
$langs = $sticker_lang[0];
}
$sticker['langs'] = $langs;
}
return $sticker;
}
public static function getAllStickers($id_lang = 1)
{
$return = Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'phstickers` s LEFT JOIN `'._DB_PREFIX_.'phstickers_lang` sl ON sl.`id_sticker` = s.`id_sticker` WHERE sl.`id_lang` = '.(int)$id_lang);
if (!empty($return)) {
foreach ($return as &$r) {
$mname = '';
if ($r['id_manufacturer'] > 0) {
$manufacturer = new Manufacturer((int)$r['id_manufacturer'], (int)$id_lang);
$mname = $manufacturer->name;
}
$r['manufacturer_name'] = $mname;
}
}
return $return;
}
public static function orderFiles($files)
{
$shuffleFile = array();
if (is_array($files)) {
$mainKey = key($files);
foreach ($files[$mainKey] as $key => $file) {
if (is_array($file)) {
foreach ($file as $ki => $item) {
$shuffleFile[$ki][$key] = $item;
}
}
}
}
return $shuffleFile;
}
public static function addStickerProduct($id_product, $stickers, $positions, $inherit)
{
if (is_array($stickers)) {
foreach ($stickers as $s) {
if ($s > 0) {
DB::getInstance()->insert('phstickers_product', array(
'id_sticker' => (int)$s,
'id_product' => (int)$id_product,
'position' => (int)$positions[(int)$s],
'inherit' => $inherit
));
}
}
}
}
public static function deleteProductSticker($id_product)
{
Db::getInstance()->delete('phstickers_product', 'id_product = '.(int)$id_product);
}
public static function getProductSticker($id_product)
{
return Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'phstickers_product` WHERE `id_product` = '.(int)$id_product);
}
public static function addCategorySticker($categorys, $stickers, $positions)
{
if (is_array($categorys)) {
foreach ($categorys as $category) {
if (!empty($stickers)) {
foreach ($stickers as $sticker) {
DB::getInstance()->insert('phstickers_category', array(
'id_sticker' => (int)$sticker,
'id_category' => (int)$category,
'position' => $positions[$sticker]
));
}
}
}
}
}
public static function editStickersCategory($id_category, $stickers, $positions)
{
if (!empty($stickers)) {
foreach ($stickers as $sticker) {
DB::getInstance()->insert('phstickers_category', array(
'id_sticker' => (int)$sticker,
'id_category' => (int)$id_category,
'position' => $positions[$sticker]
));
}
}
}
public static function getAllCategorySticker()
{
return Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'phstickers_category` ORDER BY `id_category` ASC');
}
public static function getCategorySticker($id_sticker_category)
{
return Db::getInstance()->getRow('SELECT * FROM `'._DB_PREFIX_.'phstickers_category` WHERE `id_sticker_category` = '.(int)$id_sticker_category);
}
public static function getCategoryStickers($id_category)
{
return Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'phstickers_category` WHERE `id_category` = '.(int)$id_category);
}
public static function deleteCategorySticker($id_sticker_category)
{
return Db::getInstance()->delete('phstickers_category', 'id_sticker_category = '.(int)$id_sticker_category);
}
public static function deleteCategoryStickers($id_category)
{
return Db::getInstance()->delete('phstickers_category', 'id_category = '.(int)$id_category);
}
}