mirror of
https://github.com/Oreolek/kangana.git
synced 2024-05-05 02:28:17 +03:00
WIP combining (issue #6)
This commit is contained in:
parent
3e14176c1f
commit
346608e284
|
@ -137,7 +137,7 @@ class Controller_Course extends Controller_Layout {
|
|||
{
|
||||
$this->redirect('error/404');
|
||||
}
|
||||
$this->template->title = $course->title;
|
||||
$this->template->title = I18n::translate('Subscribe to ') . $course->title;
|
||||
$controls = array(
|
||||
'name' => 'input',
|
||||
'email' => 'input'
|
||||
|
@ -160,15 +160,25 @@ class Controller_Course extends Controller_Layout {
|
|||
{
|
||||
$model->add('course', $course);
|
||||
}
|
||||
$task = ORM::factory('Task');
|
||||
$letter = $course->next_letter();
|
||||
$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);
|
||||
if ($course->type = Course::TYPE_SCHEDULED)
|
||||
{
|
||||
$task = ORM::factory('Task');
|
||||
$letter = $course->next_letter();
|
||||
$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);
|
||||
} else {
|
||||
$instant = ORM::factory('Letter');
|
||||
$instant->course_id = $id;
|
||||
$instant->subject = I18n::translate('You were subscribed to ').$course->title;
|
||||
$instant->text = I18n::translate('From now on you will receive letters from this subscription.');
|
||||
$instant->send($course->email, $course->token);
|
||||
// instant is not saved because it's just a welcome email
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -7,6 +7,16 @@
|
|||
* @author Oreolek
|
||||
**/
|
||||
class Model_Course extends ORM {
|
||||
/**
|
||||
* A pre-scheduled course - the subscriber gets a new letter once per N days in the series.
|
||||
*/
|
||||
public const TYPE_SCHEDULED = 0;
|
||||
|
||||
/**
|
||||
* A irregular subscription - the letters are sent when the author sends them
|
||||
*/
|
||||
public const TYPE_IRREGULAR = 1;
|
||||
|
||||
protected $_has_many = array(
|
||||
'clients' => array(
|
||||
'model' => 'Client',
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
/**
|
||||
* model combining
|
||||
*/
|
||||
class Migration_Kangana_20161016123906 extends Minion_Migration_Base {
|
||||
|
||||
/**
|
||||
* Run queries needed to apply this migration
|
||||
*
|
||||
* @param Kohana_Database $db Database connection
|
||||
*/
|
||||
public function up(Kohana_Database $db)
|
||||
{
|
||||
echo 'Opening the transaction.'.PHP_EOL;
|
||||
$db->begin();
|
||||
try
|
||||
{
|
||||
echo 'Altering tables.'.PHP_EOL;
|
||||
$db->query(NULL, 'ALTER TABLE `letters` ADD COLUMN `timestamp` timestamp NULL DEFAULT CURRENT_TIMESTAMP');
|
||||
$db->query(NULL, "alter table `letters` add column `sent` int(1) null default '0'");
|
||||
$db->query(NULL, "alter table `letters` add column `is_draft` int(1) not null default '0'");
|
||||
$db->query(NULL, "alter table `courses` add column `type` int(2) not null default '0'");
|
||||
echo "Copying Subscription model to Courses." . PHP_EOL;
|
||||
$subscriptions = DB::select()->from('subscriptions')->as_object()->execute();
|
||||
foreach ($subscriptions as $subscription)
|
||||
{
|
||||
$course = ORM::factory('Course');
|
||||
$course->type = Course::TYPE_IRREGULAR;
|
||||
$course->title = $subscription->title;
|
||||
$course->description = $subscription->description;
|
||||
$course->price = $subscription->price;
|
||||
$instants = $subscription->instants->find_all();
|
||||
foreach ($instants as $instant)
|
||||
{
|
||||
echo 'Migrating a letter:' . $instant->subject . PHP_EOL;
|
||||
$query = DB::query(
|
||||
Database::INSERT,
|
||||
'INSERT INTO letters (course_id, subject, text, timestamp, sent)
|
||||
VALUES(:course_id, :subject, :text, :timestamp, :sent)'
|
||||
);
|
||||
|
||||
$query->parameters(array(
|
||||
':course_id' => $course->id,
|
||||
':subject' => $instant->subject,
|
||||
':text' => $instant->text,
|
||||
':timestamp' => $instant->timestamp,
|
||||
':sent' => $instant->sent,
|
||||
));
|
||||
}
|
||||
}
|
||||
$db->commit();
|
||||
}
|
||||
catch (Database_Exception $e)
|
||||
{
|
||||
$db->rollback();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run queries needed to remove this migration
|
||||
*
|
||||
* @param Kohana_Database $db Database connection
|
||||
*/
|
||||
public function down(Kohana_Database $db)
|
||||
{
|
||||
echo 'An automatic rollback for this migration back is not implemented.';
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue