1
0
Fork 0
mirror of https://github.com/Oreolek/kangana.git synced 2024-05-04 18:18:17 +03:00

Issue #6: final course-subscription combining

The migration was in earlier commits.
This commit is contained in:
Alexander Yakovlev 2016-10-19 12:17:04 +07:00
parent 2063b740a3
commit d15a4064e7
7 changed files with 49 additions and 287 deletions

View file

@ -5,20 +5,49 @@
**/
class Controller_Course extends Controller_Layout {
protected $secure_actions = array(
'index','create', 'edit', 'delete', 'view'
);
protected $controls = array(
'title' => 'input',
'description' => 'textarea',
'period' => 'input',
'price' => 'input'
'index',
'sindex',
'create',
'edit',
'delete',
'view',
);
protected $controls = NULL;
public function action_index()
{
$this->template = new View_Course_Index;
$this->template->title = I18n::translate('Course index');
$this->template->content = '<p>'
.I18n::translate('A course is a pre-defined regular mailing list.')
.' '
.I18n::translate("You <i>add</i> a message, forming a series of messages.")
.' '
.I18n::translate("Each new subscriber gets the first message in this series.")
.' '
.I18n::translate("You can customize the delay (1 day by default) between the messages.")
.'</p>';
$this->template->items = ORM::factory('Course')
->where('type', '=', Model_Course::TYPE_SCHEDULED)
->filter_by_page($this->request->param('page'))
->find_all();
}
public function action_sindex()
{
$this->template = new View_Course_Index;
$this->template->title = I18n::translate('Subscription index');
$this->template->content = '<p>'
.I18n::translate('A subscription is a non-regular mailing list.')
.' '
.I18n::translate("You <i>add</i> a message, and then you send it to subscribers.")
.'</p>';
$this->template->items = ORM::factory('Course')
->where('type', '=', Model_Course::TYPE_IRREGULAR)
->filter_by_page($this->request->param('page'))
->find_all();
}
@ -40,6 +69,7 @@ class Controller_Course extends Controller_Layout {
$this->template->controls = array();
$this->template->title = I18n::translate('New course');
$course = ORM::factory('Course');
$course->type = Model_Course::TYPE_SCHEDULED;
$letter = ORM::factory('Letter');
if ($this->request->method() === Request::POST) {
$course->values($this->request->post(), array('title', 'description'));
@ -85,6 +115,15 @@ class Controller_Course extends Controller_Layout {
$this->template->title = I18n::translate('Edit course');
$id = $this->request->param('id');
$model = ORM::factory('Course', $id);
$this->controls = [
'title' => 'input',
'description' => 'textarea',
];
if ($model->type === Model_Course::TYPE_SCHEDULED)
{
$this->controls['period'] = 'input';
$this->controls['price'] = 'input';
};
if ( ! $model->loaded())
{
$this->redirect('error/404');

View file

@ -1,126 +0,0 @@
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Subscription controller.
**/
class Controller_Subscription extends Controller_Layout {
protected $secure_actions = array(
'index','create', 'edit', 'delete', 'view'
);
protected $controls = array(
'title' => 'input',
'description' => 'textarea',
// 'price' => 'input'
);
public function action_index()
{
$this->template = new View_Subscription_Index;
$this->template->title = I18n::translate('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 = I18n::translate('New subscription');
$this->_edit($this->template->model);
}
public function action_edit()
{
$this->template = new View_Edit;
$this->template->title = I18n::translate('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 = I18n::translate('Delete subscription');
$this->template->content_title = $model->title;
$this->template->content = $model->description;
$confirmation = $this->request->post('confirmation');
if ($confirmation === 'yes') {
$model->delete();
$this->redirect('/');
}
}
public function action_view()
{
$this->redirect('instant/index/'.$this->request->param('id'));
}
public function action_subscribe()
{
$this->template = new View_Course_Subscribe;
$id = $this->request->param('id');
$subscription = ORM::factory('Subscription', $id);
if ( ! $subscription->loaded())
{
$this->redirect('error/404');
}
$this->template->title = I18n::translate('Subscribe to ').$subscription->title;
$controls = array(
'name' => 'input',
'email' => 'input'
);
$this->template->controls = $controls;
$this->template->errors = array();
$model = ORM::factory('Client');
if ($this->request->method() === Request::POST) {
$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();
if ( ! $model->has('subscription', $subscription))
{
$model->add('subscription', $subscription);
}
$instant = ORM::factory('Instant');
$instant->subscription_id = $id;
$instant->subject = I18n::translate('You were subscribed to ').$subscription->title;
$instant->text = I18n::translate('From now on you will receive letters from this subscription.');
$instant->send($model->email, $model->token);
// instant is not saved because it's just a welcome email
}
else
{
$this->template->errors = $validation->errors('default');
}
}
catch (ORM_Validation_Exception $e)
{
$this->template->errors = $e->errors('default');
}
if (empty($this->template->errors))
{
Session::instance()->set('flash_success', I18n::translate('You were subscribed. A welcome email has been sent to you. Please check your inbox.'));
}
}
$this->template->model = $model;
}
}

View file

@ -1,64 +0,0 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* Subscription model.
* Subscription is a list of emails. An administrator can mail a letter to everyone on this list.
* @package Models
* @author Oreolek
**/
class Model_Subscription extends ORM {
protected $_has_many = array(
'clients' => array(
'model' => 'Client',
'through' => 'clients_subscriptions'
),
'instants' => array(
'model' => 'Instant'
),
'group' => array(
'model' => 'Group',
'through' => 'subscriptions_groups'
),
);
/**
* @return array validation rules
**/
public function rules()
{
return array(
'title' => array(
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)),
),
'welcome' => array(
array('min_length', array(':value', 20)),
),
'price' => array(
array('numeric')
)
);
}
/**
* Array of field labels.
* Used in forms.
**/
protected $_labels = array(
'title' => 'Title',
'price' => 'Subscription price',
'description' => 'Description (for the clients)'
);
/**
* Return subscriber count
**/
public function count_clients()
{
return DB::select(array(DB::expr('COUNT(client_id)'), 'cnt'))->from('clients_courses')->where('course_id', '=', $this->id)->execute()->get('cnt');
}
}

View file

@ -8,6 +8,8 @@
class View_Course_Index extends View_Index {
protected $is_admin = TRUE; // admin only view
public $show_date = FALSE;
public $content;
public function get_header()
{
return array(
@ -19,19 +21,6 @@ class View_Course_Index extends View_Index {
);
}
public function content()
{
return '<p>'
.I18n::translate('A course is a pre-defined regular mailing list.')
.' '
.I18n::translate("You <i>add</i> a message, forming a series of messages.")
.' '
.I18n::translate("Each new subscriber gets the first message in this series.")
.' '
.I18n::translate("You can customize the delay (1 day by default) between the messages.")
.'</p>';
}
/**
* An internal function to prepare item data.
**/

View file

@ -83,7 +83,7 @@ class View_Layout {
$navigation = array_merge($navigation, array(
I18n::translate('New course') => 'course/simple',
I18n::translate('Courses') => 'course/index',
I18n::translate('Subscriptions') => 'subscription/index',
I18n::translate('Subscriptions') => 'course/sindex',
I18n::translate('Clients') => 'client/index',
I18n::translate('Groups') => 'group/index',
));

View file

@ -1,52 +0,0 @@
<?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;
public function get_header()
{
return array(
I18n::translate('Title'),
I18n::translate('Description'),
I18n::translate('Subscribers'),
I18n::translate('Edit'),
I18n::translate('Delete')
);
}
public function content()
{
return '<p>'
.I18n::translate('A subscription is a non-regular mailing list.')
.' '
.I18n::translate(
"You <i>add</i> a message, and then you send it to subscribers."
)
.'</p>';
}
/**
* 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)), I18n::translate('Edit'), array('class' => 'link_edit')),
'delete_link' => HTML::anchor(Route::url('default', array('controller' => Request::current()->controller(), 'action' => 'delete','id' => $item->id)), I18n::translate('Delete'), array('class' => 'link_delete')),
'client_count' => $item->count_clients(),
);
return $output;
}
}

View file

@ -1,24 +0,0 @@
{{#show_create}}
{{{link_new}}}
{{/show_create}}
<div class="hyphenate">
{{{content}}}
</div>
<table class="table">
<thead>
{{#get_header}}
<th>{{.}}</th>
{{/get_header}}
</thead>
{{#get_items}}
<tr>
<td>{{{view_link}}}</td>
<td class="hyphenate">{{{description}}}</td>
<td>{{client_count}}</td>
{{#edit_link}}
<td>{{{edit_link}}}</td>
<td>{{{delete_link}}}</td>
{{/edit_link}}
</tr>
{{/get_items}}
</table>