first commit
This commit is contained in:
271
admin/class/class.BaseAdminPhotos.php
Normal file
271
admin/class/class.BaseAdminPhotos.php
Normal file
@@ -0,0 +1,271 @@
|
||||
<?php
|
||||
|
||||
class BaseAdminPhotos extends Frame
|
||||
{
|
||||
use DatabaseCommunication;
|
||||
|
||||
protected $template = 'adminPhotos.tpl';
|
||||
protected $relation_table_name;
|
||||
protected $photo_type;
|
||||
protected $tablefield;
|
||||
protected $photo_nametype;
|
||||
protected $photo_admin_name;
|
||||
protected $copy_from;
|
||||
|
||||
public function get_vars()
|
||||
{
|
||||
$vars = parent::get_vars();
|
||||
|
||||
$pageVars = getVal('body', $vars, []);
|
||||
|
||||
$acn = $this->getAction();
|
||||
$ID = $this->getID();
|
||||
$pageVars['acn'] = $acn;
|
||||
$pageVars['s'] = getVal('s');
|
||||
|
||||
$SQL = sqlQuery("SELECT ph.id, ph.descr,ph.source, ph.image_2, MAX(php.show_in_lead) as show_in_lead, ph.date,
|
||||
MAX(php.position) as position, ph.data as data, ph.date_update
|
||||
FROM photos AS ph, {$this->relation_table_name} AS php
|
||||
WHERE ph.id=php.id_photo AND php.{$this->tablefield}=:id
|
||||
GROUP BY ph.id
|
||||
ORDER BY position ASC", ['id' => $ID]); // , php.date_added ASC;
|
||||
|
||||
$photos = [];
|
||||
foreach ($SQL as $row) {
|
||||
$row['IDph'] = $row['id'];
|
||||
$row['img'] = getImage($row['id'], $row['image_2'], $row['source'], $this->photo_type, $row['descr'], strtotime($row['date_update']));
|
||||
$row['width'] = ($row['img']['width'] <= 200) ? $row['img']['width'] : 200;
|
||||
$this->unserializeCustomData($row);
|
||||
if (findModule('products_variations_photos') && $this->relation_table_name == 'photos_products_relation') {
|
||||
$row['varSel'] = [];
|
||||
$SQLp = sqlQuery("SELECT id_variation FROM {$this->relation_table_name} pp WHERE pp.id_photo=:id_photo AND pp.{$this->tablefield}=:ID AND pp.id_variation IS NOT NULL", ['id_photo' => $row['IDph'], 'ID' => $ID]);
|
||||
foreach ($SQLp as $rowp) {
|
||||
$row['varSel'][] = $rowp['id_variation'];
|
||||
}
|
||||
if (empty($row['varSel'])) {
|
||||
$row['varSel'] = ['-1'];
|
||||
}
|
||||
}
|
||||
|
||||
$photos[] = $row;
|
||||
}
|
||||
$pageVars['photos'] = $photos;
|
||||
|
||||
if (findModule('products_variations_photos') && $this->relation_table_name == 'photos_products_relation') {
|
||||
$variations = ['-1' => 'Žádná přiřazená varianta'];
|
||||
if (findModule('products_variations_photos')) {
|
||||
$SQLv = sqlQuery("SELECT id, title
|
||||
FROM products_variations pv
|
||||
WHERE pv.{$this->tablefield}=:ID", ['ID' => $ID]);
|
||||
while (($row = sqlFetchAssoc($SQLv)) !== false) {
|
||||
$variations[$row['id']] = $row['title'];
|
||||
}
|
||||
}
|
||||
$pageVars['variations'] = $variations;
|
||||
}
|
||||
$pageVars['ID'] = $ID;
|
||||
$pageVars['tablefield'] = $this->tablefield;
|
||||
$pageVars['copy_from'] = $this->copy_from;
|
||||
$pageVars['photo_nametype'] = $this->photo_nametype;
|
||||
$pageVars['search_field'] = $this->search_field;
|
||||
|
||||
$vars['body'] = $pageVars;
|
||||
|
||||
return $vars;
|
||||
}
|
||||
|
||||
public function handle()
|
||||
{
|
||||
global $cfg;
|
||||
parent::handle();
|
||||
$ID = $this->getID();
|
||||
if (getVal('data') && $ID) {
|
||||
if (findModule('products_variations_photos') && $this->relation_table_name == 'photos_products_relation') {
|
||||
$photoVariations = getVal('id_variation', null, []);
|
||||
|
||||
foreach ($photoVariations as $IDph => $variations) {
|
||||
sqlQuery("DELETE FROM {$this->relation_table_name} WHERE id_photo=:IDph AND {$this->tablefield} = :ID", ['ID' => $ID, 'IDph' => $IDph]);
|
||||
|
||||
foreach ($variations as $variation) {
|
||||
$insert = [
|
||||
'id_photo' => $IDph,
|
||||
$this->tablefield => $ID,
|
||||
'date_added' => 'NOW()',
|
||||
];
|
||||
|
||||
if ($variation > 0) {
|
||||
$insert['id_variation'] = $variation;
|
||||
} else { // maze to duplicity z databaze
|
||||
sqlQuery("DELETE FROM {$this->relation_table_name} WHERE id_photo=:IDph AND {$this->tablefield} = :ID", ['ID' => $ID, 'IDph' => $IDph]);
|
||||
}
|
||||
|
||||
sqlQueryBuilder()->insert($this->relation_table_name)
|
||||
->directValues($insert)->onDuplicateKeyUpdate([])->execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$data = getVal('data', null, []);
|
||||
$pos = 0;
|
||||
foreach ($data as $id => $photo) {
|
||||
if (!empty($photo['delete']) || !$id) {
|
||||
if ($id > 0) {
|
||||
$this->handleDeleteRelationPhoto($id);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
$values = ['position' => $pos++];
|
||||
if (isset($cfg['Photo']['kind']) && $this->tablefield == 'id_product') {
|
||||
$values['show_in_lead'] = $photo['show_in_lead'];
|
||||
}
|
||||
$this->serializeCustomData($photo);
|
||||
$this->updateSQL('photos', $photo, ['id' => $id], ['position', 'show_in_lead']);
|
||||
$this->updateSQL($this->relation_table_name, $values, [$this->tablefield => $ID, 'id_photo' => $id]);
|
||||
}
|
||||
|
||||
Photos::checkLeadPhoto($this->relation_table_name, $this->tablefield, $ID);
|
||||
|
||||
$this->returnOK('Uloženo');
|
||||
}
|
||||
if (!empty($_FILES)) {
|
||||
global $cfg;
|
||||
|
||||
sqlGetConnection()->transactional(function () use ($ID) {
|
||||
$img = new Photos($this->photo_admin_name ?: lcfirst($this->photo_nametype));
|
||||
$img->newImage();
|
||||
|
||||
// uploadovat velky obrazek
|
||||
$img->uploadPhotoOrVideo($_FILES['qqfile'], ($_REQUEST['qqfilename'] ?? false) ? $_REQUEST['qqfilename'] : '');
|
||||
|
||||
// ID nove fotky
|
||||
$IDph = $img->getID();
|
||||
|
||||
if ($this->photo_nametype == 'Product') {
|
||||
$img->{"insert{$this->photo_nametype}Relation"}($IDph, $ID, 'N', 'Y');
|
||||
} else {
|
||||
$img->insertRelation($this->relation_table_name, $IDph, $this->tablefield, $ID, 'N');
|
||||
}
|
||||
});
|
||||
|
||||
header('Content-type: text/plain');
|
||||
exit('{"success":true}');
|
||||
}
|
||||
}
|
||||
|
||||
public function handleDeleteRelationPhoto($IDph)
|
||||
{
|
||||
$IDpr = $this->getID();
|
||||
|
||||
sqlQuery("DELETE FROM {$this->relation_table_name} WHERE id_photo='{$IDph}' AND {$this->tablefield}=:IDpr", ['IDpr' => $IDpr]);
|
||||
}
|
||||
|
||||
public function handleDeletePhoto()
|
||||
{
|
||||
$IDph = getVal('IDph');
|
||||
|
||||
sqlQuery('DELETE FROM photos WHERE id=:IDph', ['IDph' => $IDph]);
|
||||
|
||||
$this->returnOK();
|
||||
}
|
||||
|
||||
public function handleInsertPhoto()
|
||||
{
|
||||
$errors = [];
|
||||
$data = getVal('data');
|
||||
$id_photos = explode(',', $data['id_photo']);
|
||||
$count = sqlQueryBuilder()->select('COUNT(*)')
|
||||
->from($this->relation_table_name)
|
||||
->where(\Query\Operator::equals([$this->tablefield => $this->getID()]))
|
||||
->execute()
|
||||
->fetchColumn();
|
||||
foreach ($id_photos as $id_photo) {
|
||||
$data = [
|
||||
$this->tablefield => $this->getID(),
|
||||
'id_photo' => $id_photo,
|
||||
'date_added' => date('Y-m-d H:i:s'),
|
||||
];
|
||||
|
||||
if (empty($data['id_photo'])) {
|
||||
$this->returnError('Nevybrána žádna fotografie!');
|
||||
}
|
||||
|
||||
if (sqlQueryBuilder()->select('COUNT(*)')
|
||||
->from($this->relation_table_name)
|
||||
->where(\Query\Operator::equals(['id_photo' => $id_photo, $this->tablefield => $data[$this->tablefield]]))
|
||||
->execute()
|
||||
->fetchColumn()) {
|
||||
$errors[] = "Fotografie s ID {$data['id_photo']} je již přiřazena!";
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
$this->insertSQL($this->relation_table_name, $data);
|
||||
} catch (Exception $e) {
|
||||
switch (intval($e->getPrevious()->errorInfo[1])) {
|
||||
case 1062:
|
||||
$errors[] = "Fotografie s ID {$data['id_photo']} je již přiřazena!";
|
||||
// no break
|
||||
default:
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
$this->updateSQL(
|
||||
$this->relation_table_name,
|
||||
['position' => $count, 'show_in_lead' => $count == 0 ? 'Y' : 'N'],
|
||||
[$this->tablefield => $data[$this->tablefield], 'id_photo' => $data['id_photo']]
|
||||
);
|
||||
$count++;
|
||||
}
|
||||
|
||||
if ($errors) {
|
||||
$this->returnError(join(' ', $errors));
|
||||
}
|
||||
|
||||
$this->returnOK();
|
||||
}
|
||||
|
||||
public function handleCopyPhotos()
|
||||
{
|
||||
$data = getVal('data');
|
||||
$data['ID'] = $this->getID();
|
||||
|
||||
$data['count'] = returnSQLResult("SELECT COUNT(*) FROM {$this->relation_table_name} WHERE {$this->tablefield}=:field", ['field' => $this->getID()]);
|
||||
|
||||
sqlQuery("REPLACE INTO {$this->relation_table_name}
|
||||
(id_photo, {$this->tablefield}, date_added, show_in_lead, position)
|
||||
SELECT id_photo, :ID, NOW(), 'N', position+:count
|
||||
FROM {$this->relation_table_name}
|
||||
WHERE {$this->tablefield}=:{$this->tablefield}", $data);
|
||||
|
||||
Photos::checkLeadPhoto($this->relation_table_name, $this->tablefield, $this->getID());
|
||||
|
||||
$this->returnOK('Obrázky byly zkopírovány');
|
||||
}
|
||||
|
||||
public function handleMovePhoto()
|
||||
{
|
||||
$item = getVal('moved_item');
|
||||
|
||||
if (!empty($item)) {
|
||||
Photos::checkLeadPhoto($this->relation_table_name, $this->tablefield, $item[$this->tablefield]);
|
||||
|
||||
sqlQuery("UPDATE {$this->relation_table_name} SET position=position+1000 WHERE position >= :position AND {$this->tablefield}=:id", ['id' => $item[$this->tablefield], 'position' => $item['position']]);
|
||||
|
||||
sqlQuery("UPDATE {$this->relation_table_name} SET position=:position WHERE id_photo=:id_photo AND {$this->tablefield}=:id", ['id' => $item[$this->tablefield], 'position' => $item['position'], 'id_photo' => $item['id_photo']]);
|
||||
|
||||
Photos::checkLeadPhoto($this->relation_table_name, $this->tablefield, $item[$this->tablefield]);
|
||||
}
|
||||
}
|
||||
|
||||
public static function checkPhotos($table, $id_field, $id_photo)
|
||||
{
|
||||
$sql = sqlQuery("SELECT {$id_field} FROM {$table} WHERE id_photo=:idph", ['idph' => $id_photo]);
|
||||
$array = sqlFetchAll($sql, [$id_field => $id_field]);
|
||||
sqlQuery("DELETE FROM {$table} WHERE id_photo=:idph", ['idph' => $id_photo]);
|
||||
|
||||
foreach ($array as $ID) {
|
||||
Photos::checkLeadPhoto($table, $id_field, $ID);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user