93 lines
2.8 KiB
PHP
93 lines
2.8 KiB
PHP
<?php
|
||
|
||
require_once 'core/functions.php';
|
||
// Переместить в фильтры!!
|
||
|
||
/**
|
||
* Выбор макета страницы.
|
||
* Выбор оформления страницы осуществляется если было совпадение с каким либо условием
|
||
*/
|
||
class LayoutManager extends Filter
|
||
{
|
||
// Массив условий с их макетами
|
||
protected $condition = array();
|
||
|
||
/**
|
||
* Функция которая добавляет условие для проверки параметров $_GET
|
||
* @param $get array() | true Ассоциативный массив ключей и значений для $_GET
|
||
*
|
||
* @example
|
||
* addConditionGet(array('module' => 'personal'), 'personal')
|
||
* addConditionGet(array('module' => 'login'), 'login')
|
||
*/
|
||
public function addConditionGet($get, Filter $layout)
|
||
{
|
||
$this->addCondition(rcurry(array($this, 'checkGet'), $get), $layout);
|
||
}
|
||
|
||
/**
|
||
* Условие для аякс запросов. Тоже самое что и addConditionGet но еще проверяется является ли запрос ajax
|
||
*/
|
||
public function addConditionXHR($get, Filter $layout)
|
||
{
|
||
$this->addCondition(rcurry(array($this, 'checkXHR'), $get), $layout);
|
||
}
|
||
|
||
public function checkGet($request, $get)
|
||
{
|
||
if (is_array($get)) {
|
||
foreach ($get as $key => $value) {
|
||
if ($request->get($key) != $value) {
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public function checkXHR($request, $get)
|
||
{
|
||
return $request->isAjax() && $this->checkGet($request, $get);
|
||
}
|
||
|
||
/**
|
||
* Добавляет есловие в общем виде
|
||
* @parma $get function(HttpRequest) Функция
|
||
* @parma $layout Layout Макет
|
||
*/
|
||
public function addCondition($get, Filter $layout)
|
||
{
|
||
$this->condition [] = array($get, $layout);
|
||
}
|
||
|
||
/**
|
||
* Выбирает и применяет макет для страницы
|
||
*/
|
||
public function execute(HttpRequest $request)
|
||
{
|
||
foreach ($this->condition as $condition) {
|
||
if (call_user_func($condition[0], $request)) {
|
||
$layout = $condition[1];
|
||
$view = $layout->execute($request);
|
||
|
||
if ($view instanceof View_Composite) {
|
||
echo $view->render();
|
||
} else {
|
||
echo $view;
|
||
}
|
||
return null;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Самый простой макет
|
||
*/
|
||
class LayoutNone extends Filter
|
||
{
|
||
function execute(HttpRequest $request)
|
||
{
|
||
return $this->processor->execute($request);
|
||
}
|
||
}
|