add subscriber

This commit is contained in:
Roland Schneider 2016-05-27 21:00:23 +02:00
parent fcb00823da
commit afa58973ae
10 changed files with 208 additions and 75 deletions

View File

@ -60,7 +60,7 @@ $this->params['breadcrumbs'][] = $this->title;
<div style='border: 1px solid #000; padding: 6px; background-color: #fff;'> <div style='border: 1px solid #000; padding: 6px; background-color: #fff;'>
<?php echo $model->body ?> <?php echo $model->body ?>
</div> </div>
<?php /*?>
<h3> <h3>
Teszt üzenet küldése Teszt üzenet küldése
</h3> </h3>
@ -68,10 +68,10 @@ Teszt üzenet küldése
A küldés gombra kattintva úgy küldünk ki egy darab e-mailt, A küldés gombra kattintva úgy küldünk ki egy darab e-mailt,
<ul> <ul>
<li> <li>
mintha a megadott vendég lenne a fogadó mintha a megadott előfizető lenne a fogadó
</li> </li>
<li> <li>
viszont a vendég e-mail címe helyett a megadott e-mail címre küldjük viszont az előfizető e-mail címe helyett a megadott e-mail címre küldjük
</li> </li>
</ul> </ul>
</p> </p>
@ -93,3 +93,4 @@ Teszt üzenet küldése
<?php ActiveForm::end(); ?> <?php ActiveForm::end(); ?>
</div> </div>
<?php */ ?>

View File

@ -397,6 +397,9 @@ class Helper {
} }
public static function getWebUrl(){ public static function getWebUrl(){
if ( \Yii::$app->params['development'] == true ){
return "http://localhost/fitness-web";
}else{
if ( self::isCompanyMovar()){ if ( self::isCompanyMovar()){
return "https://fitnessadmin.hu/fitness-web"; return "https://fitnessadmin.hu/fitness-web";
}else if ( self::isCompanyGyor()){ }else if ( self::isCompanyGyor()){
@ -405,5 +408,6 @@ class Helper {
return "https://localhost/fitness-web"; return "https://localhost/fitness-web";
} }
} }
}
} }

View File

@ -1,5 +1,6 @@
<?php <?php
return [ return [
'development' => false,
'adminEmail' => 'rocho02@gmail.com', 'adminEmail' => 'rocho02@gmail.com',
'supportEmail' => 'rocho02@gmail.com', 'supportEmail' => 'rocho02@gmail.com',
'infoEmail' => 'info@rocho-net.hu', 'infoEmail' => 'info@rocho-net.hu',
@ -47,6 +48,7 @@ return [
'day' => 5, 'day' => 5,
] ]
], ],
'newsletter_from' => 'noreply@fitnessadmin.hu' 'newsletter_from' => 'noreply@fitnessadmin.hu',
'newsletter_enabled' => false
]; ];

View File

@ -45,6 +45,10 @@ class Log extends BaseFitnessActiveRecord
public static $TYPE_WASTE = 100; public static $TYPE_WASTE = 100;
public static $TYPE_NEWSLETTER_SUBSCRIBE = 110; public static $TYPE_NEWSLETTER_SUBSCRIBE = 110;
public static $TYPE_NEWSLETTER_UNSUBSCRIBE = 120; public static $TYPE_NEWSLETTER_UNSUBSCRIBE = 120;
public static $TYPE_NEWSLETTER_SENT = 130;
public static $TYPE_TICKET_EXPIRE_SENT = 140;
public static $TYPE_NEWSLETTER_SEND_START = 150;
public static $TYPE_NEWSLETTER_SEND_END = 160;
/** /**
* @inheritdoc * @inheritdoc

View File

@ -0,0 +1,85 @@
<?php
namespace common\models;
use Yii;
use yii\helpers\ArrayHelper;
use yii\behaviors\TimestampBehavior;
/**
* This is the model class for table "subscriber".
*
* @property integer $id_subscriber
* @property string $name
* @property string $email
* @property integer $status
* @property string $token
* @property string $created_at
* @property string $updated_at
*/
class Subscriber extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'subscriber';
}
public function behaviors()
{
return ArrayHelper::merge( [
[
'class' => TimestampBehavior::className(),
'value' => function(){ return date('Y-m-d H:i:s' ); }
],
], parent::behaviors());
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['email'], 'email'],
[['status','email','name'], 'required'],
[['status'], 'integer'],
[['name'], 'string', 'max' => 255],
['status' ,'validateSubscribe' ,'skipOnEmpty' => false, 'skipOnError' => false ,'on' => 'subscribe']
];
}
public function validateSubscribe($attr,$param){
if ( $this->status != '1'){
$this->addError( "status", "Feliratkozáshoz be kell jelölnie ezt a mezőt!");
}
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id_subscriber' => Yii::t('common/subscriber', 'Id Subscriber'),
'name' => Yii::t('common/subscriber', 'Név'),
'email' => Yii::t('common/subscriber', 'Email'),
'status' => Yii::t('common/subscriber', 'Szeretnék hírlevelet kapni'),
'token' => Yii::t('common/subscriber', 'Token'),
'created_at' => Yii::t('common/subscriber', 'Created At'),
'updated_at' => Yii::t('common/subscriber', 'Updated At'),
];
}
public function beforeSave($insert){
if ( parent::beforeSave($insert)){
$this->token = Yii::$app->security->generateRandomString() . '_' . time();
}
return true;
}
}

