Версия полностью совместимая c CMS

This commit is contained in:
origami11 2017-02-17 17:31:17 +03:00
parent 7ce493414e
commit 75bb35d5bf
21 changed files with 1404 additions and 783 deletions

View file

@ -13,7 +13,7 @@ function forceUrl($name)
/**
* Контроллер страниц
*/
*/
class Controller_Action
{
@ -24,9 +24,16 @@ class Controller_Action
public $themePath; // Глобальный путь к текущей теме
// Параметры устанавливаются при создании контроллера
public $name; // Имя модуля
public $viewPath = null; // Путь к шаблонам контроллера
public /*.Database.*/$db; // Соединение с базой данных
public $name = ''; // Имя модуля
public $viewPath = null; // Путь к шаблонам контроллера
public $viewPathPrefix = null; // Путь к шаблонам контроллера
public $moduleTitle = "";
/**
* Соединение с базой данных
*/
public $db;
// Фильтры
public $access = null; // Обьект хранит параметры доступа
@ -42,14 +49,11 @@ class Controller_Action
public $iconPath = '';
public $path;
public function __construct ()
{
//
public function __construct () {
$this->path = new Widgets_PathMenu();
}
public function setUp ()
{
// override this
public function setUp() {
}
public function loadConfig($name) {
@ -69,15 +73,16 @@ class Controller_Action
public function installPath($name)
{
return Path::join(CMS_PATH, "modules", $name, "install");
return Path::join(CMS_PATH, "modules", $name);
}
public function addSuggest($view, $name)
public function addSuggest(View_View $view, $name)
{
$suggest = array();
$file = Path::join($this->viewPath, 'help', $name . '.suggest');
if (file_exists($file) && include($file)) {
$view->addScriptRaw("add_suggest(".json_encode($suggest).");\n");
if (file_exists($file)) {
include($file);
$view->suggestions = $suggest;
}
}
@ -85,39 +90,43 @@ class Controller_Action
{
return Path::join($this->iconPath, $size . 'x' . $size, $icon . '.png');
}
/**
* Создает представление
* @param string $file
* @return template
* @param $name String
* @param $viewClass String
* @return View_Composite
*/
public function getView($name)
public function getView($name, $viewClass = 'View_Composite')
{
$file = $name . self::TEMPLATE_EXTENSION;
// Список возможных директорий для поиска файла шаблона
$theme = $this->_registry->readKey(array('system', 'theme'));
$icon_theme = $this->_registry->readKey(array('system', 'icon_theme'));
$list = array(
Path::join($this->viewPath, TEMPLATES) => Path::join(WWW_PATH, "modules", $this->name, TEMPLATES),
PHPTAL_TEMPLATE_REPOSITORY => "");
$file = $name . self::TEMPLATE_EXTENSION;
$list = array(
Path::join($this->viewPath, TEMPLATES, $this->viewPathPrefix) => Path::join(WWW_PATH, "modules", $this->name, TEMPLATES, $this->viewPathPrefix),
Path::join(CMS_PATH, "templates") => Path::join(WWW_PATH, "templates")
);
// Поиск файла для шаблона
foreach($list as $ospath => $path) {
$template = Path::join($ospath, $file);
$template = Path::join($ospath, $file);
if(file_exists($template)) { break; }
}
$tpl = new View_Composite($template);
$tpl->icons = $this->iconPath; // Путь к файлам текущей темы
$tpl->media = $this->themePath; // Путь к файлам текущей темы
$tpl->script = $this->jsPath; // Путь к файлам скриптов
$tpl->template = $path; // Путь к файлам текущего шаблона
/*.View_Composite.*/$tpl = new $viewClass($template);
$tpl->set('icons', $this->iconPath); // Путь к файлам текущей темы
$tpl->set('media', $this->themePath); // Путь к файлам текущей темы
$tpl->set('assets', Path::join(WWW_PATH, "assets", "css"));
$tpl->set('script', $this->jsPath); // Путь к файлам скриптов
$tpl->set('template', $path); // Путь к файлам текущего шаблона
$tpl->setAlias(array(
'${icons}' => $this->iconPath,
'${media}' => $this->themePath,
'${script}' => $this->jsPath,
'${template}' => $path));
'icons' => $this->iconPath,
'script' => $this->jsPath,
// Для media и template поиск происходит как для файлов шаблонов
'media' => $list,
'template' => $list
));
$tpl->loadImports(Path::skipExtension($template) . ".import");
@ -128,7 +137,7 @@ class Controller_Action
public function getModel($name)
{
if (!$this->factory) {
$this->factory = new ModelFactory($this->db, $this->_registry, $this->_shortcut);
$this->factory = new Model_Factory($this->db, $this->_registry);
}
return $this->factory->getModel($name);
}
@ -143,57 +152,59 @@ class Controller_Action
public function execute1(HttpRequest $request)
{
$action = self::ACTION_PREFIX . ucfirst($request->getAction());
if (method_exists($this, $action)) {
return $this->forward($action, $request);
} else {
return $this->forward("actionIndex", $request);
if (!method_exists($this, $action)) {
$action = "actionIndex";
}
$view = $this->forward($action, $request);
if ($view instanceof View_View) {
$view->active_module = get_class($this);
$view->module_action = $action;
}
return $view;
}
public function execute(HttpRequest $request)
{
$result = $this->execute1($request);
if ($result) {
if (!empty($result)) {
$this->view = $result;
}
return $this->render();
$text = $this->render();
return $text;
}
public function forward($action, HttpRequest $args)
{
// Действия до вызова основного обработчика
/*foreach($this->_aspect as $aspect) {
if (isset($aspect->before[$action])) {
call_user_func ($aspect->before[$action], $action, $args);
}
}*/
return call_user_func(array($this, $action), $args);
public function forward($action, HttpRequest $args) {
$value = call_user_func(array($this, $action), $args);
return $value;
}
/**
* Страница по умолчанию
*/
public function actionIndex(HttpRequest $request)
{
public function actionIndex(HttpRequest $request) {
return "";
}
public function postUrl($name, $param)
{
return "?" . http_build_query(
array_merge(array('module' => strtolower(get_class($this)), "action" => $name),
$this->param, $param));
$uri = array_merge(array('module' =>
strtr($this->modulePrefix . strtolower(get_class($this)), array('module_' => '')), "action" => $name),
$this->param, $param);
return "?" . http_build_query($uri);
}
/**
* Генерация ссылки c учетом прав пользователя на ссылки
*
* @parma string $name Действие
* @parma string $param Дополнительные параметры
* @param string $name Действие
* @param array $param Дополнительные параметры
* 'mode' означает что элемент до отправки обрабатывается javascript
* @return array|null
*/
public function nUrl($name, array $param = array())
{
if (!$this->access || $this->access->checkAction($name)) {
/*.Filter_ActionAccess.*/$access = $this->access;
if ($access != null || $access->checkAction($name)) {
return lcurry(array($this, 'postUrl'), $name, $param);
}
return null;
@ -217,6 +228,12 @@ class Controller_Action
/**
* Генерация ссылки на действие контроллера
* Ajax определяется автоматически mode = ajax используется для смены layout
* @param $name
* @param array $param
* @return array|null
*
* @example ?action=$name&mode=ajax
* {$param[i].key = $param[i].value}
*/
public function aUrl($name, array $param = array())
{
@ -249,14 +266,14 @@ class Controller_Action
/**
* Загрузка файла класса
*/
public function loadClass($path, $setup = null)
public function loadClass($path, $setup = null, $prefix = '')
{
if (file_exists($path)) {
require_once ($path);
$class = pathinfo($path, PATHINFO_FILENAME);
$class = $prefix . pathinfo($path, PATHINFO_FILENAME);
return new $class($setup);
}
return null;
throw new Exception("NO CLASS $path");
}
public function loadSettings($path)
@ -269,6 +286,7 @@ class Controller_Action
// Для Widgets
public $view = null;
public $childNodes = array();
public $ctrlValues = array();
public $childViews = array();
public function setView($name)
@ -292,6 +310,11 @@ class Controller_Action
$this->childNodes[$section] = $node;
}
public function setValue(/*Widget*/ $name, $value)
{
$this->ctrlValues[$name] = $value;
}
/**
* Добавление дочернего отображения к текущему отображению
*/
@ -306,34 +329,86 @@ class Controller_Action
*/
public function render()
{
foreach ($this->childNodes as $name => $node) {
$node->make($this);
$this->view->setView($name, $node->view);
}
foreach ($this->childViews as $name => $node) {
$this->view->setView($name, $node);
}
if ($this->view instanceof View_View) {
$this->view->assignValues($this->ctrlValues);
foreach ($this->childNodes as $name => $node) {
$node->make($this);
$this->view->setView($name, $node->view);
}
foreach ($this->childViews as $name => $node) {
$this->view->setView($name, $node);
}
}
return $this->view;
}
function getPageId($request)
function getPageId(HttpRequest $request)
{
$pageId = time();
$request->session()->set('page', $pageId);
return $pageId;
}
function checkPageId($request, $page)
function checkPageId(HttpRequest $request, $page)
{
if ($request->get('__forced__')) {
return true;
}
$_page = $request->session()->get('page');
$result = ($_page && $_page == $page);
$request->session()->clean('page');
return $result;
}
function redirect($action) {
header('location: ' . $this->fUrl($action));
exit();
function _getActionPath()
{
return new Controller_State('index');
}
}
// Тоже убрать в метод Controller_Model
function getActionPath(HttpRequest $request, $action = null)
{
$this->_getActionPath()->getPath($this, ($action) ? $action : $request->getAction());
}
function addFilePathToPath($filePath, $type)
{
$pathToItem = $this->directoryPath($filePath, $type, 'default');
$this->path->addMenuItems($pathToItem['path']);
}
function directoryPath($path, $type, $action)
{
$path = empty($path) ? '/' : $path;
$base = new Path($path);
$parts = $base->getParts();
$_path = '/' . implode('/', $parts);
$rs = new Model_Resources($this->db);
$section_path = $rs->getSectionByPath($_path, $type);
$pathMenu = new Widgets_PathMenu();
$pathMenu->addTitle('title');
$modules = ['image' => 'images','page' => 'pages','document' => 'documents','media' => 'videos'];
while ($section_path->id_parent) {
$_path = '/' . implode('/', $parts);
$pathMenu->addMenuItem(
forceUrl($this->nUrl('', array('module'=>$modules[$type],'action' => $action, 'mode' => 'ajax', 'path' => $_path))),
$section_path->title
);
$section_path = $rs->getSectionById($section_path->id_parent);
array_pop($parts);
}
$pathMenu->addMenuItem(
forceUrl($this->nUrl('', array('module'=>$modules[$type],'action' => $action, 'mode' => 'ajax', 'path' => '/'))),
$this->moduleTitle
);
return $pathMenu->getItemsReverse();
}
}

View file

@ -1,7 +1,40 @@
<?php
class FileNotFountException extends Exception
{
function replaceContent($match) {
$result = phptal_component(htmlspecialchars_decode($match[3]));
return $result;
}
function applyComponents($text) {
return preg_replace_callback('/<(\w+)(\s+[a-zA-Z\-]+=\"[^\"]*\")*\s+tal:replace="structure\s+component:([^\"]*)"[^>]*>/u', 'replaceContent', $text);
}
class ComponentRequest {
public $component_id;
public $r;
function __construct($c, HttpRequest $r) {
$this->component_id = $c;
$this->r = $r;
}
function get($key, $default = null) {
if ($key == 'active_page') {
return $this->r->get($key);
}
if ($arr = $this->r->get($key)) {
if (is_array($arr)) {
return Arr::get($arr, $this->component_id, $default);
} else {
return $arr;
}
}
return $default;
}
function getAction() {
return $this->r->getAction();
}
}
/**
@ -9,51 +42,87 @@ class FileNotFountException extends Exception
*/
class Controller_Component
{
static $_uid = 1;
public $uid; // UID компонента создается при создании страницы, вставки компонента, или это статическое свойство
public $viewPath;
public $registry; // Registry->getInstance
public $template;
public $viewPath = array();
public $webPath = array();
function __construct()
{
self::$_uid ++;
$this->uid = self::$_uid;
public $template = null;
public $templatePath;
public $component_id;
public $COMPONENTS_WEB;
public /*.Settings.*/$registry;
public /*.Database.*/$db;
public /*.Collection.*/$parameter;
public $module;
public $item_module;
function before() {
}
function getUID()
{
return 'component:'. $this->uid;
function get($request, $key, $default) {
}
function execute(HttpRequest $request, $has_id = true) {
$crequest = new ComponentRequest($this->component_id, $request);
$action = 'action' . ucfirst($request->get('action', 'index'));
$this->before();
if (method_exists($this, $action)) {
return call_user_func(array($this, $action), $crequest);
} else {
return $this->actionIndex($crequest);
}
}
public function getView($name)
{
require_once "core/view/compositeview.php";
//
$template = ($this->template) ? $this->template : $this->_registry->readKey(array('system', 'template'));
// Загружать шаблон по умолчанию если не найден текущий
if (is_dir(Path::join($this->viewPath, 'templates', $template))) {
$template_file = Path::join($this->viewPath, 'templates', $template, $name);
} else {
$template_file = Path::join($this->viewPath, 'templates', 'modern', $name);
/*.Settings.*/$registry = $this->registry;
$template = ($this->template) ? $this->template : $registry->readKey(array('system', 'template'));
$selected = null;
foreach ($this->viewPath as $index => $viewPath) {
// Загружать шаблон по умолчанию если не найден текущий
if(is_dir(Path::join($this->viewPath[$index], 'templates', $template))) {
$tpl = new PHPTAL(Path::join($this->viewPath[$index], 'templates', $template, $name));
$tpl->setPhpCodeDestination(PHPTAL_PHP_CODE_DESTINATION);
$selected = $index;
break;
}
}
$tpl = new View_Composite($template_file);
$tpl->script = $_script = Path::join(WWW_PATH, 'js');
$tpl->media = $_media = Path::join(TEMPLATE_WEB, $template);
$tpl->component = $_template = Path::join(COMPONENTS_WEB, strtolower(get_class($this)), 'templates', 'modern');
$tpl->setAlias(array(
'${media}' => $_media,
'${script}' => $_script,
'${template}' => $_template));
if ($selected === null) {
$tpl = new PHPTAL(Path::join($this->viewPath[0], 'templates', 'modern', $name));
$tpl->setPhpCodeDestination(PHPTAL_PHP_CODE_DESTINATION);
$template = 'modern';
$selected = 0;
}
$tpl->loadImports(Path::skipExtension($template_file) . ".import");
$tpl->stripComments(true);
$tpl->addPreFilter(new PHPTAL_PreFilter_Normalize());
$tpl->set('common', Path::join(WWW_PATH, '../', 'common'));
$tpl->set('script', Path::join(WWW_PATH, 'js'));
$tpl->set('media', Path::join(TEMPLATE_WEB, $template));
$tpl->set('site_template', SITE_WWW_PATH . '/templates' . $registry->readKey(array('system', 'template')));
$tpl->set('base', SITE_WWW_PATH);
$tpl->set('component_base', $this->webPath[$selected]);
$tpl->set('component', Path::join($this->webPath[$selected], 'templates', $template));
$tpl->set('component_id', $this->component_id);
return $tpl;
}
public function setParameters($view)
public function getTemplatePath($name) {
return Path::join($this->viewPath[0], 'templates', 'modern', $name);
}
public function getTemplateWebPath()
{
return Path::join($this->webPath[0], 'templates', 'modern');
}
/**
@ -71,8 +140,6 @@ class Controller_Component
*/
public function getModel($name)
{
require_once 'core/mapper/mapper.php';
require_once ($this->getModelPath ($name));
$modelName = $name . "Mapper";
$model = new $modelName ();
@ -80,104 +147,179 @@ class Controller_Component
return $model;
}
public function options($key, $val, $res) {
public function options($key, $val, /*.Database_PDOStatement.*/$res) {
$result = array();
while($res->next()) {
$result[] = array('value' => $res->getInt($key), 'name' => $res->getString($val));
$result[] = array('value' => $res->getString($key), 'name' => $res->getString($val));
}
return $result;
}
public function optionsPair($list) {
public function optionsPair($list, $selected = false) {
$result = array();
foreach ($list as $key => $value) {
$result [] = array('value' => $key, 'name' => $value);
$result [] = array('value' => $key, 'name' => $value, 'selected' => $key == $selected);
}
return $result;
}
/* В дальнейшем нужно зменить на методы
+ Методы могут быть и javascript
*/
protected $editUrl;
function setEditUrl($url)
{
$this->editUrl = $url;
function getInfo() {
$filename = Path::join($this->viewPath[0], 'install.json');
if (file_exists($filename)) {
$settings = json_decode(file_get_contents($filename), true);
return $settings;
}
return array();
}
function getEditUrl()
/**
* Генерация интерфейса для выбора галлереи фотографии
*/
public function setParameters(/*.View_Composite.*/$view)
{
return $this->editUrl;
}
}
$form = new Form_Form();
$options = new OptionFactory($this->db);
/**
* TALES для подключения компонентов
* component:name?param1=value1&param2=value2
*/
class Component_Tales implements PHPTAL_Tales
{
static public function component($expression, $nothrow = false)
$settings = $this->getInfo();
$form->addFieldList($settings['parameter'], $options);
$view->form = $form;
$view->component = $settings['component'];
$view->component_title = $settings['title'];
}
static function loadComponent($expression, Database $db, Settings $registry)
{
return "phptal_component('" . $expression . "')";
}
}
$expression = htmlspecialchars_decode($expression);
$offset = strpos($expression, '?');
$url = parse_url($expression);
$arguments = array();
if ($offset === false) {
$path = $expression;
} else if (is_int($offset)) {
$path = substr($expression, 0, $offset);
$query = substr($expression, $offset + 1);
parse_str($query, $arguments);
}
$name = $path;
$path = Path::join (BASE_PATH, 'components', $name, $name . '.php');
$className = 'Component_' . $name;
if (file_exists($path)) {
require_once ($path);
$component = new $className();
$component->db = $db;
$component->registry = $registry;
$component->viewPath = array(BASE_PATH . '/components/' . $name . '/');
$component->webPath = array(SITE_WWW_PATH . '/components/' . $name);
$component->COMPONENTS_WEB = SITE_WWW_PATH . '/components/';
} else {
$path = Path::join (COMPONENTS, $name, $name . '.php');
require_once ($path);
$component = new $className();
$component->db = $db;
$component->registry = $registry;
$component->viewPath = array(COMPONENTS . '/' . $name . '/', BASE_PATH . '/components/' . $name . '/');
if (defined('COMPONENTS_WEB')) {
$component->webPath = array(COMPONENTS_WEB . '/' . $name, SITE_WWW_PATH . '/components/' . $name);
$component->COMPONENTS_WEB = COMPONENTS_WEB;
}
}
$stmt = $db->prepareStatement("SELECT * FROM component WHERE code = ?");
$stmt->setString(1, $expression);
$cid = $stmt->executeQuery();
if ($cid->next()) {
$component->component_id = $cid->getInt('id_component');
} else {
$last = $db->getIdGenerator();
if ($last->isBeforeInsert()) {
$result = $last->getId('component_id_component_seq');
$stmt = $db->prepareStatement("INSERT INTO component (id_component, code) VALUES ($result, ?)");
$stmt->setString(1, $expression);
$stmt->executeQuery();
}
if ($last->isAfterInsert()) {
$stmt = $db->prepareStatement("INSERT INTO component (code) VALUES (?)");
$stmt->setString(1, $expression);
$stmt->executeQuery();
$result = $last->getId('component_id_component_seq');
}
$component->component_id = $result;
}
$params = new Collection();
$params->import(array_merge($_GET, $arguments));
$component->parameter = $params;
$component->template = $params->get('template', false);
global $componentsConfig;
$editor = $component->getEditUrl();
if ($editor) {
$componentsConfig[] = $editor;
}
function loadComponent($name, $db, $registry)
{
$path = Path::join(COMPONENTS, $name, $name . ".php");
// echo COMPONENTS, '<br />';
// echo $path;
if (file_exists($path)) {
require_once ($path);
$component = new $name();
$component->db = $db;
$component->_registry = $registry;
$component->viewPath = COMPONENTS."/".$name."/";
return $component;
}
throw new FileNotFountException();
function getEditUrl() {
return null;
}
function raw_query(/*.ComponentRequest.*/ $request)
{
$arr = $request->r->export('get');
$param = array();
/*.Collection.*/$parameter = $this->parameter;
foreach($parameter->export() as $key => $value) {
$param[$key] = $value;
}
$data = array();
foreach($arr as $key => $value) {
if (is_array($value)) {
$data[$key] = Arr::get($value, $this->component_id);
} else {
$data[$key] = $value;
}
}
$data['param'] = $param;
return $data;
}
function query(/*.ComponentRequest.*/ $request, $list)
{
$arr = $request->r->export('get');
foreach($list as $key => $val) {
$arr[$key] [$this->component_id] = $val;
}
unset($arr['active_page']);
return '?' . http_build_query($arr);
}
function addRequireJsPath($name, $path, $shim = null) {
global $requireJsConfig;
$requireJsConfig['paths'][$name] = $path;
if ($shim) {
$requireJsConfig['shim'][$name] = $shim;
}
}
function actionIndex(/*.ComponentRequest.*/ $request) {
}
}
/**
* Функция подключения компонента
*/
global $componentList;
$componentList = array();
function phptal_component ($real_expression, $offset = 0) {
global $db, $registry, $componentList; // Нужно както передавать параметры
$expression = htmlspecialchars_decode($real_expression);
$url = parse_url($expression);
parse_str($url['query'], $arguments);
$name = $url['path'];
$component = loadComponent($name, $db, $registry);
$req = new HttpRequest();
$params = new Collection();
$params->import(array_merge($_GET, $arguments));
$component->params = $params;
$componentList [] = array(
'uid' => $component->getUID(), 'params' => $expression, 'name' => $name, 'offset' => $offset,
'size' => strlen($real_expression),
/* Вместо ссылки на редактирование нужно передавать список методов для работы с компонентом
edit (редактирование содержание), new (новое содержание), шаблон коменнента ... вместе с иконками этих методов
! Компоненты могут содержать другие компоненты
*/
'editurl' => $component->getEditUrl(),
'newurl' => ''
);
unset($req['active_page']);
$component->template = $params->get('template', false);
return $component->execute($params, $req);
}
/* Регистрация нового префикса для подключения компонента */
$registry = PHPTAL_TalesRegistry::getInstance();
$registry->registerPrefix('component', array('Component_Tales', 'component'));

View file

@ -2,25 +2,37 @@
/**
* Первичный контроллер контроллер страниц
* @package core
* @package system.controller
*/
class Controller_Front extends Controller_Action
{
/** @var Shortcut */
protected $shortcut; // Ярлык к модулю
protected $_param; // Параметр по которому выбирается модуль
protected $default; // Значение параметра по умолчанию
public function __construct(Settings $_registry, $_shortcut)
protected $modules = array();
/**
* @param Settings $_registry
* @param Shortcut $_shortcut
*/
public function __construct(Settings $_registry, $_shortcut) // $db, $installer, $shortcut
{
parent::__construct();
$registry = $_registry;
$this->_registry = $_registry;
$this->_shortcut = $_shortcut;
$this->_shortcut = $_shortcut; // $cc->newShortcut();
$this->db = Database::getConnection($registry->readKey(array('system', 'dsn')));
$dsn = $registry->readKey(array('system', 'dsn'));
$this->db = Database::getConnection($dsn); // $cc->newConnection();
}
public function isLoaded($name)
{
return isset($this->modules[$name]);
}
/**
* Создает экземпляр модуля и выполняет действия для него
@ -28,15 +40,22 @@ class Controller_Front extends Controller_Action
* @param request $request Имя модуля
* @return string
*/
public function loadModule($name, Collection $request)
public function loadModule($name, Collection $request, $controller = false)
{
if ($this->isLoaded($name)) {
$module = $this->modules[$name];
return $module->access->execute($request);
}
$moduleFile = Shortcut::getUrl($this->shortcut, $name); // ModuleLoader (2)
$module = $this->loadClass($moduleFile);
if ($controller) {
$moduleFile = Shortcut::getUrl($this->shortcut, $name, $controller); // ModuleLoader (2)
} else {
$moduleFile = Shortcut::getUrl($this->shortcut, $name, $name); // ModuleLoader (2)
}
$module = $this->loadClass($moduleFile, null, 'Module_');
if ($module) {
// Инициализация модуля
// $module->viewPath = dirname($moduleFile);
$module->viewPath = Shortcut::getUrl('modulepath', $name);
$module->name = $name;
@ -51,15 +70,17 @@ class Controller_Front extends Controller_Action
$module->db = $this->db;
// Не для всех приложений нужно вести лог действий
// Ведение лога
$logger = $this->loadClass(FRAMEWORK_PATH . '/src/filter/actionlogger.php', $module);
$logger = $this->loadClass(FRAMEWORK_PATH . '/filter/actionlogger.php', $module, 'Filter_');
$logger->before = $this->loadSettings(Shortcut::getUrl('logger', $name));
// Управление доступом
$module->access = $this->loadClass(FRAMEWORK_PATH . '/src/filter/actionaccess.php', $logger);
$module->access = $this->loadClass(FRAMEWORK_PATH . '/filter/actionaccess.php', $logger, 'Filter_');
$module->access->access = $this->loadSettings(Shortcut::getUrl('access', $name));
$module->setUp();
return $module->access->execute($request);
$this->modules[$name] = $module;
$result = $module->access->execute($request);
return $result;
}
return null; // throw new FileNotFoundException();
}
@ -72,13 +93,14 @@ class Controller_Front extends Controller_Action
$this->default = $name;
}
private function getParameter(Collection $list)
{
return ($list->get($this->_param)) ? $list->get($this->_param): $this->default;
}
public function execute(HTTPRequest $request)
{
return $this->loadModule($this->getParameter($request), $request);
$name = explode("_", $request->get($this->_param, $this->default));
if (count($name) >= 2) {
$controller = $name[1];
} else {
$controller = false;
}
return $this->loadModule($name[0], $request, $controller);
}
}

View file

@ -2,7 +2,7 @@
class Controller_Installer
{
protected $db;
protected $db_manager;
protected $installPath;
public $_registry;
@ -11,15 +11,20 @@ class Controller_Installer
$this->_registry = $_registry;
}
public function setUp($db, $installPath)
public function setUp($db_manager, $installPath)
{
$this->db = $db;
$this->db_manager = $db_manager;
$this->installPath = $installPath;
}
function getSetupFile($name)
{
return Path::join(call_user_func($this->installPath, $name), "setup.php");
$setup = Path::join(call_user_func($this->installPath, $name), "install.json");
return $setup;
}
function getUninstallFile($name){
return Path::join(call_user_func($this->installPath, $name), "sql", "uninstall.json");
}
// Проверка версии обновления
@ -38,22 +43,33 @@ class Controller_Installer
function installSQL(array $sql, $version_new, $version_old, $name)
{
require_once "core/setup.php";
$result = [];
$json_installer = new Database_JsonInstall($this->db_manager);
foreach ($sql as $version => $install) {
if (version_compare($version, $version_new, "<=") && version_compare($version, $version_old, ">")) {
// this->installPath this->db
$file = Path::join(call_user_func($this->installPath, $name), "sql", $install);
Setup::batchSQL($this->db, $file);
$json_installer->install($file,null);
$result[] = $version;
}
}
return $result;
}
function uninstall($name){
$uninstall = $this->getUninstallFile($name);
if (file_exists($uninstall)) {
$json_installer = new Database_JsonInstall($this->db_manager);
$json_installer->install($uninstall,null);
}
$this->_registry->removeKey($name);
$this->_registry->write();
}
// Устанавливает обновления если есть
function doUpdates($name, $force = false) // Установка модуля
{
$result = array();
$setup = $this->getSetupFile($name);
if (file_exists($setup) && ($this->isChanged($name) || $force)) {
$registry = $this->_registry;
$settings = new Settings($setup);
$settings->read();
@ -67,21 +83,29 @@ class Controller_Installer
$version_old = "0.0";
$registry->writeKey(array($name), array());
}
if (version_compare($version_old, $settings->get('version'), "!=")) {
$sql = $settings->get('sql');
if (is_array($sql)) {
$this->installSQL($sql, $version_new, $version_old, $name);
$res = $this->installSQL($sql, $version_new, $version_old, $name);
if($res){
$result[]=$res;
}
}
}
// Обновление версии меню
$registry->removeKey($name);
$registry->writeKey(array($name), $settings->get('settings'));
$registry->writeKey(array($name),
array('version' => $version_new,
'time' => filemtime($setup)));
$registry->write();
}
return $result;
}
function install($dbinit_path,$dbfill_path=null){
$json_installer = new Database_JsonInstall($this->db_manager);
$json_installer->install($dbinit_path,$dbfill_path);
}
}

View file

@ -28,38 +28,53 @@ class Controller_Model extends Controller_Action
public function __construct()
{
$this->path = new PathMenu();
$this->menu = new PageMenu();
$this->table = new ListTable();
parent::__construct();
$this->menu = new Widgets_PageMenu();
$this->table = new Widgets_ReactListTable();
}
/**
*/
function setUp()
{
$this->table->addMenuItem($this->aUrl('delete'), 'удалить', false, 'all', 'warning');
//$this->table->addMenuItem($this->nUrl('form'), 'редактировать', 'edit-24.png');
function setUp() {
parent::setUp();
$this->table->addMenuItem($this->aUrl('delete'), 'удалить', false, 'all', 'btn-danger', 'remove');
}
function saveParameters($args, $list)
{
function saveParameters(HttpRequest $args, $list) {
foreach ($list as $item) {
$args->session()->set(array($this, $item), $args->get($item));
}
}
protected function getJSONList(/*Mapper*/ $model, Collection $request)
{
protected function getJSONList(/*.Model_Mapper.*/ $model, HttpRequest $request) {
$result = array();
$this->saveParameters($request, array('size','page','desc', 'key'));
$result['list'] = $model->findAll($request, $request->get('ref'));
$result['size'] = $model->getCount($request, $request->get('ref'));
return json::encode($result);
return json_encode($result);
}
protected function getCSV(Model_Mapper $model, HttpRequest $request, $title) {
$ref = $request->get('ref');
$list = $model->findAll($request, $request->get('ref'));
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename='.$title.'.csv');
echo "\xEF\xBB\xBF"; //UTF-8 BOM.
$output = fopen('php://output', 'w');
if (is_resource($output)) {
foreach ($list as $row) {
fputcsv($output, (array)$row,';');
}
}
}
/**
* Удаление сторк из таблицы
* Удаление строк из таблицы
*/
public function actionDelete(HttpRequest $request)
{
@ -68,7 +83,8 @@ class Controller_Model extends Controller_Action
$list = ($request->get('table_item')) ? $request->get('table_item'): $request->get('id');
$model->deleteList($list);
return $this->getJSONList($model, $request);
// return $this->getJSONList($model, $request);
return json_encode(array('result' => 'ok'));
}
/**
@ -88,16 +104,29 @@ class Controller_Model extends Controller_Action
public function actionList(HttpRequest $request)
{
$model = $this->getModel($this->useModel);
if ($request->get('filter')) {
$data = new Collection();
$data->import($request->get('filter'));
$model->addFilter($model->requestToSQL($data, $this->formSchema));
}
return $this->getJSONList($model, $request);
}
public function actionCSV(HttpRequest $request)
{
$model = $this->getModel($this->useModel);
$title = $request->get("title");
if(!$title){
$title = "noname";
}
return $this->getCSV($model, $request, $title);
}
private function setFormSchema()
{
require_once 'core/mapper/uimapper.php';
$model = $this->getModel($this->useModel);
$ui = new UIMapper($model);
$ui = new Model_UIMapper($model);
$this->formSchema = $ui->getFormSchema();
}
@ -105,29 +134,29 @@ class Controller_Model extends Controller_Action
/**
* Сохранение формы
*/
function beforeSave(/*Model*/ $item, Collection $request)
function beforeSave(/*Model*/ $item, HttpRequest $request)
{
if (empty($this->formSchema)) {
$this->setFormSchema();
}
// Сделать отображение Формы в обьект и обратно <-- Убрать в beforeSave
// Сделать отображение Формы в обьект и обратно
foreach ($this->formSchema as $key => $conv) {
list($value, $type) = $conv;
$item->$value = call_user_func(array('Cast', 'to_' . $type), $request->get($key)); // Здесть нужно преобразовывать тип значения
$item->$value = call_user_func(array('Primitive', 'to_' . $type), $request->get($key)); // Здесь нужно преобразовывать тип значения
}
}
/**
* Обновление формы
*/
function formUpdate(TForm $form, Collection $request)
{
function formUpdate(Form_Form $form, HttpRequest $request) {
$form->setValues($request);
}
/**
* Загрузка формы
*/
function beforeLoad(/*Model*/ $item, TForm $form)
function beforeLoad(/*.Model_Model.*/$item, Form_Form $form)
{
if (empty($this->formSchema)) {
$this->setFormSchema();
@ -137,8 +166,13 @@ class Controller_Model extends Controller_Action
$form->fill($item, $this->formSchema);
}
function beforeFirstLoad(Form_Form $form)
{
}
// Проверка ввода
protected function validate($validator, $request)
protected function validate(Validator_Validator $validator, Collection $request)
{
}
@ -147,8 +181,7 @@ class Controller_Model extends Controller_Action
*/
public function actionValidate($request)
{
require_once "core/validator/validator.php";
$validator = new Validator();
$validator = new Validator_Validator();
$validator->addRuleList($this->schema);
// Действия до проверки формы
@ -156,19 +189,19 @@ class Controller_Model extends Controller_Action
$validator->validate($request); // --|
// Проверка формы
if (!$validator->isValid()) {
return json::encode($validator->getErrorMsg());
return json_encode($validator->getErrorMsg());
}
return json::encode(true);
return json_encode(true);
}
/**
* Инициализация формы
*/
protected function formSetup($form, $id = null, $ref = null)
protected function formSetup(Form_Form $form, $id = null, $ref = null)
{
if (empty($this->schema)) {
$model = $this->getModel($this->useModel);
$ui = new UIMapper($model);
$ui = new Model_UIMapper($model);
$schema = $ui->getEditSchema();
$form->addFieldList($schema);
@ -178,71 +211,57 @@ class Controller_Model extends Controller_Action
}
/**
* Добавление пользователя
* Добавление
*/
public function actionAdd(HttpRequest $request)
{
require_once "core/validator/validator.php";
// {{{ тоже может быть один ref или несколько
$ref = $request->get('ref');
$this->addParameter('ref', $ref); // Добавляет параметр в url
/// }}}
if ($this->checkPageId($request, $request->get('page'))) {
// Проверка
$validator = new Validator();
$validator->addRuleList($this->schema);
// Действия до проверки формы
$this->validate($validator, $request); // <--|
$validator->validate($request); // --|
// Проверка формы
if (!$validator->isValid()) {
$request->setAction('form');
$this->getActionPath($request);
// Проверка
$validator = new Validator_Validator();
$validator->addRuleList($this->schema);
$form = new TForm();
$this->formSetup($form, $request->get('id'), $request->get('ref')); // Инициализация формы
// Действия до проверки формы
$this->validate($validator, $request); // <--|
$validator->validate($request); // --|
// Проверка формы
if (!$validator->isValid()) {
return json_encode(array('result'=>'fail','errors'=>$validator->getErrorMsg()));
}
// Нужен тест для формы
$model = $this->getModel($this->useModel);
$className = $model->className;
$item = new $className();
$form->setValues($request); // <-- Убрать в formUpdate
$this->formUpdate($form, $request);
// Сохраняем значение в базе данных
$item->id = $request->get('id');
// Если таблица связана с другой таблицей
if ($request->get('ref') && $model->reference[1]) {
$ref_id = $model->reference[1];
$item->$ref_id = $request->get('ref');
}
$form->setError($validator); // Установка ошибок для формы
// Подготовка к сохранению
$this->beforeSave($item, $request); // Сюдаже и истрия переходов
// nextId ??? или выход или новая форма для создания новости
$id = $model->saveDB($item, $request);
$tpl = $this->formPage($form, $request);
$id = $request->get('id');
if ($id) { // Редактирование
$tpl->action = forceUrl($this->nUrl('add', array('id' => $id, 'page' => $this->getPageId($request)))); // action Совйство формы
}
return $tpl /*->execute()*/;
}
// Нужен тест для формы
$model = $this->getModel($this->useModel);
$className = $model->className;
$item = new $className();
// Сохраняем значение в базе данных
$item->id = $request->get('id');
// Если таблица связана с другой таблицей
if ($request->get('ref') && $model->reference[1]) {
$ref_id = $model->reference[1];
$item->$ref_id = $request->get('ref');
}
// Подготовка к сохранению
$this->beforeSave($item, $request); // Сюдаже и истрия переходов
// nextId ??? или выход или новая форма для создания новости
$model->saveDB($item, $request);
}
// Для страницы со списком id -> идентефикатор родительской таблицы !!??
// Для страницы со списком id -> идентификатор родительской таблицы !!??
// $request->set('id', $request->get('ref'));
if ($request->get('apply')) {
$request->setAction('form');
return $this->forward('actionForm', $request);
return (string) $this->forward('actionForm', $request);
}
return $this->forward('actionIndex', $request);
//$request->setAction('index');
$result = array('result'=>'ok');
if($id){
$result['action'] = forceUrl($this->nUrl('add', array('id' => $id, 'ref' => $request->get('ref'))));
}
return json_encode($result);
}
/**
@ -275,38 +294,40 @@ class Controller_Model extends Controller_Action
$this->setTitlePath($ref);
$model = $this->getModel($this->useModel);
$form = new TForm(); // Показываем форму
$form = new Form_Form(); // Показываем форму
$form->header = 'Редактирование записи';
$this->formSetup($form, $request->get('id'), $request->get('ref')); // Инициализация формы
$list = $request->get('table_item');
$id = ($list[0]) ? $list[0] : $request->get('id');
$tpl = $this->formPage ($form, $request);
//
$tpl = $this->formPage($form, $request);
if ($id) { // Редактирование
$form->action = forceUrl($this->nUrl('add', array('id' => $id, 'page' => $this->getPageId($request)))); // action Свойство формы
$item = $model->findById($id);
// Загрузка формы
$this->beforeLoad($item, $form);
///
}else{//Создание нового элемента
$this->beforeFirstLoad($form);
}
return $tpl;
return json_encode($tpl);
}
/**
*/
function tableSetup($table, $id = null, $ref = null)
function tableSetup(Widgets_ListTable $table, $id = null, $ref = null)
{
// FIXME: После замены везде $tableSchema -> table->setHeader удалить!
if ($this->tableSchema) {
$table->setHeader($this->tableSchema);
} else {
// Настройка таблицы отображения по схеме данных
require_once 'core/mapper/uimapper.php';
$model = $this->getModel($this->useModel);
$ui = new UIMapper($model);
$schema = $ui->getTableSchema();
$schema[0]['action'] = $table->getFirstItem();
$ui = new Model_UIMapper($model);
$ui->hidden = $table->hidden;
$schema = $ui->getTableSchema();
$schema[0]['action'] = forceUrl($table->getFirstItem());
$table->setHeader($schema);
}
@ -314,7 +335,7 @@ class Controller_Model extends Controller_Action
/**
*/
public function actionIndex(HttpRequest $request)
public function actionDefault(HttpRequest $request)
{
$this->getActionPath($request, 'index');
// Такое мета действие наверное можно вынести в отдельный класс
@ -335,14 +356,14 @@ class Controller_Model extends Controller_Action
$ref = $request->session()->get('ref');
}
$request->session->set('ref', $ref);
$request->session()->set('ref', $ref);
$this->addParameter('ref', $ref);
// }}}
$this->setTitlePath($ref);
$tpl = $this->getView('list');
$tpl = new stdClass();
// Помошники действий
// Помощники действий
$this->callHelpers($request);
// Таблица
if ($request->session()->get(strtolower(get_class($this)))) {
@ -355,7 +376,7 @@ class Controller_Model extends Controller_Action
'size' => $session['size'],
'desc' => $session['desc']));
$this->table->setData('sorter', $session['key']);
//$this->table->setData('sorter', $session['key']);
if (isset($session['desc'])) {
$this->table->setData('desc', $session['desc']);
}
@ -363,64 +384,137 @@ class Controller_Model extends Controller_Action
call_user_func($setup, $this->table, $request->get('id'), $ref);// --> Эквивалент formSetup
$this->table->setAction($list);
//
if (!$this->table->getData('module')) {
$this->table->setData('module', strtolower($this->useModel));
}
$tpl->menu_path = $this->path->getItems();
// Поиск
$search = new SearchDialog();
$search->setTitle('Поиск');
$search->setAction($this->aUrl('search'));
$search->setFriend($this->table);
$search->addFields($this->schemaSearch);
$this->table->makeData();
$tpl->table = array('data' => $this->table->data);
$tpl->menu = $this->menu->menu->getItems();
$tpl->path = $this->path->getItems();
// Настройки
$setup = new SetupDialog();
$setup->setTitle('Настройки');
$setup->setAction($this->nUrl('setup'));
$setup->setFriend($this->table);
// Меню
$this->menu->addMenuItem('?menu=toggle&id=' . $search->getName(), 'поиск', 'actions/system-search'); // Стандартный размер для иконок 22-24px
$this->menu->addMenuItem('?menu=toggle&id=' . $setup->getName(), 'настройки', 'categories/applications-system');
// Добавление компонентов
$this->addChild('menu', $this->menu);
$this->addChild('search', $search);
$this->addChild('setup', $setup);
$this->addChild('table', $this->table);
//
return $tpl;
return json_encode($tpl);
}
/**
*/
public function actionSetup($request)
{
public function actionSetup(HttpRequest $request) {
$left = explode(",", $request->get('left'));
$right = explode(",", $request->get('right'));
$$request->session()->set(strtolower(get_class($this)),
array('view' => array('left' => $left, 'right' => $right)));
$request->session()->set(strtolower(get_class($this)),
array('view' => array('left' => $left, 'right' => $right), 'page' => 1, 'size' => 0, 'desc' => 'asc', 'key' => false));
return $this->forward('actionIndex', $request);
}
/**
*/
private function formPage($form, $request)
{
$view = $this->getView('form');
$view->setView('form', $form);
$view->action = forceUrl($this->nUrl('add', array('page' => $this->getPageId($request)))); // Действие для формы
protected function formPage($form, $request, $template = 'form') {
$view = new stdClass();//$this->getView($template);
$view->form = $form;
$form->action = forceUrl($this->nUrl('add', array('page' => $this->getPageId($request)))); // Действие для формы
$view->menu_path = $this->path->getItems();
$view->path = $this->path->getItems();
$view->back = $this->path->getPrev();
return $view;
}
function _getActionPath() {
return new Controller_State('index');
}
// Тоже убрать в метод Controller_Model
function getActionPath(HttpRequest $request/*, $action = false*/)
{
require_once 'state.php';
function getActionPath(HttpRequest $request, $action = null) {
$this->_getActionPath()->getPath($this, ($action) ? $action : $request->getAction());
}
function parse_params($expression) {
$expression = htmlspecialchars_decode($expression);
$offset = strpos($expression, '?');
$url = parse_url($expression);
$arguments = array();
if ($offset === false) {
$path = $expression;
} else if (is_int($offset)) {
$path = substr($expression, 0, $offset);
$query = substr($expression, $offset+1);
parse_str($query, $arguments);
}
return $arguments;
}
public function actionFormPage(HttpRequest $request) {
//$tpl = $this->getView('formpage', 'View_Top');
$view = $this->getView('formpage', 'View_Top');
$params = $this->parse_params($request->get('params'));
// $model = $this->getModel($this->useModel);
$form = new Form_Form(); // Показываем форму
$form->header = 'Редактирование записи';
$this->formSetup($form, $request->get('id'), $request->get('ref')); // Инициализация формы
/*
$ui = new Model_UIMapper($model);
$schema = $ui->getEditSchema();
$form->addFieldList($schema);
*/
$list = $request->get('table_item');
$id = ($list[0]) ? $list[0] : $request->get('id');
// $tpl = $this->formPage($form, $request);
$view->setView('form', $form);
$view->action = forceUrl($this->nUrl('addpage', array('ref' => $params['id']))); // Действие для формы
// $view->menu_path = $this->path->getItems();
$view->back = '';
return $view;
// return $tpl;
}
/* Для поддержки редактрования на сайте */
public function actionAddPage(HttpRequest $request)
{
// {{{ тоже может быть один ref или несколько
$ref = $request->get('ref');
$this->addParameter('ref', $ref); // Добавляет параметр в url
/// }}}
$validator = new Validator_Validator();
$validator->addRuleList($this->schema);
// Действия до проверки формы
$this->validate($validator, $request); // <--|
$validator->validate($request); // --|
// Проверка формы
if (!$validator->isValid()) {
return $validator;
}
// Нужен тест для формы
$model = $this->getModel($this->useModel);
$className = $model->className;
$item = new $className();
// Сохраняем значение в базе данных
$item->id = $request->get('id');
// Если таблица связана с другой таблицей
if ($request->get('ref') && $model->reference[1]) {
$ref_id = $model->reference[1];
$item->$ref_id = $request->get('ref');
}
// Подготовка к сохранению
$this->beforeSave($item, $request); // Сюдаже и истрия переходов
$model->saveDB($item, $request);
return null;
}
}