Библиотека для cis, online, cms1

This commit is contained in:
Фёдор Подлеснов 2016-06-29 18:51:32 +03:00
commit 3c2e614d87
269 changed files with 39854 additions and 0 deletions

View file

@ -0,0 +1,433 @@
<?php
/**
*
* @package Core
*/
require_once 'core/json.php';
require_once 'core/form/form.php';
require_once 'core/controller/controller.php';
require_once 'core/widgets/pagemenu.php';
require_once 'core/widgets/menu.php';
require_once 'core/widgets/search.php';
require_once 'core/widgets/setup.php';
require_once 'core/widgets/listtable.php';
/**
* Ïåðåèìåíîâàòü êîíòðîëëåð !! (StubController, CrudController, PageController, BaseController) ModelController
* Âîçìîæíî íóæåí åùå êëàññ ñ ìåòà äåéñòâèÿìè êàê äëÿ actionIndex <= metaActionIndex ëèáî ñ êëàññàì äëÿ ýòèõ äåéñòâèé
* Åñòü êëàññ äëÿ óïðàâëåíèÿìè äåéñòâèÿìè à åñòü ñàìè äåéñòâèÿ â âèäå êëàññîâ èëè ôóíêöèé !!
*/
class Controller_Model extends Controller_Action
{
public $schema = array();
public $schemaSearch = array();
/**
* FIXME: Ëó÷øå $this->table->setHeader
*/
public $tableSchema = null;
public $formSchema = array();
public $menu;
public $path;
public $table;
public function __construct()
{
$this->path = new PathMenu();
$this->menu = new PageMenu();
$this->table = new ListTable();
}
/**
*/
function setUp()
{
$this->table->addMenuItem($this->aUrl('delete'), 'óäàëèòü', false, 'all', 'warning');
//$this->table->addMenuItem($this->nUrl('form'), 'ðåäàêòèðîâàòü', 'edit-24.png');
}
function saveParameters($args, $list)
{
foreach ($list as $item) {
$args->session()->set(array($this, $item), $args->get($item));
}
}
protected function getJSONList(/*Mapper*/ $model, Collection $request)
{
$result = array();
$this->saveParameters($request, array('size','page','desc', 'key'));
$result['list'] = $model->findAll($request, $request->get('ref'));
$result['size'] = $model->getCount($request, $request->get('ref'));
return json::encode($result);
}
/**
* Óäàëåíèå ñòîðê èç òàáëèöû
*/
public function actionDelete(HttpRequest $request)
{
$model = $this->getModel($this->useModel);
// Ïî÷åìó table_item ???
$list = ($request->get('table_item')) ? $request->get('table_item'): $request->get('id');
$model->deleteList($list);
return $this->getJSONList($model, $request);
}
/**
* Îòâåò íà çàïðîñ ïî ïîèñêó
*/
public function actionSearch(HttpRequest $request)
{
$model = $this->getModel($this->useModel);
$model->addFilter($model->requestToSQL($request, $this->formSchema));
return $this->getJSONList($model, $request);
}
/**
* Ñïèñîê ýëåìåíòîâ
*/
public function actionList(HttpRequest $request)
{
$model = $this->getModel($this->useModel);
return $this->getJSONList($model, $request);
}
private function setFormSchema()
{
require_once 'core/mapper/uimapper.php';
$model = $this->getModel($this->useModel);
$ui = new UIMapper($model);
$this->formSchema = $ui->getFormSchema();
}
/**
* Ñîõðàíåíèå ôîðìû
*/
function beforeSave(/*Model*/ $item, Collection $request)
{
if (empty($this->formSchema)) {
$this->setFormSchema();
}
// Ñäåëàòü îòîáðàæåíèå Ôîðìû â îáüåêò è îáðàòíî <-- Óáðàòü â beforeSave
foreach ($this->formSchema as $key => $conv) {
list($value, $type) = $conv;
$item->$value = call_user_func(array('Cast', 'to_' . $type), $request->get($key)); // Çäåñòü íóæíî ïðåîáðàçîâûâàòü òèï çíà÷åíèÿ
}
}
/**
* Îáíîâëåíèå ôîðìû
*/
function formUpdate(TForm $form, Collection $request)
{
}
/**
* Çàãðóçêà ôîðìû
*/
function beforeLoad(/*Model*/ $item, TForm $form)
{
if (empty($this->formSchema)) {
$this->setFormSchema();
}
// Âñòàâêà çíà÷åíèé èç äàííûõ â ôîðìó
// Îòîáðàæåíèå îáüåêòà â ïîëÿ ôîðìû
$form->fill($item, $this->formSchema);
}
// Ïðîâåðêà ââîäà
protected function validate($validator, $request)
{
}
/**
* Äåéñòâèå äëÿ ïðîâåðêè ôîðìû
*/
public function actionValidate($request)
{
require_once "core/validator/validator.php";
$validator = new Validator();
$validator->addRuleList($this->schema);
// Äåéñòâèÿ äî ïðîâåðêè ôîðìû
$this->validate($validator, $request); // <--|
$validator->validate($request); // --|
// Ïðîâåðêà ôîðìû
if (!$validator->isValid()) {
return json::encode($validator->getErrorMsg());
}
return json::encode(true);
}
/**
* Èíèöèàëèçàöèÿ ôîðìû
*/
protected function formSetup($form, $id = null, $ref = null)
{
if (empty($this->schema)) {
$model = $this->getModel($this->useModel);
$ui = new UIMapper($model);
$schema = $ui->getEditSchema();
$form->addFieldList($schema);
} else {
$form->addFieldList($this->schema);
}
}
/**
* Äîáàâëåíèå ïîëüçîâàòåëÿ
*/
public function actionAdd(HttpRequest $request)
{
require_once "core/validator/validator.php";
// {{{ òîæå ìîæåò áûòü îäèí ref èëè íåñêîëüêî
$ref = $request->get('ref');
$this->addParameter('ref', $ref); // Äîáàâëÿåò ïàðàìåòð â url
/// }}}
if ($this->checkPageId($request, $request->get('page'))) {
// Ïðîâåðêà
$validator = new Validator();
$validator->addRuleList($this->schema);
// Äåéñòâèÿ äî ïðîâåðêè ôîðìû
$this->validate($validator, $request); // <--|
$validator->validate($request); // --|
// Ïðîâåðêà ôîðìû
if (!$validator->isValid()) {
$request->setAction('form');
$this->getActionPath($request);
$form = new TForm();
$this->formSetup($form, $request->get('id'), $request->get('ref')); // Èíèöèàëèçàöèÿ ôîðìû
$form->setValues($request); // <-- Óáðàòü â formUpdate
$this->formUpdate($form, $request);
$form->setError($validator); // Óñòàíîâêà îøèáîê äëÿ ôîðìû
$tpl = $this->formPage($form, $request);
$id = $request->get('id');
if ($id) { // Ðåäàêòèðîâàíèå
$tpl->action = forceUrl($this->nUrl('add', array('id' => $id, 'page' => $this->getPageId($request)))); // action Ñîâéñòâî ôîðìû
}
return $tpl /*->execute()*/;
}
// Íóæåí òåñò äëÿ ôîðìû
$model = $this->getModel($this->useModel);
$className = $model->className;
$item = new $className();
// Ñîõðàíÿåì çíà÷åíèå â áàçå äàííûõ
$item->id = $request->get('id');
// Åñëè òàáëèöà ñâÿçàíà ñ äðóãîé òàáëèöåé
if ($request->get('ref') && $model->reference[1]) {
$ref_id = $model->reference[1];
$item->$ref_id = $request->get('ref');
}
// Ïîäãîòîâêà ê ñîõðàíåíèþ
$this->beforeSave($item, $request); // Ñþäàæå è èñòðèÿ ïåðåõîäîâ
// nextId ??? èëè âûõîä èëè íîâàÿ ôîðìà äëÿ ñîçäàíèÿ íîâîñòè
$model->saveDB($item, $request);
}
// Äëÿ ñòðàíèöû ñî ñïèñêîì id -> èäåíòåôèêàòîð ðîäèòåëüñêîé òàáëèöû !!??
// $request->set('id', $request->get('ref'));
if ($request->get('apply')) {
$request->setAction('form');
return $this->forward('actionForm', $request);
}
return $this->forward('actionIndex', $request);
}
/**
* Çàãîëîâîê
*/
private function setTitlePath($ref)
{
if ($ref) {
$model = $this->getModel($this->useModel);
if (is_array($model->reference) && $model->reference[0]) {
$refmodel = $this->getModel($model->reference[0]);
try {
$parent = $refmodel->findById($ref);
$this->path->addTitle($parent->getTitle()); // Çàãîëîâîê ê ïîäïèñÿì ïóòåé
} catch (Exception $e) {
// Íå íàéäåí çàãîëîâîê ïîòîìó ÷òî íåïðàâèëüíî îïðåäåëåí ðîäèòåëüñêèé ýëåìåíò
}
}
}
}
/**
* Ôîðìà äëÿ ðåäàêòèðîâàíèÿ
*/
public function actionForm(HttpRequest $request)
{
$this->getActionPath($request);
$ref = $request->get('ref');
$this->addParameter('ref', $ref); // Äîáàâëÿåò ïàðàìåòð â url
$this->setTitlePath($ref);
$model = $this->getModel($this->useModel);
$form = new TForm(); // Ïîêàçûâàåì ôîðìó
$form->header = 'Ðåäàêòèðîâàíèå çàïèñè';
$this->formSetup($form, $request->get('id'), $request->get('ref')); // Èíèöèàëèçàöèÿ ôîðìû
$list = $request->get('table_item');
$id = ($list[0]) ? $list[0] : $request->get('id');
$tpl = $this->formPage ($form, $request);
if ($id) { // Ðåäàêòèðîâàíèå
$form->action = forceUrl($this->nUrl('add', array('id' => $id, 'page' => $this->getPageId($request)))); // action Ñâîéñòâî ôîðìû
$item = $model->findById($id);
// Çàãðóçêà ôîðìû
$this->beforeLoad($item, $form);
///
}
return $tpl;
}
/**
*/
function tableSetup($table, $id = null, $ref = null)
{
// FIXME: Ïîñëå çàìåíû âåçäå $tableSchema -> table->setHeader óäàëèòü!
if ($this->tableSchema) {
$table->setHeader($this->tableSchema);
} else {
// Íàñòðîéêà òàáëèöû îòîáðàæåíèÿ ïî ñõåìå äàííûõ
require_once 'core/mapper/uimapper.php';
$model = $this->getModel($this->useModel);
$ui = new UIMapper($model);
$schema = $ui->getTableSchema();
$schema[0]['action'] = $table->getFirstItem();
$table->setHeader($schema);
}
}
/**
*/
public function actionIndex(HttpRequest $request)
{
$this->getActionPath($request, 'index');
// Òàêîå ìåòà äåéñòâèå íàâåðíîå ìîæíî âûíåñòè â îòäåëüíûé êëàññ
return $this->metaActionIndex($request, array($this, 'tableSetup'), $this->aUrl('list'));
}
/**
* Ñòðàíèöà ïî óìîë÷àíèþ
*/
public function metaActionIndex(HttpRequest $request, $setup, $list)
{
// ìîæåò áûòü îäíî ref èëè íåñêîëüêî
// {{{ èñòîðèÿ ïåðåõîäîâ
$ref = null;
if ($request->get('ref')) {
$ref = $request->get('ref');
} else if ($request->session()->get('ref')) {
$ref = $request->session()->get('ref');
}
$request->session->set('ref', $ref);
$this->addParameter('ref', $ref);
// }}}
$this->setTitlePath($ref);
$tpl = $this->getView('list');
// Ïîìîøíèêè äåéñòâèé
$this->callHelpers($request);
// Òàáëèöà
if ($request->session()->get(strtolower(get_class($this)))) {
$session = $request->session()->get(strtolower(get_class($this)));
if (isset($session['view'])) {
$this->table->setView($session['view']);
}
$this->table->setData('state', array(
'page' => $session['page'],
'size' => $session['size'],
'desc' => $session['desc']));
$this->table->setData('sorter', $session['key']);
if (isset($session['desc'])) {
$this->table->setData('desc', $session['desc']);
}
}
call_user_func($setup, $this->table, $request->get('id'), $ref);// --> Ýêâèâàëåíò formSetup
$this->table->setAction($list);
//
$tpl->menu_path = $this->path->getItems();
// Ïîèñê
$search = new SearchDialog();
$search->setTitle('Ïîèñê');
$search->setAction($this->aUrl('search'));
$search->setFriend($this->table);
$search->addFields($this->schemaSearch);
// Íàñòðîéêè
$setup = new SetupDialog();
$setup->setTitle('Íàñòðîéêè');
$setup->setAction($this->nUrl('setup'));
$setup->setFriend($this->table);
// Ìåíþ
$this->menu->addMenuItem('?menu=toggle&id=' . $search->getName(), 'ïîèñê', 'actions/system-search'); // Ñòàíäàðòíûé ðàçìåð äëÿ èêîíîê 22-24px
$this->menu->addMenuItem('?menu=toggle&id=' . $setup->getName(), 'íàñòðîéêè', 'categories/applications-system');
// Äîáàâëåíèå êîìïîíåíòîâ
$this->addChild('menu', $this->menu);
$this->addChild('search', $search);
$this->addChild('setup', $setup);
$this->addChild('table', $this->table);
//
return $tpl;
}
/**
*/
public function actionSetup($request)
{
$left = explode(",", $request->get('left'));
$right = explode(",", $request->get('right'));
$$request->session()->set(strtolower(get_class($this)),
array('view' => array('left' => $left, 'right' => $right)));
return $this->forward('actionIndex', $request);
}
/**
*/
private function formPage($form, $request)
{
$view = $this->getView('form');
$view->setView('form', $form);
$view->action = forceUrl($this->nUrl('add', array('page' => $this->getPageId($request)))); // Äåéñòâèå äëÿ ôîðìû
$view->menu_path = $this->path->getItems();
$view->back = $this->path->getPrev();
return $view;
}
// Òîæå óáðàòü â ìåòîä Controller_Model
function getActionPath(HttpRequest $request/*, $action = false*/)
{
require_once 'state.php';
$this->_getActionPath()->getPath($this, ($action) ? $action : $request->getAction());
}
}

