Система управления «Сайт PRO»
Версия 20240107

Класс Storage

Объект $Storage: Cms\Root\Main\Storage наследует Cms\Site\Base

Объект для запроса контента из базы данных

Объект $Storage

Исходный код
class Storage extends \Cms\Site\Base { … }

Свойства

$cache

$Storage->cache = array();

Список всех загруженных

Смотрите описание $Storage->load()

Исходный код
    var $cache = array();

$tables

$Storage->tables = array();

Список полей таблиц, в которых хранятся данные

Исходный код
    var $tables = array();

$tableList

$Storage->tableList = array();

Список таблиц в виде объектов

Исходный код
    var $tableList = array();

$default

$Storage->default = array(
        '-' => array(
            'title' => 'Главная',
            'parent' => '',
        ),
        'cms' => array(
            'parent' => '-',
            'title' => 'Администратор',
            'type' => 'login',
        ),
        'pms' => array(
            'parent' => '-',
            'title' => 'Администратор',
            'type' => 'login',
        ),
        'enter' => array(
            'parent' => '-',
            'title' => 'Вход в систему',
            'type' => 'login',
        ),
        'sitemap' => array(
            'parent' => '-',
            'title' => 'Карта сайта',
            'type' => 'sitemap',
        ),
        '404' => array(
            'parent' => '-',
            'title' => 'Страница не найдена',
            'html' => '<table cellpadding="0" cellspacing="0"><tr><td width="0" valign="top"><div style="font-size:5em;line-height:1em;">404</div></td><td width="0" valign="top"><div style="width:2em;"></div></td><td width="100%" valign="top">Запрашиваемая Вами страница не найдена<br><br><a href="/">Перейти на Главную страницу</a></td></tr></table>',
            '_open_html' => true,
        ),
        '/admin/sitemap' => array(
            'parent' => 'admin',
            'title' => 'Страницы',
            'type' => 'admin',
        ),
        '/admin/storage' => array(
            'parent' => 'admin',
            'title' => 'Таблицы',
            'type' => 'admin',
        ),
        '/admin/modules' => array(
            'parent' => 'admin',
            'title' => 'Типы страниц',
            'type' => 'admin',
        ),
        '/admin/database' => array(
            'parent' => 'admin',
            'title' => 'База данных',
            'type' => 'admin',
        ),
        '/admin/template' => array(
            'parent' => 'admin',
            'title' => 'Шаблоны',
            'type' => 'admin',
        ),
    );

Данные для страниц отсутствующих в базе данных

Исходный код
    var $default = array( … );

$_itemTableList

$Storage->_itemTableList
Исходный код
    var $_itemTableList;

$_enabledTableList

$Storage->_enabledTableList
Исходный код
    var $_enabledTableList;

Методы

init()

$Storage->init();

Инициализация

Исходный код
    function init() {
        $Data = $this->App()->Data();
        if (!$Data->data && !$Data->error) $Data->init();
        $this->loadData();
    }

loadData()

$Storage->loadData();
Исходный код
    function loadData() {
        $App = $this->App();
        $tableClass = $App->getClass('data_table');
        $Table = new $tableClass('item');
        $this->tableList['item'] = $Table;
        if ($path = $App->resolvePath('cms/site/main/storage.data.php')) {
            foreach (include($path) as $tableData) {
                if (!$Table = $this->getTable($tableData['name'])) $Table = new $tableClass($tableData['name']);
                $tableData['site'] = 'Y';
                if (is_array($tableData['fields'])) foreach ($tableData['fields'] as $fieldName => $fieldData) if (is_array($fieldData)) $tableData['fields'][$fieldName]['site'] = 'Y';
                $Table->addTableData($tableData, true);
                $this->addTable($Table);
            }
        }
        if ($path = __DIR__ . DIRECTORY_SEPARATOR . 'storage.data.php') {
            foreach (include($path) as $tableData) {
                if (!$Table = $this->getTable($tableData['name'])) $Table = new $tableClass($tableData['name']);
                $tableData['root'] = 'Y';
                if (is_array($tableData['fields'])) foreach ($tableData['fields'] as $fieldName => $fieldData) if (is_array($fieldData)) $tableData['fields'][$fieldName]['root'] = 'Y';
                $Table->addTableData($tableData, false);
                $this->addTable($Table);
            }
        }
    }

saveData()

$Storage->saveData();
Исходный код
    function saveData() {
        $data = array();
        foreach ($this->getTableList() as $Table) if ($Table->isSite()) {
            $data[$Table->getName()] = $Table->getTableData();
        }
        return $this->App()->get('file')->saveFileData($this->root() . '/cms/site/main/storage.data.php', $data);
    }

addTable()

$Storage->addTable($table, $fields=null);
Исходный код
    function addTable($table, $fields = null) {
        $tableData = array();
        if (is_object($table)) {
            $Table = $table;
        } else {
            if (!$Table = $this->getTable($table)) {
                $tableClass = $this->App()->getClass('data_table');
                $Table = new $tableClass($table);
            }
            $tableData['enable'] = 'Y';
        }
        if (is_array($fields)) {
            if (!in_array('id', $fields)) array_unshift($fields, 'id');
            if (!in_array('stamp', $fields)) array_push($fields, 'stamp');
            $tableData['fields'] = $fields;
            $tableData['item'] = 'Y';
        }
        if ($tableData) $Table->addTableData($tableData);
        $this->tableList[$Table->getName()] = $Table;
        if ($Table->isEnabled() && $Table->isItem()) {
            $this->tables[$Table->getName()] = $Table->getStorageFields();
        }
    }

deleteTable()

