Версия полностью совместимая c CMS
This commit is contained in:
parent
7ce493414e
commit
75bb35d5bf
21 changed files with 1404 additions and 783 deletions
|
|
@ -1,7 +1,40 @@
|
|||
<?php
|
||||
|
||||
class FileNotFountException extends Exception
|
||||
{
|
||||
function replaceContent($match) {
|
||||
$result = phptal_component(htmlspecialchars_decode($match[3]));
|
||||
return $result;
|
||||
}
|
||||
|
||||
function applyComponents($text) {
|
||||
return preg_replace_callback('/<(\w+)(\s+[a-zA-Z\-]+=\"[^\"]*\")*\s+tal:replace="structure\s+component:([^\"]*)"[^>]*>/u', 'replaceContent', $text);
|
||||
}
|
||||
|
||||
class ComponentRequest {
|
||||
public $component_id;
|
||||
public $r;
|
||||
|
||||
function __construct($c, HttpRequest $r) {
|
||||
$this->component_id = $c;
|
||||
$this->r = $r;
|
||||
}
|
||||
|
||||
function get($key, $default = null) {
|
||||
if ($key == 'active_page') {
|
||||
return $this->r->get($key);
|
||||
}
|
||||
if ($arr = $this->r->get($key)) {
|
||||
if (is_array($arr)) {
|
||||
return Arr::get($arr, $this->component_id, $default);
|
||||
} else {
|
||||
return $arr;
|
||||
}
|
||||
}
|
||||
return $default;
|
||||
}
|
||||
|
||||
function getAction() {
|
||||
return $this->r->getAction();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -9,51 +42,87 @@ class FileNotFountException extends Exception
|
|||
*/
|
||||
class Controller_Component
|
||||
{
|
||||
static $_uid = 1;
|
||||
public $uid; // UID компонента создается при создании страницы, вставки компонента, или это статическое свойство
|
||||
public $viewPath;
|
||||
public $registry; // Registry->getInstance
|
||||
public $template;
|
||||
public $viewPath = array();
|
||||
public $webPath = array();
|
||||
|
||||
function __construct()
|
||||
{
|
||||
self::$_uid ++;
|
||||
$this->uid = self::$_uid;
|
||||
public $template = null;
|
||||
public $templatePath;
|
||||
|
||||
public $component_id;
|
||||
public $COMPONENTS_WEB;
|
||||
|
||||
public /*.Settings.*/$registry;
|
||||
public /*.Database.*/$db;
|
||||
public /*.Collection.*/$parameter;
|
||||
|
||||
public $module;
|
||||
public $item_module;
|
||||
|
||||
function before() {
|
||||
}
|
||||
|
||||
function getUID()
|
||||
{
|
||||
return 'component:'. $this->uid;
|
||||
function get($request, $key, $default) {
|
||||
}
|
||||
|
||||
function execute(HttpRequest $request, $has_id = true) {
|
||||
$crequest = new ComponentRequest($this->component_id, $request);
|
||||
$action = 'action' . ucfirst($request->get('action', 'index'));
|
||||
|
||||
$this->before();
|
||||
if (method_exists($this, $action)) {
|
||||
return call_user_func(array($this, $action), $crequest);
|
||||
} else {
|
||||
return $this->actionIndex($crequest);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
/*.Settings.*/$registry = $this->registry;
|
||||
$template = ($this->template) ? $this->template : $registry->readKey(array('system', 'template'));
|
||||
|
||||
$selected = null;
|
||||
foreach ($this->viewPath as $index => $viewPath) {
|
||||
// Загружать шаблон по умолчанию если не найден текущий
|
||||
if(is_dir(Path::join($this->viewPath[$index], 'templates', $template))) {
|
||||
$tpl = new PHPTAL(Path::join($this->viewPath[$index], 'templates', $template, $name));
|
||||
$tpl->setPhpCodeDestination(PHPTAL_PHP_CODE_DESTINATION);
|
||||
$selected = $index;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$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));
|
||||
if ($selected === null) {
|
||||
$tpl = new PHPTAL(Path::join($this->viewPath[0], 'templates', 'modern', $name));
|
||||
$tpl->setPhpCodeDestination(PHPTAL_PHP_CODE_DESTINATION);
|
||||
$template = 'modern';
|
||||
$selected = 0;
|
||||
}
|
||||
|
||||
$tpl->loadImports(Path::skipExtension($template_file) . ".import");
|
||||
$tpl->stripComments(true);
|
||||
$tpl->addPreFilter(new PHPTAL_PreFilter_Normalize());
|
||||
|
||||
$tpl->set('common', Path::join(WWW_PATH, '../', 'common'));
|
||||
$tpl->set('script', Path::join(WWW_PATH, 'js'));
|
||||
$tpl->set('media', Path::join(TEMPLATE_WEB, $template));
|
||||
$tpl->set('site_template', SITE_WWW_PATH . '/templates' . $registry->readKey(array('system', 'template')));
|
||||
$tpl->set('base', SITE_WWW_PATH);
|
||||
|
||||
$tpl->set('component_base', $this->webPath[$selected]);
|
||||
$tpl->set('component', Path::join($this->webPath[$selected], 'templates', $template));
|
||||
$tpl->set('component_id', $this->component_id);
|
||||
|
||||
return $tpl;
|
||||
}
|
||||
|
||||
public function setParameters($view)
|
||||
public function getTemplatePath($name) {
|
||||
return Path::join($this->viewPath[0], 'templates', 'modern', $name);
|
||||
}
|
||||
|
||||
public function getTemplateWebPath()
|
||||
{
|
||||
return Path::join($this->webPath[0], 'templates', 'modern');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -71,8 +140,6 @@ class Controller_Component
|
|||
*/
|
||||
public function getModel($name)
|
||||
{
|
||||
require_once 'core/mapper/mapper.php';
|
||||
|
||||
require_once ($this->getModelPath ($name));
|
||||
$modelName = $name . "Mapper";
|
||||
$model = new $modelName ();
|
||||
|
|
@ -80,104 +147,179 @@ class Controller_Component
|
|||
return $model;
|
||||
}
|
||||
|
||||
public function options($key, $val, $res) {
|
||||
public function options($key, $val, /*.Database_PDOStatement.*/$res) {
|
||||
$result = array();
|
||||
while($res->next()) {
|
||||
$result[] = array('value' => $res->getInt($key), 'name' => $res->getString($val));
|
||||
$result[] = array('value' => $res->getString($key), 'name' => $res->getString($val));
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function optionsPair($list) {
|
||||
public function optionsPair($list, $selected = false) {
|
||||
$result = array();
|
||||
foreach ($list as $key => $value) {
|
||||
$result [] = array('value' => $key, 'name' => $value);
|
||||
$result [] = array('value' => $key, 'name' => $value, 'selected' => $key == $selected);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/* В дальнейшем нужно зменить на методы
|
||||
+ Методы могут быть и javascript
|
||||
*/
|
||||
protected $editUrl;
|
||||
|
||||
function setEditUrl($url)
|
||||
{
|
||||
$this->editUrl = $url;
|
||||
function getInfo() {
|
||||
$filename = Path::join($this->viewPath[0], 'install.json');
|
||||
if (file_exists($filename)) {
|
||||
$settings = json_decode(file_get_contents($filename), true);
|
||||
return $settings;
|
||||
}
|
||||
return array();
|
||||
}
|
||||
|
||||
function getEditUrl()
|
||||
/**
|
||||
* Генерация интерфейса для выбора галлереи фотографии
|
||||
*/
|
||||
public function setParameters(/*.View_Composite.*/$view)
|
||||
{
|
||||
return $this->editUrl;
|
||||
}
|
||||
}
|
||||
$form = new Form_Form();
|
||||
$options = new OptionFactory($this->db);
|
||||
|
||||
/**
|
||||
* TALES для подключения компонентов
|
||||
* component:name?param1=value1¶m2=value2
|
||||
*/
|
||||
class Component_Tales implements PHPTAL_Tales
|
||||
{
|
||||
static public function component($expression, $nothrow = false)
|
||||
$settings = $this->getInfo();
|
||||
$form->addFieldList($settings['parameter'], $options);
|
||||
|
||||
$view->form = $form;
|
||||
$view->component = $settings['component'];
|
||||
$view->component_title = $settings['title'];
|
||||
}
|
||||
|
||||
static function loadComponent($expression, Database $db, Settings $registry)
|
||||
{
|
||||
return "phptal_component('" . $expression . "')";
|
||||
}
|
||||
}
|
||||
|
||||
$expression = htmlspecialchars_decode($expression);
|
||||
$offset = strpos($expression, '?');
|
||||
$url = parse_url($expression);
|
||||
|
||||
$arguments = array();
|
||||
if ($offset === false) {
|
||||
$path = $expression;
|
||||
} else if (is_int($offset)) {
|
||||
$path = substr($expression, 0, $offset);
|
||||
$query = substr($expression, $offset + 1);
|
||||
parse_str($query, $arguments);
|
||||
}
|
||||
$name = $path;
|
||||
|
||||
$path = Path::join (BASE_PATH, 'components', $name, $name . '.php');
|
||||
$className = 'Component_' . $name;
|
||||
|
||||
if (file_exists($path)) {
|
||||
require_once ($path);
|
||||
|
||||
$component = new $className();
|
||||
$component->db = $db;
|
||||
$component->registry = $registry;
|
||||
|
||||
$component->viewPath = array(BASE_PATH . '/components/' . $name . '/');
|
||||
$component->webPath = array(SITE_WWW_PATH . '/components/' . $name);
|
||||
|
||||
$component->COMPONENTS_WEB = SITE_WWW_PATH . '/components/';
|
||||
|
||||
} else {
|
||||
$path = Path::join (COMPONENTS, $name, $name . '.php');
|
||||
require_once ($path);
|
||||
$component = new $className();
|
||||
$component->db = $db;
|
||||
$component->registry = $registry;
|
||||
|
||||
$component->viewPath = array(COMPONENTS . '/' . $name . '/', BASE_PATH . '/components/' . $name . '/');
|
||||
if (defined('COMPONENTS_WEB')) {
|
||||
$component->webPath = array(COMPONENTS_WEB . '/' . $name, SITE_WWW_PATH . '/components/' . $name);
|
||||
$component->COMPONENTS_WEB = COMPONENTS_WEB;
|
||||
}
|
||||
}
|
||||
|
||||
$stmt = $db->prepareStatement("SELECT * FROM component WHERE code = ?");
|
||||
$stmt->setString(1, $expression);
|
||||
$cid = $stmt->executeQuery();
|
||||
if ($cid->next()) {
|
||||
$component->component_id = $cid->getInt('id_component');
|
||||
} else {
|
||||
$last = $db->getIdGenerator();
|
||||
if ($last->isBeforeInsert()) {
|
||||
$result = $last->getId('component_id_component_seq');
|
||||
|
||||
$stmt = $db->prepareStatement("INSERT INTO component (id_component, code) VALUES ($result, ?)");
|
||||
$stmt->setString(1, $expression);
|
||||
$stmt->executeQuery();
|
||||
}
|
||||
if ($last->isAfterInsert()) {
|
||||
$stmt = $db->prepareStatement("INSERT INTO component (code) VALUES (?)");
|
||||
$stmt->setString(1, $expression);
|
||||
$stmt->executeQuery();
|
||||
|
||||
$result = $last->getId('component_id_component_seq');
|
||||
}
|
||||
$component->component_id = $result;
|
||||
}
|
||||
|
||||
$params = new Collection();
|
||||
$params->import(array_merge($_GET, $arguments));
|
||||
$component->parameter = $params;
|
||||
$component->template = $params->get('template', false);
|
||||
|
||||
global $componentsConfig;
|
||||
$editor = $component->getEditUrl();
|
||||
if ($editor) {
|
||||
$componentsConfig[] = $editor;
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
function getEditUrl() {
|
||||
return null;
|
||||
}
|
||||
|
||||
function raw_query(/*.ComponentRequest.*/ $request)
|
||||
{
|
||||
$arr = $request->r->export('get');
|
||||
|
||||
$param = array();
|
||||
/*.Collection.*/$parameter = $this->parameter;
|
||||
foreach($parameter->export() as $key => $value) {
|
||||
$param[$key] = $value;
|
||||
}
|
||||
|
||||
$data = array();
|
||||
foreach($arr as $key => $value) {
|
||||
if (is_array($value)) {
|
||||
$data[$key] = Arr::get($value, $this->component_id);
|
||||
} else {
|
||||
$data[$key] = $value;
|
||||
}
|
||||
}
|
||||
$data['param'] = $param;
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
function query(/*.ComponentRequest.*/ $request, $list)
|
||||
{
|
||||
$arr = $request->r->export('get');
|
||||
|
||||
foreach($list as $key => $val) {
|
||||
$arr[$key] [$this->component_id] = $val;
|
||||
}
|
||||
|
||||
unset($arr['active_page']);
|
||||
return '?' . http_build_query($arr);
|
||||
}
|
||||
|
||||
function addRequireJsPath($name, $path, $shim = null) {
|
||||
global $requireJsConfig;
|
||||
$requireJsConfig['paths'][$name] = $path;
|
||||
if ($shim) {
|
||||
$requireJsConfig['shim'][$name] = $shim;
|
||||
}
|
||||
}
|
||||
|
||||
function actionIndex(/*.ComponentRequest.*/ $request) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Функция подключения компонента
|
||||
*/
|
||||
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'));
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue