Частичная синхронизация с CMS

This commit is contained in:
origami11 2017-02-17 16:22:44 +03:00
parent 312f18a20a
commit b26e521657
62 changed files with 827 additions and 5992 deletions

73
src/View/Page.php Normal file
View file

@ -0,0 +1,73 @@
<?php
class View_Page extends View_View
{
private $counter;
public $text;
function __construct($data)
{
// Вставка компонентов на странице
$pattern = '/<(\w+)(\s+[a-zA-Z\-]+=\"[^\"]*\")*\s+tal:replace="structure\s+component:([^\"]*)"[^>]*>/u';
$matches = array();
preg_match_all($pattern, $data, $matches, PREG_OFFSET_CAPTURE, 0);
$split = array();
$offset = 0;
foreach ($matches[0] as $key => $match) {
$text = $this->fixHTML(substr($data, $offset, $match[1] - $offset));
if (trim($text)) {
$split[] = array('type' => 'page-text', 'content' => $text, 'component' => '', 'module' => '');
}
$offset = $match[1] + strlen($match[0]);
$split[] = $this->replaceContent($matches[3][$key][0], $matches[3][$key][1]);
}
$text = $this->fixHTML(substr($data, $offset));
if (trim($text)) {
$split[] = array('type' => 'page-text', 'content' => $text, 'component' => '', 'module' => '');
}
$this->text = $this->merge($split);
}
function fixHTML($fragment) {
return $fragment;
}
function merge($data) {
if (count($data) == 0) {
$data[] = array('type' => 'page-text', 'content' =>"<p>Добавьте текст<p>", 'component' => '', 'module' => '');
}
$result = array();
foreach($data as $key => $part) {
$result[] = $part['content'];
}
return implode("", $result);
}
function replaceContent($match, $offset)
{
//$result = phptal_component($match, $offset);
//*
global $db, $registry; // Нужно как-то передавать параметры
$component = Controller_Component::loadComponent($match, $db, $registry);
$req = new HttpRequest();
unset($req['active_page']);
$info = $component->getInfo();
$result = $component->execute($req);
if (is_string($result)) {
return array('type' => 'page-component', 'content' => $result, 'component' => $match);
} else {
$this->setView('view' . $this->counter++, $result);
return array('type' => 'page-component', 'content' => $result->execute(), 'component' => $match);
}
}
function execute() {
return $this->text;
}
}

48
src/View/Plain.php Normal file
View file

@ -0,0 +1,48 @@
<?php
// Класс отображения
// CompositeView !! Composite pattern
/**
* @package system.view
*/
class View_Plain
{
protected $document;
protected $values = array();
public function __construct ($document)
{
$this->document = $document;
}
public function set($key, $value)
{
$this->values[$key] = $value;
}
public function import($list)
{
$this->values = array_merge($this->values, $list);
}
public function __set($key, $value)
{
$this->set($key, $value);
}
public function execute()
{
$result = $this->values;
return self::getTemplateContent ($this->document, $result);
}
static function getTemplateContent($document, $result)
{
ob_start ();
include ($document);
$content = ob_get_contents ();
ob_clean ();
return $content;
}
}

View file

