1
0
Fork 0
mirror of https://github.com/Oreolek/kangana.git synced 2024-06-16 23:11:07 +03:00

WIP DAR-15: instant message subscriptions

This commit is contained in:
Alexander Yakovlev 2014-02-05 19:03:04 +07:00
parent ee62fd61b2
commit 13f7362a2b
11 changed files with 443 additions and 3 deletions

View file

@ -0,0 +1,57 @@
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Instant controller.
**/
class Controller_Instant extends Controller_Layout {
protected $secure_actions = array(
'index','create', 'edit', 'delete'
);
protected $controls = array(
'subject' => 'input',
'text' => 'textarea',
'is_draft' => 'checkbox'
);
public function action_create()
{
$this->template = new View_Edit;
$id = $this->request->param('id');
if (!Model_Course::exists($id))
{
$this->redirect('error/500');
}
$this->template->model = ORM::factory('Instant');
$this->template->model->subscription_id = $id;
$this->template->title = __('New letter');
$this->_edit($this->template->model);
}
public function action_index()
{
$this->template = new View_Instant_Index;
$id = $this->request->param('id');
$model = ORM::factory('Subscription', $id)->with('instants');
if (!$model->loaded())
{
$this->redirect('error/404');
}
$this->template->title = __('Subscription').' '.$model->title;
$this->template->subscription_id = $id;
$this->template->items = $model->instants
->filter_by_page($this->request->param('page'))
->order_by('timestamp')
->find_all();
}
public function action_edit()
{
$this->template = new View_Edit;
$this->template->title = __('Instant editing');
$id = $this->request->param('id');
$model = ORM::factory('Instant', $id);
if (!$model->loaded())
{
$this->redirect('error/404');
}
$this->_edit($model);
}
}

View file

@ -0,0 +1,124 @@
<?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',
'welcome' => 'textarea'
// 'price' => 'input'
);
public function action_index()
{
$this->template = new View_Subscription_Index;
$this->template->title = __('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 = __('New subscription');
$this->_edit($this->template->model);
}
public function action_edit()
{
$this->template = new View_Edit;
$this->template->title = __('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 = __('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 = __('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() === HTTP_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();
$model->add('subscription', $subscription);
$instant = ORM::factory('Instant');
$instant->subscription_id = $id;
$instant->subject = __('You were subscribed to ').$subscription->title;
$instant->text = $subscription->welcome;
$instant->create();
$instant->send($model->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', __('You were subscribed. A welcome email has been sent to you. Please check your inbox.'));
}
}
$this->template->model = $model;
}
}

View file

@ -0,0 +1,90 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* Electronic instant letter model. This is a strict one-time letter.
* Stores HTML to be placed in e-mail template.
* @package Models
* @author Oreolek
**/
class Model_Instant extends ORM {
protected $belongs_to = array(
'subscription'
);
/**
* @return array validation rules
**/
public function rules()
{
return array(
'subject' => array(
array('not_empty'),
array('max_length', array(':value', 128)),
),
'text' => array(
array('not_empty'),
array('min_length', array(':value', 20)),
),
'is_draft' => array(
array('numeric')
)
);
}
/**
* Array of field labels.
* Used in forms.
**/
protected $_labels = array(
'text' => 'Message text',
'subject' => 'Message subject',
'is_draft' => 'Is a draft?'
);
/**
* Function to send a email to a specified address.
* Not suitable for a large-scale use.
* @param email $address email address
**/
public function send($address, $token = '')
{
return self::_send($address, $this->text, $this->subject, $token);
}
/**
* @param $address string or array of strings - email addresses
* @param $text message body
* @param $subject message subject
* @param $token user course token
**/
public static function _send($address, $text, $subject, $token = '')
{
Log::instance()->add(Log::NOTICE, __('Sending letter with subject "%subject" to address %address', array('%subject' => $subject, '%address' => $address)));
$sender = Kohana::$config->load('email')->get('sender');
$email = Email::factory($subject, $text)->from($sender[0], $sender[1]);
if (is_array($address))
{
$email->bcc($address);
}
else
{
$email->to($address);
}
return $email->send();
}
/**
* Send the instant to all subscribers if it's not a draft
**/
public function save()
{
if (!$this->is_draft())
{
$subscription = ORM::factory('Subscription', $this->subscription_id);
$subscribers = $subscription->clients->find_all();
foreach ($subscribers as $subscriber)
{
}
}
}
}

View file

@ -0,0 +1,53 @@
<?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(
'client' => array(
'model' => 'Client',
'through' => 'clients_subscriptions'
),
'instants' => array(
'model' => 'Instant'
)
);
/**
* @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',
'welcome' => 'Welcome message',
'description' => 'Description (for the clients)'
);
}

View file

@ -0,0 +1,34 @@
<?php defined('SYSPATH') OR die('No direct script access.');
/**
* Instant index view controller.
* @package Views
* @author Oreolek
**/
class View_Instant_Index extends View_Index {
protected $is_admin = TRUE; // admin only view
public $show_date = FALSE;
public $subscription_id;
/**
* An internal function to prepare item data.
**/
protected function show_item($item)
{
if (!$item instanceof ORM)
{
return FALSE;
}
$output = array(
'description' => $item->text,
'view_link' => $item->subject,//HTML::anchor(Route::url('default', array('controller' => 'Letter', 'action' => 'view','id' => $item->id)), $item->subject, array('class' => 'link_view')),
'edit_link' => HTML::anchor(Route::url('default', array('controller' => 'Instant', 'action' => 'edit','id' => $item->id)), __('Edit'), array('class' => 'link_edit')),
);
return $output;
}
public function link_new()
{
return HTML::anchor(Route::url('default', array('controller' => 'Instant', 'action' => 'create', 'id' => $this->subscription_id)), __('Add'), array('class' => 'link_new'));
}
}

View file

@ -78,8 +78,8 @@ class View_Layout {
{
$navigation = array_merge($navigation, array(
__('Courses') => 'course/index',
'Клиенты' => 'client/index',
'Поиск клиентов' => 'client/search',
__('Subscriptions') => 'subscription/index',
__('Clients') => 'client/index',
));
}

View file

@ -0,0 +1,40 @@
<?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(
__('Title'),
__('Description'),
__('Subscribers'),
__('Edit'),
__('Delete')
);
}
/**
* 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)), __('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')),
'client_count' => $item->count_clients(),
);
return $output;
}
}

View file

@ -57,5 +57,6 @@ return array(
'Edit page' => 'Редактировать страницу',
'Page drafts' => 'Черновики страниц',
'Page index' => 'Список страниц',
'Delete page' => 'Удалить страницу'
'Delete page' => 'Удалить страницу',
'Welcome message' => 'Приветственное сообщение'
);

View file

@ -0,0 +1,17 @@
{{#show_create}}
{{{link_new}}}
{{/show_create}}
<div class="hyphenate">
{{{content}}}
</div>
<table class="table">
{{#get_items}}
<tr>
<td>{{{view_link}}}</td>
<td class="hyphenate">{{{description}}}</td>
{{#edit_link}}
<td>{{{edit_link}}}</td>
{{/edit_link}}
</tr>
{{/get_items}}
</table>

View file

@ -0,0 +1,24 @@
{{#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>

BIN
model.mwb

Binary file not shown.