Commit 48537583 authored by Marcello Pivanti's avatar Marcello Pivanti
Browse files

Initial Commit

parents
{
"name": "arter/amos-email-manager",
"description": "Modulo di gestione code email - plugin",
"keywords": [
"amos",
"notifiche",
"yii2"
],
"homepage": "http://www.elitedivision.it/",
"type": "component",
"license": "BSD-3-Clause",
"support": {
"issues": "https://github.com/yiisoft/yii2/issues?state=open",
"forum": "http://www.yiiframework.com/forum/",
"wiki": "http://www.yiiframework.com/wiki/",
"irc": "irc://irc.freenode.net/yii",
"source": "https://github.com/yiisoft/yii2"
},
"require": {
"php": ">=5.4.0",
"arter/amos-core": "~1.7",
"arter/amos-dashboard": ">=1.8.15",
"mustache/mustache": "^2.9.0",
"baibaratsky/yii2-serialized-attributes-behavior": "*",
"yiidoc/yii2-redactor": "*"
},
"config": {
"process-timeout": 1800,
"secure-http": false
},
"autoload": {
"psr-4": {
"elitedivision\\amos\\emailmanager\\": "src"
}
},
"scripts": {
"post-install-cmd": "php yii migrate --migrationPath=@vendor/elitedivision/amos-email-manager/migrations --interactive=0"
}
}
\ No newline at end of file
<?php
/**
* Art-ER Attrattività, ricerca e territorio dell’Emilia-Romagna
* OPEN 2.0
*
*
* @package elitedivision\amos\email
* @category CategoryName
* @author Elite Division S.r.l.
*/
namespace elitedivision\amos\emailmanager;
use elitedivision\amos\core\module\AmosModule;
use elitedivision\amos\emailmanager\base\AmosEmailManager;
use elitedivision\amos\emailmanager\models\EmailView;
use yii\base\BootstrapInterface;
use yii\console\Application;
use yii\log\Logger;
use Yii;
use yii\base\Exception;
class AmosEmail extends AmosModule implements BootstrapInterface
{
public static $moduleId = 'email';
public $name = 'Email manager';
/**
* @var string Template type, can be "db" or "php".
*/
public $templateType = "php";
public $templatePath = "/emails/";
/**
* @var string The default layout to use for template emails.
*/
public $defaultTemplate = "layout_default";
public $defaultLayout = "layout_fancy";
public $controllerNamespace = 'elitedivision\amos\emailmanager\controllers';
private $emailManager = null;
/**
*
* @return string
*/
public static function getModuleName()
{
return self::$moduleId;
}
/**
*
*/
public function init()
{
parent::init();
\Yii::setAlias('@email', __DIR__ . '/');
\Yii::setAlias('@elitedivision/amos/emailmanager/commands', __DIR__ . '/commands/');
// initialize the module with the configuration loaded from config.php
\Yii::configure($this, require(__DIR__ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config.php'));
if (Yii::$app instanceof \yii\web\Application) {
$this->controllerMap = array(
'spool' => 'elitedivision\amos\emailmanager\controllers\EmailSpoolController',
'template' => 'elitedivision\amos\emailmanager\controllers\EmailTemplateController',
);
}
$this->initializeEmailManager();
}
/**
*
*/
private function initializeEmailManager()
{
$this->emailManager = new AmosEmailManager();
$this->emailManager->defaultLayout = $this->defaultLayout;
$this->emailManager->defaultTemplate = $this->defaultTemplate;
$this->emailManager->templateType = $this->templateType;
$this->emailManager->templatePath = $this->templatePath;
}
/**
*
* @param Application $app
*/
public function bootstrap($app)
{
if ($app instanceof Application) {
$this->controllerNamespace = 'elitedivision\amos\emailmanager\commands';
}
}
/**
*
* @see
*/
protected function getDefaultModels()
{
return [
'EmailSpool' => __NAMESPACE__ . '\\' . 'models\EmailSpool',
'EmailTemplate' => __NAMESPACE__ . '\\' . 'models\EmailTemplate',
];
}
/**
*
* @see
*/
public function getWidgetGraphics()
{
return [];
}
/**
*
* @see
*/
public function getWidgetIcons()
{
return [];
}
/**
* Sends email message queue using default transport
*
* @param string $from format accepted:
*
* 1) 'example@example.com'
* 2) 'example@example.com alias' the method considers the email address up to the first space, everything that follows is considered alias.
*
* @param string $to
* @param string $subject
* @param string $text
* @param integer $priority
* @param array $files
* @param array $bcc
*/
public function queue($from, $to, $subject, $text, $files = [], $bcc = [], $params = [], $priority = 0)
{
$retValue = false;
try {
if ($this->emailManager) {
$this->emailManager->defaultLayout = $this->defaultLayout;
$this->emailManager->defaultTemplate = $this->defaultTemplate;
$retValue = $this->emailManager->queue($from, $to, $subject, $text, $files, $bcc, $params, $priority);
}
} catch (Exception $ex) {
Yii::getLogger()->log($ex->getMessage(), Logger::LEVEL_ERROR);
}
return $retValue;
}
/**
* Sends email message immediately using default transport
*
* @param string $from format accepted:
*
* 1) 'example@example.com'
* 2) 'example@example.com alias' the method considers the email address up to the first space, everything that follows is considered alias.
*
* @param string $to
* @param string $subject
* @param string $text
* @param array $files
* @param array $bcc
* @return bool
*/
public function send($from, $to, $subject, $text, $files = [], $bcc = [], $params = [], $save_in_queue = true, $cc = [], $replyTo = [])
{
$retValue = false;
try {
if ($this->emailManager) {
$this->emailManager->defaultLayout = $this->defaultLayout;
$this->emailManager->defaultTemplate = $this->defaultTemplate;
$retValue = $this->emailManager->send($from, $to, $subject, $text, $files, $bcc,$params,$save_in_queue, $cc, $replyTo);
}
} catch (Exception $ex)
{
Yii::getLogger()->log($ex->getMessage(), Logger::LEVEL_ERROR);
}
return $retValue;
}
/**
*
* @param integer $loopLimit
* @return integer
*/
public function spool($loopLimit = 1000)
{
$retValue = 0;
try {
if ($this->emailManager) {
$retValue = $this->emailManager->spool($loopLimit);
}
} catch (Exception $ex) {
Yii::getLogger()->log($ex->getMessage(), Logger::LEVEL_ERROR);
}
return $retValue;
}
public function render($module, $view, $params = [])
{
$emailView = EmailView::find()->andWhere(['module' => $module, 'view' => $view])->one();
if (!$emailView || !$emailView->id) {
$emailView = new EmailView();
$emailView->module = $module;
$emailView->content = "{original}";
$emailView->view = $view;
$emailView->save(false);
}
$paramsMap = [];
foreach ($params as $name => $param) {
$type = gettype($param);
switch ($type) {
case 'boolean':
case 'integer':
case 'double':
case 'string':
{
$paramsMap[$name] = $type;
}
break;
case 'array':
{
$paramsMap[$name] = array_keys($param);
}
break;
case 'object':
{
$paramsMap[$name] = get_class($param);
}
break;
}
}
$emailView->params = json_encode($paramsMap);
$emailView->save(false);
$message = $emailView->content;
$placeholders = [];
foreach ((array) $params as $name => $value) {
$placeholders['{' . $name . '}'] = $value;
}
return ($placeholders === []) ? $message : strtr($message, $placeholders);
}
}
# amos-email-manager
Extension to manage emails (templates, spool,..)
## Installation
### 1. Add module to your application
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Either run
```bash
composer require elitedivision/amos-email-manager
```
or add this row
```
"elitedivision/amos-email-manager": "dev-master"
```
to the require section of your `composer.json` file.
### 2. Add module configuration
Add module to your main config in common like this:
```php
<?php
'modules' => [
'email' => [
'class' => 'elitedivision\amos\emailmanager\AmosEmail',
'templatePath' => '@common/mail/emails',
],
],
```
### 3. Apply migrations
To apply migrations you can launch this command:
```bash
php yii migrate/up --migrationPath=@vendor/elitedivision/amos-email-manager/src/migrations
```
or add this row to your migrations config in console:
```php
<?php
return [
'@vendor/elitedivision/amos-email-manager/src/migrations',
];
```
<?php
/**
* Art-ER Attrattività, ricerca e territorio dell’Emilia-Romagna
* OPEN 2.0
*
*
* @package elitedivision\amos\email
* @category CategoryName
* @author Elite Division S.r.l.
*/
namespace elitedivision\amos\emailmanager\assets;
use Yii;
use yii\web\AssetBundle;
/**
* Class AmosMailAsset
* @package elitedivision\amos\emailmanager\assets
*/
class AmosMailAsset extends AssetBundle
{
public $sourcePath = '@vendor/elitedivision/amos-email-manager/src/assets/web';
public $css = [
'css/less/amos_mail.less'
];
public $depends = [
];
public function init()
{
$moduleL = Yii::$app->getModule('layout');
if (!empty($moduleL)) {
$this->depends [] = 'elitedivision\amos\layout\assets\BaseAsset';
} else {
$this->depends [] = 'elitedivision\amos\core\views\assets\AmosCoreAsset';
}
parent::init(); // TODO: Change the autogenerated stub
}
}
\ No newline at end of file
@import (optional) "../../vendor/elitedivision/amos-layout/src/assets/resources/base/less/variables.less";
@import (optional) "../../vendor/elitedivision/amos-core/views/assets/web/css/less/variables.less";
.section-data {
.mail_message_info {
width: 70%;
float: left;
p {
padding: 0 !important;
}
}
}
\ No newline at end of file
@import "../../vendor/elitedivision/amos-layout/src/assets/resources/base/less/variables.less";
@import "variables-email-manager.less";
.section-data {
.mail_message_info {
width: 70%;
float: left;
p {
padding: 0 !important;
}
}
}
\ No newline at end of file
<?php
/**
* Art-ER Attrattività, ricerca e territorio dell’Emilia-Romagna
* OPEN 2.0
*
*
* @package elitedivision\amos\email
* @category CategoryName
* @author Elite Division S.r.l.
*/
namespace elitedivision\amos\emailmanager\base;
use elitedivision\amos\emailmanager\interfaces\ManagerInterface;
use elitedivision\amos\emailmanager\interfaces\TransportInterface;
use elitedivision\amos\emailmanager\models\EmailSpool;
use elitedivision\amos\emailmanager\models\EmailTemplate;
use elitedivision\amos\emailmanager\models\File;
use elitedivision\amos\emailmanager\transports\YiiMailer;
use Mustache_Engine;
use Yii;
use yii\base\Exception;
use yii\helpers\FileHelper;
use yii\helpers\ArrayHelper;
use yii\log\Logger;
class AmosEmailManager implements ManagerInterface
{
const PENDING = 'pending';
const PROCESSING = 'processing';
const ERROR = 'error';
const EMAILED = 'emailed';
/**
* @var string Template type, can be "db" or "php".
*/
public $templateType = "php";
public $templatePath = "/emails/";
/**
* @var string The default layout to use for template emails.
*/
public $defaultTemplate = "layout_default";
public $defaultLayout = "layout_fancy";
/**
* @var array List of template parts that will be rendered.
*/
public $templateFields = array('subject', 'heading', 'message');
/**
* @var string The default transport to use.
*/
public $defaultTransport = 'mail';
/** @var TransportInterface[] */
public $transports = [];
private $engine;
public function __construct()
{
$this->transports[$this->defaultTransport] = new YiiMailer();
$options = [
'charset' => \Yii::$app->charset,
'entity_flags' => ENT_QUOTES | ENT_SUBSTITUTE,
'strict_callables' => true
];
$this->engine = new Mustache_Engine($options);
}
/**
*
* @return type
*/
public function getDefaultLayout()
{
return $this->defaultLayout;
}
/**
*
* @return type
*/
public function getDefaultTemplate()
{
return $this->defaultTemplate;
}
/**
*
* @return type
*/
public function getTemplatePath()
{
return $this->templatePath;
}
/**
*
* @return type
*/
public function getTemplateType()
{
return $this->templateType;
}
/**
*
* @param type $layout
*/
public function setDefaultLayout($layout)
{
$this->defaultLayout = $layout;
}
/**
*
* @param type $tamplate
*/
public function setDefaultTemplate($tamplate)
{
$this->defaultTemplate = $tamplate;
}
/**
*
* @param type $path
*/
public function setTemplatePath($path)
{
$this->templatePath = $path;
}
/**
*
* @param type $templateType
*/
public function setTemplateType($templateType)
{
$this->templateType = $templateType;
}
/**
*
* @param type $template
* @param type $viewParams
* @param type $layout
* @return type
*/
public function buildTemplateMessage($template, $viewParams = array(), $layout = null)
{
if ($layout === null) {
$layout = $this->defaultTemplate;
}
$method = 'buildTemplateMessage_' . $this->templateType;
if (!method_exists($this, $method)) {
$this->templateType = 'php';
}
return call_user_func_array(array($this, $method), array($template, $viewParams, $layout));
}
/**
* @param $template string
* @param $viewParams array
* @param string $layout
* @return array
*/
private function buildTemplateMessage_php($template, $viewParams = array(), $layout = null)
{
$message = array();
try {
$controller = Yii::$app->controller;
foreach ($this->templateFields as $field) {
$viewParams['contents'] = $controller->renderPartial($this->templatePath . DIRECTORY_SEPARATOR . $template . DIRECTORY_SEPARATOR . $field, $viewParams);
if (!$layout) {
$viewParams[$field] = $message[$field] = $viewParams['contents'];
} else {
$viewParams[$field] = $message[$field] = $controller->renderPartial($this->templatePath . DIRECTORY_SEPARATOR . $layout . DIRECTORY_SEPARATOR . $field, $viewParams);
}
unset($viewParams['contents']);
}
} catch (Exception $bex) {
Yii::getLogger()->log($bex->getMessage(), Logger::LEVEL_ERROR);
}
return $message;
}
/**
*
* @param type $template
* @param type $viewParams
* @param type $layout