-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1124d97
commit b12b8d0
Showing
14 changed files
with
726 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.