View file

@ -0,0 +1,185 @@
<?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'));

View file

@ -0,0 +1,334 @@
<?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 {}

View file

@ -0,0 +1,92 @@
<?php
require_once 'core/controller/controller.php';
require_once 'core/controller/installer.php';
/**
* Ïåðâè÷íûé êîíòðîëëåð êîíòðîëëåð ñòðàíèö
* @package core
*/
class Controller_Front extends Controller
{
protected $shortcut; // ßðëûê ê ìîäóëþ
protected $_param; // Ïàðàìåòð ïî êîòîðîìó âûáèðàåòñÿ ìîäóëü
protected $default; // Çíà÷åíèå ïàðàìåòðà ïî óìîë÷àíèþ
protected $installer;
public function __construct(Settings $_registry, $_shortcut)
{
require_once 'creole/Creole.php';
parent::__construct();
$registry = $_registry;
$this->_registry = $_registry;
$this->_shortcut = $_shortcut;
$this->db = Creole::getConnection($registry->readKey(array('system', 'dsn')));
$this->installer = new Installer($_registry);
}
/**
* Ñîçäàåò ýêçåìïëÿð ìîäóëÿ è âûïîëíÿåò äåéñòâèÿ äëÿ íåãî
* @param string $name Èìÿ ìîäóëÿ
* @param request $request Èìÿ ìîäóëÿ
* @return string
*/
public function loadModule($name, Collection $request)
{
$this->installer->setUp($this->db, array($this, 'installPath'));
$this->installer->doUpdates($name); // ModuleLoader (1)
$moduleFile = Shortcut::getUrl($this->shortcut, $name); // ModuleLoader (2)
$module = $this->loadClass($moduleFile);
if ($module) {
// Èíèöèàëèçàöèÿ ìîäóëÿ
// $module->viewPath = dirname($moduleFile);
$module->viewPath = Shortcut::getUrl('modulepath', $name);
$module->name = $name;
$module->param = $this->param;
//
$module->_registry = $this->_registry;
$module->_shortcut = $this->_shortcut;
$module->iconPath = $this->iconPath; // -> Registry
$module->themePath = $this->themePath; // -> Registry
$module->jsPath = $this->jsPath; // -> Registry
$module->db = $this->db;
// Íå äëÿ âñåõ ïðèëîæåíèé íóæíî âåñòè ëîã äåéñòâèé
// Âåäåíèå ëîãà
$logger = $this->loadClass(FRAMEWORK_PATH . '/core/filter/actionlogger.php', $module);
$logger->before = $this->loadSettings(Shortcut::getUrl('logger', $name));
// Óïðàâëåíèå äîñòóïîì
$module->access = $this->loadClass(FRAMEWORK_PATH . '/core/filter/actionaccess.php', $logger);
$module->access->access = $this->loadSettings(Shortcut::getUrl('access', $name));
$module->setUp();
return $module->access->execute($request);
}
return null; // throw new FileNotFoundException();
}
public function setParameter($shortcut, $param, $name)
{
$this->shortcut = $shortcut;
// Ïàðàìåòð
$this->_param = $param;
$this->default = $name;
}
private function getParameter(Collection $list)
{
return ($list->get($this->_param)) ? $list->get($this->_param): $this->default;
}
public function execute(HTTPRequest $request)
{
return $this->loadModule($this->getParameter($request), $request);
}
}

