1
0
Fork 0
mirror of https://github.com/Oreolek/kangana.git synced 2024-07-02 22:55:04 +03:00
kangana/application/classes/Controller/Letter.php

108 lines
3.3 KiB
PHP
Raw Normal View History

2014-01-28 13:07:36 +02:00
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Letter controller.
**/
class Controller_Letter extends Controller_Layout {
protected $secure_actions = array(
2014-01-29 10:52:33 +02:00
'index','create', 'edit', 'delete'
2014-01-28 13:07:36 +02:00
);
2014-01-29 10:52:33 +02:00
protected $controls = array(
2014-01-29 12:41:25 +02:00
'subject' => 'input',
2014-01-29 10:52:33 +02:00
'text' => 'textarea',
'order' => 'input',
);
public function action_create()
{
$this->template = new View_Edit;
$id = $this->request->param('id');
2016-10-11 08:37:54 +03:00
if ( ! Model_Course::exists($id))
2014-01-29 10:52:33 +02:00
{
$this->redirect('error/500');
}
$this->template->model = ORM::factory('Letter');
$this->template->model->course_id = $id;
2016-10-11 08:37:54 +03:00
$this->template->title = I18n::translate('New letter');
2014-01-29 10:52:33 +02:00
$this->_edit($this->template->model);
}
public function action_edit()
{
$this->template = new View_Edit;
2016-10-11 08:37:54 +03:00
$this->template->title = I18n::translate('Letter editing');
2014-01-29 10:52:33 +02:00
$id = $this->request->param('id');
$model = ORM::factory('Letter', $id);
2016-10-11 08:37:54 +03:00
if ( ! $model->loaded())
2014-01-29 10:52:33 +02:00
{
$this->redirect('error/404');
}
$this->_edit($model);
}
2016-10-11 08:37:54 +03:00
2014-02-16 06:13:01 +02:00
protected function _edit_redirect($model)
{
return Route::url('default', array('controller' => 'Course','action' => 'view','id' => $model->course_id));
}
2014-01-29 10:52:33 +02:00
public function action_delete()
{
$this->template = new View_Delete;
$id = $this->request->param('id');
$model = ORM::factory('Letter', $id);
2016-10-11 08:37:54 +03:00
if ( ! $model->loaded())
2014-01-29 10:52:33 +02:00
{
$this->redirect('error/404');
}
2016-10-11 08:37:54 +03:00
$this->template->title = I18n::translate('Delete letter');
2014-01-29 10:52:33 +02:00
$this->template->content_title = $model->title;
$this->template->content = $model->description;
$confirmation = $this->request->post('confirmation');
if ($confirmation === 'yes') {
2014-02-19 12:09:06 +02:00
$model->delete();
2014-01-29 10:52:33 +02:00
$this->redirect('/');
}
}
2014-02-21 08:53:42 +02:00
public function action_view()
{
$id = $this->request->param('id');
$model = ORM::factory('Letter', $id);
2016-10-11 08:37:54 +03:00
if ( ! $model->loaded())
2014-02-21 08:53:42 +02:00
{
$this->redirect('error/404');
}
Debugtoolbar::disable();
2014-02-21 08:53:42 +02:00
$this->template = new View_Letter_View;
$this->template->content = $model->text;
$this->template->subject = $model->subject;
$this->template->id = $model->id;
2014-02-21 08:53:42 +02:00
}
2014-03-12 08:57:57 +02:00
public function action_unsubscribe()
{
$id = $this->request->param('id');
$letter = ORM::factory('Letter', $id);
$token = $this->request->query('token');
$email = $this->request->query('email');
2016-10-11 08:37:54 +03:00
if ( ! $letter->loaded() OR empty($token) OR empty($email))
2014-03-12 08:57:57 +02:00
{
$this->redirect('error/500');
}
$client = ORM::factory('Client')->where('email', '=', $email)->and_where('token', '=', $token)->find();
$course = ORM::factory('Course', $letter->course_id);
2016-10-11 08:37:54 +03:00
if ( ! $client->loaded() OR ! $course->loaded())
2014-03-12 08:57:57 +02:00
{
2016-10-11 08:37:54 +03:00
Session::instance()->set_once('flash_error', I18n::translate('Client not found. Possible subscription token problem.'));
2014-03-12 08:57:57 +02:00
$this->redirect('error/403');
}
// token is valid, client is found, letter is found - we can unsubscribe him now
$course->remove('clients', $client);
Log::instance()->add(Log::NOTICE, 'Client '.$client->name.' has been unsubscribed from course '.$course->title);
Model_Task::cancel($client->id, $course->id);
$this->template = new View_Message;
2016-10-11 08:37:54 +03:00
$this->template->message = I18n::translate('You have been successfully unsubscribed from course %s', array('%s' => $course->title));
2014-03-12 08:57:57 +02:00
}
2014-01-28 13:07:36 +02:00
}