| Current Path : /var/www/html/school/backend/controllers/ |
| Current File : /var/www/html/school/backend/controllers/ReportViewController.php |
<?php
namespace backend\controllers;
use Yii;
use backend\models\ReportView\ReportView;
use backend\models\ReportViewSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
use yii\web\UploadedFile;
use PHPJasper\PHPJasper;
/**
* ReportViewController implements the CRUD actions for ReportView model.
*/
class ReportViewController extends Controller
{
public function actionOpenReport($filename=null){
if($filename == null){
$get =Yii::$app->request->get();
$filename = $get['filename'];
}
$output = dirname(__FILE__). '/../reports';
$filepath = $output . "/" . $filename;
if(file_exists($filepath))
{
// Set up PDF headers
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($filepath));
header('Accept-Ranges: bytes');
// Render the file
readfile($filepath);
die();
}
}
public function actionGenerate(){
$post = Yii::$app->request->post();
if(count($post) == 0){
$post = Yii::$app->request->get();
}
$condition = [];
$params = false;
$sql = "
select * from dmg_report_filter where report_id='$post[report_id]'
";
$dsn = explode(":",Yii::$app->db->dsn);
$items = explode(";",$dsn[1]);
$dbConn = [];
foreach($items as $item){
$details = explode('=',$item);
$dbConn[$details[0]] = $details[1];
}
$dbConn['username'] = Yii::$app->db->username;
$dbConn['password'] = Yii::$app->db->password;
$filterArr = Yii::$app->db->createCommand($sql)->queryAll();
foreach($filterArr as $itemFilter){
switch($itemFilter['filter_type']){
case 1:
if(array_key_exists($itemFilter['filter_field'],$post['ReportFilter'])){
$postValue = $post['ReportFilter'][$itemFilter['filter_field']];
if($postValue != ''){
switch($itemFilter['filter_input_type']){
case 'T':
$condition[] = $itemFilter['filter_field'] . " LIKE '%$postValue%'";
break;
case 'R':
case 'S':
$condition[] = $itemFilter['filter_field'] . " = '$postValue'";
break;
}
$params = true;
}
}
break;
case 2:
$condition[] = " " . $itemFilter['filter_content'];
break;
}
}
$paramsOption = [];
if($params == true){
$conditionParam = " AND " . implode(" AND ",$condition);
$paramsOption = array('param_condition'=>$conditionParam);
}
else{
if(isset($post['query_action'])){
$where = base64_decode($post['query_action']);
$paramsOption = array('param_condition'=>' AND ' . $where);
}
else
$paramsOption = array('param_condition'=>' AND 1=1');
}
$id = $post['report_id'];
$model = $this->findModel($id);
$input = dirname(__FILE__). '/../reports/' . $model->report_jasper;
$output = dirname(__FILE__). '/../reports';
if($dbConn['password'] != ''){
$options = [
'format' => ['pdf'],
'locale' => 'en',
'params' => $paramsOption,
'db_connection' => [
'driver' => 'mysql', //mysql, ....
'username' => $dbConn['username'],
'password' => "'".$dbConn['password']."'",
'host' => $dbConn['host'],
'database' => $dbConn['dbname'],
'port' => isset($dbConn['port']) ? $dbConn['port'] : '3306'
]
];
}
else{
$options = [
'format' => ['pdf'],
'locale' => 'en',
'params' => $paramsOption,
'db_connection' => [
'driver' => 'mysql', //mysql, ....
'username' => $dbConn['username'],
'host' => $dbConn['host'],
'database' => $dbConn['dbname'],
'port' => isset($dbConn['port']) ? $dbConn['port'] : '3306'
]
];
}
$jasper = new PHPJasper;
/*
$jasper->process(
$input,
$output,
$options
)->execute();
$options = [
'format' => ['pdf'],
'params' => $paramsOption
];
*/
$jasper->process(
$input,
$output,
$options
)->execute();
//var_dump($jasper);
//die();
if(isset($post['showpdf'])){
$this->actionOpenReport(basename($model->report_jasper,'.jasper') . '.pdf');
}
else{
$url = Url::to(['report-view/open-report']);
$result = array('success'=>true, 'file'=>basename($model->report_jasper,'.jasper') . '.pdf','url'=>$url);
$result = json_encode($result);
return $result;
}
}
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
public function actionTesting(){
$inputJrxml = dirname(__FILE__). '/../reports/report1.jrxml';
$jasper = new PHPJasper;
$jasper->compile($inputJrxml)->execute();
var_dump($jasper);
}
/**
* Lists all ReportView models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new ReportViewSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$reports=ReportView::find()->all();
//use yii\helpers\ArrayHelper;
$listData=ArrayHelper::map($reports,'id','report_name');
$models = ReportView::find()->orderBy(['report_name'=>SORT_ASC])->all();
foreach($models as $model){
$dropdown[$model->id] = $model->report_name;
}
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'model'=> $model,
'dropdown'=>$dropdown
]);
}
/**
* Displays a single ReportView model.
* @param integer $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 ReportView model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new ReportView();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
if (Yii::$app->request->isPost) {
$model->file = UploadedFile::getInstance($model, 'file');
if ($model->file && $model->validate()) {
$input = dirname(__FILE__). '/../reports/';
$filename = $model->file->baseName . '_'.time();
$filenameJrxml = $filename .'.' . $model->file->extension;
$model->file->saveAs($input . $filenameJrxml);
$model->report_jrxml = $filenameJrxml;
$inputJrxml = $input . $filenameJrxml;
$filenameJasper = $filename . '.jasper';
$model->report_jasper = $filenameJasper;
$jasper = new PHPJasper;
$jasper->compile($inputJrxml)->execute();
$model->save();
}
}
return $this->redirect(['index']);
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Updates an existing ReportView model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
if (Yii::$app->request->isPost) {
$model->file = UploadedFile::getInstance($model, 'file');
if ($model->file && $model->validate()) {
$input = dirname(__FILE__). '/../reports/';
if(file_exists($input . $model->report_jrxml)){
unlink($input . $model->report_jrxml);
}
if(file_exists($input . $model->report_jasper)){
unlink($input . $model->report_jasper);
}
$filename = $model->file->baseName . '_'.time();
$filenameJrxml = $filename .'.' . $model->file->extension;
$model->file->saveAs($input . $filenameJrxml);
$model->report_jrxml = $filenameJrxml;
$inputJrxml = $input . $filenameJrxml;
$filenameJasper = $filename . '.jasper';
$model->report_jasper = $filenameJasper;
$jasper = new PHPJasper;
$jasper->compile($inputJrxml)->execute();
$model->save();
}
}
return $this->redirect(['index']);
}
return $this->render('update', [
'model' => $model,
]);
}
/**
* Deletes an existing ReportView model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $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 ReportView model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return ReportView the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = ReportView::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
}
}