Your IP : 216.73.216.79


Current Path : /var/www/html/dyna-crm/controllers/
Upload File :
Current File : /var/www/html/dyna-crm/controllers/SiteController.php

<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\models\Log;
use app\models\User;
use yii\helpers\Url;
use app\models\Rider;
use app\models\StockDelivery;
use yii\web\UploadedFile;

class SiteController extends Controller {

    public $layout = "main_original";

    public function beforeAction($action) {
        if (parent::beforeAction($action)) {
            if ($action->id == 'error') {
                $this->layout = 'error';
            }
            return true;
        }
    }

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    public function actions() {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    public function actionIndex() {
        return $this->render('index');
    }

    public function actionLogin() {
        $this->layout = 'login';
        if (!Yii::$app->user->isGuest) {
            return $this->redirect(['dashboard/index']);
        }
        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            $Log = new Log();
            $Log->insertlog(5, 'login', '');

            $redirect_url = '';
            if (($model = User::findOne(Yii::$app->user->identity->id)) !== null) {
                $redirect_url = $model->redirect_url;
            }
            if (empty($redirect_url))
                return $this->redirect(['dashboard/index']);
            else
                return $this->redirect($redirect_url);
        }
        return $this->render('login', ['model' => $model,]);
    }

    public function actionLogout() {
        $Log = new Log();
        $Log->insertlog(6, 'logout', '');
        Yii::$app->user->logout();
        return $this->goHome();
    }

    public function actionForgot() {
        $msg = '';
        $this->layout = 'login';
        if (Yii::$app->request->post()) {
            $email_post = Yii::$app->request->post('email');
            //find inside user
            if (($model = User::findOne(['email' => $email_post])) !== null) {

                //reset password
                $pwd = Yii::$app->security->generateRandomString(8);
                $model->pwd = \Yii::$app->security->generatePasswordHash($pwd);

                if ($model->save(false)) {
                    //send email
                    $email_content = '<html>
                        <body>
                            <div style="font-family: \'Century Gothic\', CenturyGothic, AppleGothic, sans-serif; font-size: 14px;background-color:rgb(234,234,234);padding:2em 0;">
                                <div style="width:70%;background-color:white;margin:0 auto;padding:0em 5em 5em 5em;border:1px solid #ccc;">
                                    <p><center><img src="' . Url::base(true) . '/assets/media/crmlogo.png"></center>Hi ' . $model->name . ',</p>
                                    <p>We\'ve received your request to reset your password. You can access the system now using below details :</p>
                                    <p>URL: <a href="' . Url::base(true) . '" target="_blank">' . Url::base(true) . '</a></p>
                                    <p>Email: ' . $email_post . '</p>
                                    <p>Password: ' . $pwd . '</p>
                                    <p>- Admin -</p>
                                </div>
                            </div>
                        </body>
                    </html>';

                    $headers = "MIME-Version: 1.0" . "\r\n";
                    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

                    /* if (Yii::$app->mailer->compose()
                      ->setFrom(Yii::$app->params['senderEmail'])
                      ->setTo($email_post)
                      ->setBcc(Yii::$app->params['bccEmail'])
                      ->setSubject('Reset Password')
                      ->setHtmlBody($email_content)
                      ->send()) { */
                    if (mail($email_post, 'Reset Password', $email_content, $headers)) {
                        $msg = '<div class="alert alert-success mt-3" role="alert">
                        <div class="alert-text">Password success to reset. <br>Please check your email for your new password</div>
                    </div>';
                    }
                } else {
                    //print_r($model->getErrors());
                    $msg = '<div class="alert alert-danger mt-3" role="alert">
                        <div class="alert-text">Failed to reset password. <br>Please try again.</div>
                    </div>';
                }
            } else {
                $msg = '<div class="alert alert-danger mt-3" role="alert">
                        <div class="alert-text">' . $email_post . ' not found. <br>Please make sure the email address are correct.</div>
                    </div>';
            }
        }
        return $this->render('forgot', ['msg' => $msg]);
    }

    public function actionUpdateorder($uuid) {
        $msg = '';
        if (($model = StockDelivery::findOne(['uuid' => $uuid])) !== null) {
            $model->scenario = 'riderupdate';
            if ($model->load(Yii::$app->request->post())) {
                if ($model->delivery_status == 'delivered')
                    $model->receive_date = date('Y-m-d');

                $model->wages_status = 1;
                $model->wages_rider_id = $model->rider_id;
                $model->rider_wages_amount = 10;

                $model->receive_proof = UploadedFile::getInstance($model, 'receive_proof');
                if (!empty($model->receive_proof)) {
                    $receive_proof = 'receive-proof-' . $model->uuid . '-' . date("YmdHis") . '.' . $model->receive_proof->extension;
                    if ($model->receive_proof->saveAs('uploads/stock_delivery/' . $receive_proof))
                        $model->receive_proof = $receive_proof;
                }

                if ($model->save(false)) {
                    $msg = '<div class="alert alert-success">Information have been updated</div>';
                } else {
                    $msg = '<div class="alert alert-danger">Information failed to update. Please try again</div>';
                }
            }

            return $this->renderpartial('updateorder', ['model' => $model, 'msg' => $msg]);
        }
    }

}