$Storage->deleteTable($table);
Исходный код
    function deleteTable($table) {
        if (is_object($table)) {
            $Table = $table;
            unset($this->tableList[$Table->getName()]);
        } else {
            unset($this->tableList[$table]);
        }
        $this->resetEnabledTableList();
        $this->resetItemTableList();
        return true;
    }

getTable()

$Storage->getTable($table);
Исходный код
    function getTable($table) {
        return $this->tableList[$table];
    }

getTableList()

$Storage->getTableList();
Исходный код
    function getTableList() {
        return $this->tableList;
    }

getItemTableList()

$Storage->getItemTableList();
Исходный код
    function getItemTableList() {
        if (isset($this->_itemTableList)) return $this->_itemTableList;
        $r = array();
        foreach ($this->getTableList() as $table => $Table) {
            if ($Table->isEnabled() && $Table->isItem()) $r[$table] = $Table;
        }
        return $this->_itemTableList = $r;
    }

resetItemTableList()

$Storage->resetItemTableList();
Исходный код
    function resetItemTableList() {
        unset($this->_itemTableList);
    }

getEnabledTableList()

$Storage->getEnabledTableList();
Исходный код
    function getEnabledTableList() {
        if (isset($this->_enabledTableList)) return $this->_enabledTableList;
        $r = array();
        foreach ($this->getTableList() as $table => $Table) {
            if ($Table->isEnabled()) $r[$table] = $Table;
        }
        return $this->_enabledTableList = $r;
    }

resetEnabledTableList()

$Storage->resetEnabledTableList();
Исходный код
    function resetEnabledTableList() {
        unset($this->_enabledTableList);
    }

addDefault()

$Storage->addDefault($id, $info);
Исходный код
    function addDefault($id, $info) {
        $this->default[$id] = $info;
    }

load()

$Storage->load($id, $type=null);

Загрузка объекта

Параметры:

ИмяОписание
$id

ID объекта или массив с данными

$type

тип объекта

Возвращает: object; null

Исходный код
    function load($id, $type = null) {
        if (is_object($id)) {
            return $id;
        } elseif (is_array($id)) {
            $row = $id;
            $id = strval($row['id']);
            if (!isset($row['table'])) if (strlen($row['type']) > 1) {
                $Modules = $this->Main()->Modules();
                $module = $Modules->modules[$row['type']];
                $row['table'] = $module && $module['table'] ? $module['table'] : 'item';
            }
        } elseif (strlen($id = strval($id))) {
            $object = $this->cache[$id];
            if (isset($object)) {
                if (strlen($type) > 1 && $object->type != $type) return;
                return $object;
            }
            $row = $this->getRow($id, $type);
        } else {
            $row = array( "created" => true );
        }
        if (!isset($row)) return;
        if (strlen($row['type']) > 1) $type = $row['type'];
        $object = $this->create($type);
        foreach ($row as $k => $v) $object->$k = $v;
        if (strlen($type) > 1 && $object->type != $type) return;
        if (strlen($id)) $this->cache[$id] = $object;
        return $object;
    }

loadUrl()

$Storage->loadUrl($url);
Исходный код
    function loadUrl($url) {
        $Data = $this->Data();
        if (!strlen($url = strval($url))) return;
        $qUrlList = array( $Data->quote($url) );
        foreach ($this->Main()->Request()->extensions as $extension) {
            if (substr($url, -strlen($extension)) !== $extension) $qUrlList[] = $Data->quote($url . $extension);
        }
        $qUrlList = implode(', ', $qUrlList);
        foreach ($this->getTableList() as $Table) if ($Table->isEnabled() && $Table->isItem()) {
            if ($Field = $Table->getField('url')) if ($Field->isEnabled()) {
                if ($row = $Data->getRow("SELECT * FROM `{$Table->getName()}` WHERE `url` IN ({$qUrlList}) LIMIT 1")) {
                    return $this->load($row);
                }
            }
        }
    }

loadSafe()

$Storage->loadSafe($id, $type=null);
Исходный код
    function loadSafe($id, $type = null) {
        $object = $this->load($id, $type);
        if (!$object) $object = $this->load(array(
            "id" => $id,
            "type" => strlen($type) > 1 ? $type : "page",
            "created" => true,
        ));
        return $object;
    }

getRow()

$Storage->getRow($id, $type=null);

Загрузка данных об объекте

Параметры:

ИмяОписание
$id

ID объекта

$type

тип объекта

Возвращает: array; null

Исходный код
    function getRow($id, $type = null) {
        $Data = $this->Data();
        $Main = $this->Main();
        $Modules = $Main->Modules();
        if (!strlen($id)) return;
        if (strlen($type) > 1) {
            $table = $Modules->modules[$type]['table'];
            if (!isset($table)) $table = "item";
            if ($row = $Data->getRow("SELECT * FROM `$table` WHERE `id`=" . $Data->quote($id))) {
                if (strlen($row['type']) > 1 && $row['type'] != $type) return;
                $row['table'] = $table;
            }
        } else {
            $row = $Data->getRow("SELECT * FROM `item` WHERE `id`=" . $Data->quote($id));
            if (!$row) {
                foreach ($this->tables as $table => $fields) {
                    if ($row = $Data->getRow("SELECT * FROM `$table` WHERE `id`=" . $Data->quote($id))) {
                        $row['table'] = $table;
                        break;
                    }
                }
            }
        }
        if (!$row) {
            $row = $this->default[$id];
            if ($row) $row['id'] = $id;
        }
        return $row;
    }

create()

$Storage->create($type);

Создание объекта

Параметры:

