1
0
Fork 0
mirror of https://github.com/Oreolek/kangana.git synced 2024-06-26 03:40:56 +03:00

CRUD letters

This commit is contained in:
Alexander Yakovlev 2014-01-29 15:52:33 +07:00
parent b054af8b81
commit bb0ce9bcbc
8 changed files with 146 additions and 51 deletions

View file

@ -49,4 +49,47 @@ class Controller_Layout extends Controller {
$this->check_cache();
}
}
/**
* Edit or create model.
**/
protected function _edit($model, $controls = NULL)
{
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 (is_null($controls))
{
$controls = $this->controls;
}
if ($this->request->method() === HTTP_Request::POST) {
$model->values($this->request->post(), array_keys($controls));
$model->customize();
$validation = $model->validate_create($this->request->post());
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;
}
}

View file

@ -5,6 +5,70 @@
**/
class Controller_Letter extends Controller_Layout {
protected $secure_actions = array(
'index',
'index','create', 'edit', 'delete'
);
protected $controls = array(
'text' => 'textarea',
'order' => 'input',
);
public function action_create()
{
$this->template = new View_Edit;
$id = $this->request->param('id');
if (!Model_Subscription::exists($id))
{
$this->redirect('error/500');
}
$this->template->model = ORM::factory('Letter');
$this->template->model->subscription_id = $id;
$this->template->title = __('New letter');
$this->_edit($this->template->model);
}
public function action_edit()
{
$this->template = new View_Edit;
$this->template->title = __('Letter editing');
$id = $this->request->param('id');
$model = ORM::factory('Letter', $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('Letter', $id);
if (!$model->loaded())
{
$this->redirect('error/404');
}
$this->template->title = __('Delete letter');
$this->template->content_title = $model->title;
$this->template->content = $model->description;
$confirmation = $this->request->post('confirmation');
if ($confirmation === 'yes') {
$post->delete();
$this->redirect('/');
}
}
public function action_view()
{
$this->template = new View_Letter_Index;
$id = $this->request->param('id');
$model = ORM::factory('Subscription', $id)->with('letters');
if (!$model->loaded())
{
$this->redirect('error/404');
}
$this->template->title = __('Subscription').' '.$model->title;
$this->template->items = $model->letters
->filter_by_page($this->request->param('page'))
->find_all();
}
}

View file

@ -5,7 +5,7 @@
**/
class Controller_Subscription extends Controller_Layout {
protected $secure_actions = array(
'index',
'index','create', 'edit', 'delete', 'view'
);
protected $controls = array(
'title' => 'input',
@ -73,8 +73,10 @@ class Controller_Subscription extends Controller_Layout {
$this->redirect('error/404');
}
$this->template->title = __('Subscription').' '.$model->title;
$this->template->subscription_id = $id;
$this->template->items = $model->letters
->filter_by_page($this->request->param('page'))
->order_by('order')
->find_all();
}
@ -129,47 +131,4 @@ class Controller_Subscription extends Controller_Layout {
$this->template->model = $model;
}
/**
* Edit or create model.
**/
protected function _edit($model, $controls = NULL)
{
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 (is_null($controls))
{
$controls = $this->controls;
}
if ($this->request->method() === HTTP_Request::POST) {
$model->values($this->request->post(), array_keys($controls));
$model->customize();
$validation = $model->validate_create($this->request->post());
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;
}
}

View file

@ -35,4 +35,12 @@ class Model_Letter extends ORM {
'order' => 'Message order'
);
public function customize()
{
if(empty($this->order))
{
$this->order = Model_Subscription::count_letters($this->subscription_id) + 1;
}
}
}

View file

@ -59,4 +59,15 @@ class Model_Subscription extends ORM {
$this->period = 1;
}
}
public static function count_letters($id)
{
return DB::select(array(DB::expr('COUNT(*)'), 'cnt'))->from('letters')->where('subscription_id', '=', $id)->execute()->get('cnt');
}
public static function exists($id)
{
$count = DB::select(array(DB::expr('COUNT(*)'), 'cnt'))->from('subscriptions')->where('id', '=', $id)->execute()->get('cnt');
return ($count == 1);
}
}

View file

@ -145,7 +145,7 @@ class View_Index extends View_Layout {
public function link_new()
{
if (Auth::instance()->logged_in('admin'))
if (Auth::instance()->logged_in())
{
return '<a href="'.Route::url('default', array('controller' => Request::current()->controller(), 'action' => 'create')).'" class="link_new">'.__('Add').'</a>';
}

View file

@ -9,6 +9,7 @@ class View_Letter_Index extends View_Index {
protected $is_admin = TRUE; // admin only view
public $_view = 'subscription/index';
public $show_date = FALSE;
public $subscription_id;
/**
* An internal function to prepare item data.
**/
@ -20,11 +21,16 @@ class View_Letter_Index extends View_Index {
}
$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')),
'description' => $item->text,
'view_link' => HTML::anchor(Route::url('default', array('controller' => 'Letter', 'action' => 'view','id' => $item->id)),__('View'), array('class' => 'link_view')),
'edit_link' => HTML::anchor(Route::url('default', array('controller' => 'Letter', 'action' => 'edit','id' => $item->id)), __('Edit'), array('class' => 'link_edit')),
'delete_link' => HTML::anchor(Route::url('default', array('controller' => 'Letter', 'action' => 'delete','id' => $item->id)), __('Delete'), array('class' => 'link_delete')),
);
return $output;
}
public function link_new()
{
return HTML::anchor(Route::url('default', array('controller' => 'Letter', 'action' => 'create', 'id' => $this->subscription_id)), __('Add'), array('class' => 'link_new'));
}
}

View file

@ -28,5 +28,9 @@ return array(
'Subscribe to ' => 'Подписка на ',
'Subscription' => 'Рассылка',
'Name' => 'Имя',
'You were subscribed. A welcome email has been sent to you. Please check your inbox.' => 'Вы были подписаны. Вам было выслано вступительное письмо; пожалуйста, проверьте входящие сообщения.'
'You were subscribed. A welcome email has been sent to you. Please check your inbox.' => 'Вы были подписаны. Вам было выслано вступительное письмо; пожалуйста, проверьте входящие сообщения.',
'New letter' => 'Новое письмо',
'Letter editing' => 'Редактирование письма',
'Delete letter' => 'Удалить письмо',
'View' => 'Просмотреть'
);