From d15a4064e7dbb9ce0affebf2f0919b04ef53c857 Mon Sep 17 00:00:00 2001
From: Alexander Yakovlev
Date: Wed, 19 Oct 2016 12:17:04 +0700
Subject: [PATCH] Issue #6: final course-subscription combining
The migration was in earlier commits.
---
application/classes/Controller/Course.php | 53 +++++++-
.../classes/Controller/Subscription.php | 126 ------------------
application/classes/Model/Subscription.php | 64 ---------
application/classes/View/Course/Index.php | 15 +--
application/classes/View/Layout.php | 2 +-
.../classes/View/Subscription/Index.php | 52 --------
.../templates/subscription/index.mustache | 24 ----
7 files changed, 49 insertions(+), 287 deletions(-)
delete mode 100644 application/classes/Controller/Subscription.php
delete mode 100644 application/classes/Model/Subscription.php
delete mode 100644 application/classes/View/Subscription/Index.php
delete mode 100644 application/templates/subscription/index.mustache
diff --git a/application/classes/Controller/Course.php b/application/classes/Controller/Course.php
index 30ff46a..6f77c1a 100644
--- a/application/classes/Controller/Course.php
+++ b/application/classes/Controller/Course.php
@@ -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 = ''
+ .I18n::translate('A course is a pre-defined regular mailing list.')
+ .' '
+ .I18n::translate("You add 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.")
+ .'
';
+
$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 = ''
+ .I18n::translate('A subscription is a non-regular mailing list.')
+ .' '
+ .I18n::translate("You add a message, and then you send it to subscribers.")
+ .'
';
+
+ $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');
diff --git a/application/classes/Controller/Subscription.php b/application/classes/Controller/Subscription.php
deleted file mode 100644
index edb40b9..0000000
--- a/application/classes/Controller/Subscription.php
+++ /dev/null
@@ -1,126 +0,0 @@
- '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;
- }
-
-}
diff --git a/application/classes/Model/Subscription.php b/application/classes/Model/Subscription.php
deleted file mode 100644
index 161ff7c..0000000
--- a/application/classes/Model/Subscription.php
+++ /dev/null
@@ -1,64 +0,0 @@
- 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');
- }
-}
diff --git a/application/classes/View/Course/Index.php b/application/classes/View/Course/Index.php
index 59a27fa..d5d32a3 100644
--- a/application/classes/View/Course/Index.php
+++ b/application/classes/View/Course/Index.php
@@ -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 ''
- .I18n::translate('A course is a pre-defined regular mailing list.')
- .' '
- .I18n::translate("You add 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.")
- .'
';
- }
-
/**
* An internal function to prepare item data.
**/
diff --git a/application/classes/View/Layout.php b/application/classes/View/Layout.php
index 83fe6ad..d8fbc14 100644
--- a/application/classes/View/Layout.php
+++ b/application/classes/View/Layout.php
@@ -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',
));
diff --git a/application/classes/View/Subscription/Index.php b/application/classes/View/Subscription/Index.php
deleted file mode 100644
index bd9ef8e..0000000
--- a/application/classes/View/Subscription/Index.php
+++ /dev/null
@@ -1,52 +0,0 @@
-'
- .I18n::translate('A subscription is a non-regular mailing list.')
- .' '
- .I18n::translate(
- "You add a message, and then you send it to subscribers."
- )
- .'
';
- }
-
- /**
- * 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;
- }
-}
diff --git a/application/templates/subscription/index.mustache b/application/templates/subscription/index.mustache
deleted file mode 100644
index 320b984..0000000
--- a/application/templates/subscription/index.mustache
+++ /dev/null
@@ -1,24 +0,0 @@
-{{#show_create}}
- {{{link_new}}}
-{{/show_create}}
-
- {{{content}}}
-
-
-
-{{#get_header}}
- {{.}} |
-{{/get_header}}
-
-{{#get_items}}
-
- {{{view_link}}} |
- {{{description}}} |
- {{client_count}} |
- {{#edit_link}}
- {{{edit_link}}} |
- {{{delete_link}}} |
- {{/edit_link}}
-
-{{/get_items}}
-