| Current Path : /var/www/html/portalv3/backend/modules/documentAutomation/controllers/ |
| Current File : /var/www/html/portalv3/backend/modules/documentAutomation/controllers/DocumentsController.php |
<?php
namespace backend\modules\documentAutomation\controllers;
use Yii;
use backend\modules\documentAutomation\models\Documents;
use backend\modules\documentAutomation\models\DocumentsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\modules\documentAutomation\models\Template;
use kartik\mpdf\Pdf;
use backend\modules\documentAutomation\models\Answer;
use yii\helpers\Url;
/**
* DocumentsController implements the CRUD actions for Documents model.
*/
class DocumentsController extends Controller {
/**
* @inheritDoc
*/
public function behaviors() {
return array_merge(
parent::behaviors(),
[
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
]
);
}
/**
* Lists all Documents models.
*
* @return string
*/
public function actionIndex() {
$searchModel = new DocumentsSearch();
$dataProvider = $searchModel->search($this->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
public function actionSelecttemplate() {
return $this->render('selecttemplate');
}
/**
* Displays a single Documents model.
* @param int $id ID
* @return string
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id) {
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Documents model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return string|\yii\web\Response
*/
public function actionCreate($template_id = '') {
$update_content = '';
$model = new Documents();
$model->scenario = 'create';
$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 (($modelTemplate = Template::findOne($template_id)) !== null) {
$model->title = $modelTemplate->title;
$model->template = $modelTemplate->content;
$model->content = $modelTemplate->content;
$update_content = $modelTemplate->content;
}
if ($model->load(Yii::$app->request->post())) {
if ($model->save()) {
if (!empty($_POST['tagsID'])) {
foreach ($_POST['tagsID'] as $tagsID) {
//insert data into table : document_automation_answer
$modelAnswer = new Answer();
$modelAnswer->document_id = $model->id;
$modelAnswer->tags_id = $tagsID;
$modelAnswer->tags_code = $_POST['tags_code_' . $tagsID];
$modelAnswer->tags_label = $_POST['tags_label_' . $tagsID];
$modelAnswer->answer = $_POST['tags_answer_' . $tagsID];
$modelAnswer->created_at = date("Y-m-d H:i:s");
$modelAnswer->created_by = Yii::$app->user->identity->id;
$modelAnswer->updated_at = date("Y-m-d H:i:s");
$modelAnswer->updated_by = Yii::$app->user->identity->id;
$modelAnswer->save(false);
if (!empty($modelAnswer->answer))
$update_content = str_replace("#" . $modelAnswer->tags_code . "#", $modelAnswer->answer, $update_content);
}
}
//replace content
if (($modelUpdate = Documents::findOne($model->id)) !== null) {
$model->content = $update_content;
$model->save(false);
}
$this->getView()->registerJs("swal.fire({
title:'Saved',
icon: 'success',
text: 'Data has been saved successfully',
}).then(function(result){
window.location = '" . Url::toRoute(['update', 'id' => $model->id]) . "';
});");
//return $this->redirect(['update', 'id' => $model->id]);
}
}
return $this->render('create', ['model' => $model,]);
}
/**
* Updates an existing Documents model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param int $id ID
* @return string|\yii\web\Response
* @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;
if ($model->load(Yii::$app->request->post())) {
if ($model->save()) {
$this->getView()->registerJs("swal.fire({
title:'Saved',
icon: 'success',
text: 'Data has been saved successfully',
}).then(function(result){
window.location = '" . Url::toRoute(['update', 'id' => $model->id]) . "';
});");
//return $this->redirect(['update', 'id' => $id]);
}
}
return $this->renderpartial('update', ['model' => $model,]);
}
/**
* Deletes an existing Documents model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param int $id ID
* @return \yii\web\Response
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete() {
$id = $_POST['id'];
$status = 'failed';
if ($this->findModel($id)->delete())
$status = 'success';
return $status;
}
/**
* Finds the Documents 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 Documents the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id) {
if (($model = Documents::findOne(['id' => $id])) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
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();
}
}