add newsletter

This commit is contained in:
2016-05-20 08:16:30 +02:00
parent 0b866917d5
commit e51f4a5934
27 changed files with 1120 additions and 16 deletions

View File

@@ -11,6 +11,9 @@ use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\base\Object;
use backend\models\CustomerUpdate;
use backend\models\CustomerNewsLetterModel;
use yii\db\Query;
use yii\data\ActiveDataProvider;
/**
* CustomerController implements the CRUD actions for Customer model.
@@ -18,6 +21,27 @@ use backend\models\CustomerUpdate;
class CustomerController extends \backend\controllers\BackendController
{
public function behaviors()
{
return [
'access' => [
'class' => \yii\filters\AccessControl::className(),
'rules' => [
// allow authenticated users
[
'actions' => ['create','index','view','update','mail'],
'allow' => true,
'roles' => ['admin','employee','reception'],
],
// everything else is denied
],
],
];
}
/**
* Lists all Customer models.
* @return mixed
@@ -119,4 +143,36 @@ class CustomerController extends \backend\controllers\BackendController
throw new NotFoundHttpException('The requested page does not exist.');
}
}
public function actionMail(){
$model = new CustomerNewsLetterModel();
$query = new Query();
$query->distinct();
$query->select([ 'email']);
$query->from("customer");
$query->andWhere(['newsletter' => 1]);
$query->andWhere(['status' => Customer::STATUS_ACTIVE]);
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$message = \Yii::$app->mailer->compose ( );
$message
->setFrom ( "noreply@fitnessadmin.hu" )
->setBcc(['rocho02@gmail.com',"rocho02@freemail.hu"])
//->setTo(['rocho02@gmail.com'])
->setHtmlBody($model->text )
->setSubject ( $model->subject )
->send ();
return $this->redirect(['customer/mail']);
}
return $this->render('mail', [ 'model' => $model ]);
}
}

View File

@@ -0,0 +1,146 @@
<?php
namespace backend\controllers;
use Yii;
use common\models\Newsletter;
use backend\models\NewsletterSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\models\Customer;
use backend\models\NewsletterTestForm;
use common\components\Helper;
/**
* NewsletterController implements the CRUD actions for Newsletter model.
*/
class NewsletterController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
/**
* Lists all Newsletter models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new NewsletterSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Newsletter model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
$model = $this->findModel($id);
$testModel = new NewsletterTestForm(
[
'newsletter' => $model
]
);
if ($testModel->load(Yii::$app->request->post()) && $testModel->validate()) {
$testModel->sendEmail();
Helper::flash("success", "Teszt e-mail elküldve");
return $this->redirect(['view', 'id' => $model->id_newsletter]);
}
return $this->render('view', [
'model' => $model,
'testModel' => $testModel
]);
}
/**
* Creates a new Newsletter model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Newsletter();
$model->status = Newsletter::$STATUS_ACTIVE;
$model->sent = Newsletter::$SENT_NOT;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id_newsletter]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Newsletter model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id_newsletter]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Newsletter model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$model = $this->findModel($id);
$model->status = Newsletter::$STATUS_DELETED;
$model->save(false);
return $this->redirect(['index']);
}
/**
* Finds the Newsletter model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Newsletter the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Newsletter::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace backend\models;
use Yii;
use yii\base\Model;
use yii\web\UploadedFile;
/**
* ContactForm is the model behind the contact form.
* @property \Yii\web\UploadedFile $file
*/
class CustomerNewsLetterModel extends Model{
public $text;
public $subject;
public $allCustomer;
public function rules(){
return [
[['text','subject'],'required'],
['text','string'],
['subject','string'],
];
}
}

View File

@@ -0,0 +1,75 @@
<?php
namespace backend\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Newsletter;
/**
* NewsletterSearch represents the model behind the search form about `common\models\Newsletter`.
*/
class NewsletterSearch extends Newsletter
{
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id_newsletter', 'status', 'sent'], 'integer'],
[['subject', 'body', 'sent_at', 'created_at', 'updated_at'], 'safe'],
];
}
/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = Newsletter::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' =>[
'defaultOrder' =>[ 'created_at' => SORT_DESC ]
]
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
'id_newsletter' => $this->id_newsletter,
'status' => $this->status,
'sent' => $this->sent,
'sent_at' => $this->sent_at,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
]);
$query->andFilterWhere(['like', 'subject', $this->subject])
->andFilterWhere(['like', 'body', $this->body]);
return $dataProvider;
}
}

View File

