334 lines
8.9 KiB
PHP
334 lines
8.9 KiB
PHP
<?php
|
|
|
|
require_once 'core/path.php';
|
|
require_once 'core/mapper/factory.php';
|
|
require_once 'core/functions.php';
|
|
|
|
|
|
function forceUrl($name)
|
|
{
|
|
if (is_callable($name)) {
|
|
return call_user_func($name);
|
|
}
|
|
return $name;
|
|
}
|
|
|
|
/**
|
|
* Êîíòðîëëåð ñòðàíèö
|
|
* @package core
|
|
*/
|
|
class Controller
|
|
{
|
|
|
|
const TEMPLATE_EXTENSION = ".html"; // Ðàñøèðåíèå äëÿ øàáëîíîâ
|
|
const ACTION_PREFIX = "action"; // Ïðåôèêñ äëÿ ôóíêöèé äåéñòâèé
|
|
|
|
public $jsPath; // Ãëîáàëüíûé ïóòü ê ñêðèïòàì
|
|
public $themePath; // Ãëîáàëüíûé ïóòü ê òåêóùåé òåìå
|
|
|
|
// Ïàðàìåòðû óñòàíàâëèâàþòñÿ ïðè ñîçäàíèè êîíòðîëëåðà
|
|
public $name; // Èìÿ ìîäóëÿ
|
|
public $viewPath = null; // Ïóòü ê øàáëîíàì êîíòðîëëåðà
|
|
public $db; // Ñîåäèíåíèå ñ áàçîé äàííûõ
|
|
|
|
// Ôèëüòðû
|
|
public $access; // Îáüåêò õðàíèò ïàðàìåòðû äîñòóïà
|
|
public $logger; // Îáüåêò äëÿ âåäåíèÿ ëîãà
|
|
|
|
private $factory; // Ññûëêà íà îáüåêò ñîçäàíèÿ ìîäåëè
|
|
private $helpers = array(); // Ïîìîøíèêè äëÿ äåéñòâèé
|
|
public $param = array(); // Ïàðàìåòðû äëÿ ññûëêè
|
|
|
|
public $_registry; // Ññûëêà íà ðååñòð
|
|
public $_shortcut;
|
|
|
|
public function __construct ()
|
|
{
|
|
//
|
|
}
|
|
|
|
public function setUp ()
|
|
{
|
|
// override this
|
|
}
|
|
|
|
public function loadConfig($name) {
|
|
$filename = Shortcut::getUrl('config', $this->name, $name);
|
|
include($filename);
|
|
return $settings;
|
|
}
|
|
|
|
public function getConnection()
|
|
{
|
|
return $this->db;
|
|
}
|
|
|
|
public function installPath($name)
|
|
{
|
|
return Path::join(CMS_PATH, "modules", $name, "install");
|
|
}
|
|
|
|
public function addSuggest($view, $name)
|
|
{
|
|
$suggest = array();
|
|
$file = Path::join($this->viewPath, 'help', $name . '.suggest');
|
|
if (file_exists($file) && include($file)) {
|
|
$view->addScriptRaw("add_suggest(".json::encode($suggest).");\n");
|
|
}
|
|
}
|
|
|
|
function findIcon($icon, $size)
|
|
{
|
|
return Path::join($this->iconPath, $size . 'x' . $size, $icon . '.png');
|
|
}
|
|
|
|
/**
|
|
* Ñîçäàåò ïðåäñòàâëåíèå
|
|
* @param string $file
|
|
* @return template
|
|
*/
|
|
public function getView($name)
|
|
{
|
|
require_once "core/view/compositeview.php";
|
|
|
|
$file = $name . self::TEMPLATE_EXTENSION;
|
|
// Ñïèñîê âîçìîæíûõ äèðåêòîðèé äëÿ ïîèñêà ôàéëà øàáëîíà
|
|
$theme = $this->_registry->readKey(array('system', 'theme'));
|
|
$icon_theme = $this->_registry->readKey(array('system', 'icon_theme'));
|
|
$list = array(
|
|
Path::join($this->viewPath, TEMPLATES) => Path::join(WWW_PATH, "modules", $this->name, TEMPLATES),
|
|
PHPTAL_TEMPLATE_REPOSITORY => "");
|
|
|
|
|
|
// Ïîèñê ôàéëà äëÿ øàáëîíà
|
|
foreach($list as $ospath => $path) {
|
|
$template = Path::join($ospath, $file);
|
|
if(file_exists($template)) { break; }
|
|
}
|
|
|
|
$tpl = new View_Composite($template);
|
|
$tpl->icons = $this->iconPath; // Ïóòü ê ôàéëàì òåêóùåé òåìû
|
|
$tpl->media = $this->themePath; // Ïóòü ê ôàéëàì òåêóùåé òåìû
|
|
$tpl->script = $this->jsPath; // Ïóòü ê ôàéëàì ñêðèïòîâ
|
|
$tpl->template = $path; // Ïóòü ê ôàéëàì òåêóùåãî øàáëîíà
|
|
$tpl->setAlias(array(
|
|
'${icons}' => $this->iconPath,
|
|
'${media}' => $this->themePath,
|
|
'${script}' => $this->jsPath,
|
|
'${template}' => $path));
|
|
|
|
$tpl->loadImports(Path::skipExtension($template) . ".import");
|
|
|
|
$this->addSuggest($tpl, $name);
|
|
return $tpl;
|
|
}
|
|
|
|
public function getModel($name)
|
|
{
|
|
if (!$this->factory) {
|
|
$this->factory = new ModelFactory($this->db, $this->_registry, $this->_shortcut);
|
|
}
|
|
return $this->factory->getModel($name);
|
|
}
|
|
|
|
/**
|
|
* Âûáîð äåéñòâèÿ
|
|
* Ò.ê äåéñòâèÿ ÿâëÿþòñÿ ìåòîäàìè êëàññà òî
|
|
* 1. Ìîæíî ïåðåîïðåäåëèòü äåéñòâèÿ
|
|
* 2. Èñïîëüçîâàòü íàñëåäîâàíèå ÷òîáû äîáàâèòü ê ñòàðîìó îáðàáîò÷èêó íîâîå ïîâåäåíèå
|
|
* @param $request Îáüåêò çàïðîñà
|
|
*/
|
|
public function execute1(HTTPRequest $request)
|
|
{
|
|
$action = self::ACTION_PREFIX . ucfirst($request->getAction());
|
|
if (method_exists($this, $action)) {
|
|
return $this->forward($action, $request);
|
|
} else {
|
|
return $this->forward("actionIndex", $request);
|
|
}
|
|
}
|
|
|
|
public function execute(HTTPRequest $request)
|
|
{
|
|
$result = $this->execute1($request);
|
|
if ($result) {
|
|
$this->view = $result;
|
|
}
|
|
return $this->render();
|
|
}
|
|
|
|
public function forward($action, HTTPRequest $args)
|
|
{
|
|
// Äåéñòâèÿ äî âûçîâà îñíîâíîãî îáðàáîò÷èêà
|
|
/*foreach($this->_aspect as $aspect) {
|
|
if (isset($aspect->before[$action])) {
|
|
call_user_func ($aspect->before[$action], $action, $args);
|
|
}
|
|
}*/
|
|
return call_user_func(array($this, $action), $args);
|
|
}
|
|
|
|
/**
|
|
* Ñòðàíèöà ïî óìîë÷àíèþ
|
|
*/
|
|
public function actionIndex(HttpRequest $request)
|
|
{
|
|
return "";
|
|
}
|
|
|
|
public function postUrl($name, $param)
|
|
{
|
|
return "?" . http_build_query(
|
|
array_merge(array('module' => strtolower(get_class($this)), "action" => $name),
|
|
$this->param, $param));
|
|
}
|
|
|
|
/**
|
|
* Ãåíåðàöèÿ ññûëêè c ó÷åòîì ïðàâ ïîëüçîâàòåëÿ íà ññûëêè
|
|
*
|
|
* @parma string $name Äåéñòâèå
|
|
* @parma string $param Äîïîëíèòåëüíûå ïàðàìåòðû
|
|
*/
|
|
public function nUrl($name, array $param = array())
|
|
{
|
|
if (!$this->access || $this->access->checkAction($name)) {
|
|
return lcurry(array($this, 'postUrl'), $name, $param);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public function fUrl($name, array $param = array())
|
|
{
|
|
return forceUrl($this->nUrl($name, $param));
|
|
}
|
|
|
|
/**
|
|
* Äîáàâëÿåò ïàðàìåòð äëÿ âñåõ ññûëîê ñîçäàâàåìûõ ôóíêöèåé nUrl, aUrl
|
|
*/
|
|
public function addParameter($name, $value)
|
|
{
|
|
if ($value) {
|
|
$this->param [$name] = $value;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Ãåíåðàöèÿ ññûëêè íà äåéñòâèå êîíòðîëëåðà
|
|
* Ajax îïðåäåëÿåòñÿ àâòîìàòè÷åñêè mode = ajax èñïîëüçóåòñÿ äëÿ ñìåíû layout
|
|
*/
|
|
public function aUrl($name, array $param = array())
|
|
{
|
|
return $this->nUrl($name, array_merge(array('mode' => 'ajax'), $param)); // FIXME
|
|
}
|
|
|
|
/**
|
|
* Äîáàâëåíèå ïîìîøíèêà êîíòðîëëåðà
|
|
*/
|
|
public function addHelper($class)
|
|
{
|
|
$this->helpers [] = $class;
|
|
}
|
|
|
|
/**
|
|
* Âûçîâ ïîìîøíèêîâ êîíòðîëëåðà
|
|
*/
|
|
public function callHelpers(HttpRequest $request)
|
|
{
|
|
$action = self::ACTION_PREFIX . $request->getAction();
|
|
foreach ($this->helpers as $helper) {
|
|
if (method_exists($helper, $action)) {
|
|
return call_user_func(array($helper, $action), $request, $this);
|
|
} else {
|
|
return $helper->actionIndex($request, $this); // Âìåñòî return response ???
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Çàãðóçêà ôàéëà êëàññà
|
|
*/
|
|
public function loadClass($path, $setup = null)
|
|
{
|
|
if (file_exists($path)) {
|
|
require_once ($path);
|
|
$class = pathinfo($path, PATHINFO_FILENAME);
|
|
return new $class($setup);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public function loadSettings($path)
|
|
{
|
|
$result = new Settings($path);
|
|
$result->read();
|
|
return $result->export();
|
|
}
|
|
|
|
// Äëÿ Widgets
|
|
public $view = null;
|
|
public $childNodes = array();
|
|
public $childViews = array();
|
|
|
|
public function setView($name)
|
|
{
|
|
$this->view = $this->getView($name);
|
|
}
|
|
|
|
/**
|
|
* Óñòàíîâêà çàãîëîâêà äëÿ îòîáðàæåíèÿ
|
|
*/
|
|
public function setTitle($title)
|
|
{
|
|
$this->view->setTitle($title);
|
|
}
|
|
|
|
/**
|
|
* Äîáàâëåíèå widget ê îòîáðàæåíèþ
|
|
*/
|
|
public function addChild(/*Widget*/ $section, $node)
|
|
{
|
|
$this->childNodes[$section] = $node;
|
|
}
|
|
|
|
/**
|
|
* Äîáàâëåíèå äî÷åðíåãî îòîáðàæåíèÿ ê òåêóùåìó îòîáðàæåíèþ
|
|
*/
|
|
public function addView(/*CompositeView*/ $section, $node)
|
|
{
|
|
$this->childViews[$section] = $node;
|
|
}
|
|
|
|
/**
|
|
* Ãåíåðàöèÿ ñîäåðæàíèÿ
|
|
* Ïóòàíèöà c execute è render
|
|
*/
|
|
public function render()
|
|
{
|
|
foreach ($this->childNodes as $name => $node) {
|
|
$node->make($this);
|
|
$this->view->setView($name, $node->view);
|
|
}
|
|
foreach ($this->childViews as $name => $node) {
|
|
$this->view->setView($name, $node);
|
|
}
|
|
return $this->view;
|
|
}
|
|
|
|
function getPageId($request)
|
|
{
|
|
$pageId = time();
|
|
$request->session()->set('page', $pageId);
|
|
return $pageId;
|
|
}
|
|
|
|
function checkPageId($request, $page)
|
|
{
|
|
$_page = $request->session()->get('page');
|
|
$result = ($_page && $_page == $page);
|
|
$request->session()->clean('page');
|
|
return $result;
|
|
}
|
|
}
|
|
|
|
class Controller_Action extends Controller {}
|
|
|