View file

@ -0,0 +1,89 @@
<?php
require_once 'core/settings.php';
class Installer
{
protected $db;
protected $installPath;
public $_registry;
public function __construct(Settings $_registry)
{
$this->_registry = $_registry;
}
public function setUp($db, $installPath)
{
$this->db = $db;
$this->installPath = $installPath;
}
function getSetupFile($name)
{
return Path::join(call_user_func($this->installPath, $name), "setup.php");
}
// Ïðîâåðêà âåðñèè îáíîâëåíèÿ
function isChanged($name) // Èíôîðìàöèÿ î ìîäóëÿõ
{
$item = $this->_registry->readKey(array($name));
if ($item) {
$setup = $this->getSetupFile($name);
if (file_exists($setup) && (filemtime($setup) > $item['time'])) {
return true;
}
return false;
}
return true;
}
function installSQL(array $sql, $version_new, $version_old, $name)
{
require_once "core/setup.php";
foreach ($sql as $version => $install) {
if (version_compare($version, $version_new, "<=") && version_compare($version, $version_old, ">")) {
// this->installPath this->db
$file = Path::join(call_user_func($this->installPath, $name), "sql", $install);
Setup::batchSQL($this->db, $file);
}
}
}
// Óñòàíàâëèâàåò îáíîâëåíèÿ åñëè åñòü
function doUpdates($name, $force = false) // Óñòàíîâêà ìîäóëÿ
{
$setup = $this->getSetupFile($name);
if (file_exists($setup) && ($this->isChanged($name) || $force)) {
$registry = $this->_registry;
$settings = new Settings($setup);
$settings->read();
$item = $registry->readKey(array($name));
$version_new = $settings->get('version');
if ($item) {
$version_old = $item['version'];
} else {
$version_old = "0.0";
$registry->writeKey(array($name), array());
}
if (version_compare($version_old, $settings->get('version'), "!=")) {
$sql = $settings->get('sql');
if (is_array($sql)) {
$this->installSQL($sql, $version_new, $version_old, $name);
}
}
// Îáíîâëåíèå âåðñèè ìåíþ
$registry->writeKey(array($name), $settings->get('settings'));
$registry->writeKey(array($name),
array('version' => $version_new,
'time' => filemtime($setup)));
$registry->write();
}
}
}