ИмяОписание
$type

тип объекта

Возвращает: object

Исходный код
    function create($type) {
        $Main = $this->Main();
        $Modules = $Main->Modules();
        if (strlen($type) > 1) {
            # 2013-07-30
            if (strpos($type, ":") !== false) list($type, $list) = explode(":", $type);
            if (!$Modules->isModule($type)) $Modules->initModule($type);
            $class = $Modules->getClass($type);
        } else {
            $class = $Modules->getClass("page");
        }
        $object = new $class;
        if (strlen($type) > 1) $object->type = $type;
        # 2013-07-30
        if (isset($list) && strlen($list) > 1) $object->list = $list;
        return $object;
    }

getPageMenu()

$Storage->getPageMenu($id=null);

Получение меню страницы

Параметры:

ИмяОписание
$id

ID страницы

Возвращает: array

Исходный код
    function getPageMenu($id = null) {
        if ($id === "") return array();
        $Auth = $this->Auth();
        $Data = $this->Data();
        $Main = $this->Main();
        $Page = isset($id) ? $this->load($id) : $Main->Page;
        if (!$Page->id) return array();
        return $Data->getRows("SELECT * FROM `item` WHERE `parent`=" . $Data->quote($Page->id) . " AND `type`<>" . $Data->quote($Page->list) . ($Auth->admin ? "" : " AND `menu`='Y'") . " ORDER BY `order`");
    }

getPath()

$Storage->getPath($id=null);

Получение пути к странице

Возвращает: array

Исходный код
    function getPath($id = null) {
        if ($id === "") return array();
        $Auth = $this->Auth();
        $Data = $this->Data();
        $Main = $this->Main();
        $Page = isset($id) ? $this->load($id) : $Main->Page;
        $id = $Page->parent;
        $list = array( "" => $Page->title );
        if (!strlen($Page->title) && $Main->Request()->_GET('a')) {
            if ($Auth->admin) {
                $list[''] = $Main->default_title;
                if (!strlen($id)) $id = $Main->Request()->_GET('parent', "-");
            }
        }
        $i = 0;
        while (strlen($id)) {
            if ($Item = $this->load($id)) {
                if (strlen($Item->title)) $list[$id] = $Item->title;
                $id = $Item->parent;
            } else break;
            if (++ $i > 10) break;
        }
        $list = array_reverse($list, true);
        if (count($list) == 1) $list = array();
        return $list;
    }

getTitle()

$Storage->getTitle($id=null, $type=null);

Заголовок объекта с кэшированием

Параметры:

ИмяОписание
$id

ID

$type

тип объекта

Возвращает: string; null

Исходный код
    function getTitle($id = null, $type = null) {
        if (!isset($id)) $id = $this->Main()->id;
        if ($Item = $this->load($id, $type)) {
            return $Item->title;
        }
    }

getList()

$Storage->getList($id, $key);

Получение информации в виде списка

Параметры:

ИмяОписание
$id

ID страницы

$key

Параметр настроек

Возвращает: array

Исходный код
    function getList($id, $key) {
        $r = array();
        if ($Item = $this->load($id)) {
            $Item->openInfo();
            if (!$Item->info[$key]) return array();
            elseif (!is_array($Item->info[$key])) return array($Item->info[$key]);
            else foreach ($Item->info[$key] as $v) $r[$v] = $v;
        }
        return $r;
    }

isUnique()

$Storage->isUnique($id);

Является ли ID уникальным

Параметры:

ИмяОписание
$id

Возвращает: true; false

Исходный код
    function isUnique($id) {
        $Conf = $this->Conf();
        $Auth = $this->Auth();
        $Data = $this->Data();
        $qid = $Data->quote($id);
        if ($Data->dLookup("SELECT `id` FROM `item` WHERE `id`=$qid")) return false;
        foreach ($this->tables as $table => $fields) if ($table != "item") if ($Data->dLookup("SELECT `id` FROM `$table` WHERE `id`=$qid")) return false;
        if (mb_strtolower($id) == mb_strtolower($Conf->get("admin_login"))) return false;
        foreach ($Auth->getRootAuth() as $root_login => $root_password) if (mb_strtolower($id) == $root_login) return false;
        return true;
    }

getStampTime()

$Storage->getStampTime();
Исходный код
    function getStampTime() {
        $time = 0;
        $Data = $this->Data();
        foreach ($this->tables as $table => $fields) {
            if ($stamp = $Data->dLookup("SELECT MAX(`stamp`) FROM `$table`")) if (($_time = strtotime($stamp)) > $time) $time = $_time;
        }
        return $time;
    }

getMenu()

$Storage->getMenu($a);
Исходный код
    function getMenu($a) {
        extract($a);
        if (is_array($rows)) return $rows;
        $Conf = $this->Conf();
        $Auth = $this->Auth();
        $Data = $this->Data();
        $Main = $this->Main();
        if (!isset($query)) $query = ""
         . "SELECT `item`.*"
         . " FROM `menu` JOIN `item` ON `menu`.`id`=`item`.`id` AND `menu`.`menu`='$menu'"
         . " WHERE `item`.`lang`=" . $Data->quote($this->Main()->lang)
         . " ORDER BY `menu`.`order`, `item`.`order`"
        ;
        $r = $this->Data()->getRows($query);
        if ($Auth->admin && $menu == $Conf->get("menu-main", "left")) {
            $r[] = array(
                "id" => $Main->admin_point,
                "type" => "page",
                "parent" => "-",
                "title" => "Администратор",
            );
        }
        return $r;
    }

openHtml()