@ -1,40 +1,203 @@
<?php
// Класс отображения
// CompositeView !! Composite pattern
// View_Top => View_Base + View_List + View_Template
class View_View
{
protected $document;
protected $values;
protected $_section = array(); // Вложенные шаблоны
// Блоки
protected $_stylesheet = array(); // Массив стилей текущего шаблона
protected $_script = array(); // Массив скриптов текущего шаблона
protected $_scriptstring = array();
protected $_startup = array();
protected $_values = array();
public function __construct ($document)
{
$this->document = $document;
protected $_title = null; // Заголовок текущего шаблона
public $active_module;
public $module_action;
public $suggestions; //подсказки
public $alias = array();
public $codeGenerator = null;
public $parent_view = null;
function __construct() {
}
public function set($key, $value)
/**
* Связывет переменную с вложенным шаблоном
*
* @param string $section переменная шаблона
* @param CompositeView $view вложенный шаблон
*/
public function setView($section, View_View $view)
{
$this->values[$key] = $value;
$this->_section [$section] = $view;
$view->parent_view = $this;
}
public function __set($key, $value)
public function assignValues($values)
{
$this->set($key, $value);
$this->_values = $values;
$this->_values["suggestions"] = $this->suggestions;
}
public function execute()
public function jGrowl($message, $args)
{
$result = $this->values;
return self::getTemplateContent ($this->document, $result);
$this->addScriptRaw('$.jGrowl("' . $message . '", ' . json_encode($args) . ");\n", true);
}
static function getTemplateContent($document, $result)
public function setGlobal($name, $args)
{
ob_start ();
include ($document);
$result = ob_get_contents ();
ob_clean ();
$this->addScriptRaw("var " . $name . " = " . json_encode($args) . ";\n", false);
}
/**
* Добавляет скипт к текущему шаблону
*
* @param string $name путь к скрипту
*/
public function addScript($name)
{
$output = $this->resolveName($this->alias, $name);
$this->_script [] = $output;
}
/**
* Добавляет код скипта к текущему шаблону
*
* @param string $name строка javascript кода
*/
public function addScriptRaw($name, $startup = false)
{
if ($startup) {
$this->_startup [] = $name;
} else {
$this->_scriptstring [] = $name;
}
}
/**
* Добавляет стили к текущему шаблону
*
* @param string $name путь к стилю
*/
public function addStyleSheet($name)
{
$output = $this->resolveName($this->alias, $name);
$this->_stylesheet [] = $output;
}
/**
* Рекурсивно извлекает из значение свойства обьекта
*
* @param string $list Имя свойства
* @param boolean $flatten
*/
protected function doTree($list, $flatten = true)
{
$result = ($flatten == true) ? $this->$list : array($this->$list);
foreach ($this->_section as $value) {
if (is_object($value)) {
if ($list == '_script' || $list == '_stylesheet') {
$result = array_merge($result, $value->doTree($list, $flatten));
} else {
$result = array_merge($value->doTree($list, $flatten), $result);
}
}
}
return $result;
}
}
/*abstract*/ public function set($key, $value)
{
}
/**
* Обработка всех вложенных шаблонов
*
* @return string
*/
public function execute()
{
foreach ($this->_section as $key => $value) {
$this->set($key, (is_object($value)) ? $value->execute() : $value); // ?
}
}
/**
* Установка заголовка шаблона
*
* @param string $title
*/
public function setTitle($title)
{
$this->_title = $title;
}
protected function isNotNull($title)
{
return $title !== null;
}
function setAlias($alias)
{
$this->alias = $alias;
}
function addAlias($name, $path)
{
$this->alias[$name] = $path;
$this->set($name, $path);
}
function find_file($pathlist, $file) {
foreach($pathlist as $key => $www) {
if (file_exists($key . '/' . $file)) {
return $www . '/' . $file;
}
}
throw new Exception("file not found: $file");
}
function resolveName($alias, $file) {
list($type, $filename) = explode(":", $file, 2);
// Сделать поиск а не просто замену папки при совпадении имени
if (is_array($alias[$type])) {
$output = $this->find_file($alias[$type], $filename);
} else {
$output = $alias[$type] . '/' . $filename;
}
return $output;
}
function loadImports($importFile)
{
$types = array(
'js' => array($this, 'addScript'),
'css' => array($this, 'addStyleSheet')
);
// Подключение стилей и скриптов
if (file_exists($importFile)) {
$files = file($importFile);
foreach ($files as $file) {
// Получить расширение вместо strpos
$file = trim($file);
if (!empty($file)) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
call_user_func($types[$ext], $file);
}
}
}
}
public function resolveAllNames($alias, $list) {
$result = array();
foreach($list as $item) {
$result [] = $this->resolveName($alias, $item);
}
return $result;
}
}

13
src/View/list.php Normal file
View file

@ -0,0 +1,13 @@
<?php
class View_List extends View_View
{
function execute()
{
$result = array();
foreach ($this->_section as $key => $value) {
$result [] = $value->execute();
}
return $result;
}
}