mirror of
https://github.com/Oreolek/kangana.git
synced 2024-05-24 11:58:11 +03:00
Working CRUD for subscriptions
This commit is contained in:
parent
061d026cad
commit
024c69870d
|
@ -5,6 +5,11 @@ class Controller_Layout extends Controller {
|
|||
protected $is_private = FALSE;
|
||||
public $auto_render = TRUE;
|
||||
public $template = '';
|
||||
/**
|
||||
* Array of CRUD controls (create & edit).
|
||||
* @see View_Edit
|
||||
**/
|
||||
protected $controls = array();
|
||||
|
||||
public function before()
|
||||
{
|
||||
|
@ -31,6 +36,10 @@ class Controller_Layout extends Controller {
|
|||
{
|
||||
if ($this->auto_render)
|
||||
{
|
||||
if (!empty($this->controls))
|
||||
{
|
||||
$this->template->controls = $this->controls;
|
||||
}
|
||||
$renderer = Kostache_Layout::factory('layout');
|
||||
$this->response->body($renderer->render($this->template, $this->template->_view));
|
||||
}
|
||||
|
|
106
application/classes/Controller/Subscription.php
Normal file
106
application/classes/Controller/Subscription.php
Normal file
|
@ -0,0 +1,106 @@
|
|||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
|
||||
/**
|
||||
* Subscription controller.
|
||||
**/
|
||||
class Controller_Subscription extends Controller_Layout {
|
||||
protected $secure_actions = array(
|
||||
'index',
|
||||
);
|
||||
protected $controls = array(
|
||||
'title' => 'input',
|
||||
'description' => 'textarea',
|
||||
'period' => 'input',
|
||||
// 'price' => 'input'
|
||||
);
|
||||
|
||||
public function action_index()
|
||||
{
|
||||
$this->template = new View_Subscription_Index;
|
||||
$this->template->title = __('Subscription index');
|
||||
$this->template->items = ORM::factory('Subscription')
|
||||
->filter_by_page($this->request->param('page'))
|
||||
->find_all();
|
||||
}
|
||||
|
||||
public function action_create()
|
||||
{
|
||||
$this->template = new View_Edit;
|
||||
$this->template->model = ORM::factory('Subscription');
|
||||
$this->template->title = __('New subscription');
|
||||
$this->_edit($this->template->model);
|
||||
}
|
||||
|
||||
public function action_edit()
|
||||
{
|
||||
$this->template = new View_Edit;
|
||||
$this->template->title = __('Edit subscription');
|
||||
$id = $this->request->param('id');
|
||||
$model = ORM::factory('Subscription', $id);
|
||||
if (!$model->loaded())
|
||||
{
|
||||
$this->redirect('error/404');
|
||||
}
|
||||
$this->_edit($model);
|
||||
}
|
||||
|
||||
public function action_delete()
|
||||
{
|
||||
$this->template = new View_Delete;
|
||||
$id = $this->request->param('id');
|
||||
$model = ORM::factory('Subscription', $id);
|
||||
if (!$model->loaded())
|
||||
{
|
||||
$this->redirect('error/404');
|
||||
}
|
||||
$this->template->title = __('Delete subscription');
|
||||
$this->template->content_title = $model->title;
|
||||
$this->template->content = $model->description;
|
||||
|
||||
$confirmation = $this->request->post('confirmation');
|
||||
if ($confirmation === 'yes') {
|
||||
$post->delete();
|
||||
$this->redirect('/');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit or create model.
|
||||
**/
|
||||
protected function _edit($model)
|
||||
{
|
||||
if (!($model instanceof ORM))
|
||||
{
|
||||
Log::instance()->add(Log::ERROR, __('Attempt to call _edit() on non-ORM model. Parameter class should be ORM, not ').get_class($model).'.');
|
||||
$this->redirect('error/500');
|
||||
}
|
||||
$this->template->errors = array();
|
||||
|
||||
if ($this->request->method() === HTTP_Request::POST) {
|
||||
$model->values($this->request->post(), array_keys($this->controls));
|
||||
$validation = $model->validate_create($this->request->post());
|
||||
$model->customize();
|
||||
try
|
||||
{
|
||||
if ($validation->check())
|
||||
{
|
||||
$model->save();
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->template->errors = $validation->errors('default');
|
||||
}
|
||||
}
|
||||
catch (ORM_Validation_Exception $e)
|
||||
{
|
||||
$this->template->errors = $e->errors('default');
|
||||
}
|
||||
if (empty($this->template->errors))
|
||||
{
|
||||
$this->redirect(Route::url('default', array('controller' => Request::current()->controller(), 'action' => 'view','id' => $model->id)));
|
||||
}
|
||||
}
|
||||
$this->template->model = $model;
|
||||
}
|
||||
|
||||
}
|
|
@ -18,7 +18,7 @@ class Model_Subscription extends ORM {
|
|||
array('not_empty'),
|
||||
array('min_length', array(':value', 4)),
|
||||
array('max_length', array(':value', 100)),
|
||||
)
|
||||
),
|
||||
'description' => array(
|
||||
array('not_empty'),
|
||||
array('min_length', array(':value', 20)),
|
||||
|
@ -42,4 +42,12 @@ class Model_Subscription extends ORM {
|
|||
'period' => 'Mailing period (in days)',
|
||||
'description' => 'Description (for the clients)'
|
||||
);
|
||||
|
||||
public function customize()
|
||||
{
|
||||
if ($this->period < 1)
|
||||
{
|
||||
$this->period = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -126,4 +126,25 @@ class ORM extends Kohana_ORM {
|
|||
return $this->where($this->_object_name .'.'. $this->_primary_key, 'IN', $id)->find_all();
|
||||
return $this->where('id', '=', $id)->find();
|
||||
}
|
||||
|
||||
/**
|
||||
* Pagination filter: get only current page
|
||||
* @param int $num page number (i.e. 1 for first page, 2 - second page and so on).
|
||||
**/
|
||||
public function filter_by_page($num = 0)
|
||||
{
|
||||
$num = (int) $num - 1;
|
||||
if ($num < 0)
|
||||
{
|
||||
$num = 0;
|
||||
}
|
||||
$page_size = Kohana::$config->load('common.page_size');
|
||||
$first_item = $page_size * $num;
|
||||
return $this->offset($first_item)->limit($page_size);
|
||||
}
|
||||
|
||||
/**
|
||||
* A utility function to customize model before saving it
|
||||
**/
|
||||
public function customize() {}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
/**
|
||||
* Index view controller.
|
||||
|
@ -56,7 +56,7 @@ class View_Index extends View_Layout {
|
|||
$result = array();
|
||||
if (is_null($this->items) OR $this->items === FALSE OR count($this->items) === 0)
|
||||
{
|
||||
return 'Не найдено объектов для отображения.';
|
||||
return __('No objects found to show');
|
||||
};
|
||||
if ($this->item_count === count($this->items))
|
||||
{
|
||||
|
@ -85,53 +85,20 @@ class View_Index extends View_Layout {
|
|||
$this->is_admin = Auth::instance()->logged_in('admin');
|
||||
}
|
||||
$output = array(
|
||||
'date' => '',
|
||||
'name' => '',
|
||||
'edit_link' => '',
|
||||
'view_link' => '',
|
||||
'delete_link' => '',
|
||||
'view_link' => HTML::anchor(Route::url('default', array('controller' => Request::current()->controller(), 'action' => 'view','id' => $item->id)), $item->name, array('class' => 'link_view')),
|
||||
);
|
||||
if ($this->show_date)
|
||||
{
|
||||
$output['date'] = $item->posted_at;
|
||||
}
|
||||
$output['name'] = $item->name;
|
||||
$output['view_link'] = $this->link_view($item->id);
|
||||
if ($this->is_admin and $this->show_edit)
|
||||
{
|
||||
$output['edit_link'] = $this->link_edit($item->id);
|
||||
$output['delete_link'] = $this->link_delete($item->id);
|
||||
$output['edit_link'] = HTML::anchor(Route::url('default', array('controller' => Request::current()->controller(), 'action' => 'edit','id' => $item->id)), __('Edit'), array('class' => 'link_edit'));
|
||||
$output['delete_link'] = HTML::anchor(Route::url('default', array('controller' => Request::current()->controller(), 'action' => 'delete','id' => $item->id)), __('Delete'), array('class' => 'link_delete'));
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a link to view item by its ID
|
||||
* @param integer ID
|
||||
**/
|
||||
protected function link_view($id)
|
||||
{
|
||||
return Route::url('default', array('controller' => Request::current()->controller(), 'action' => 'view','id' => $id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a link to edit item by its ID
|
||||
* @param integer ID
|
||||
**/
|
||||
protected function link_edit($id)
|
||||
{
|
||||
return Route::url('default', array('controller' => Request::current()->controller(), 'action' => 'edit','id' => $id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a link to delete item by its ID
|
||||
* @param integer ID
|
||||
**/
|
||||
protected function link_delete($id)
|
||||
{
|
||||
return Route::url('default', array('controller' => Request::current()->controller(), 'action' => 'delete','id' => $id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters $this->items to only current page.
|
||||
**/
|
||||
|
@ -180,7 +147,7 @@ class View_Index extends View_Layout {
|
|||
{
|
||||
if (Auth::instance()->logged_in('admin'))
|
||||
{
|
||||
return '<a href="'.Route::url('default', array('controller' => Request::current()->controller(), 'action' => 'create')).'" class="link_new">Добавить</a>';
|
||||
return '<a href="'.Route::url('default', array('controller' => Request::current()->controller(), 'action' => 'create')).'" class="link_new">'.__('Add').'</a>';
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
|
|
@ -68,8 +68,7 @@ class View_Layout {
|
|||
{
|
||||
$result = array();
|
||||
$navigation = array(
|
||||
// 'Список страниц' => 'page/index',
|
||||
// 'О сайте' => 'page/view/1',
|
||||
__('Subscriptions') => 'subscription/index',
|
||||
);
|
||||
if (!Auth::instance()->logged_in())
|
||||
{
|
||||
|
|
29
application/classes/View/Subscription/Index.php
Normal file
29
application/classes/View/Subscription/Index.php
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
/**
|
||||
* Subscription index view controller.
|
||||
* @package Views
|
||||
* @author Oreolek
|
||||
**/
|
||||
class View_Subscription_Index extends View_Index {
|
||||
protected $is_admin = TRUE; // admin only view
|
||||
public $show_date = FALSE;
|
||||
/**
|
||||
* An internal function to prepare item data.
|
||||
**/
|
||||
protected function show_item($item)
|
||||
{
|
||||
if (!$item instanceof ORM)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
$output = array(
|
||||
'description' => $item->description,
|
||||
'view_link' => HTML::anchor(Route::url('default', array('controller' => Request::current()->controller(), 'action' => 'view','id' => $item->id)),$item->title, array('class' => 'link_view')),
|
||||
'edit_link' => HTML::anchor(Route::url('default', array('controller' => Request::current()->controller(), 'action' => 'edit','id' => $item->id)), __('Edit'), array('class' => 'link_edit')),
|
||||
'delete_link' => HTML::anchor(Route::url('default', array('controller' => Request::current()->controller(), 'action' => 'delete','id' => $item->id)), __('Delete'), array('class' => 'link_delete')),
|
||||
);
|
||||
return $output;
|
||||
}
|
||||
}
|
|
@ -12,7 +12,17 @@ return array(
|
|||
'Mailing period (in days)' => 'Период рассылки (в днях)',
|
||||
'Description (for the clients)' => 'Описание (для клиентов)',
|
||||
'Email' => 'Email',
|
||||
'Subscription token' => 'Токен подписки',
|
||||
'Subscription token' => 'Токен рассылки',
|
||||
'Mailing date' => 'Дата отправки',
|
||||
'Status' => 'Статус'
|
||||
'Status' => 'Статус',
|
||||
'Subscriptions' => 'Рассылки',
|
||||
'No objects found to show' => 'Не найдено объектов для отображения.',
|
||||
'Add' => 'Добавить',
|
||||
'Edit' => 'Редактировать',
|
||||
'Delete' => 'Удалить',
|
||||
'Attempt to call _edit() on non-ORM model. Parameter class should be ORM, not ' => 'Попытка вызвать __edit() на не-ORM модели. Класс параметра должен быть ORM, а не ',
|
||||
'New subscription' => 'Новая рассылка',
|
||||
'Subscription index' => 'Все рассылки',
|
||||
'Delete subscription' => 'Удалить рассылку',
|
||||
'Edit subscription' => 'Редактировать рассылку'
|
||||
);
|
||||
|
|
|
@ -10,10 +10,10 @@
|
|||
{{#date}}
|
||||
<div class="date">{{date}}</div>
|
||||
{{/date}}
|
||||
<div class="hyphenate"><a class="link_view" href="{{{view_link}}}">{{{name}}}</a></div>
|
||||
<div class="hyphenate">{{{view_link}}}</div>
|
||||
{{#edit_link}}
|
||||
<div><a class="link_edit" href="{{{edit_link}}}">Редактировать</a></div>
|
||||
<div><a class="link_delete" href="{{{delete_link}}}">Удалить</a></div>
|
||||
<div>{{{edit_link}}}</div>
|
||||
<div>{{{delete_link}}}</div>
|
||||
{{/edit_link}}
|
||||
</div>
|
||||
{{/get_items}}
|
||||
|
|
18
application/templates/subscription/index.mustache
Normal file
18
application/templates/subscription/index.mustache
Normal file
|
@ -0,0 +1,18 @@
|
|||
{{#show_create}}
|
||||
{{{link_new}}}
|
||||
{{/show_create}}
|
||||
<div class="hyphenate">
|
||||
{{{content}}}
|
||||
</div>
|
||||
<table class="table">
|
||||
{{#get_items}}
|
||||
<tr>
|
||||
<td>{{{view_link}}}</td>
|
||||
<td class="hyphenate">{{{description}}}</td>
|
||||
{{#edit_link}}
|
||||
<td>{{{edit_link}}}</td>
|
||||
<td>{{{delete_link}}}</td>
|
||||
{{/edit_link}}
|
||||
</tr>
|
||||
{{/get_items}}
|
||||
</table>
|
Loading…
Reference in a new issue