phplibrary/core/view/compositeview.php
2016-06-29 18:51:32 +03:00

298 lines
7.3 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);
require_once "PHPTAL.php";
$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();
}
}