78 lines
2.1 KiB
PHP
78 lines
2.1 KiB
PHP
<?php
|
|
|
|
use Query\Operator;
|
|
|
|
$main_class = 'ProductsVarLabelsValues';
|
|
|
|
class ProductsVarLabelsValues extends Window
|
|
{
|
|
use \KupShop\AdminBundle\Util\CategoryTree;
|
|
|
|
protected $tableName = 'products_variations_choices_values';
|
|
protected $nameField = 'id_label';
|
|
protected $template = 'window/productsVarLabelsValues.tpl';
|
|
|
|
protected $labelId;
|
|
|
|
private function fetchLabelData(&$data): int
|
|
{
|
|
$id = $data['id_label'] ?? null;
|
|
if (!$id) {
|
|
$id = sqlQueryBuilder()
|
|
->select('id_label')
|
|
->from($this->tableName)
|
|
->where(Operator::equals(['id' => $data['ID'] ?? null]))
|
|
->execute()
|
|
->fetchOne();
|
|
}
|
|
|
|
$data['id_label'] = $id;
|
|
|
|
return (int) $id;
|
|
}
|
|
|
|
public function getData()
|
|
{
|
|
$data = parent::getData();
|
|
$acn = $this->getAction();
|
|
$this->labelId = $this->fetchLabelData($data);
|
|
if (getVal('Submit')) {
|
|
$this->serializeCustomData($data);
|
|
|
|
if ($acn === 'add') {
|
|
if (empty($data['sort'])) {
|
|
$extraWhere = "id_label = {$this->labelId}";
|
|
$data['sort'] = sqlQuery("SELECT MAX(sort) + 1 FROM {$this->tableName} WHERE {$extraWhere}")->fetchOne();
|
|
}
|
|
}
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function handleUpdate()
|
|
{
|
|
parent::handleUpdate();
|
|
if (getVal('Submit') && $this->labelId) {
|
|
$position = 'sort';
|
|
$extraWhere = " AND id_label = {$this->labelId}";
|
|
sqlQuery("SELECT @i := -1; UPDATE {$this->tableName} SET {$position} = (select @i := @i + 1) WHERE 1{$extraWhere} ORDER BY {$position} ASC");
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public function get_vars(): array
|
|
{
|
|
$vars = parent::get_vars();
|
|
|
|
$vars['body']['data']['id_label'] = getVal('id_label') ?? $this->labelId
|
|
?? getVal('id_label', $vars['body']['data']);
|
|
$vars['body']['data']['ID'] = getVal('ID');
|
|
|
|
$this->unserializeCustomData($vars['body']['data']);
|
|
|
|
return $vars;
|
|
}
|
|
}
|