guild/backend/modules/reports/controllers/ReportsController.php

259 lines
7.4 KiB
PHP
Raw Permalink Normal View History

2020-02-05 12:08:01 +03:00
<?php
namespace backend\modules\reports\controllers;
use backend\modules\card\models\UserCardSearch;
2024-04-03 18:31:17 +03:00
use backend\modules\reports\models\ExtractForm;
use common\classes\Debug;
use common\models\ReportsTask;
2020-02-05 12:08:01 +03:00
use Yii;
use common\models\Reports;
use backend\modules\reports\models\ReportsSearch;
2021-08-12 14:00:23 +03:00
use yii\filters\AccessControl;
2021-09-09 17:00:27 +03:00
use yii\helpers\ArrayHelper;
2020-02-05 12:08:01 +03:00
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* ReportsController implements the CRUD actions for Reports model.
*/
class ReportsController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
2022-12-23 20:35:12 +03:00
'as AccessBehavior' => [
'class' => \developeruz\db_rbac\behaviors\AccessBehavior::className(),
],
2020-02-05 12:08:01 +03:00
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
2022-12-23 20:35:12 +03:00
// 'access' => [
// 'class' => AccessControl::className(),
// 'rules' => [
// [
// 'allow' => true,
// 'roles' => ['admin'],
// ],
// ],
// ],
2020-02-05 12:08:01 +03:00
];
}
/**
* Lists all Reports models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new ReportsSearch();
2021-09-09 17:00:27 +03:00
$user_id__fio = ArrayHelper::map(ArrayHelper::toArray($searchModel->search([])->getModels(), [
'common\models\Reports' => [
'user_card_id',
'fio' => function ($report) {
return Reports::getFio($report);
}
],
2024-04-03 18:31:17 +03:00
]), 'user_card_id', 'fio');
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
2021-09-07 15:24:24 +03:00
return $this->render('index');
}
public function actionList()
{
$searchModel = new ReportsSearch();
$user_id__fio = ArrayHelper::map(ArrayHelper::toArray($searchModel->search([])->getModels(), [
'common\models\Reports' => [
'user_card_id',
'fio' => function ($report) {
return Reports::getFio($report);
}
],
2024-04-03 18:31:17 +03:00
]), 'user_card_id', 'fio');
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('list', [
2020-02-05 12:08:01 +03:00
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
2021-09-07 15:24:24 +03:00
'user_id__fio' => $user_id__fio,
2020-02-05 12:08:01 +03:00
]);
}
2021-09-06 10:29:50 +03:00
2021-09-09 17:00:27 +03:00
public function actionCalendar($user_id)
2021-09-06 10:29:50 +03:00
{
$searchModel = new ReportsSearch();
2024-02-09 15:29:13 +03:00
$searchModel->user_id = $user_id;
2021-09-09 17:00:27 +03:00
$dataProvider = $searchModel->search([]);
2021-09-06 10:29:50 +03:00
2021-09-09 17:00:27 +03:00
$reports_array = ArrayHelper::toArray($dataProvider->getModels(), [
'common\models\Reports' => [
'id',
'created_at',
'difficulties',
'tomorrow',
'user_card_id',
'today' => function ($report) {
return ArrayHelper::toArray($report->task, [
'common\models\ReportsTask' => [
'hours_spent',
'task'
],
]);
}
],
]);
2024-04-03 18:31:17 +03:00
if (!$dataProvider->getCount()) {
return $this->render('non-exist_user_id', ['id' => $user_id]);
2021-09-09 17:00:27 +03:00
}
return $this->render('calendarOneUser', [
2021-09-09 17:00:27 +03:00
'reports' => $reports_array,
'fio' => Reports::getFio($searchModel),
'USER_ID' => $user_id
]);
2021-09-06 10:29:50 +03:00
}
2020-02-05 12:08:01 +03:00
public function actionGroup()
{
$searchModel = new UserCardSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->query->innerJoin('reports', 'user_card.id = reports.user_card_id');
return $this->render('group', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Reports 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 Reports model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Reports();
2021-06-07 17:18:49 +03:00
if ($model->load(Yii::$app->request->post())) {
$model->save();
2020-02-05 12:08:01 +03:00
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Updates an existing Reports 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()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
]);
}
/**
* Deletes an existing Reports 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']);
}
2024-04-03 18:31:17 +03:00
public function actionExtractForm(): string
{
$model = new ExtractForm();
return $this->render('extract-form', ['model' => $model]);
}
public function actionExtract()
{
$model = new ExtractForm();
if ($model->load(Yii::$app->request->post())) {
$query = ReportsTask::find()->joinWith('reports')
->where(['reports.user_id' => $model->user_id])
->andWhere(['between', 'reports.created_at', $model->date_from, $model->date_to]);
$file = \Yii::createObject([
'class' => 'codemix\excelexport\ExcelFile',
'sheets' => [
'Reports' => [
'class' => 'codemix\excelexport\ActiveExcelSheet',
'query' => $query,
'attributes' => [
'reports.created_at',
'task',
'hours_spent'
],
]
]
]);
$file->send('reports.xlsx');
} else {
return $this->render('extract-form', ['model' => $model]);
}
}
2020-02-05 12:08:01 +03:00
/**
* Finds the Reports model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Reports the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Reports::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}