$Storage->openHtml($Page);
Исходный код
    function openHtml($Page) {
        if ($Page->_open_html) return;
        $Data = $this->Data();
        if (strlen($Page->id)) {
            $Page->html = $Data->dLookup("SELECT `html` FROM `html` WHERE `id`=" . $Data->quote($Page->id));
        }
        $Page->_open_html = true;
    }

openMenu()

$Storage->openMenu($Page);
Исходный код
    function openMenu($Page) {
        if ($Page->_open_menu) return;
        $Data = $this->Data();
        if (strlen($Page->id)) {
            if ($result = $Data->query("SELECT * FROM `menu` WHERE `id`=" . $Data->quote($Page->id))) while ($row = $Data->fetch($result)) {
                $Page->{'menu-' . $row['menu']} = "Y";
                $Page->{'_open_menu_' . $row['menu']} = $row['order'];
            }
        }
        $Page->_open_menu = true;
    }

openInfo()

$Storage->openInfo($Page);
Исходный код
    function openInfo($Page) {
        if ($Page->_open_info) return;
        $Data = $this->Data();
        if (strlen($Page->id)) {
            if ($result = $Data->query("SELECT * FROM `info` WHERE `id`=" . $Data->quote($Page->id) . " ORDER BY `order`")) while ($row = $Data->fetch($result)) {
                if ($row['order']) $Page->info[$row['key']][] = $row['value'];
                else $Page->info[$row['key']] = $row['value'];
            }
        }
        $Page->_open_info = true;
    }

openTags()

$Storage->openTags($Page);
Исходный код
    function openTags($Page) {
        if ($Page->_open_tags) return;
        $Data = $this->Data();
        if (strlen($Page->id)) {
            $tags = array();
            if ($result = $Data->query("SELECT * FROM `tags` WHERE `id`=" . $Data->quote($Page->id) . " ORDER BY `order`")) while ($row = $Data->fetch($result)) {
                $tags[] = $row['tag'];
            }
            $Page->tags = implode(", ", $tags);
        }
        $Page->_open_tags = true;
    }

savePage()

$Storage->savePage($Page);
Исходный код
    function savePage($Page) {
        $Conf = $this->Conf();
        $Data = $this->Data();
        $Auth = $this->Auth();
        $Main = $this->Main();
        $table = $Page->table();
        if (!$Table = $this->getTable($table)) {
            $Page->addError('Не найдена таблица «' . $table . '»');
            return false;
        }
        $fields = $Table->getEnabledFieldList();
        $row = null;
        foreach ($fields as $field => $Field) {
            if ($Field->isPrimary() || $Field->isUnique()) {
                if ($value = $Page->$field) {
                    if ($row = $Data->getRow("SELECT * FROM `$table` WHERE `$field`={$Data->quote($value)}")) break;
                }
            }
        }
        $insert = $row ? false : true;
        foreach ($fields as $field => $Field) {
            if ($Field->isPrimary() && !$Page->$field && $row) $Page->$field = $row[$field];
            switch (strval($Field->getDefault())) {
                case 'N':
                    if (!$Page->$field) $Page->$field = $Main->getDefaultN();
                    break;
                case 'ID':
                    $id = trim($Page->$field, '/');
                    if (!strlen($id)) {
                        $Page->$field = $Page->getDefaultId();
                        $insert = true;
                    } else {
                        $Page->$field = $Main->Request->getId($id);
                    }
                    if (in_array(mb_strtolower($Page->$field), array_merge(array(mb_strtolower(strval($Conf->get('admin_login')))), array_keys($Auth->getRootAuth())), true)) {
                        $Page->addError('Сохранение данных невозможно');
                        return false;
                    }
                    break;
                case 'UUID':
                    if (!$Page->$field) $Page->$field = $Main->getDefaultUuid();
                    break;
                case 'URL':
                    break;
                case 'ORDER':
                    if (!$Page->$field) {
                        if (isset($fields['parent'])) {
                            $Page->$field = $Data->dLookup("SELECT COALESCE(MAX(`$field`), 0) + 1 FROM `$table` WHERE `parent`={$Data->quote($Page->parent)}");
                        } else {
                            $Page->$field = $Data->dLookup("SELECT COALESCE(MAX(`$field`), 0) + 1 FROM `$table`");
                        }
                    }
                    break;
                case 'LOGIN':
                    if (!$Page->$field) $Page->$field = $Auth->login;
                    break;
                case 'NOW':
                    if (!intval($Page->$field)) $Page->$field = date('Y-m-d H:i:s');
                    elseif (strlen($Page->$field) != 19) $Page->$field = date('Y-m-d H:i:s', strtotime($Page->$field));
                    break;
            }
            if ($Field->getType() === 'timestamp') $Page->$field = date('Y-m-d H:i:s');
        }
        if (isset($fields['menu'])) if (!isset($Page->menu)) $Page->menu = 'Y';
        if (isset($fields['type'])) if (!strlen($Page->type)) $Page->type = 'page';
        if (isset($fields['lang'])) if (!$Page->lang) $Page->lang = $Main->default_lang;
        if (isset($fields['parent'])) {
            if (!strlen($Page->parent)) {
                if ($Page->id !== '-' && $Page->id !== $Page->lang) {
                    $Page->parent = $Page->id[0] === '/' ? dirname($Page->id) : '-';
                    if (substr_count($Page->parent, '/') == 1 && $Page->parent[0] === '/') $Page->parent = ltrim($Page->parent, '/');
                    if (!strlen($Page->parent)) $Page->parent = '-';
                }
            }
        }
        if ($insert) {
            $queryFields = array();
            $queryValues = array();
            $fieldCounter = null;
            foreach ($fields as $field => $Field) {
                $default = strval($Field->getDefault());
                if ($default === 'AUTO_INCREMENT') {
                    $fieldCounter = $field;
                    continue;
                } elseif ($default === 'VERSION') {
                    $Page->$field = 1;
                }
                if ($Field->isUnique() && !$Page->$field) {
                    $value = 'NULL';
                } else {
                    $value = $Data->quote($Page->$field);
                }
                $queryFields[] = "`$field`";
                $queryValues[] = $value;
            }
            $query = "INSERT INTO `$table` (" . implode(', ', $queryFields) . ") VALUES (" . implode(', ', $queryValues) . ")";
            if ($result = $Data->query($query)) {
                if ($field = $fieldCounter) {
                    if ($value = $Data->insertId()) $Page->$field = $value;
                }
            } else {
                $Page->addError($Data->getError());
                return false;
            }
        } else {
            $queryUpdate = array();
            $queryWheres = array();
            foreach ($fields as $field => $Field) {
                $default = strval($Field->getDefault());
                if ($default === 'AUTO_INCREMENT') {
                    $queryWheres[$field] = "`$field`={$Data->quote(intval($Page->$field))}";
                    continue;
                } elseif ($default === 'VERSION') {
                    $queryWheres[$field] = "`$field`={$Data->quote(intval($Page->$field))}";
                    $Page->$field ++;
                }
                if ($Field->isPrimary()) {
                    $queryWheres[$field] = "`$field`={$Data->quote($Page->$field)}";
                    continue;
                }
                if ($Field->isUnique() && !$Page->$field) {
                    $value = 'NULL';
                } else {
                    $value = $Data->quote($Page->$field);
                }
                $queryUpdate[] = "`$field`=$value";
            }
            $query = "UPDATE `$table` SET " . implode(', ', $queryUpdate) . " WHERE " . implode(' AND ', $queryWheres);
            if ($result = $Data->query($query)) {
                if (!$Data->numRows($result) && (count($queryWheres) > 1 || !isset($queryWheres['id']))) {
                    $Page->addError("Данные изменены с другого устройства: " . $query);
                    return false;
                }
            } else {
                $Page->addError($Data->getError());
                return false;
            }
        }
        if ($Page->id) {
            if ($table === 'item') {
                foreach ($this->getItemTableList() as $Table) if ($Table->getName() !== 'item') {
                    $Data->query("DELETE FROM `{$Table->getName()}` WHERE `id`={$Data->quote($Page->id)}");
                }
            } else {
                $Data->query("DELETE FROM `item` WHERE `id`={$Data->quote($Page->id)}");
            }
        }
        return true;
    }

