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

185 lines
5.1 KiB
PHP

<?php
require_once 'core/path.php';
class FileNotFountException extends Exception
{
}
/**
* Êëàññ êîìïîíåíòà
*/
class Component
{
static $_uid = 1;
public $uid; // UID êîìïîíåíòà ñîçäàåòñÿ ïðè ñîçäàíèè ñòðàíèöû, âñòàâêè êîìïîíåíòà, èëè ýòî ñòàòè÷åñêîå ñâîéñòâî
public $viewPath;
public $registry; // Registry->getInstance
public $template;
function __construct()
{
self::$_uid ++;
$this->uid = self::$_uid;
}
function getUID()
{
return 'component:'. $this->uid;
}
public function getView($name)
{
require_once "core/view/compositeview.php";
//
$template = ($this->template) ? $this->template : $this->_registry->readKey(array('system', 'template'));
// Çàãðóæàòü øàáëîí ïî óìîë÷àíèþ åñëè íå íàéäåí òåêóùèé
if (is_dir(Path::join($this->viewPath, 'templates', $template))) {
$template_file = Path::join($this->viewPath, 'templates', $template, $name);
} else {
$template_file = Path::join($this->viewPath, 'templates', 'modern', $name);
}
$tpl = new View_Composite($template_file);
$tpl->script = $_script = Path::join(WWW_PATH, 'js');
$tpl->media = $_media = Path::join(TEMPLATE_WEB, $template);
$tpl->component = $_template = Path::join(COMPONENTS_WEB, strtolower(get_class($this)), 'templates', 'modern');
$tpl->setAlias(array(
'${media}' => $_media,
'${script}' => $_script,
'${template}' => $_template));
$tpl->loadImports(Path::skipExtension($template_file) . ".import");
return $tpl;
}
public function setParameters($view)
{
}
/**
* @param $name Èìÿ ìîäåëè
*/
private function getModelPath($name)
{
return Path::join (CMS_PATH, "model", $name . ".php");
}
/**
* Ñîçäàåò ìîäåëü
* @param string $name
* @return model
*/
public function getModel($name)
{
require_once 'core/mapper/mapper.php';
require_once ($this->getModelPath ($name));
$modelName = $name . "Mapper";
$model = new $modelName ();
$model->db = $this->db;
return $model;
}
public function options($key, $val, $res) {
$result = array();
while($res->next()) {
$result[] = array('value' => $res->getInt($key), 'name' => $res->getString($val));
}
return $result;
}
public function optionsPair($list) {
$result = array();
foreach ($list as $key => $value) {
$result [] = array('value' => $key, 'name' => $value);
}
return $result;
}
/* Â äàëüíåéøåì íóæíî çìåíèòü íà ìåòîäû
+ Ìåòîäû ìîãóò áûòü è javascript
*/
protected $editUrl;
function setEditUrl($url)
{
$this->editUrl = $url;
}
function getEditUrl()
{
return $this->editUrl;
}
}
/**
* TALES äëÿ ïîäêëþ÷åíèÿ êîìïîíåíòîâ
* component:name?param1=value1&param2=value2
*/
class Component_Tales implements PHPTAL_Tales
{
static public function component($expression, $nothrow = false)
{
return "phptal_component('" . $expression . "')";
}
}
function loadComponent($name, $db, $registry)
{
$path = Path::join(COMPONENTS, $name, $name . ".php");
// echo COMPONENTS, '<br />';
// echo $path;
if (file_exists($path)) {
require_once ($path);
$component = new $name();
$component->db = $db;
$component->_registry = $registry;
$component->viewPath = COMPONENTS."/".$name."/";
return $component;
}
throw new FileNotFountException();
}
/**
* Ôóíêöèÿ ïîäêëþ÷åíèÿ êîìïîíåíòà
*/
global $componentList;
$componentList = array();
function phptal_component ($real_expression, $offset = 0) {
global $db, $registry, $componentList; // Íóæíî êàêòî ïåðåäàâàòü ïàðàìåòðû
$expression = htmlspecialchars_decode($real_expression);
$url = parse_url($expression);
parse_str($url['query'], $arguments);
$name = $url['path'];
$component = loadComponent($name, $db, $registry);
$req = new HttpRequest();
$params = new Collection();
$params->import(array_merge($_GET, $arguments));
$component->params = $params;
$componentList [] = array(
'uid' => $component->getUID(), 'params' => $expression, 'name' => $name, 'offset' => $offset,
'size' => strlen($real_expression),
/* Âìåñòî ññûëêè íà ðåäàêòèðîâàíèå íóæíî ïåðåäàâàòü ñïèñîê ìåòîäîâ äëÿ ðàáîòû ñ êîìïîíåíòîì
edit (ðåäàêòèðîâàíèå ñîäåðæàíèå), new (íîâîå ñîäåðæàíèå), øàáëîí êîìåííåíòà ... âìåñòå ñ èêîíêàìè ýòèõ ìåòîäîâ
! Êîìïîíåíòû ìîãóò ñîäåðæàòü äðóãèå êîìïîíåíòû
*/
'editurl' => $component->getEditUrl(),
'newurl' => ''
);
unset($req['active_page']);
$component->template = $params->get('template', false);
return $component->execute($params, $req);
}
/* Ðåãèñòðàöèÿ íîâîãî ïðåôèêñà äëÿ ïîäêëþ÷åíèÿ êîìïîíåíòà */
$registry = PHPTAL_TalesRegistry::getInstance();
$registry->registerPrefix('component', array('Component_Tales', 'component'));