Skip to content

Commit

Permalink
Added the universe
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverFire committed Jan 4, 2017
1 parent 1124d97 commit b12b8d0
Show file tree
Hide file tree
Showing 14 changed files with 726 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ HiPanel Mailing Module
[![Build Status](https://img.shields.io/travis/hiqdev/hipanel-module-mailing.svg)](https://travis-ci.org/hiqdev/hipanel-module-mailing)
[![Scrutinizer Code Coverage](https://img.shields.io/scrutinizer/coverage/g/hiqdev/hipanel-module-mailing.svg)](https://scrutinizer-ci.com/g/hiqdev/hipanel-module-mailing/)
[![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/hiqdev/hipanel-module-mailing.svg)](https://scrutinizer-ci.com/g/hiqdev/hipanel-module-mailing/)
[![Dependency Status](https://www.versioneye.com/php/hiqdev:hipanel-module-mailing/dev-master/badge.svg)](https://www.versioneye.com/php/hiqdev:hipanel-module-mailing/dev-master)

[![Logo](https://raw.githubusercontent.com/hiqdev/hipanel-core/master/docs/logo.png)](https://hipanel.com/)

Expand Down
18 changes: 18 additions & 0 deletions src/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* Mailing module for HiPanel
*
* @link https://github.com/hiqdev/hipanel-module-mailing
* @package hipanel-module-mailing
* @license BSD-3-Clause
* @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
*/

namespace hipanel\modules\mailing;

/**
* Class Module.
*/
class Module extends \hipanel\base\Module
{
}
29 changes: 23 additions & 6 deletions src/config/hisite.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php

/*
* Finance module for HiPanel
/**
* Mailing module for HiPanel
*
* @link https://github.com/hiqdev/hipanel-module-finance
* @package hipanel-module-finance
* @link https://github.com/hiqdev/hipanel-module-mailing
* @package hipanel-module-mailing
* @license BSD-3-Clause
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
* @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
*/

return [
Expand All @@ -29,5 +28,23 @@
],
],
'container' => [
'definitions' => [
\hiqdev\thememanager\menus\AbstractSidebarMenu::class => [
'add' => [
'client' => [
'menu' => [
'merge' => [
'mailing' => [
'menu' => \hipanel\modules\mailing\menus\SidebarSubMenu::class,
'where' => [
'after' => ['documents'],
],
],
],
],
],
],
],
],
],
];
17 changes: 17 additions & 0 deletions src/config/i18n.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Mailing module for HiPanel
*
* @link https://github.com/hiqdev/hipanel-module-mailing
* @package hipanel-module-mailing
* @license BSD-3-Clause
* @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
*/

return [
'languages' => ['ru'],
'sourcePath' => dirname(__DIR__),
'messagePath' => dirname(__DIR__) . '/messages',
'sort' => true,
'removeUnused' => true,
];
104 changes: 104 additions & 0 deletions src/controllers/PrepareController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* Mailing module for HiPanel
*
* @link https://github.com/hiqdev/hipanel-module-mailing
* @package hipanel-module-mailing
* @license BSD-3-Clause
* @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
*/

namespace hipanel\modules\mailing\controllers;

use hipanel\actions\OrientationAction;
use hipanel\base\CrudController;
use hipanel\modules\mailing\forms\FiltersForm;
use hipanel\modules\mailing\logic\TargetsPreparation;
use hipanel\modules\mailing\renderers\TabularRenderer;
use Yii;
use yii\data\ArrayDataProvider;
use yii\filters\AccessControl;
use yii\web\NotFoundHttpException;

class PrepareController extends CrudController
{
public function behaviors()
{
return array_merge(parent::behaviors(), [
'access' => [
'class' => AccessControl::class,
'rules' => [
[
'allow' => true,
'roles' => ['mailing.prepare'],
],
],
],
]);
}

public function actions()
{
return [
'set-orientation' => [
'class' => OrientationAction::class,
'allowedRoutes' => [
'@mailing/prepare/index',
],
],
];
}

public function actionIndex()
{
$model = new FiltersForm();
$params = [
'model' => $model,
'dataProvider' => new ArrayDataProvider(),
'clientTypes' => $this->getClientTypes(),
'serverTypes' => $this->getServerTypes(),
'serverStates' => $this->getServerStates(),
];

if ($model->load(Yii::$app->request->get()) && $model->validate()) {
/** @var TargetsPreparation $targetsPreparation */
$targetsPreparation = Yii::createObject(TargetsPreparation::class, [$model]);
$params['dataProvider']->allModels = $targetsPreparation->execute();
}

return $this->render('index', $params);
}

public function actionExport()
{
$model = new FiltersForm();

if ($model->load(Yii::$app->request->get(), '') && $model->validate()) {
/** @var TargetsPreparation $targetsPreparation */
$targetsPreparation = Yii::createObject(TargetsPreparation::class, [$model]);
$targets = $targetsPreparation->execute();

/** @var TabularRenderer $renderer */
$renderer = Yii::createObject(TabularRenderer::class, [$targets]);

return Yii::$app->response->sendContentAsFile($renderer->render(), 'emails_' . date('Y-m-d--H-i') . '.txt');
}

throw new NotFoundHttpException('Failed to export the list');
}

private function getClientTypes()
{
return $this->getRefs('type,client');
}

private function getServerTypes()
{
return $this->getRefs('type,server');
}

private function getServerStates()
{
return $this->getRefs('state,server');
}
}
81 changes: 81 additions & 0 deletions src/forms/FiltersForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* Mailing module for HiPanel
*
* @link https://github.com/hiqdev/hipanel-module-mailing
* @package hipanel-module-mailing
* @license BSD-3-Clause
* @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
*/

namespace hipanel\modules\mailing\forms;

use Yii;
use yii\base\Model;

class FiltersForm extends Model
{
public $client_in;

public $client_state_in;

public $seller_in;

public $server_in;

public $has_server;

public $server_state_in;

public $server_type_in;

public $server_switch_like;

public $server_rack_like;

public $server_pdu_like;

public $domain_like;

public $has_domain;

public $exclude_unsubscribed;

public function attributes()
{
return [
'client_in', 'client_state_in',
'seller_in',
'server_in', 'has_server', 'server_state_in', 'server_type_in',
'server_switch_like', 'server_rack_like', 'server_pdu_like',
'domain_like', 'has_domain',
'exclude_unsubscribed',
];
}

public function rules()
{
return [
[$this->attributes(), 'safe'],
];
}

public function attributeLabels()
{
return [
'seller_in' => Yii::t('hipanel:mailing', 'Sellers'),
'client_in' => Yii::t('hipanel:mailing', 'Clients'),
'client_state_in' => Yii::t('hipanel:mailing', 'Client state'),
'has_server' => Yii::t('hipanel:mailing', 'Has server'),
'server_in' => Yii::t('hipanel:mailing', 'Servers'),
'server_state_in' => Yii::t('hipanel:mailing', 'State'),
'server_type_in' => Yii::t('hipanel:mailing', 'Type'),
'server_switch_like' => Yii::t('hipanel:mailing', 'Switch'),
'server_rack_like' => Yii::t('hipanel:mailing', 'Rack'),
'server_pdu_like' => Yii::t('hipanel:mailing', 'PDU'),
'has_domain' => Yii::t('hipanel:mailing', 'Has domain'),
'domain_like' => Yii::t('hipanel:mailing', 'Domain'),
'exclude_unsubscribed' => Yii::t('hipanel:mailing', 'Exclude unsubscribed'),
];
}
}
58 changes: 58 additions & 0 deletions src/grid/TargetsGridView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Mailing module for HiPanel
*
* @link https://github.com/hiqdev/hipanel-module-mailing
* @package hipanel-module-mailing
* @license BSD-3-Clause
* @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
*/

namespace hipanel\modules\mailing\grid;

use yii\base\InvalidConfigException;

class TargetsGridView extends \hipanel\grid\BoxedGridView
{
/**
* @var array possible columns. Each column will be displayed in case when it is filled at least for one of models
*/
public $possibleColumns;

public static function defaultColumns()
{
return [
'seller' => [
'attribute' => 'seller',
],
'client' => [
'attribute' => 'client',
],
];
}

public function init()
{
$this->ensureActiveColumns();

parent::init();
}

public function ensureActiveColumns()
{
$dataProvider = $this->dataProvider;

foreach ($this->possibleColumns as $possibleColumn) {
if (is_array($possibleColumn)) {
throw new InvalidConfigException('"possibleColumns" parameter does not support array configuration yet.');
}

foreach ($dataProvider->getModels() as $model) {
if ($model->$possibleColumn !== null) {
$this->columns[] = $possibleColumn;
continue 2;
}
}
}
}
}
66 changes: 66 additions & 0 deletions src/logic/TargetsPreparation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Mailing module for HiPanel
*
* @link https://github.com/hiqdev/hipanel-module-mailing
* @package hipanel-module-mailing
* @license BSD-3-Clause
* @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
*/

namespace hipanel\modules\mailing\logic;

use hipanel\components\ApiConnectionInterface;
use hipanel\modules\mailing\forms\FiltersForm;
use hipanel\modules\mailing\models\Target;
use hiqdev\hiart\ErrorResponseException;

class TargetsPreparation
{
/**
* @var FiltersForm
*/
private $filtersForm;
/**
* @var ApiConnectionInterface
*/
private $api;
/**
* @var
*/
private $targetClass;

public function __construct(FiltersForm $filtersForm, ApiConnectionInterface $api, $targetClass = Target::class)
{
$this->filtersForm = $filtersForm;
$this->api = $api;
$this->targetClass = $targetClass;
}

public function execute()
{
try {
$data = $this->api->post('mailingPrepare', $this->getFilters());

return $this->createTargets($data);
} catch (ErrorResponseException $e) {
throw $e;
}
}

protected function createTargets($data)
{
$result = [];

foreach ($data as $item) {
$result[] = new $this->targetClass($item);
}

return $result;
}

protected function getFilters()
{
return array_filter($this->filtersForm->getAttributes());
}
}
Loading

0 comments on commit b12b8d0

Please sign in to comment.