saveHtml()

$Storage->saveHtml($Page);
Исходный код
    function saveHtml($Page) {
        if ($Page->_open_html) {
            $Data = $this->Data();
            $query = "DELETE FROM `html` WHERE `id`=" . $Data->quote($Page->id);
            if (!$Data->query($query)) {
                $Page->addError($Data->getError());
                return false;
            }
            if (strlen(trim($Page->html))) {
                $query = "INSERT INTO `html` (`id`, `html`) VALUES (" . $Data->quote($Page->id) . ", " . $Data->quote($Page->html) . ")";
                if (!$Data->query($query)) {
                    $Page->addError($Data->getError());
                    return false;
                }
            }
        }
        return true;
    }

saveFull()

$Storage->saveFull($Page);
Исходный код
    function saveFull($Page) {
        if ($Find = $this->App()->getObject("find")) {
            $Data = $this->Data();
            $qid = $Data->quote($Page->id);
            $full = $Page->getFull();
            $query = "DELETE FROM `full` WHERE `id`=$qid";
            if (!$Data->query($query)) {
                $Page->addError($Data->getError());
                return false;
            }
            /*
            $query = "DELETE FROM `stat` WHERE `id`=$qid";
            if (!$Data->query($query)) {
                $Page->addError($Data->getError());
                return false;
            }
            */
            if (strlen(trim($full))) {
                # 2013-04-22 Сохранить текст в поле `full`
                $stem = $this->getStem($full);
                $query = "INSERT INTO `full` (`id`, `type`, `menu`, `lang`, `title`, `text`, `full`) VALUES ("
                 . $Data->quote($Page->id) . ", "
                 . $Data->quote($Page->type) . ", "
                 . $Data->quote($Page->menu) . ", "
                 . $Data->quote($Page->lang) . ", "
                 . $Data->quote($Page->title) . ", "
                 . $Data->quote($full) . ", "
                 . $Data->quote(is_array($stem) ? implode(" ", $stem) : $stem) . ""
                 . ")"
                ;
                if (!$Data->query($query)) {
                    $query = "INSERT INTO `full` (`id`, `type`, `menu`, `lang`, `title`, `text`) VALUES ("
                     . $Data->quote($Page->id) . ", "
                     . $Data->quote($Page->type) . ", "
                     . $Data->quote($Page->menu) . ", "
                     . $Data->quote($Page->lang) . ", "
                     . $Data->quote($Page->title) . ", "
                     . $Data->quote($full) . ""
                     . ")"
                    ;
                    if (!$Data->query($query)) {
                        $Page->addError($Data->getError());
                        return false;
                    }
                }
                /*
                if (!$this->Conf()->get("find-full-stem")) {
                    $stat = $this->getStat($stem);
                    if ($stat) foreach ($stat as $word => $info) {
                        $query = "INSERT INTO `stat` (`id`, `word`, `c`, `p`, `f`, `d`) VALUES ("
                         . $qid . ", "
                         . $Data->quote($word) . ", "
                         . $Data->quote($info['c']) . ", "
                         . $Data->quote($info['p']) . ", "
                         . $Data->quote($info['f']) . ", "
                         . $Data->quote($info['d']) . ""
                         . ")"
                        ;
                        if (!$Data->query($query)) {
                            $Page->addError($Data->getError());
                            return false;
                        }
                    }
                }
                */
            }
        }
        return true;
    }

