298 lines
7.8 KiB
PHP
298 lines
7.8 KiB
PHP
<?php
|
||
|
||
class _View_Composite // AbstractCompositeView
|
||
{
|
||
protected $_section = array(); // Вложенные шаблоны
|
||
// Блоки
|
||
protected $_stylesheet = array(); // Массив стилей текущего шаблона
|
||
protected $_script = array(); // Массив скриптов текущего шаблона
|
||
protected $_scriptstring = array();
|
||
protected $_startup = array();
|
||
|
||
protected $_title = null; // Заголовок текущего шаблона
|
||
public $alias = array();
|
||
|
||
function __construct()
|
||
{
|
||
}
|
||
|
||
/**
|
||
* Связывет переменную с вложенным шаблоном
|
||
*
|
||
* @param string $section переменная шаблона
|
||
* @param CompositeView $view вложенный шаблон
|
||
*/
|
||
public function setView($section, /*CompositeView*/ $view)
|
||
{
|
||
$this->_section [$section] = $view;
|
||
}
|
||
|
||
public function jGrowl($message, $args)
|
||
{
|
||
$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;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Добавляет стили к текущему шаблону
|
||
*
|
||
* @param string $name путь к стилю
|
||
*/
|
||
public function addStyleSheet($name)
|
||
{
|
||
$this->_stylesheet [] = $name;
|
||
}
|
||
|
||
/**
|
||
* Рекурсивно извлекает из значение свойства обьекта
|
||
*
|
||
* @param string $list Имя свойства
|
||
* @param boolean $flatten
|
||
*/
|
||
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;
|
||
}
|
||
|
||
/**
|
||
* Массив имен файлов скриптов
|
||
*
|
||
* return array
|
||
*/
|
||
public function getScripts()
|
||
{
|
||
return $this->doTree('_script');
|
||
}
|
||
|
||
function resolveAlias($alias, $list)
|
||
{
|
||
$result = array();
|
||
foreach($list as $item) {
|
||
$result [] = strtr($item, $alias);
|
||
}
|
||
return $result;
|
||
}
|
||
|
||
/**
|
||
* Строка со скриптом
|
||
*
|
||
* @return string
|
||
*/
|
||
public function getScriptRaw()
|
||
{
|
||
return implode("\n", $this->doTree('_scriptstring'));
|
||
}
|
||
|
||
public function getScriptStartup()
|
||
{
|
||
return implode("\n", $this->doTree('_startup'));
|
||
}
|
||
|
||
/*abstract*/ public function set($key, $value)
|
||
{
|
||
}
|
||
|
||
/**
|
||
* Массив имен файлов стилей
|
||
*
|
||
* return array
|
||
*/
|
||
public function getStyleSheet()
|
||
{
|
||
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));
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// CompositeView+PHPTAL
|
||
class View_Composite extends _View_Composite
|
||
{
|
||
private $tal;
|
||
|
||
function __construct($file)
|
||
{
|
||
parent::__construct($file);
|
||
|
||
$this->tal = new PHPTAL($file);
|
||
$this->tal->setEncoding('WINDOWS-1251'); // PHP_TAL_DEFAULT_ENCODING !!
|
||
$this->tal->stripComments(true);
|
||
}
|
||
|
||
function set($key, $val)
|
||
{
|
||
if ($key == 'title') {
|
||
$this->setTitle($val);
|
||
}
|
||
$this->tal->set($key, $val);
|
||
}
|
||
|
||
function __set($key, $val)
|
||
{
|
||
$this->tal->set($key, $val);
|
||
}
|
||
|
||
function setTranslator($tr)
|
||
{
|
||
$this->tal->setTranslator($tr);
|
||
}
|
||
|
||
function execute()
|
||
{
|
||
parent::execute();
|
||
// postProcess
|
||
return $this->tal->execute();
|
||
}
|
||
}
|