79
core/controller/state.php Normal file
View file

@ -0,0 +1,79 @@
<?php
class State
{
public $action = '';
public $states = array();
public $titles = array();
public function __construct($action)
{
$this->action = $action;
}
static function make($action)
{
return new State($action);
}
public function addTitle($name, $url = array())
{
$this->titles [] = array($name, $url);
return $this;
}
public function addState(State $state)
{
$this->states [$state->getAction()] = $state;
return $this;
}
public function getAction()
{
return $this->action;
}
function checkAction($action, &$list)
{
if ($this->action == $action) {
array_push($list, $this);
return true;
} else {
foreach ($this->states as $state) {
if ($state->checkAction($action, $list)) {
array_push($list, $this);
return true;
}
}
}
return false;
}
function makeTitle($module)
{
foreach ($this->titles as $item) {
$module->path->addMenuItem($module->nUrl($this->action, $item[1]), $item[0]);
}
}
function getPath($module, $action)
{
$list = array();
if ($this->checkAction($action, $list)) {
foreach (array_reverse($list) as $item) {
$item->makeTitle($module);
}
} else {
$this->makeTitle($module);
}
}
}
/*
$path = State::make('index')
->addState(State::make('form'))
->addState(State::make('view'));
$path->getPath(0, 'form');
*/