saveInfo()

$Storage->saveInfo($Page);
Исходный код
    function saveInfo($Page) {
        if ($Page->_open_info) {
            $Main = $this->Main();
            $Data = $this->Data();
            foreach (array("info") as $table) {
                $query = "DELETE FROM `$table` WHERE `id`=" . $Data->quote($Page->id);
                if (!$Data->query($query)) {
                    $Page->addError($Data->getError());
                    return false;
                }
            }
            if (is_array($Page->info)) foreach ($Page->info as $key => $info) {
                $table = "info";
                $pre = substr($key, 0, 5);
                $query = null;
                if (is_array($info)) {
                    if (is_int(array_shift(array_keys($info)))) ksort($info);
                    foreach (array_values($info) as $order => $value) {
                        $order ++;
                        $query = "INSERT INTO `$table` (`id`, `key`, `order`, `value`) VALUES ("
                         . $Data->quote($Page->id) . ", "
                         . $Data->quote($key) . ", "
                         . $Data->quote($order) . ", "
                         . $Data->quote($value) . ""
                         . ")";
                        if (!$Data->query($query)) {
                            $Page->addError($Data->getError());
                            return false;
                        }
                    }
                } elseif (strlen($info)) {
                    $query = "INSERT INTO `$table` (`id`, `key`, `order`, `value`) VALUES ("
                     . $Data->quote($Page->id) . ", "
                     . $Data->quote($key) . ", "
                     . $Data->quote(0) . ", "
                     . $Data->quote($info) . ""
                     . ")";
                     if (!$Data->query($query)) {
                        $Page->addError($Data->getError());
                        return false;
                    }
                }
            }
        }
        return true;
    }

saveMenu()

$Storage->saveMenu($Page);
Исходный код
    function saveMenu($Page) {
        if ($Page->_open_menu) {
            $Data = $this->Data();
            $query = "DELETE FROM `menu` WHERE `id`=" . $Data->quote($Page->id);
            if (!$Data->query($query)) {
                $Page->addError($Data->getError());
                return false;
            }
            foreach ($Page as $k => $v) if (substr($k, 0, 5) == "menu-" && $v) {
                $menu = substr($k, 5);
                $order = intval($Page->{'_open_menu_' . substr($k, 5)});
                if (!$order) $order = $Data->dLookup("SELECT MAX(`order`)+1 FROM `menu` WHERE `menu`=" . $Data->quote($menu));
                if (!$order) $order = 1;
                $query = "INSERT INTO `menu` (`id`, `menu`, `order`) VALUES ("
                 . $Data->quote($Page->id) . ", "
                 . $Data->quote($menu) . ", "
                 . $Data->quote($order) . ""
                 . ")";
                if (!$Data->query($query)) {
                    $Page->addError($Data->getError());
                    return false;
                }
            }
        }
        return true;
    }

saveTags()

$Storage->saveTags($Page);
Исходный код
    function saveTags($Page) {
        if ($Page->_open_tags) {
            $Data = $this->Data();
            $qid = $Data->quote($Page->id);
            $done = array();
            $tags = is_array($Page->tags) ? $Page->tags : explode(",", $Page->tags);
            if (!$Data->query("DELETE FROM `tags` WHERE `id`=$qid")) {
                $Page->addError($Data->getError());
                return false;
            }
            $order = 0;
            foreach ($tags as $tag) if (strlen($tag = trim($tag))) {
                $tag_lc = mb_strtolower($tag);
                if (!isset($done[$tag_lc])) {
                    $order ++;
                    $done[$tag_lc] = true;
                    $query = "INSERT INTO `tags` (`id`, `tag`, `order`) VALUES ("
                     . $qid . ", "
                     . $Data->quote($tag) . ", "
                     . $order
                     . ")";
                    if (!$Data->query($query)) {
                        $Page->addError($Data->getError());
                        return false;
                    }
                }
            }
        }
        return true;
    }

savePath()

$Storage->savePath($Page);
Исходный код
    function savePath($Page) {
        if ($path = $Page->getPath()) {
            $Data = $this->Data();
            $qid = $Data->quote($Page->id);
            if (!$Data->query("DELETE FROM `path` WHERE `id`=$qid")) {
                $Page->addError($Data->getError());
                return false;
            }
            $done = array();
            $order = 0;
            foreach ($path as $parent => $title) if ($parent != "" && $parent != "-") {
                if (!isset($done[$parent])) {
                    $order ++;
                    $done[$parent] = true;
                    $query = "INSERT INTO `path` (`id`, `parent`, `order`) VALUES ("
                     . $qid . ", "
                     . $Data->quote($parent) . ", "
                     . $order
                     . ")";
                    if (!$Data->query($query)) {
                        $Page->addError($Data->getError());
                        return false;
                    }
                }
            }
        }
        return true;
    }

delete()

