Initial commit

This commit is contained in:
Ivan Petrov
2025-12-24 19:19:01 +03:00
commit a7097c6178
19493 changed files with 94306 additions and 0 deletions

319
engine/class.item.php Normal file
View File

@@ -0,0 +1,319 @@
<?php
defined('ROOT_DIR') || exit;
class Item {
public string $item_id;
public int $parent_id = 0;
public Item $parent;
public string $parent_class = "";
public array $props;
public array $props_values;
public string $title = "Объект";
public string $description = "Описание объекта не задано.";
public bool $admin_menu = true;
public bool $visible_content = true;
public string $icon = "format_list_bulleted";
public function __construct($item_id = 0)
{
$this->item_id = $item_id;
}
// Создать Item
public function create($name, $content): bool
{
global $b;
if($this->item_id) return false;
if($content === null) $content = "";
if($name === null) $name = "";
$this->item_id = $b->db_insert("INSERT INTO `bive_items`(`item_name`, `item_content`, `item_class`) VALUES (?, ?, ?)", array($name, $content, $this->get_class_name()));
$this->create_props();
return true;
}
public function delete(): bool
{
global $b;
if(!$this->item_id) return false;
foreach ($this->props as $key => $prop) $prop->delete();
$this->clear_parents();
$this->clear_childs();
$b->db_query("DELETE FROM `bive_items` WHERE `item_id` = ?", array($this->item_id), true);
return true;
}
private function create_props(): bool
{
foreach ($this->props as $key => $prop) $prop->add(null);
return true;
}
// Получить Объект
public function get_item($new = false)
{
global $b;
$result = $b->db_query("SELECT * FROM `bive_items` WHERE `item_id` = ?", [ $this->item_id ], $new);
if(!isset($result[0])) return false;
$this->fill_main_parent($result[0]["item_parent"]);
return $result[0];
}
// Получить класс Объекта
public function get_class_name(): string
{
return static::class;
}
// Получить ID объекта
public function get_item_id($new = false)
{
return $this->get_field("item_id", $new);
}
// Получить ID объекта (алиас)
public function get_id($new = false)
{
return $this->get_item_id($new);
}
// Получить Имя объекта
public function get_item_name($new = false)
{
return $this->get_field("item_name", $new);
}
// Получить Имя объекта (алиас)
public function get_name($new = false)
{
return $this->get_item_name($new);
}
// Получить Ярлык объекта
public function get_item_slug($new = false)
{
return $this->get_field("item_slug", $new);
}
// Получить Ярлык объекта
public function get_slug($new = false)
{
return $this->get_item_slug($new);
}
// Получить Контент объекта
public function get_item_content($new = false)
{
return $this->get_field("item_content", $new);
}
// Получить Контент объекта
public function get_content($new = false)
{
return $this->get_item_content($new);
}
// Получить Родительский объект
public function get_item_parent($new = false)
{
return $this->get_field("item_parent", $new);
}
// Получить родительский объект
public function get_parent()
{
global $b;
$result = $b->db_query("SELECT * FROM `bive_items` WHERE `item_id` = ? LIMIT 1", [ $this->parent_id ], true);
if(!count($result)) return false;
$parent = new $result[0]["item_class"]($result[0]["item_id"]);
return $parent;
}
// Задать Родительский ID
public function set_parent($parent_id, $main = false): bool
{
if($this->item_id == $parent_id) return false;
if($main) {
$this->fill_main_parent($parent_id);
$this->set_field("item_parent", $parent_id);
$this->get_parent();
}
$this->set_second_parent($parent_id);
return true;
}
// Очистить все родительские Объекты
public function clear_parents($class_name = ""): bool
{
global $b;
$where = "";
if($class_name != "") $where = " AND `items_class` LIKE '" . $class_name . "-". $this->get_class_name() ."%'";
$b->db_query("DELETE FROM `bive_items_links` WHERE `child_item_id` = ?" . $where, array($this->item_id), true);
$b->db_query("DELETE FROM `bive_items_intermediate` WHERE `child_item_id` = ?" . $where, array($this->item_id), true);
$this->set_field("item_parent", 0);
return true;
}
public function clear_childs($class_name = ""): bool
{
global $b;
$where = "";
if($class_name != "") $where = " AND `items_class` LIKE '%-" . $class_name . "'";
$b->db_query("DELETE FROM `bive_items_links` WHERE `parent_item_id` = ?" . $where, array($this->item_id), true);
$b->db_query("DELETE FROM `bive_items_intermediate` WHERE `parent_item_id` = ?" . $where, array($this->item_id), true);
return true;
}
// Удалить родительский Объект
public function clear_parent($parent_id)
{
global $b;
$b->db_query("DELETE FROM `bive_items_links` WHERE `child_item_id` = ? AND `parent_item_id` = ?", array($this->item_id, $parent_id), true);
$b->db_query("DELETE FROM `bive_items_intermediate` WHERE `child_item_id` = ? AND `parent_item_id` = ?", array($this->item_id, $parent_id), true);
return true;
}
// Задать родительский ID без сохранения
public function fill_main_parent($parent_id)
{
if($this->item_id == $parent_id) return false;
$this->parent_id = $parent_id;
return $parent_id;
}
// Занести Родительский Объект
public function set_second_parent($parent_id): bool
{
global $b;
if($this->item_id == $parent_id) return false;
$result = $b->db_query("SELECT * FROM `bive_items_links` WHERE `parent_item_id` = ? AND `child_item_id` = ? LIMIT 1", [ $parent_id, $this->item_id ], true);
if(count($result)) return false;
$items_class = $b->get_item_class_by_id($parent_id) . "-" . $this->get_class_name();
$b->db_insert("INSERT INTO `bive_items_links`(`parent_item_id`, `child_item_id`, `items_class`) VALUES (?, ?, ?)", array($parent_id, $this->item_id, $items_class));
return true;
}
// Занести связи в таблицу с оптимизацией
public function set_optimize()
{
global $b;
$parent_list = $this->get_parent_items();
$child_list = $this->get_child_items();
foreach ($parent_list as $key => $item_parent) {
foreach ($child_list as $k => $item_child) {
$items_class = $item_parent->get_class_name() . "-" . $this->get_class_name() . "-" . $item_child->get_class_name();
$b->db_insert("INSERT IGNORE INTO `bive_items_intermediate` (`parent_item_id`, `child_item_id`, `germ_item_id`, `items_class`) VALUES (?, ?, ?, ?)", [ $item_parent->item_id, $this->item_id, $item_child->item_id, $items_class], true);
}
}
return true;
}
// Получить все родительские Объекты
public function get_parent_items($class_name = ""): array
{
global $b;
$parents = array();
$main_parent = $this->get_parent();
if($main_parent && $main_parent->get_class_name() == $class_name) $parents[] = $main_parent;
$class_condition = "";
if($class_name != "") $class_condition = " AND i.`item_class` = '" . $class_name . "' ";
$query = "SELECT * FROM `bive_items_links` as l JOIN `bive_items` as i ON i.`item_id` = l.`parent_item_id` " . $class_condition . " WHERE l.`child_item_id` = ?";
$parent_list = $b->db_query($query, [ $this->item_id ], true);
if(!count($parent_list)) return $parents;
foreach ($parent_list as $key => $db_parent) {
$parent = new $db_parent["item_class"]($db_parent["item_id"]);
$parent->set_parent($db_parent["item_parent"], true);
$parents[] = $parent;
}
return $parents;
}
// Получить Дочерние Объекты
public function get_child_items($class_name = "")
{
global $b;
$childs = array();
$class_condition = "";
if($class_name != "") $class_condition = " AND i.`item_class` = '" . $class_name . "' ";
$query = "SELECT * FROM `bive_items_links` as l JOIN `bive_items` as i ON i.`item_id` = l.`child_item_id` " . $class_condition . " WHERE l.`parent_item_id` = ?";
$child_list = $b->db_query($query, [ $this->item_id ], true);
if(!count($child_list)) return $childs;
foreach ($child_list as $key => $db_child) {
$child = new $db_child["item_class"]($db_child["item_id"]);
$child->set_parent($db_child["item_parent"], true);
$childs[] = $child;
}
return $childs;
}
public function get_main_child_items($class_name = "")
{
global $b;
$childs = array();
$class_condition = "";
if($class_name != "") $class_condition = " AND i.`item_class` = '" . $class_name . "' ";
$query = "SELECT * FROM `bive_items` as i WHERE i.`item_parent` = ? " . $class_condition;
$child_list = $b->db_query($query, [ $this->item_id ], true);
if(!count($child_list)) return $childs;
foreach ($child_list as $key => $db_child) {
$child = new $db_child["item_class"]($db_child["item_id"]);
$child->set_parent($db_child["item_parent"], true);
$childs[] = $child;
}
return $childs;
}
// Получить поле из базы
public function get_field($field_name, $new = false)
{
$item = $this->get_item($new);
if(!$item) return false;
$response = $item[$field_name];
if(!$response) return false;
return $response;
}
// Обновить поле
public function set_field($field_name, $new_value): bool
{
global $b;
$b->db_query("UPDATE `bive_items` SET `" . $field_name . "` = ? WHERE `item_id` = ?", array($new_value, $this->item_id), true);
return true;
}
// Добавить Свойство Объекту
public function set_prop($key, $value): bool
{
if(!$this->props[$key]) return false;
$prop = $this->props[$key];
return $prop->add($value);
}
// Получить Свойство Объекта
public function get_prop($key, $new = true)
{
if(!$this->props[$key]) return false;
$prop = $this->props[$key];
return $prop->get($new);
}
// Получить Свойство Объекта для вывода
public function get_prop_render_value($key, $new = true)
{
if(!$this->props[$key]) return false;
$prop = $this->props[$key];
return $prop->render_value($new);
}
}