433 lines
14 KiB
PHP
433 lines
14 KiB
PHP
<?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());
|
|
}
|
|
}
|