$Storage->delete($Page);
Исходный код
    function delete($Page) {
        $Data = $this->Data();
        $Main = $this->Main();
        $table = $Page->table;
        if (!isset($table)) {
            $table = $Main->Modules()->modules[$Page->type]['table'];
            if (!isset($table)) $table = "item";
        }
        $query = "DELETE FROM `back` WHERE `id`=" . $Data->quote($Page->id);
        if (!$Data->query($query)) {
            $Page->addError($Data->getError());
            return false;
        }
        $query = "INSERT INTO `back` (`id`) VALUES (" . $Data->quote($Page->id) . ")";
        if (!$Data->query($query)) {
            $Page->addError($Data->getError());
            return false;
        }
        $query = "";
        $data = array();
        foreach ($Page as $k => $v) $data[$k] = $v;
        if (is_array($Main->Storage->tables['item'])) foreach ($Main->Storage->tables['item'] as $field) {
            unset($data[$field]);
            $query .= "`$field`=" . $Data->quote($Page->$field) . ", ";
        }
        $query .= "`data`=" . $Data->quote(serialize($data)) . ", ";
        $query = "UPDATE `back` SET $query `stamp`=" . $Data->quote(date("Y-m-d H:i:s")) . " WHERE `id`=" . $Data->quote($Page->id);
        if (!$Data->query($query)) {
            $Page->addError($Data->getError());
            return false;
        }
        $query = "DELETE FROM `$table` WHERE `id`=" . $Data->quote($Page->id);
        if (!$Data->query($query)) {
            $Page->addError($Data->getError());
            return false;
        }
        if (!(true
            && $Data->query("DELETE FROM `info` WHERE `id`=" . $Data->quote($Page->id))
            && $Data->query("DELETE FROM `menu` WHERE `id`=" . $Data->quote($Page->id))
            && $Data->query("DELETE FROM `html` WHERE `id`=" . $Data->quote($Page->id))
            && $Data->query("DELETE FROM `full` WHERE `id`=" . $Data->quote($Page->id))
            && $Data->query("DELETE FROM `path` WHERE `id`=" . $Data->quote($Page->id))
            /*
            && $Data->query("DELETE FROM `stat` WHERE `id`=" . $Data->quote($Page->id))
            */
            && $Data->query("DELETE FROM `tags` WHERE `id`=" . $Data->quote($Page->id))
        )) {
            $Page->addError($Data->getError());
            return false;
        }
        $Data->query("DELETE FROM `item_file` WHERE `id`={$Data->quote($Page->id)}");
        return true;
    }

getBlock()

$Storage->getBlock($Page, $block, $param);
Исходный код
    function getBlock($Page, $block, $param) {
        if (is_array($param)) extract($param);
        $Conf = $this->Conf();
        $Data = $this->Data();
        $Auth = $this->Auth();
        $Main = $this->Main();
        $Form = $this->Form();
        if (!isset($result)) {
            if (!isset($query)) {
                $typed = $Main->Modules->modules[$block];
                if (!isset($typed)) return;
                $table = $typed['table'];
                if (!isset($table)) $table = "item";
                $order = isset($order) ? $order : $typed['order'];
                if (!isset($order)) {
                    if (in_array("menu", $Main->Storage->tables[$table])) $order = "`menu` DESC, `order`";
                    else $order = "order";
                }
                if ($order[0] == "-") {
                    $order_by = substr($order, 1);
                    $asc_desc = "DESC";
                } else {
                    $order_by = $order;
                    $asc_desc = "";
                }
                if (!isset($admin)) $admin = $Auth->admin && $typed['admin'];
                if (strpos($order_by, "`") === false) $order_by = "`$order_by`";
                $query = ""
                 . "SELECT *"
                 . " FROM `$table`"
                 . " WHERE `parent`=" . $Data->quote($Page->id) . " AND `type`=" . $Data->quote($block)
                 . (!$admin && in_array("menu", $Main->Storage->tables[$table]) ? " AND `menu`='Y'" : "")
                 . " ORDER BY $order_by $asc_desc"
                ;
            }
            $result = $Data->query($query);
        }
        return $result;
    }

getStem()

$Storage->getStem($full);
Исходный код
    function getStem($full) {
        static $r;
        if ((++ $r) > 1000000) return;
        global $Find;
        if (isset($Find)) return $Find->getStem($full);
    }

getStat()

$Storage->getStat(&$stem);
Исходный код
    function getStat(&$stem) {
        static $r;
        if ((++ $r) > 1000000) return;
        global $Find;
        if (isset($Find)) return $Find->getStat($stem);
    }

changeId()

$Storage->changeId($old_id, $new_id);
Исходный код
    function changeId($old_id, $new_id) {
        if (!strlen($old_id) || !strlen($new_id)) return;
        $Data = $this->Data();
        $Main = $this->Main();
        $qold_id = $Data->quote($old_id);
        $qnew_id = $Data->quote($new_id);
        $qold_href = $Data->quote('"' . $Main->href($old_id) . '"');
        $qnew_href = $Data->quote('"' . $Main->href($new_id) . '"');
        # item, news, catalog, ...
        foreach ($this->tables as $table => $fields) {
            if ($Table = $this->getTable($table)) {
                if (!$Table->getField('id')) {
                    continue;
                }
            }
            $r = $Data->query("UPDATE `$table` SET `id`=$qnew_id WHERE `id`=$qold_id");
            if (!$r) return;
            if (in_array("parent", $fields))
            $r = $Data->query("UPDATE `$table` SET `parent`=$qnew_id WHERE `parent`=$qold_id");
        }
        # id in tables
        foreach (array(
            "menu", "info", "tags",
            "html", "full", "stat",
            'item_file',
        ) as $table) {
            $r = $Data->query("UPDATE `$table` SET `id`=$qnew_id WHERE `id`=$qold_id");
        }
        # value in info
        foreach (array(
            "info",
        ) as $table) {
            $r = $Data->query("UPDATE `$table` SET `value`=$qnew_id WHERE `value`=$qold_id");
            $r = $Data->query("UPDATE `$table` SET `value`=REPLACE(`value`, $qold_href, $qnew_href) WHERE POSITION($qold_href IN `value`)>0");
        }
        # id, parent in path
        foreach (array(
            "path",
        ) as $table) {
            $r = $Data->query("UPDATE `$table` SET `id`=$qnew_id WHERE `id`=$qold_id");
            $r = $Data->query("UPDATE `$table` SET `parent`=$qnew_id WHERE `parent`=$qold_id");
        }
        # html in html
        foreach (array(
            "html",
        ) as $table) {
            $r = $Data->query("UPDATE `$table` SET `html`=REPLACE(`html`, $qold_href, $qnew_href) WHERE POSITION($qold_href IN `value`)>0");
        }
        # id, parent in cart_item
        foreach (array(
            "cart_item",
        ) as $table) {
            $r = $Data->query("UPDATE `$table` SET `id`=$qnew_id WHERE `id`=$qold_id");
            $r = $Data->query("UPDATE `$table` SET `parent`=$qnew_id WHERE `parent`=$qold_id");
        }
        $this->cache = array();
        return true;
    }

