Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix somethings #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,32 @@

## Installation ##

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist yarcode/yii2-email-manager "~0.2.*"
```

or add

```
"yarcode/yii2-email-manager": "~0.2.*"
```

to the require section of your `composer.json` file.

## Migration ##

Migration run

```php
php yii migrate --migrationPath=@vendor/yarcode/yii2-email-manager/src/migrations
```

## Configuration ##

Simple configuration:

'components' => [
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"require": {
"php": ">=5.4.0",
"yiisoft/yii2": "~2.0.4",
"react/event-loop": "~0.4",
"mailgun/mailgun-php": "^2.0",
"yiisoft/yii2-twig": "~2.0.3",
"trntv/yii2-aceeditor": "~2.0",
Expand Down
4 changes: 2 additions & 2 deletions src/EmailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use yii\base\Component;
use yii\base\InvalidConfigException;
use yarcode\email\interfaces\TransportInterface;
use yarcode\email\models\Message;
use yarcode\email\models\EmailMessage;

class EmailManager extends Component implements TransportInterface
{
Expand Down Expand Up @@ -73,7 +73,7 @@ public function queue($from, $to, $subject, $text, $priority = 0, $files = [], $
$bcc = implode(', ', $bcc);
}

$model = new Message();
$model = new EmailMessage();
$model->from = $from;
$model->to = $to;
$model->subject = $subject;
Expand Down
12 changes: 6 additions & 6 deletions src/backend/controllers/TemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use yii\filters\VerbFilter;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yarcode\email\models\Template;
use yarcode\email\models\EmailTemplate;

/**
* @author Alexey Samoylov <[email protected]>
Expand Down Expand Up @@ -40,7 +40,7 @@ public function behaviors()
*/
public function actionTest($shortcut, $language)
{
Template::loadTemplate($shortcut, $language)->queue(\Yii::$app->params['adminEmail']);
EmailTemplate::loadTemplate($shortcut, $language)->queue(\Yii::$app->params['adminEmail']);

return $this->redirect(['index']);
}
Expand All @@ -52,7 +52,7 @@ public function actionTest($shortcut, $language)
public function actionIndex()
{
$dataProvider = new ActiveDataProvider([
'query' => Template::find(),
'query' => EmailTemplate::find(),
]);

return $this->render('index', [
Expand All @@ -79,7 +79,7 @@ public function actionView($id)
*/
public function actionCreate()
{
$model = new Template();
$model = new EmailTemplate();

if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['index', 'id' => $model->id]);
Expand Down Expand Up @@ -126,12 +126,12 @@ public function actionDelete($id)
* Finds the Template model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Template the loaded model
* @return EmailTemplate the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Template::findOne($id)) !== null) {
if (($model = EmailTemplate::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
Expand Down
2 changes: 1 addition & 1 deletion src/backend/views/template/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/**
* @var yii\web\View $this
* @var yarcode\email\models\Template $model
* @var yarcode\email\models\EmailTemplate $model
* @var yii\widgets\ActiveForm $form
*/
?>
Expand Down
2 changes: 1 addition & 1 deletion src/backend/views/template/create.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* @var yii\web\View $this
* @var yarcode\email\models\Template $model
* @var yarcode\email\models\EmailTemplate $model
*/

$this->title = 'Create Email Template';
Expand Down
2 changes: 1 addition & 1 deletion src/backend/views/template/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* @var yii\web\View $this
* @var yarcode\email\models\Template $model
* @var yarcode\email\models\EmailTemplate $model
*/

$this->title = 'Update Email Template: ' . ' ' . $model->id;
Expand Down
2 changes: 1 addition & 1 deletion src/backend/views/template/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/**
* @var yii\web\View $this
* @var yarcode\email\models\Template $model
* @var yarcode\email\models\EmailTemplate $model
*/

$this->title = $model->subject;
Expand Down
22 changes: 11 additions & 11 deletions src/commands/EmailCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use yii\console\Controller;
use yii\db\Expression;
use yarcode\email\EmailManager;
use yarcode\email\models\Message;
use yarcode\email\models\EmailMessage;

/**
* @author Alexey Samoylov <[email protected]>
Expand Down Expand Up @@ -81,23 +81,23 @@ public function sendOne() {

$transaction = $db->beginTransaction();
try {
$id = $db->createCommand('SELECT id FROM {{%email_message}} WHERE status=:status ORDER BY priority DESC, id ASC LIMIT 1 FOR UPDATE', [
'status' => Message::STATUS_NEW,
$id = $db->createCommand('SELECT id FROM {{%email_EmailMessage}} WHERE status=:status ORDER BY priority DESC, id ASC LIMIT 1 FOR UPDATE', [
'status' => EmailMessage::STATUS_NEW,
])->queryScalar();

if ($id === false) {
$transaction->rollback();
$transaction->rollBack();
return false;
}

/** @var Message $model */
$model = Message::findOne($id);
$model->status = Message::STATUS_IN_PROGRESS;
/** @var EmailMessage $model */
$model = EmailMessage::findOne($id);
$model->status = EmailMessage::STATUS_IN_PROGRESS;
$model->updateAttributes(['status']);

$transaction->commit();
} catch (\Exception $e) {
$transaction->rollback();
$transaction->rollBack();
throw $e;
}

Expand All @@ -114,15 +114,15 @@ public function sendOne() {
);
if ($result) {
$model->sentAt = new Expression('NOW()');
$model->status = Message::STATUS_SENT;
$model->status = EmailMessage::STATUS_SENT;
} else {
$model->status = Message::STATUS_ERROR;
$model->status = EmailMessage::STATUS_ERROR;
}

$model->updateAttributes(['sentAt', 'status']);
$transaction->commit();
} catch (\Exception $e) {
$transaction->rollback();
$transaction->rollBack();
throw $e;
}

Expand Down
3 changes: 1 addition & 2 deletions src/models/Message.php → src/models/EmailMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace yarcode\email\models;

use baibaratsky\yii\behaviors\model\SerializedAttributes;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\db\Expression;
Expand All @@ -26,7 +25,7 @@
* @property string $bcc
* @property string $files
*/
class Message extends ActiveRecord
class EmailMessage extends ActiveRecord
{
const STATUS_NEW = 0;
const STATUS_IN_PROGRESS = 1;
Expand Down
11 changes: 5 additions & 6 deletions src/models/Template.php → src/models/EmailTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use yarcode\email\EmailManager;
use yarcode\email\twig\EmailTemplateLoader;
use Yii;
use yii\db\ActiveRecord;
use yii\helpers\VarDumper;

Expand All @@ -21,7 +20,7 @@
* @property string $text
* @property string $language
*/
class Template extends ActiveRecord
class EmailTemplate extends ActiveRecord
{
public $params = [];

Expand All @@ -36,24 +35,24 @@ public static function tableName()
/**
* @param string $shortcut
* @param string $language
* @return Template
* @return self
*/
public static function findByShortcut($shortcut, $language = null)
{
$manager = EmailManager::getInstance();

$languageCode = $language;
foreach ([$language, $manager->defaultLanguage, 'en-US'] as $l) {
$template = static::findOne([
'shortcut' => $shortcut,
'language' => $l,
]);

$languageCode = $l;
if ($template) {
return $template;
}
}

throw new \BadMethodCallException('Template not found: ' . VarDumper::dumpAsString($shortcut) . ', language ' . $l);
throw new \BadMethodCallException('Template not found: ' . VarDumper::dumpAsString($shortcut) . ', language ' . $languageCode);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/twig/EmailTemplateLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace yarcode\email\twig;

use yarcode\email\EmailManager;
use yarcode\email\models\Template;
use yarcode\email\models\EmailTemplate;
use yii\base\Component;
use yii\helpers\ArrayHelper;

Expand All @@ -21,8 +21,8 @@ public function getSource($name)
$currentLanguage = \Yii::$app->language;
$defaultLanguage = ArrayHelper::getValue(EmailManager::getInstance()->languages, 0, 'en-US');

/** @var Template $model */
$model = Template::find()->where(['shortcut' => $name])
/** @var EmailTemplate $model */
$model = EmailTemplate::find()->where(['shortcut' => $name])
->andWhere('language = :currentLanguage OR language = :defaultLanguage OR language = :systemDefaultLanguage', [
':currentLanguage' => $currentLanguage,
':defaultLanguage' => $defaultLanguage,
Expand Down