Files
BiveEngine/engine/class.prop.php
2025-12-24 19:19:01 +03:00

124 lines
4.2 KiB
PHP

<?php
defined('ROOT_DIR') || exit;
class Prop {
private Item $item;
public string $prop_key;
public string $default_value = "";
public string $title = "Prop name";
public bool $table_view = true;
public bool $index = false;
public $field;
public function __construct($item, $prop_key, $default_value, $title, $table_view = true, $field, $index = false)
{
$this->item = $item;
$this->prop_key = $prop_key;
$this->default_value = $default_value;
$this->title = $title;
$this->table_view = $table_view;
$this->field = $field;
$this->index = $index;
}
public function render_value()
{
global $b;
$field = $this->field;
$value = $this->get();
if(!isset($field)) return $value;
return $b->field_render_value($field, $this->prop_key, $value);
}
// Получить значение
public function get($new = true)
{
global $b;
$item_id = $this->item->get_item_id();
$props = $b->db_query("SELECT * FROM `bive_items_props` WHERE `item_id` = ? AND `prop_key` = ?", array($item_id, $this->prop_key), $new);
if(!count($props)) return $this->default_value;
return $props[0]["prop_value"];
}
public function add($value): bool
{
global $b;
$has = $this->has();
$field = $this->field;
$old_value = $this->get(true);
if(isset($field)) $value = $b->field_render_db_value($field, $this->prop_key, $value, $old_value);
if(gettype($value) == "NULL") $value = $this->default_value;
if($has) return $this->update($value);
return $this->create($value);
}
public function has(): bool
{
global $b;
$item_id = $this->item->get_item_id();
$props = $b->db_query("SELECT * FROM `bive_items_props` WHERE `item_id` = ? AND `prop_key` = ? LIMIT 1", array($item_id, $this->prop_key), true);
if(!count($props)) return false;
return true;
}
public function update($value): bool
{
global $b;
$item_id = $this->item->get_item_id();
$b->db_query("UPDATE `bive_items_props` SET `prop_value` = ? WHERE `item_id` = ? AND `prop_key` = ?", array($value, $item_id, $this->prop_key), true);
if($this->index) $this->index_update($value);
return true;
}
private function create($value): bool
{
global $b;
$item_id = $this->item->get_item_id();
$b->db_query("INSERT INTO `bive_items_props`(`prop_key`, `prop_value`, `item_id`) VALUES (?, ?, ?)", array($this->prop_key, $value, $item_id), true);
if($this->index) $this->index_create($value);
return true;
}
public function delete(): bool
{
global $b;
$item_id = $this->item->get_item_id();
$b->db_query("DELETE FROM `bive_items_props` WHERE `item_id` = ? AND `prop_key` = ?", array($item_id, $this->prop_key), true);
if($this->index) $this->index_delete();
return true;
}
public function index_get()
{
global $b;
$item_id = $this->item->get_item_id();
return $b->db_query("SELECT * FROM `bive_items_index` WHERE `item_id` = ? AND `prop_key` = ? LIMIT 1", array($item_id, $this->prop_key), true);
}
public function index_update($value): bool
{
global $b;
$item_id = $this->item->get_item_id();
$index = $this->index_get();
if(!count($index)) $this->index_create($value);
$b->db_query("UPDATE `bive_items_index` SET `prop_value` = ? WHERE `item_id` = ? AND `prop_key` = ?", array($value, $item_id, $this->prop_key), true);
return true;
}
private function index_create($value): bool
{
global $b;
$item_id = $this->item->get_item_id();
$b->db_query("INSERT INTO `bive_items_index`(`prop_key`, `prop_value`, `item_id`) VALUES (?, ?, ?)", array($this->prop_key, $value, $item_id), true);
return true;
}
public function index_delete(): bool
{
global $b;
$item_id = $this->item->get_item_id();
$b->db_query("DELETE FROM `bive_items_index` WHERE `item_id` = ? AND `prop_key` = ?", array($item_id, $this->prop_key), true);
return true;
}
}