Your IP : 216.73.216.79


Current Path : /var/www/html/ains/frontend/controllers/
Upload File :
Current File : /var/www/html/ains/frontend/controllers/SiteController.php

<?php

namespace frontend\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\web\Controller;
use kartik\mpdf\Pdf;
use frontend\models\Mybook;
use frontend\models\MybookChapter;
use frontend\models\ReadingRecord;
use backend\modules\book\models\Book;

/**
 * Site controller
 */
class SiteController extends Controller {

    /**
     * {@inheritDoc}
     * @see \yii\web\Controller::beforeAction()
     */
    public function beforeAction($action) {
        //         $this->layout = 'main_default';
        // TODO Auto-generated method stub
        return parent::beforeAction($action);
    }

    /**
     * @inheritdoc
     */
    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout', 'signup', 'error', 'stub'],
                'rules' => [
                    [
                        'actions' => ['signup', 'error', 'stub'],
                        'allow' => true,
                        'roles' => ['?'],
                    ],
                    [
                        'actions' => ['logout', 'error'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions() {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return mixed
     */
    public function actionIndex() {
        return $this->render('index');
    }

    /**
     * use slug
     * */
    public function actionPage() {
        return $this->render('page'); // placeholder
    }

    /**
     * Logs out the current user.
     *
     * @return mixed
     */
    public function actionLogout() {
        Yii::$app->user->logout();
        return $this->redirect(['/web/home']);
        //return $this->goHome();
    }

    public function actionLogoutOfficer() {
        Yii::$app->user->logout();
        return $this->redirect(['/web/home2']);
        //return $this->goHome();
    }

    public function actionLogin() {
        Yii::$app->user->logout();
        return $this->redirect(['/web/home']);
        //return $this->goHome();
    }

    public function actionError() {
        $exception = Yii::$app->errorHandler->exception;
        if ($exception !== null) {
            return $exception->getMessage();
        }
    }

    public function actionViewpdf($doc_id) {
        if (($model = \common\models\CourseDocument::findOne($doc_id)) !== null) {
            $pdf = new Pdf([
                'mode' => Pdf::MODE_BLANK,
                'filename' => $model->doc_title . '.pdf',
                'format' => Pdf::FORMAT_A4,
                'orientation' => Pdf::ORIENT_PORTRAIT,
                'destination' => Pdf::DEST_DOWNLOAD,
                'content' => $model->doc_content,
            ]);
            return $pdf->render();
        }
    }

    public function actionTranslate($language) {
        $cookies = Yii::$app->response->cookies;
        Yii::$app->language = $language;
        $cookies->add(new \yii\web\Cookie([
                    'name' => 'language',
                    'value' => $language
        ]));
        return $this->redirect(Yii::$app->request->referrer ?: Yii::$app->homeUrl);
    }

    public function actionApi($code) {
        if (($model = \backend\modules\api\models\Api::findOne(['api_code' => $code])) !== null) {
            header('Content-Type: application/json');
            eval('?>' . $model->api_content);
            die();
        }
    }

    public function actionUpdatemybookstatus($id, $status) {
        $content = "";
        $model = Mybook::findOne($id);
        $model->status_approval = $status;
        $model->approval_at = date("Y-m-d H:i:s");
        $model->approval_by = Yii::$app->user->identity->id;
        if ($model->save(false)) {

            if ($status == 'approved') {

                if ($model->type == 'manual') {
                    $chaptercontent = '';
                    $tablecontent = '';
                    $modelChapter = MybookChapter::find()->where(['=', 'parent_id', $model->id])->orderby(['sorting' => SORT_ASC])->all();
                    if (!empty($modelChapter)) {
                        foreach ($modelChapter as $rowChapter) {
                            $tablecontent .= '<tr><td><h4>Bab ' . $rowChapter->sorting . ' : ' . $rowChapter->title . '</h4></td></tr>';

                            if ($rowChapter->type == 'text') {
                                $chaptercontent .= '<pagebreak margin-left="15mm" margin-right="15mm" margin-top="15mm" margin-bottom="20mm">
                            <h2 style="text-align:center;">Bab ' . $rowChapter->sorting . '</h2>
                            <h2 style="text-align:center;">' . $rowChapter->title . '</h2><br><br>' . $rowChapter->content_text;
                            }

                            if ($rowChapter->type == 'image') {
                                $chaptercontent .= '<pagebreak margin-left="15mm" margin-right="15mm" margin-top="15mm" margin-bottom="20mm">
                            <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
                            <h1 style="text-align:center;">Bab ' . $rowChapter->sorting . '</h1>
                            <h1 style="text-align:center;">' . $rowChapter->title . '</h1><pagebreak margin-left="0" margin-right="0" margin-top="0" margin-bottom="0"><img src="' . $rowChapter->content_img . '" width="100%">';
                            }
                        }
                    }

                    if (!empty($model->cover_img))
                        $content .= '<img src="/' . $model->cover_img . '" width="100%">';

                    $content .= '<pagebreak margin-left="15mm" margin-right="15mm" margin-top="15mm" margin-bottom="20mm">
                <table width="100%" cellpadding="10" cellspacing="0">
                <tr>
                    <td align="center"><h1>Kandungan</h1><br><br></td>
                </tr>' . $tablecontent . '</table>' . $chaptercontent;

                    $pdf = new Pdf([
                        'mode' => Pdf::MODE_BLANK,
                        'filename' => 'buku_' . $model->id . '.pdf',
                        'format' => Pdf::FORMAT_A4,
                        'orientation' => Pdf::ORIENT_PORTRAIT,
                        'marginTop' => 0,
                        'marginBottom' => 0,
                        'marginLeft' => 0,
                        'marginRight' => 0,
                        //'destination' => Pdf::DEST_BROWSER,
                        'destination' => Pdf::DEST_FILE,
                        'filename' => Yii::getAlias('@webroot/uploads/') . 'buku_' . $model->id . '.pdf',
                        'content' => $content,
                        'cssInline' => 'p { font-size: 24px; }',
                    ]);

                    //insert into reading record

                    $contentpdf = file_get_contents(Yii::getAlias('@webroot/uploads/') . 'buku_' . $model->id . '.pdf');
                    // Matches the number of pages in the PDF file
                    $pages = preg_match_all("/\/Page\W/", $contentpdf, $dummy);

                    $file_url = 'uploads/buku_' . $model->id . '.pdf';
                }

                if ($model->type == 'upload')
                    $file_url = $model->file_url;

                $modelBook = new Book();
                $modelBook->title = $model->title;
                $modelBook->total_page = $pages;
                $modelBook->author = $model->user->fullname;
                $modelBook->publisher = 'AINS';
                $modelBook->year_publish = date('Y');
                $modelBook->summary = $model->summary;
                $modelBook->file_url = $file_url;
                $modelBook->cover_img = $model->cover_img;
                $modelBook->type = 'book';
                $modelBook->url_type = 'upload';
                $modelBook->source = 'murid';
                $modelBook->button_label = 'read-now';
                $modelBook->mybook_id = $id;

                $modelBook->save(false);

                if ($model->type == 'manual')
                    return $pdf->render();
            }
        }
    }

    public function actionChatbotkeyword($term) {
        $language = 'MY';
        $arr = [];
        $model = \backend\modules\chat\models\ChatQuestionTag::find()
                ->where(['LIKE', 'chat_question_tag.question_tag_name', $term])
                ->andwhere(['=', 'chat_question_tag.question_tag_language', $language])
                ->orderby(['chat_question_tag.question_tag_name' => SORT_ASC])
                ->limit(10)
                ->all();
        if (!empty($model)) {
            foreach ($model as $row) {

                $modelAnswer = \backend\modules\chat\models\ChatAnswerBot::findOne(['question_tag_id' => $row->question_tag_id]);
                if (!empty($modelAnswer))
                    $arr[] = $row->question_tag_name;
            }
        }
        return json_encode($arr);
    }
}