@@ -0,0 +1,75 @@
<?php
namespace backend\models;
use Yii;
use yii\base\Model;
use common\models\Card;
use common\models\Customer;
use common\models\Ticket;
use common\models\Account;
use yii\web\UploadedFile;
use common\components\Helper;
/**
* ContactForm is the model behind the contact form.
* @property \Yii\web\UploadedFile $file
*/
class NewsletterTestForm extends Model{
public $customer_name;
public $email;
public $newsletter;//init param
private $_customer;
public function rules(){
return [
[['customer_name','email'], 'required'],
[['email'], 'email'],
[['customer_name'], 'string'],
[['customer_name'], 'validateCustomer'],
];
}
public function attributeLabels(){
return [
'email' => "E-mail",
'customer_name' => "Vendég neve"
];
}
public function validateCustomer( $attribute, $params ) {
$customer = Customer::find()->andWhere(['like','name',$this->customer_name])->one();
if ( !isset($customer)){
$this->addError($attribute,"Vendég nem található");
}else{
$this->_customer = $customer;
}
}
public function sendEmail(){
$newsletter = $this->newsletter;
$message = \Yii::$app->mailer->compose ( );
$replacePairs = [
'{vendeg_neve}' => $this->_customer->name
];
$mailBody = $newsletter->body;
$mailBody = strtr($mailBody, $replacePairs );
$mailSubject = $newsletter->subject;
$mailSubject = strtr($mailSubject, $replacePairs );
$message
->setFrom ( [ \Yii::$app->params['newsletter_from'] => Helper::getCompanyName() ])
->setTo ( [
$this->email ])
->setHtmlBody( $mailBody )
->setSubject ( $mailSubject )
->send ();
}
}

View File

@@ -0,0 +1,63 @@
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use common\models\Customer;
use common\components\CityNameTypeahead;
use common\components\CityZipTypeahead;
use common\components\CardNumberTypeahead;
use kartik\widgets\DatePicker;
use dosamigos\tinymce\TinyMce;
use yii\grid\GridView;
use yii\base\Widget;
/* @var $this yii\web\View */
/* @var $model common\models\Customer */
/* @var $form yii\widgets\ActiveForm */
?>
<h1>Hírlevél küldése</h1>
<p>
Hírlevél küldése minden vendégnek, aki
</p>
<ul>
<li>
Nincs törölve
</li>
<li>
be van állítva a kér hírlevelet jelölőnégyzet a vendég adatlapján
</li>
</ul>
<div class="customer-form">
<?php $form = ActiveForm::begin(); ?>
<?php echo $form->field($model,'subject')?>
<?= $form->field($model, 'text')->widget(TinyMce::className(), [
'options' => ['rows' => 12],
// 'language' => 'en',
'clientOptions' => [
'plugins' => [
"advlist autolink lists link charmap print preview anchor",
"searchreplace visualblocks code fullscreen",
"insertdatetime media table contextmenu paste"
],
'toolbar' => "undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
]
]);?>
<?php // echo $form->field($model, 'address')->textInput(['maxlength' => true]) ?>
<div class="form-group">
<?= Html::submitButton( Yii::t('common/customer', 'Küldés') , ['class' => 'btn btn-success' ]) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

View File