loadSortList()

$Storage->loadSortList($Page, $a=null);
Исходный код
    function loadSortList($Page, $a = null) {
        $Conf = $this->Conf();
        $Data = $this->Data();
        $Main = $this->Main();
        if (is_string($a) && strpos($a, " ") !== false) {
            $query = $a;
        } elseif (is_array($a) && isset($a[0])) {
            $list = $a;
        } elseif (($parent = $a['parent']) && substr($parent, 0, 5) == "menu-" && isset($Conf->data[$parent])) {
            $list = array();
            foreach ($Data->getRows("SELECT * FROM `menu` WHERE `menu`.`menu`=" . $Data->quote(substr($parent, 5)) . " ORDER BY `menu`.`order`") as $row) {
                if ($Item = $Main->load($row['id'])) $list[] = array(
                    "id" => $Item->id,
                    "title" => $Item->title,
                    "image" => $Item->image,
                    "table" => $parent,
                );
            }
        } else {
            if (is_string($a)) {
                $type = $a;
            } elseif ($Page->list) {
                $type = $Page->list;
            } else {
                $type = $Main->Modules->modules[$Page->type]['list'];
            }
            $table = isset($Main->Modules->modules[$type]) ? $Main->Modules->modules[$type]['table'] : null;
            if ($table && $table != "item") {
                $query = array(
                ""
                 . "SELECT `id`, `title`, `image`, `menu`, 'item' AS `table` "
                 . "FROM `item` WHERE `parent`=" . $Data->quote($Page->id) . " "
                 . "ORDER BY `order`"
                ,
                ""
                 . "SELECT `id`, `title`, " . (in_array("menu", $Main->Storage->tables[$table]) ? "`menu`" : "NULL AS `menu`") . ", " . (in_array("image", $Main->Storage->tables[$table]) ? "`image`" : "NULL AS `image`") . ", '$table' AS `table` "
                 . "FROM `$table` WHERE `parent`=" . $Data->quote($Page->id) . " "
                 . "ORDER BY `order`"
                );
            } else {
                $query = ""
                 . "SELECT `id`, `title`, `image`, `menu`, 'item' AS `table` "
                 . "FROM `item` WHERE `parent`=" . $Data->quote($Page->id) . " "
                 . "ORDER BY `order`"
                ;
            }
        }
        if (!is_array($list)) {
            $list = array();
            if (is_array($query)) {
                foreach ($query as $query) {
                    if ($result = $Data->query($query)) while ($row = $Data->fetch($result)) $list[] = $row;
                }
            } else {
                if ($result = $Data->query($query)) while ($row = $Data->fetch($result)) $list[] = $row;
            }
        }
        if (!is_array($list)) $list = array();
        return $list;
    }

saveSortList()

$Storage->saveSortList($Page, $list);
Исходный код
    function saveSortList($Page, $list) {
        $Data = $this->Data();
        $return = true;
        foreach ($list as $table => $sort) {
            if (substr($table, 0, 5) == "menu-") {
                $menu = substr($table, 5);
                foreach ($sort as $id => $order) {
                    $order ++;
                    if (!$Data->query("UPDATE `menu` SET `order`=" . $Data->quote($order) . " WHERE `id`=" . $Data->quote($id) . " AND `menu`=" . $Data->quote($menu))) $return = false;
                }
            } else {
                foreach ($sort as $id => $order) {
                    $order ++;
                    if (!$Data->query("UPDATE `$table` SET `order`=" . $Data->quote($order) . " WHERE `id`=" . $Data->quote($id))) $return = false;
                }
            }
        }
        return $return;
    }

hasChildren()

$Storage->hasChildren($Page);
Исходный код
    function hasChildren($Page) {
        $Data = $this->Data();
        $qid = $Data->quote($Page->id);
        if ($Table = $this->getTable('item')) {
            if ($Table->getEnabledField('parent')) {
                if ($Data->getRow("SELECT `id` FROM `item` WHERE `parent`=$qid LIMIT 1")) {
                    return true;
                }
            }
        }
        if ($list = $Page->getList()) {
            if ($List = $this->Main()->Modules->getModule($list)) {
                if ($Table = $List->loadTable()) if ($Table->getName() !== 'item') {
                    if ($Table->getEnabledField('parent')) {
                        if ($Data->getRow("SELECT `id` FROM `{$Table->getName()}` WHERE `parent`=$qid LIMIT 1")) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }