Версия полностью совместимая c CMS
This commit is contained in:
parent
7ce493414e
commit
75bb35d5bf
21 changed files with 1404 additions and 783 deletions
|
|
@ -28,38 +28,53 @@ class Controller_Model extends Controller_Action
|
|||
|
||||
public function __construct()
|
||||
{
|
||||
$this->path = new PathMenu();
|
||||
$this->menu = new PageMenu();
|
||||
$this->table = new ListTable();
|
||||
parent::__construct();
|
||||
$this->menu = new Widgets_PageMenu();
|
||||
$this->table = new Widgets_ReactListTable();
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
function setUp()
|
||||
{
|
||||
$this->table->addMenuItem($this->aUrl('delete'), 'удалить', false, 'all', 'warning');
|
||||
//$this->table->addMenuItem($this->nUrl('form'), 'редактировать', 'edit-24.png');
|
||||
function setUp() {
|
||||
parent::setUp();
|
||||
$this->table->addMenuItem($this->aUrl('delete'), 'удалить', false, 'all', 'btn-danger', 'remove');
|
||||
}
|
||||
|
||||
function saveParameters($args, $list)
|
||||
{
|
||||
function saveParameters(HttpRequest $args, $list) {
|
||||
foreach ($list as $item) {
|
||||
$args->session()->set(array($this, $item), $args->get($item));
|
||||
}
|
||||
}
|
||||
|
||||
protected function getJSONList(/*Mapper*/ $model, Collection $request)
|
||||
{
|
||||
protected function getJSONList(/*.Model_Mapper.*/ $model, HttpRequest $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);
|
||||
return json_encode($result);
|
||||
}
|
||||
|
||||
protected function getCSV(Model_Mapper $model, HttpRequest $request, $title) {
|
||||
$ref = $request->get('ref');
|
||||
|
||||
$list = $model->findAll($request, $request->get('ref'));
|
||||
|
||||
header('Content-Type: text/csv; charset=utf-8');
|
||||
header('Content-Disposition: attachment; filename='.$title.'.csv');
|
||||
|
||||
echo "\xEF\xBB\xBF"; //UTF-8 BOM.
|
||||
|
||||
$output = fopen('php://output', 'w');
|
||||
if (is_resource($output)) {
|
||||
foreach ($list as $row) {
|
||||
fputcsv($output, (array)$row,';');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Удаление сторк из таблицы
|
||||
* Удаление строк из таблицы
|
||||
*/
|
||||
public function actionDelete(HttpRequest $request)
|
||||
{
|
||||
|
|
@ -68,7 +83,8 @@ class Controller_Model extends Controller_Action
|
|||
$list = ($request->get('table_item')) ? $request->get('table_item'): $request->get('id');
|
||||
$model->deleteList($list);
|
||||
|
||||
return $this->getJSONList($model, $request);
|
||||
// return $this->getJSONList($model, $request);
|
||||
return json_encode(array('result' => 'ok'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -88,16 +104,29 @@ class Controller_Model extends Controller_Action
|
|||
public function actionList(HttpRequest $request)
|
||||
{
|
||||
$model = $this->getModel($this->useModel);
|
||||
|
||||
if ($request->get('filter')) {
|
||||
$data = new Collection();
|
||||
$data->import($request->get('filter'));
|
||||
$model->addFilter($model->requestToSQL($data, $this->formSchema));
|
||||
}
|
||||
return $this->getJSONList($model, $request);
|
||||
}
|
||||
|
||||
public function actionCSV(HttpRequest $request)
|
||||
{
|
||||
$model = $this->getModel($this->useModel);
|
||||
$title = $request->get("title");
|
||||
if(!$title){
|
||||
$title = "noname";
|
||||
}
|
||||
return $this->getCSV($model, $request, $title);
|
||||
}
|
||||
|
||||
private function setFormSchema()
|
||||
{
|
||||
require_once 'core/mapper/uimapper.php';
|
||||
|
||||
$model = $this->getModel($this->useModel);
|
||||
$ui = new UIMapper($model);
|
||||
$ui = new Model_UIMapper($model);
|
||||
|
||||
$this->formSchema = $ui->getFormSchema();
|
||||
}
|
||||
|
|
@ -105,29 +134,29 @@ class Controller_Model extends Controller_Action
|
|||
/**
|
||||
* Сохранение формы
|
||||
*/
|
||||
function beforeSave(/*Model*/ $item, Collection $request)
|
||||
function beforeSave(/*Model*/ $item, HttpRequest $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)); // Здесть нужно преобразовывать тип значения
|
||||
$item->$value = call_user_func(array('Primitive', 'to_' . $type), $request->get($key)); // Здесь нужно преобразовывать тип значения
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Обновление формы
|
||||
*/
|
||||
function formUpdate(TForm $form, Collection $request)
|
||||
{
|
||||
function formUpdate(Form_Form $form, HttpRequest $request) {
|
||||
$form->setValues($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Загрузка формы
|
||||
*/
|
||||
function beforeLoad(/*Model*/ $item, TForm $form)
|
||||
function beforeLoad(/*.Model_Model.*/$item, Form_Form $form)
|
||||
{
|
||||
if (empty($this->formSchema)) {
|
||||
$this->setFormSchema();
|
||||
|
|
@ -137,8 +166,13 @@ class Controller_Model extends Controller_Action
|
|||
$form->fill($item, $this->formSchema);
|
||||
}
|
||||
|
||||
function beforeFirstLoad(Form_Form $form)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Проверка ввода
|
||||
protected function validate($validator, $request)
|
||||
protected function validate(Validator_Validator $validator, Collection $request)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -147,8 +181,7 @@ class Controller_Model extends Controller_Action
|
|||
*/
|
||||
public function actionValidate($request)
|
||||
{
|
||||
require_once "core/validator/validator.php";
|
||||
$validator = new Validator();
|
||||
$validator = new Validator_Validator();
|
||||
$validator->addRuleList($this->schema);
|
||||
|
||||
// Действия до проверки формы
|
||||
|
|
@ -156,19 +189,19 @@ class Controller_Model extends Controller_Action
|
|||
$validator->validate($request); // --|
|
||||
// Проверка формы
|
||||
if (!$validator->isValid()) {
|
||||
return json::encode($validator->getErrorMsg());
|
||||
return json_encode($validator->getErrorMsg());
|
||||
}
|
||||
return json::encode(true);
|
||||
return json_encode(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Инициализация формы
|
||||
*/
|
||||
protected function formSetup($form, $id = null, $ref = null)
|
||||
protected function formSetup(Form_Form $form, $id = null, $ref = null)
|
||||
{
|
||||
if (empty($this->schema)) {
|
||||
$model = $this->getModel($this->useModel);
|
||||
$ui = new UIMapper($model);
|
||||
$ui = new Model_UIMapper($model);
|
||||
$schema = $ui->getEditSchema();
|
||||
|
||||
$form->addFieldList($schema);
|
||||
|
|
@ -178,71 +211,57 @@ class Controller_Model extends Controller_Action
|
|||
}
|
||||
|
||||
/**
|
||||
* Добавление пользователя
|
||||
* Добавление
|
||||
*/
|
||||
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);
|
||||
// Проверка
|
||||
$validator = new Validator_Validator();
|
||||
$validator->addRuleList($this->schema);
|
||||
|
||||
$form = new TForm();
|
||||
$this->formSetup($form, $request->get('id'), $request->get('ref')); // Инициализация формы
|
||||
// Действия до проверки формы
|
||||
$this->validate($validator, $request); // <--|
|
||||
$validator->validate($request); // --|
|
||||
// Проверка формы
|
||||
if (!$validator->isValid()) {
|
||||
return json_encode(array('result'=>'fail','errors'=>$validator->getErrorMsg()));
|
||||
}
|
||||
// Нужен тест для формы
|
||||
$model = $this->getModel($this->useModel);
|
||||
$className = $model->className;
|
||||
$item = new $className();
|
||||
|
||||
$form->setValues($request); // <-- Убрать в formUpdate
|
||||
$this->formUpdate($form, $request);
|
||||
// Сохраняем значение в базе данных
|
||||
$item->id = $request->get('id');
|
||||
// Если таблица связана с другой таблицей
|
||||
if ($request->get('ref') && $model->reference[1]) {
|
||||
$ref_id = $model->reference[1];
|
||||
$item->$ref_id = $request->get('ref');
|
||||
}
|
||||
|
||||
$form->setError($validator); // Установка ошибок для формы
|
||||
// Подготовка к сохранению
|
||||
$this->beforeSave($item, $request); // Сюдаже и истрия переходов
|
||||
// nextId ??? или выход или новая форма для создания новости
|
||||
|
||||
$id = $model->saveDB($item, $request);
|
||||
|
||||
$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 -> идентефикатор родительской таблицы !!??
|
||||
// Для страницы со списком id -> идентификатор родительской таблицы !!??
|
||||
// $request->set('id', $request->get('ref'));
|
||||
if ($request->get('apply')) {
|
||||
$request->setAction('form');
|
||||
return $this->forward('actionForm', $request);
|
||||
return (string) $this->forward('actionForm', $request);
|
||||
}
|
||||
return $this->forward('actionIndex', $request);
|
||||
//$request->setAction('index');
|
||||
$result = array('result'=>'ok');
|
||||
if($id){
|
||||
$result['action'] = forceUrl($this->nUrl('add', array('id' => $id, 'ref' => $request->get('ref'))));
|
||||
}
|
||||
return json_encode($result);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -275,38 +294,40 @@ class Controller_Model extends Controller_Action
|
|||
$this->setTitlePath($ref);
|
||||
|
||||
$model = $this->getModel($this->useModel);
|
||||
$form = new TForm(); // Показываем форму
|
||||
$form = new Form_Form(); // Показываем форму
|
||||
$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);
|
||||
//
|
||||
$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);
|
||||
///
|
||||
}else{//Создание нового элемента
|
||||
$this->beforeFirstLoad($form);
|
||||
}
|
||||
return $tpl;
|
||||
return json_encode($tpl);
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
function tableSetup($table, $id = null, $ref = null)
|
||||
function tableSetup(Widgets_ListTable $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();
|
||||
$ui = new Model_UIMapper($model);
|
||||
$ui->hidden = $table->hidden;
|
||||
|
||||
$schema = $ui->getTableSchema();
|
||||
$schema[0]['action'] = forceUrl($table->getFirstItem());
|
||||
|
||||
$table->setHeader($schema);
|
||||
}
|
||||
|
|
@ -314,7 +335,7 @@ class Controller_Model extends Controller_Action
|
|||
|
||||
/**
|
||||
*/
|
||||
public function actionIndex(HttpRequest $request)
|
||||
public function actionDefault(HttpRequest $request)
|
||||
{
|
||||
$this->getActionPath($request, 'index');
|
||||
// Такое мета действие наверное можно вынести в отдельный класс
|
||||
|
|
@ -335,14 +356,14 @@ class Controller_Model extends Controller_Action
|
|||
$ref = $request->session()->get('ref');
|
||||
}
|
||||
|
||||
$request->session->set('ref', $ref);
|
||||
$request->session()->set('ref', $ref);
|
||||
$this->addParameter('ref', $ref);
|
||||
// }}}
|
||||
$this->setTitlePath($ref);
|
||||
|
||||
$tpl = $this->getView('list');
|
||||
$tpl = new stdClass();
|
||||
|
||||
// Помошники действий
|
||||
// Помощники действий
|
||||
$this->callHelpers($request);
|
||||
// Таблица
|
||||
if ($request->session()->get(strtolower(get_class($this)))) {
|
||||
|
|
@ -355,7 +376,7 @@ class Controller_Model extends Controller_Action
|
|||
'size' => $session['size'],
|
||||
'desc' => $session['desc']));
|
||||
|
||||
$this->table->setData('sorter', $session['key']);
|
||||
//$this->table->setData('sorter', $session['key']);
|
||||
if (isset($session['desc'])) {
|
||||
$this->table->setData('desc', $session['desc']);
|
||||
}
|
||||
|
|
@ -363,64 +384,137 @@ class Controller_Model extends Controller_Action
|
|||
|
||||
call_user_func($setup, $this->table, $request->get('id'), $ref);// --> Эквивалент formSetup
|
||||
$this->table->setAction($list);
|
||||
//
|
||||
if (!$this->table->getData('module')) {
|
||||
$this->table->setData('module', strtolower($this->useModel));
|
||||
}
|
||||
$tpl->menu_path = $this->path->getItems();
|
||||
|
||||
// Поиск
|
||||
$search = new SearchDialog();
|
||||
$search->setTitle('Поиск');
|
||||
$search->setAction($this->aUrl('search'));
|
||||
$search->setFriend($this->table);
|
||||
$search->addFields($this->schemaSearch);
|
||||
$this->table->makeData();
|
||||
$tpl->table = array('data' => $this->table->data);
|
||||
$tpl->menu = $this->menu->menu->getItems();
|
||||
$tpl->path = $this->path->getItems();
|
||||
|
||||
// Настройки
|
||||
$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;
|
||||
return json_encode($tpl);
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public function actionSetup($request)
|
||||
{
|
||||
public function actionSetup(HttpRequest $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)));
|
||||
$request->session()->set(strtolower(get_class($this)),
|
||||
array('view' => array('left' => $left, 'right' => $right), 'page' => 1, 'size' => 0, 'desc' => 'asc', 'key' => false));
|
||||
|
||||
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)))); // Действие для формы
|
||||
protected function formPage($form, $request, $template = 'form') {
|
||||
$view = new stdClass();//$this->getView($template);
|
||||
$view->form = $form;
|
||||
$form->action = forceUrl($this->nUrl('add', array('page' => $this->getPageId($request)))); // Действие для формы
|
||||
|
||||
$view->menu_path = $this->path->getItems();
|
||||
$view->path = $this->path->getItems();
|
||||
$view->back = $this->path->getPrev();
|
||||
return $view;
|
||||
}
|
||||
|
||||
function _getActionPath() {
|
||||
return new Controller_State('index');
|
||||
}
|
||||
|
||||
// Тоже убрать в метод Controller_Model
|
||||
function getActionPath(HttpRequest $request/*, $action = false*/)
|
||||
{
|
||||
require_once 'state.php';
|
||||
function getActionPath(HttpRequest $request, $action = null) {
|
||||
$this->_getActionPath()->getPath($this, ($action) ? $action : $request->getAction());
|
||||
}
|
||||
|
||||
|
||||
function parse_params($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);
|
||||
}
|
||||
return $arguments;
|
||||
}
|
||||
|
||||
public function actionFormPage(HttpRequest $request) {
|
||||
//$tpl = $this->getView('formpage', 'View_Top');
|
||||
|
||||
$view = $this->getView('formpage', 'View_Top');
|
||||
|
||||
$params = $this->parse_params($request->get('params'));
|
||||
|
||||
|
||||
// $model = $this->getModel($this->useModel);
|
||||
$form = new Form_Form(); // Показываем форму
|
||||
$form->header = 'Редактирование записи';
|
||||
$this->formSetup($form, $request->get('id'), $request->get('ref')); // Инициализация формы
|
||||
/*
|
||||
$ui = new Model_UIMapper($model);
|
||||
$schema = $ui->getEditSchema();
|
||||
$form->addFieldList($schema);
|
||||
*/
|
||||
|
||||
$list = $request->get('table_item');
|
||||
$id = ($list[0]) ? $list[0] : $request->get('id');
|
||||
|
||||
// $tpl = $this->formPage($form, $request);
|
||||
|
||||
$view->setView('form', $form);
|
||||
$view->action = forceUrl($this->nUrl('addpage', array('ref' => $params['id']))); // Действие для формы
|
||||
|
||||
// $view->menu_path = $this->path->getItems();
|
||||
$view->back = '';
|
||||
return $view;
|
||||
|
||||
// return $tpl;
|
||||
}
|
||||
|
||||
/* Для поддержки редактрования на сайте */
|
||||
public function actionAddPage(HttpRequest $request)
|
||||
{
|
||||
// {{{ тоже может быть один ref или несколько
|
||||
$ref = $request->get('ref');
|
||||
$this->addParameter('ref', $ref); // Добавляет параметр в url
|
||||
/// }}}
|
||||
$validator = new Validator_Validator();
|
||||
$validator->addRuleList($this->schema);
|
||||
|
||||
// Действия до проверки формы
|
||||
$this->validate($validator, $request); // <--|
|
||||
$validator->validate($request); // --|
|
||||
// Проверка формы
|
||||
if (!$validator->isValid()) {
|
||||
return $validator;
|
||||
}
|
||||
|
||||
// Нужен тест для формы
|
||||
$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); // Сюдаже и истрия переходов
|
||||
$model->saveDB($item, $request);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue