1
0
Fork 0
mirror of https://github.com/Oreolek/kangana.git synced 2024-06-16 15:01:09 +03:00
kangana/application/classes/Controller/Course.php

191 lines
5.6 KiB
PHP
Raw Normal View History

2014-01-18 10:02:16 +02:00
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Course controller.
2014-01-18 10:02:16 +02:00
**/
class Controller_Course extends Controller_Layout {
2014-01-18 10:02:16 +02:00
protected $secure_actions = array(
2014-01-29 10:52:33 +02:00
'index','create', 'edit', 'delete', 'view'
2014-01-18 10:02:16 +02:00
);
protected $controls = array(
'title' => 'input',
'description' => 'textarea',
'period' => 'input',
2014-02-15 05:53:52 +02:00
'price' => 'input'
2014-01-18 10:02:16 +02:00
);
2016-10-11 08:37:54 +03:00
2014-01-18 10:02:16 +02:00
public function action_index()
{
$this->template = new View_Course_Index;
2016-10-11 08:37:54 +03:00
$this->template->title = I18n::translate('Course index');
$this->template->items = ORM::factory('Course')
2014-01-18 10:02:16 +02:00
->filter_by_page($this->request->param('page'))
->find_all();
}
public function action_create()
{
$this->template = new View_Edit;
$this->template->model = ORM::factory('Course');
2016-10-11 08:37:54 +03:00
$this->template->title = I18n::translate('New course');
2014-01-18 10:02:16 +02:00
$this->_edit($this->template->model);
}
2014-02-19 09:15:40 +02:00
/**
* One-page course creation
**/
public function action_simple()
{
$this->template = new View_Course_Simple;
$this->template->controls = array();
2016-10-11 08:37:54 +03:00
$this->template->title = I18n::translate('New course');
$course = ORM::factory('Course');
$letter = ORM::factory('Letter');
2016-10-05 04:53:39 +03:00
if ($this->request->method() === Request::POST) {
2014-02-19 09:15:40 +02:00
$course->values($this->request->post(), array('title', 'description'));
$letter->values($this->request->post(), array('subject', 'text'));
2014-02-19 09:15:40 +02:00
$course->price = 0;
$course->period = 1;
$letter->order = 1;
$validation_course = $course->validate_create($this->request->post());
2016-10-12 08:48:39 +03:00
$validation_letter = $letter->validate_create($this->request->post());
2014-02-19 09:15:40 +02:00
try
{
if ($validation_course->check() AND $validation_letter->check())
{
$course->create();
2016-10-12 08:48:39 +03:00
$course->add('group', (int) $this->request->post('group'));
2014-02-19 09:15:40 +02:00
$letter->course_id = $course->id;
$letter->create();
}
else
{
$this->template->errors = array_merge(
$validation_course->errors('default'),
$validation_letter->errors('default')
);
}
}
catch (ORM_Validation_Exception $e)
{
$this->template->errors = $e->errors('default');
}
if (empty($this->template->errors))
{
$this->redirect($this->_edit_redirect($course));
}
}
$this->template->model_letter = $letter;
$this->template->model_course = $course;
2014-02-19 09:15:40 +02:00
}
2014-01-18 10:02:16 +02:00
public function action_edit()
{
$this->template = new View_Edit;
2016-10-11 08:37:54 +03:00
$this->template->title = I18n::translate('Edit course');
2014-01-18 10:02:16 +02:00
$id = $this->request->param('id');
$model = ORM::factory('Course', $id);
2016-10-11 08:37:54 +03:00
if ( ! $model->loaded())
2014-01-18 10:02:16 +02:00
{
$this->redirect('error/404');
}
$this->_edit($model);
}
public function action_delete()
{
$this->template = new View_Delete;
$id = $this->request->param('id');
$model = ORM::factory('Course', $id);
2016-10-11 08:37:54 +03:00
if ( ! $model->loaded())
2014-01-18 10:02:16 +02:00
{
$this->redirect('error/404');
}
2016-10-11 08:37:54 +03:00
$this->template->title = I18n::translate('Delete course');
2014-01-18 10:02:16 +02:00
$this->template->content_title = $model->title;
$this->template->content = $model->description;
$confirmation = $this->request->post('confirmation');
if ($confirmation === 'yes') {
2014-02-04 10:53:23 +02:00
$model->delete();
2014-01-18 10:02:16 +02:00
$this->redirect('/');
}
}
2014-01-28 13:07:36 +02:00
public function action_view()
{
$this->template = new View_Letter_Index;
$id = $this->request->param('id');
$model = ORM::factory('Course', $id)->with('letters');
2016-10-11 08:37:54 +03:00
if ( ! $model->loaded())
2014-01-28 13:07:36 +02:00
{
$this->redirect('error/404');
}
2016-10-11 08:37:54 +03:00
$this->template->title = I18n::translate('Course').' '.$model->title;
$this->template->course_id = $id;
2014-01-28 13:07:36 +02:00
$this->template->items = $model->letters
->filter_by_page($this->request->param('page'))
2014-01-29 10:52:33 +02:00
->order_by('order')
2014-01-28 13:07:36 +02:00
->find_all();
}
2014-01-18 10:02:16 +02:00
2014-01-28 09:50:18 +02:00
public function action_subscribe()
{
$this->template = new View_Course_Subscribe;
2014-01-28 09:50:18 +02:00
$id = $this->request->param('id');
$course = ORM::factory('Course', $id);
2016-10-11 08:37:54 +03:00
if ( ! $course->loaded())
2014-01-28 09:50:18 +02:00
{
$this->redirect('error/404');
}
$this->template->title = $course->title;
2014-01-28 09:50:18 +02:00
$controls = array(
'name' => 'input',
'email' => 'input'
);
$this->template->controls = $controls;
$this->template->errors = array();
2014-02-04 06:35:26 +02:00
$model = ORM::factory('Client');
2016-10-11 08:37:54 +03:00
2016-10-05 04:53:39 +03:00
if ($this->request->method() === Request::POST) {
2014-01-28 09:50:18 +02:00
$model = ORM::factory('Client')->where('email', '=', $this->request->post('email'))->find();
$model->values($this->request->post(), array_keys($controls));
$model->customize();
$validation = $model->validate_create($this->request->post());
try
{
if ($validation->check())
{
$model->save();
2016-10-11 08:37:54 +03:00
if ( ! $model->has('course', $course))
2014-02-27 09:19:52 +02:00
{
$model->add('course', $course);
}
2014-01-29 14:13:04 +02:00
$task = ORM::factory('Task');
$letter = $course->next_letter();
2014-01-29 14:13:04 +02:00
$task->letter_id = $letter->id;
$task->client_id = $model->id;
// now we break the abstraction to speed things up
$task->status = Model_Task::STATUS_SENT;
$task->date = date('Y-m-d');
$task->create();
$letter->send($model->email);
2014-01-28 09:50:18 +02:00
}
else
{
$this->template->errors = $validation->errors('default');
}
}
catch (ORM_Validation_Exception $e)
{
$this->template->errors = $e->errors('default');
}
if (empty($this->template->errors))
{
2016-10-11 08:37:54 +03:00
Session::instance()->set('flash_success', I18n::translate('You were subscribed. A welcome email has been sent to you. Please check your inbox.'));
2014-01-28 09:50:18 +02:00
}
}
$this->template->model = $model;
}
2014-01-18 10:02:16 +02:00
}