@@ -0,0 +1,39 @@
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use dosamigos\tinymce\TinyMce;
/* @var $this yii\web\View */
/* @var $model common\models\Newsletter */
/* @var $form yii\widgets\ActiveForm */
?>
<p>
Az új hirlevelek következő nap hajnal 3-kor kerülnek kiküldésre
</p>
<div class="newsletter-form">
<?php $form = ActiveForm::begin(); ?>
<?php echo $form->field($model,'subject')?>
<?= $form->field($model, 'body')->widget(TinyMce::className(), [
'options' => ['rows' => 12],
// 'language' => 'en',
'clientOptions' => [
'plugins' => [
"advlist autolink lists link charmap print preview anchor",
"searchreplace visualblocks code fullscreen",
"insertdatetime media table contextmenu paste"
],
'toolbar' => "undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
]
]);?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('common/newsletter', 'Mentés') : Yii::t('common/newsletter', 'Módosítás'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

View File

@@ -0,0 +1,41 @@
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model backend\models\NewsletterSearch */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="newsletter-search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'id_newsletter') ?>
<?= $form->field($model, 'subject') ?>
<?= $form->field($model, 'body') ?>
<?= $form->field($model, 'status') ?>
<?= $form->field($model, 'sent') ?>
<?php // echo $form->field($model, 'sent_at') ?>
<?php // echo $form->field($model, 'created_at') ?>
<?php // echo $form->field($model, 'updated_at') ?>
<div class="form-group">
<?= Html::submitButton(Yii::t('common/newsletter', 'Search'), ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton(Yii::t('common/newsletter', 'Reset'), ['class' => 'btn btn-default']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

View File

@@ -0,0 +1,21 @@
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model common\models\Newsletter */
$this->title = Yii::t('common/newsletter', 'Új hírlevél');
$this->params['breadcrumbs'][] = ['label' => Yii::t('common/newsletter', 'Hírlevelek'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="newsletter-create">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

View File

@@ -0,0 +1,59 @@
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use common\models\Newsletter;
/* @var $this yii\web\View */
/* @var $searchModel backend\models\NewsletterSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = Yii::t('common/newsletter', 'Hírlevelek');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="newsletter-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a(Yii::t('common/newsletter', 'Új hírlevél'), ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'id_newsletter',
'subject',
[
'attribute' => 'status',
'value' => 'statusName'
],
[
'attribute' => 'sent',
'value' => 'sentName'
],
'sent_at:datetime',
'created_at:datetime',
// 'updated_at',
['class' => 'yii\grid\ActionColumn',
'buttons' =>[
'update' => function ($url, $model, $key) {
return $model->sent === Newsletter::$SENT_NOT
? Html::a('Módosít', $url,['class' => 'btn btn-xs btn-success']) : '';
},
'view' => function ($url, $model, $key) {
return Html::a('Részletek', $url,['class' => 'btn btn-xs btn-success']) ;
},
'delete' => function ($url, $model, $key) {
return $model->sent === Newsletter::$SENT_NOT ? Html::a('Törlés', $url,['class' => 'btn btn-xs btn-success']) : '';
},
],
],
],
]); ?>
</div>

View File

@@ -0,0 +1,21 @@
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model common\models\Newsletter */
$this->title = Yii::t('common/newsletter', 'Hírlevél módosítása');
$this->params['breadcrumbs'][] = ['label' => Yii::t('common/newsletter', 'Hírlevelek'), 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->id_newsletter, 'url' => ['view', 'id' => $model->id_newsletter]];
$this->params['breadcrumbs'][] = Yii::t('common/newsletter', 'Update');
?>
<div class="newsletter-update">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

View File

@@ -0,0 +1,95 @@
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
use common\models\Newsletter;
use yii\widgets\ActiveForm;
use yii\helpers\Url;
/* @var $this yii\web\View */
/* @var $model common\models\Newsletter */
$this->title = $model->id_newsletter;
$this->params['breadcrumbs'][] = ['label' => Yii::t('common/newsletter', 'Hírlevelek'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="newsletter-view">
<h1>Hírlevél részletei</h1>
<?php if ( $model->sent != Newsletter::$SENT_TRUE){?>
<p>
<?= Html::a("Módosítás", ['update', 'id' => $model->id_newsletter], ['class' => 'btn btn-primary']) ?>
<?= Html::a("Törlés", ['delete', 'id' => $model->id_newsletter], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => Yii::t('common/log', 'Are you sure you want to delete this item?'),
'method' => 'post',
],
]) ?>
</p>
<?php }?>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id_newsletter',
[
'attribute' => 'status',
'value' => $model->statusName
],
[
'attribute' => 'sent',
'value' => $model->sent == Newsletter::$SENT_NOT ? "Küldésre vár" : "Elküldve"
],
'sent_at:datetime',
'created_at:datetime',
'updated_at:datetime',
],
]) ?>
</div>
<p><b>Tárgy:</b></p>
<div style='border: 1px solid #000; padding: 6px; background-color: #fff;'>
<p>
<?php echo $model->subject?>
</p>
</div>
<p><b >Üzenet</b></p>
<div style='border: 1px solid #000; padding: 6px; background-color: #fff;'>
<?php echo $model->body ?>
</div>
<h3>
Teszt üzenet küldése
</h3>
<p>
A küldés gombra kattintva úgy küldünk ki egy darab e-mailt,
<ul>
<li>
mintha a megadott vendég lenne a fogadó
</li>
<li>
viszont a vendég e-mail címe helyett a megadott e-mail címre küldjük
</li>
</ul>
</p>
<div class="newsletter-form">
<?php $form = ActiveForm::begin(
['action' => Url::current()]
); ?>
<?= $form->field($testModel, 'customer_name') ?>
<?= $form->field($testModel, 'email') ?>
<div class="form-group">
<?= Html::submitButton( 'Teszt email küldése' , ['class' => [ 'btn btn-primary' ]]) ?>
</div>
<?php ActiveForm::end(); ?>
</div>