Your IP : 216.73.216.79


Current Path : /var/www/html/autisme/backend/modules/documentAutomation/controllers/
Upload File :
Current File : /var/www/html/autisme/backend/modules/documentAutomation/controllers/TemplateController.php

<?php

namespace backend\modules\documentAutomation\controllers;

use Yii;
use backend\modules\documentAutomation\models\Template;
use backend\modules\documentAutomation\models\TemplateSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use backend\modules\documentAutomation\models\Tags;
use kartik\mpdf\Pdf;

/**
 * TemplateController implements the CRUD actions for Template model.
 */
class TemplateController extends Controller {

    /**
     * @inheritDoc
     */
    public function behaviors() {
        return array_merge(
                parent::behaviors(),
                [
                    'verbs' => [
                        'class' => VerbFilter::className(),
                        'actions' => [
                            'delete' => ['POST'],
                        ],
                    ],
                ]
        );
    }

    /**
     * Lists all Template models.
     * @return mixed
     */
    public function actionIndex() {
        $searchModel = new TemplateSearch();
        $dataProvider = $searchModel->search($this->request->queryParams);
        return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider,]);
    }

    /**
     * Displays a single Template model.
     * @param int $id ID
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id) {
        return $this->render('view', ['model' => $this->findModel($id),]);
    }

    /**
     * Creates a new Template model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new Template();
        $model->created_at = date("Y-m-d H:i:s");
        $model->created_by = Yii::$app->user->identity->id;
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        if ($model->load(Yii::$app->request->post())) {
            $model->thumbnail = UploadedFile::getInstance($model, 'thumbnail');
            if (!empty($model->thumbnail)) {
                $thumbnail = 'uploads/' . date("YmdHis") . '.' . $model->thumbnail->extension;
                if ($model->thumbnail->saveAs('../' . $thumbnail))
                    $model->thumbnail = $thumbnail;
            }
            if ($model->save())
                return $this->redirect(['updatecontent', 'id' => $model->id]);
        }
        return $this->render('create', ['model' => $model,]);
    }

    /**
     * Updates an existing Template model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param int $id ID
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $original_thumbnail = $model->thumbnail;
        if ($model->load(Yii::$app->request->post())) {
            $model->thumbnail = UploadedFile::getInstance($model, 'thumbnail');
            if (!empty($model->thumbnail)) {
                $thumbnail = 'uploads/' . date("YmdHis") . '.' . $model->thumbnail->extension;
                if ($model->thumbnail->saveAs('../' . $thumbnail))
                    $model->thumbnail = $thumbnail;
            } else {
                $model->thumbnail = $original_thumbnail;
            }
            if ($model->save())
                return $this->redirect(['index']);
        }
        return $this->render('update', ['model' => $model,]);
    }

    public function actionUpdatecontent($id) {
        $model = $this->findModel($id);
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        if ($model->load(Yii::$app->request->post())) {
            if ($model->save()) {
                return $this->redirect(['updatecontent', 'id' => $id]);
            }
        }
        return $this->renderpartial('updatecontent', ['model' => $model,]);
    }

    /**
     * Deletes an existing Template model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param int $id ID
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id) {
        $this->findModel($id)->delete();
        return $this->redirect(['index']);
    }

    /**
     * Finds the Template model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param int $id ID
     * @return Template the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = Template::findOne($id)) !== null) {
            return $model;
        }
        throw new NotFoundHttpException('The requested page does not exist.');
    }

    public function actionGettags($keywords) {
        $output = '<div class="list-group">';
        $modeltags = Tags::find()->where(['OR', ['LIKE', 'code', $keywords], ['LIKE', 'label', $keywords]])->orderby(['code' => SORT_ASC])->all();
        if (!empty($modeltags)) {
            foreach ($modeltags as $rowtags) {
                $output .= '<button type="button" class="list-group-item list-group-item-action" onclick="inserttags(\'' . $rowtags->code . '\')"><b>' . $rowtags->code . '</b><br>' . $rowtags->label . '</button>';
            }
        } else {
            $output .= 'No record found';
        }
        $output .= '</div>';
        return $output;
    }
    
    public function actionDownloadpdf($id) {
        $model = $this->findModel($id);
        $pdf = new Pdf([
            'mode' => Pdf::MODE_BLANK,
            'filename' => $model->title . '.pdf',
            'format' => Pdf::FORMAT_A4,
            //'orientation' => Pdf::ORIENT_PORTRAIT,
            'destination' => Pdf::DEST_BROWSER,
            'content' => $model->content,
        ]);
        return $pdf->render();
    }

}