Initial commit
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
|
||||
class AttributeField {
|
||||
public string $name;
|
||||
public array $params;
|
||||
public $content = "";
|
||||
|
||||
public function __construct($params = array())
|
||||
{
|
||||
$this->params = $params;
|
||||
}
|
||||
|
||||
public function set_content($content)
|
||||
{
|
||||
$this->content = $content;
|
||||
}
|
||||
|
||||
public function set_name($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
public function render_edit()
|
||||
{
|
||||
global $b;
|
||||
$item_id = $_GET["item_id"];
|
||||
if(!isset($item_id)) {
|
||||
echo "Редактировать атрибуты можно только после создания объекта.";
|
||||
return;
|
||||
}
|
||||
$item = $b->get_item_by_id($item_id);
|
||||
|
||||
$search = new Search(array(
|
||||
"class" => "Attribute",
|
||||
));
|
||||
|
||||
$items = $search->collect();
|
||||
$attributes = $item->get_parent_items("Attribute");
|
||||
$ai = $item->get_child_items("Attribute_Item");
|
||||
?>
|
||||
|
||||
<?php if(count($attributes)) { ?>
|
||||
<div class="bive-attributes">
|
||||
<?php foreach ($attributes as $arg => $attribute) { ?>
|
||||
<div class="bive-attributes_row">
|
||||
<div class="bive-attributes__wrapper">
|
||||
<div class="bive-attributes__title">
|
||||
<?= $attribute->get_item_name(); ?>
|
||||
</div>
|
||||
<div class="bive-attributes__items">
|
||||
<?php foreach ($ai as $key => $a) { ?>
|
||||
<?php if($a->parent_id != $attribute->get_item_id()) continue; ?>
|
||||
<div class="bive-attributes__item">
|
||||
<div class="bive-attributes__item-text"><?= $a->get_item_name(); ?></div>
|
||||
<button type="submit" class="bive-attributes__item-remove" name="<?= $this->name . "[" . $attribute->get_item_id() . "][remove]"; ?>" value="<?= $a->get_item_id(); ?>">x</button>
|
||||
</div>
|
||||
<?php } ?>
|
||||
<div class="bive-attributes__item-new">
|
||||
<input class="bive-attributes__item-input" type="text" name="<?= $this->name . "[" . $attribute->get_item_id() . "][new]"; ?>" placeholder="Новый атрибут">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<button type="submit" class="bive-attributes__remove" name="<?= $this->name . "[remove]"; ?>" value="<?= $attribute->get_item_id(); ?>">X</button>
|
||||
</div>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<div>
|
||||
<select class="bive-select" name="<?= $this->name . "[new][attribute]"; ?>">
|
||||
<?php foreach ($items as $arg => $item) { ?>
|
||||
<option value="<?= $item->get_item_id(); ?>"><?= $item->get_item_name(); ?></option>
|
||||
<?php } ?>
|
||||
</select>
|
||||
<button type="submit" class="btn btn-primary mb-1 ml-1 mt-1" name="<?= $this->name . "[new][button]"; ?>" value="1">Добавить</button>
|
||||
</div>
|
||||
|
||||
<?php
|
||||
}
|
||||
|
||||
public function render_value()
|
||||
{
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
public function render_db_value()
|
||||
{
|
||||
global $b;
|
||||
$item_id = $_GET["item_id"];
|
||||
if(!isset($item_id)) return "";
|
||||
$item = $b->get_item_by_id($item_id);
|
||||
if (is_array($this->content)) {
|
||||
foreach ($this->content as $key => $data) {
|
||||
if ((string) $key == "new" && isset($data["button"])) {
|
||||
$item->set_parent($data["attribute"], false);
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((string) $key == "remove") {
|
||||
$item->clear_parent($data);
|
||||
continue;
|
||||
}
|
||||
|
||||
if((string) $data["new"] !== "") {
|
||||
$parent_id = $key;
|
||||
$slug = $this->translit_sef($data["new"]);
|
||||
$search = new Search(array(
|
||||
"class" => "Attribute_Item",
|
||||
"terms" => array("item_slug" => $slug),
|
||||
"parent_id" => $parent_id
|
||||
));
|
||||
$items = $search->collect();
|
||||
if(!$items) {
|
||||
$ai = new Attribute_Item(0);
|
||||
$ai->create($data["new"], "");
|
||||
$ai->set_field("item_slug", $slug);
|
||||
$ai->set_parent($parent_id, true);
|
||||
$ai->set_parent($item_id);
|
||||
} else {
|
||||
$items[0]->set_parent($item_id);
|
||||
}
|
||||
}
|
||||
|
||||
if((string) $data["remove"] !== "") {
|
||||
$parent = $b->get_item_by_id($data["remove"]);
|
||||
$parent->clear_parent($item_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private function translit_sef($value)
|
||||
{
|
||||
$converter = array(
|
||||
'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd',
|
||||
'е' => 'e', 'ё' => 'e', 'ж' => 'zh', 'з' => 'z', 'и' => 'i',
|
||||
'й' => 'y', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n',
|
||||
'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't',
|
||||
'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c', 'ч' => 'ch',
|
||||
'ш' => 'sh', 'щ' => 'sch', 'ь' => '', 'ы' => 'y', 'ъ' => '',
|
||||
'э' => 'e', 'ю' => 'yu', 'я' => 'ya',
|
||||
);
|
||||
|
||||
$value = mb_strtolower($value);
|
||||
$value = strtr($value, $converter);
|
||||
$value = mb_ereg_replace('[^-0-9a-z]', '-', $value);
|
||||
$value = mb_ereg_replace('[-]+', '-', $value);
|
||||
$value = trim($value, '-');
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user