View File

@ -13,6 +13,7 @@ use common\components\Helper;
use common\models\Newsletter; use common\models\Newsletter;
use common\models\Log; use common\models\Log;
use yii\helpers\Html; use yii\helpers\Html;
use common\models\Subscriber;
class TicketController extends Controller { class TicketController extends Controller {
public function actionIndex() { public function actionIndex() {
@ -110,6 +111,12 @@ class TicketController extends Controller {
->setTo ( $recepientEmail ) ->setTo ( $recepientEmail )
->setSubject ( "Értesítés - " . Helper::getCompanyName()." - Bérleted ".$dayCount." nap múlva lejár" ) ->setSubject ( "Értesítés - " . Helper::getCompanyName()." - Bérleted ".$dayCount." nap múlva lejár" )
->send (); ->send ();
Log::logC([
'type' => Log::$TYPE_TICKET_EXPIRE_SENT,
'message' => "Bérlet lejár üzenet: " . $recepient['customer_name']." [id_customer=". $recepient['customer_id_customer'] ."]",
'id_customer' => $recepient['customer_id_customer']
]);
} }
} }
} }
@ -129,6 +136,12 @@ class TicketController extends Controller {
public function actionNewsletter(){ public function actionNewsletter(){
set_time_limit(0); set_time_limit(0);
if ( \Yii::$app->params['newsletter_enabled'] != true ){
return;
}
$q1 = Newsletter::find(); $q1 = Newsletter::find();
$q1->andWhere(['sent' => Newsletter::$SENT_NOT]); $q1->andWhere(['sent' => Newsletter::$SENT_NOT]);
$q1->andWhere(['status' => Newsletter::$STATUS_ACTIVE]); $q1->andWhere(['status' => Newsletter::$STATUS_ACTIVE]);
@ -139,38 +152,32 @@ class TicketController extends Controller {
\Yii::info("Nincs elküldendő hírlevél"); \Yii::info("Nincs elküldendő hírlevél");
} }
$q2 = new Query(); $subscribers = Subscriber::find()->andWhere(['status' => 1])->all();
$q2->distinct();
$q2->select(['customer.email as customer_email','customer.name as customer_name'
,'customer.id_customer as customer_id_customer'
,'customer.newsletter_token as customer_newsletter_token'
]);
$q2->from("customer");
$q2->innerJoin("ticket","ticket.id_card = customer.id_customer_card");
// $q2->andWhere(['status' => Customer::STATUS_ACTIVE ]);
$q2->andWhere(['newsletter' => Customer::$ENABLED ]);
$q2->andWhere(['>=' ,'ticket.end',new Expression('DATE( DATE_ADD(NOW(),INTERVAL -3 MONTH) )')]);
$customers = $q2->all();
\Yii::info("Hírlevél küldése " .count($customers) . " vendégnek!" ); \Yii::info("Hírlevél küldése " .count($subscribers) . " előfizetőnek!" );
$i = 0; $i = 0;
foreach ($newsletters as $newsletter ){ foreach ($newsletters as $newsletter ){
$i = 0; $i = 0;
foreach ($customers as $customer ){ Log::logC([
'type' => Log::$TYPE_NEWSLETTER_SEND_START,
'message' => "Hírlevél küldés indítása[id_newsletter=" . $newsletter->id_newsletter ."]"
]);
foreach ($subscribers as $subscriber ){
try{ try{
$log = "Sending Newsletter"; $log = "Sending Newsletter";
$log .="[id_newsletter=".$newsletter->id_newsletter ."]"; $log .="[id_newsletter=".$newsletter->id_newsletter ."]";
$log .="[customer_email=" . $customer['customer_email'] . "]"; $log .="[subscriber_email=" . $subscriber->email . "]";
$log .="[subscriber_id=" . $subscriber->id_subscriber . "]";
\Yii::info($log); \Yii::info($log);
$message = \Yii::$app->mailer->compose ( ); $message = \Yii::$app->mailer->compose ( );
$replacePairs = [ $replacePairs = [
'{vendeg_neve}' => $customer['customer_name'] '{vendeg_neve}' => $subscriber->name
]; ];
$mailBody = $newsletter->body; $mailBody = $newsletter->body;
@ -178,7 +185,7 @@ class TicketController extends Controller {
$mailBody .="<hr>"; $mailBody .="<hr>";
$mailBody .="Leiratkozás hírlevélről:"; $mailBody .="Leiratkozás hírlevélről:";
$mailBody .= Html::a("Leiratkozom",Helper::getWebUrl() ."/frontend/web/index.php?r=site/newsletter-unsubscribe&id=".$customer['customer_id_customer']."&token=".$customer['customer_newsletter_token']); $mailBody .= Html::a("Leiratkozom",Helper::getWebUrl() ."/frontend/web/index.php?r=site/newsletter-unsubscribe&id=".$subscriber->id_subscriber."&token=". $subscriber->token );
$mailSubject = $newsletter->subject; $mailSubject = $newsletter->subject;
$mailSubject = strtr($mailSubject, $replacePairs ); $mailSubject = strtr($mailSubject, $replacePairs );
@ -186,14 +193,19 @@ class TicketController extends Controller {
$message $message
->setFrom ( [ \Yii::$app->params['newsletter_from'] => Helper::getCompanyName() ]) ->setFrom ( [ \Yii::$app->params['newsletter_from'] => Helper::getCompanyName() ])
->setTo ( [ ->setTo ( [
$customer['customer_email'] $subscriber->email => $subscriber->name
]) ])
->setHtmlBody( $mailBody ) ->setHtmlBody( $mailBody )
->setSubject ( $mailSubject ) ->setSubject ( $mailSubject )
->send (); ->send ();
Log::logC([
'type' => Log::$TYPE_NEWSLETTER_SENT,
'message' => "Hírlevél küldés[id_newsletter=" . $newsletter->id_newsletter ."][id_subscriber=" . $subscriber->id_subscriber ."]"
]);
}catch (\Exception $ex){ }catch (\Exception $ex){
\Yii::error("Nem sikerült hírlevelet kikülden: Hírlevél azonosító=" .$newsletter->id_newsletter. ";Vendég azonosító ". $customer['customer_name'] ) ; \Yii::error("Nem sikerült hírlevelet kikülden: Hírlevél azonosító=" .$newsletter->id_newsletter. ";Előfizető azonosító ". $subscriber->id_subscriber ) ;
} }
$i++; $i++;
@ -202,6 +214,11 @@ class TicketController extends Controller {
$newsletter->sent_at = Helper::getDateTimeString(); $newsletter->sent_at = Helper::getDateTimeString();
$newsletter->save(false); $newsletter->save(false);
Log::logC([
'type' => Log::$TYPE_NEWSLETTER_SEND_END,
'message' => "Hírlevél küldés befejezése[id_newsletter=" . $newsletter->id_newsletter ."]"
]);
// Log::log([ // Log::log([
// 'type' =>Log::$TYPE_LOGIN, // 'type' =>Log::$TYPE_LOGIN,
// 'message' => $message // 'message' => $message

View File

@ -0,0 +1,43 @@
<?php
use yii\db\Schema;
use yii\db\Migration;
class m160527_054414_add_table__subscriber extends Migration
{
public function up()
{
$tableOptions = null;
if ($this->db->driverName === 'mysql') {
// http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
}
$this->createTable('{{%subscriber}}', [
'id_subscriber' => $this->primaryKey(),
'name' => $this->string(),
'email' => $this->text(),
'status' => $this->integer(11),
'token' => $this->string(),
'created_at' => $this->dateTime()->notNull(),
'updated_at' => $this->dateTime()->notNull(),
], $tableOptions);
}
public function down()
{
return true;
}
/*
// Use safeUp/safeDown to run migration code within a transaction
public function safeUp()
{
}
public function safeDown()
{
}
*/
}

View File

@ -18,6 +18,7 @@ use common\models\Log;
use common\models\Customer; use common\models\Customer;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
use frontend\models\SubscribeForm; use frontend\models\SubscribeForm;
use common\models\Subscriber;
/** /**
* Site controller * Site controller
@ -185,9 +186,9 @@ class SiteController extends Controller
$this->layout = "main-nologin"; $this->layout = "main-nologin";
$customer = Customer::find() $customer = Subscriber::find()
->andWhere(['id_customer' => $id ]) ->andWhere(['id_subscriber' => $id ])
->andWhere(['newsletter_token' => $token]) ->andWhere(['token' => $token])
->one(); ->one();
@ -195,51 +196,32 @@ class SiteController extends Controller
throw new NotFoundHttpException("Az oldal nem található"); throw new NotFoundHttpException("Az oldal nem található");
} }
$customer->newsletter = 0; $customer->status = 0;
$customer->newsletter_token = Yii::$app->security->generateRandomString() . '_' . time(); $customer->token = Yii::$app->security->generateRandomString() . '_' . time();
$customer->save(); $customer->save();
Log::logC([ Log::logC([
'id_customer' => $customer->id_customer,
'type' => Log::$TYPE_NEWSLETTER_UNSUBSCRIBE, 'type' => Log::$TYPE_NEWSLETTER_UNSUBSCRIBE,
"message" => "Hírlevél feliratkozás: ". $customer->name "message" => "Hírlevél leiratkozás: ". $customer->name
]); ]);
return $this->render("unsubscribe"); return $this->render("unsubscribe",['subscriber' => $customer]);
} }
public function actionNewsletterSubscribe($id,$token) public function actionNewsletterSubscribe()
{ {
$this->layout = "main-nologin"; $this->layout = "main-nologin";
$model = new Subscriber(['scenario' => 'subscribe']);
$customer = Customer::find() if ($model->load(Yii::$app->request->post()) && $model->save() ) {
->andWhere(['id_customer' => $id ])
->andWhere(['newsletter_token' => $token])
->one();
if ( !isset($customer)){
throw new NotFoundHttpException("Az oldal nem található");
}
$model = new SubscribeForm([
'customer' => $customer
]);
if ($model->load(Yii::$app->request->post()) && $model->validate() ) {
$customer->newsletter = 1;
$customer->newsletter_token = Yii::$app->security->generateRandomString() . '_' . time();
$customer->save();
Log::logC([ Log::logC([
'id_customer' => $customer->id_customer,
'type' => Log::$TYPE_NEWSLETTER_SUBSCRIBE, 'type' => Log::$TYPE_NEWSLETTER_SUBSCRIBE,
"message" => "Hírlevél feliratkozás: ". $customer->name ." - " . $customer->email "message" => "Hírlevél feliratkozás: ". $model->name ." - " . $model->email
]); ]);
return $this->redirect(["newsletter-subscribe-success"]); return $this->redirect(["newsletter-subscribe-success"]);

View File

@ -4,18 +4,6 @@ use yii\helpers\Html;
use common\components\Helper; use common\components\Helper;
?> ?>
<div class="key-form"> <div class="key-form">
<p>
Kedves <?php echo $model->customer->name ?>!
</p>
<?php if ( empty($model->customer->email)) {?>
<p>
Ön nem adott meg e-mail címet. Ha szeretne feliratkozni a hírlevelünkre,
kérjük keressen bennünket személyesen valamilyik termünkben.
</p>
<p>
Köszönettel: <?php echo Helper::getCompanyName()?>
</p>
<?php }else{?>
<p> <p>
Ha Ön is szeretné megkapni a <?php echo Helper::getCompanyName() ?> hírleveleit, Ha Ön is szeretné megkapni a <?php echo Helper::getCompanyName() ?> hírleveleit,
@ -26,7 +14,9 @@ Kedves <?php echo $model->customer->name ?>!
<?php $form = ActiveForm::begin(); ?> <?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'subscribe')->checkbox([])?> <?= $form->field($model, 'email')->textInput([])?>
<?= $form->field($model, 'name')->textInput([])?>
<?= $form->field($model, 'status')->checkbox([ 'label' => false ])->label("Szeretnék hírlevelet kapni")?>
@ -35,6 +25,5 @@ Kedves <?php echo $model->customer->name ?>!
</div> </div>
<?php ActiveForm::end(); ?> <?php ActiveForm::end(); ?>
<?php }?>
</div> </div>

View File

@ -1,7 +1,13 @@
<?php <?php
use common\components\Helper; use common\components\Helper;
?> ?>
<p>
Kedves <?php echo $subscriber->name ?>!
</p>
<p>
Ön sikeresen leiratkozott a hírlevelünkről! Ön sikeresen leiratkozott a hírlevelünkről!
A leiratkozott e-mail cím : <?php echo $subscriber->email?>
</p>
<p> <p>
Köszönettel: Köszönettel:
</p> </p>