Версия полностью совместимая 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

@ -1,36 +1,37 @@
<?php
/**
* @package system.view
*/
// CompositeView+PHPTAL
class View_Composite extends View_Top
// View_Base + PHPTAL = View_Template (View_Composite)
class View_Composite extends View_View
{
private $tal;
function __construct($file)
{
parent::__construct($file);
parent::__construct();
$this->tal = new PHPTAL($file);
$this->tal->setPhpCodeDestination(PHPTAL_PHP_CODE_DESTINATION);
$this->tal->setEncoding(PHPTAL_DEFAULT_ENCODING); // PHPTAL_DEFAULT_ENCODING !!
$this->tal->setTemplateRepository(PHPTAL_TEMPLATE_REPOSITORY);
$this->tal->setOutputMode(PHPTAL::HTML5);
$this->tal->stripComments(true);
// $this->tal->addPreFilter(new PHPTAL_PreFilter_Normalize());
}
function set($key, $val)
{
function set($key, $val) {
if ($key == 'title') {
$this->setTitle($val);
}
$this->tal->set($key, $val);
}
function __set($key, $val)
{
function __set($key, $val) {
$this->tal->set($key, $val);
}
function setTranslator($tr)
{
$this->tal->setTranslator($tr);
}
function execute()
{
parent::execute();

View file

@ -1,88 +1,139 @@
<?php
class View_Top // AbstractCompositeView
{
protected $_section = array(); // Вложенные шаблоны
// Блоки
protected $_stylesheet = array(); // Массив стилей текущего шаблона
protected $_script = array(); // Массив скриптов текущего шаблона
protected $_scriptstring = array();
protected $_startup = array();
protected $_title = null; // Заголовок текущего шаблона
public $alias = array();
function __construct()
{
}
class View_Top extends View_Composite {
/**
* Связывет переменную с вложенным шаблоном
*
* @param string $section переменная шаблона
* @param CompositeView $view вложенный шаблон
* Общая строка заголовка
*/
public function setView($section, /*CompositeView*/ $view)
public $mid = 0;
public $require = array();
public $deps = array();
public function getTitle()
{
$this->_section [$section] = $view;
return implode(" - ", array_filter($this->doTree('_title', false), array($this, 'isNotNull')));
}
public function jGrowl($message, $args)
private function findGroup($groups, $file)
{
$this->addScriptRaw('$.jGrowl("' . $message . '", ' . json_encode($args) . ");\n", true);
}
public function setGlobal($name, $args)
{
$this->addScriptRaw("var " . $name . " = " . json_encode($args) . ";\n", false);
}
/**
* Добавляет скипт к текущему шаблону
*
* @param string $name путь к скрипту
*/
public function addScript($name)
{
$this->_script [] = $name;
}
/**
* Добавляет код скипта к текущему шаблону
*
* @param string $name строка javascript кода
*/
public function addScriptRaw($name, $startup = false)
{
if ($startup) {
$this->_startup [] = $name;
} else {
$this->_scriptstring [] = $name;
foreach($groups as $key => $group) {
if(in_array($file, $group)) {
return $key;
}
}
return false;
}
private function groupFiles(array $list, $debug = true)
{
$debug = ($debug) ? 'debug=1' : '';
$path = parse_url(WWW_PATH, PHP_URL_PATH);
// Группы определять в import через ,
$groups = array( );
$use = array();
$result = array();
foreach ($list as $file) {
$name = $this->findGroup($groups, $file);
if($name) {
$use [$name] = 1;
} else {
if (strpos($file, 'ckeditor')) {
$result [] = $file;
} else {
// $result [] = WWW_PATH . "/min/?$debug&f=" . parse_url($file, PHP_URL_PATH);
$result [] = $file;
}
}
}
$list = array();
foreach ($use as $name => $value) {
// $list [] = WWW_PATH . "/min/?$debug&f=" . implode(",", $groups[$name]);
$list [] = $groups[$name];
}
return array_merge($list, $result);
}
function getId($pref) {
$this->mid++;
return $pref.$this->mid;
}
/**
* Добавляет стили к текущему шаблону
*
* @param string $name путь к стилю
*/
public function addStyleSheet($name)
{
$this->_stylesheet [] = $name;
}
/**
* Рекурсивно извлекает из значение свойства обьекта
* Обработка шаблона
*
* @param string $list Имя свойства
* @param boolean $flatten
* @return string
*/
private function doTree($list, $flatten = true) {
$result = ($flatten == true) ? $this->$list : array($this->$list);
foreach ($this->_section as $key => $value) {
if (is_object($value)) $result = array_merge($value->doTree($list, $flatten), $result);
}
return $result;
public function render()
{
$alias = $this->doTree('alias');
// require_once 'minify.php';
// Скрипты и стили
$this->set('scriptstring', $this->getScriptRaw());
$this->set('scripts', array_unique($this->groupFiles($this->getScripts(), false)));
$this->set('stylesheet', array_unique($this->groupFiles($this->getStyleSheet(), false)));
$this->require = array('admin' => 'ma');
$this->deps = array();
$startup = array();
foreach($this->_section as $s) {
$module = explode('_', $s->active_module, 2);
if (count($module) < 2) {
continue;
}
$module = $module[1];
$name = mb_strtolower($module);
$fname = $name . '/' . $name;
$current = $this->getId('m');
$this->require[$fname] = $current;
$value = $this->getId('v');
$script = "var " . $value . " = new " . $current . '.' . $module . "();\n";
foreach($s->_values as $key => $v) {
$script .= $value . "." . $key . " = " . json_encode($v/*, JSON_PRETTY_PRINT*/) . ";\n";
}
$init = array();
foreach($s->_section as $key => $item) {
/*.View_View.*/$ss = $item;
if ($ss->codeGenerator !== null) {
// функцию которая вычисляет а не результат
$part = call_user_func($ss->codeGenerator, $this, $key, $value);
$init [] = $part;
}
}
$script .= $value . ".execute('" . $s->module_action . "', " . json_encode($init) .");\n";
$startup[] = $script;
}
/* echo "<pre>";
print_r($this->require);
print_r(implode("", $startup));
echo "</pre>";*/
//$this->set('scriptstring', '');
$this->set('startup', implode("", $startup) . $this->getScriptStartup());
$this->set('require', implode(",", array_map(function ($x) { return "'$x'";}, array_keys($this->require))));
$this->set('deps', implode(",", array_values($this->require)));
$this->set('title', $this->getTitle());
$this->set('jspath', WWW_PATH);
//
return $this->execute(); // execute+phptal ??
}
/**
@ -95,15 +146,6 @@ class View_Top // AbstractCompositeView
return $this->doTree('_script');
}
function resolveAlias($alias, $list)
{
$result = array();
foreach($list as $item) {
$result [] = strtr($item, $alias);
}
return $result;
}
/**
* Строка со скриптом
*
@ -119,10 +161,6 @@ class View_Top // AbstractCompositeView
return implode("\n", $this->doTree('_startup'));
}
/*abstract*/ public function set($key, $value)
{
}
/**
* Массив имен файлов стилей
*
@ -132,128 +170,5 @@ class View_Top // AbstractCompositeView
{
return $this->doTree('_stylesheet');
}
/**
* Обработка всех вложенных шаблонов
*
* @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;
}
private function isNotNull($title)
{
return $title !== null;
}
/**
* Общая строка заголовка
*/
public function getTitle()
{
return implode(" - ", array_filter($this->doTree('_title', false), array($this, 'isNotNull')));
}
private function findGroup($groups, $file)
{
foreach($groups as $key => $group) {
if(in_array($file, $group)) {
return $key;
}
}
return false;
}
private function groupFiles(array $list, $debug)
{
$debug = ($debug) ? 'debug=1' : '';
$path = parse_url(WWW_PATH, PHP_URL_PATH);
$list = array_reverse($list);
// Группы нужно передвавать как параметр !!!
$groups = array(
'table' => array($path . '/js/table.js', $path . '/js/listtable.js',
$path . '/js/page.js', $path . '/js/pagemenu.js'),
'base' => array($path . '/js/admin.js', $path . '/js/cookie.js'),
);
$use = array();
$result = array();
foreach ($list as $file) {
$name = $this->findGroup($groups, $file);
if($name) {
$use [$name] = 1;
} else {
$result [] = $file;
}
}
$list = array();
foreach ($use as $name => $value) {
$list [] = WWW_PATH . "/min/?$debug&f=" . implode(",", $groups[$name]);
}
return array_merge($list, $result);
}
/**
* Обработка шаблона
*
* @return string
*/
public function render()
{
$alias = $this->doTree('alias');
// require_once 'minify.php';
// Скрипты и стили
$this->set('scripts', array_unique($this->groupFiles($this->resolveAlias($alias, $this->getScripts()), false)));
$this->set('stylesheet', array_unique($this->groupFiles($this->resolveAlias($alias, $this->getStyleSheet()), false)));
$this->set('scriptstring', $this->getScriptRaw());
$this->set('startup', $this->getScriptStartup());
$this->set('title', $this->getTitle());
//
return $this->execute(); // execute+phptal ??
}
function setAlias($alias)
{
$this->alias = $alias;
}
function addAlias($name, $path)
{
$this->alias['${' . $name . '}'] = $path;
$this->set($name, $path);
}
function loadImports($importFile)
{
// Подключение стилей и скриптов
if (file_exists($importFile)) {
$import = file_get_contents($importFile);
$files = explode("\n", $import);
foreach ($files as $file) {
if (strpos($file, ".js") !== false) {
$this->addScript(strtr(trim($file), $this->alias));
} else if(strpos($file, ".css") !== false) {
$this->addStyleSheet(strtr(trim($file), $this->alias));
}
}
}
}
}