fix reports
This commit is contained in:
parent
b5a9d4be40
commit
bb9959edf1
@ -7,66 +7,28 @@ use common\models\ReportsTask;
|
|||||||
use common\models\UserCard;
|
use common\models\UserCard;
|
||||||
use DateTime;
|
use DateTime;
|
||||||
use frontend\modules\api\models\ReportSearchForm;
|
use frontend\modules\api\models\ReportSearchForm;
|
||||||
use JsonException;
|
|
||||||
use Yii;
|
use Yii;
|
||||||
use yii\filters\auth\CompositeAuth;
|
|
||||||
use yii\filters\auth\HttpBearerAuth;
|
|
||||||
use yii\filters\ContentNegotiator;
|
|
||||||
use yii\helpers\ArrayHelper;
|
use yii\helpers\ArrayHelper;
|
||||||
use yii\web\BadRequestHttpException;
|
use yii\web\BadRequestHttpException;
|
||||||
use yii\web\NotFoundHttpException;
|
use yii\web\NotFoundHttpException;
|
||||||
use yii\web\Response;
|
|
||||||
|
|
||||||
class ReportsController extends ApiController
|
class ReportsController extends ApiController
|
||||||
{
|
{
|
||||||
public function init()
|
public function actionIndex($user_card_id)
|
||||||
{
|
|
||||||
parent::init(); // TODO: Change the autogenerated stub
|
|
||||||
}
|
|
||||||
|
|
||||||
public function behaviors()
|
|
||||||
{
|
|
||||||
$parent = parent::behaviors();
|
|
||||||
$b = [
|
|
||||||
[
|
|
||||||
'class' => ContentNegotiator::className(),
|
|
||||||
'formats' => [
|
|
||||||
'application/json' => Response::FORMAT_JSON,
|
|
||||||
],
|
|
||||||
],
|
|
||||||
'authenticator' => [
|
|
||||||
'class' => CompositeAuth::class,
|
|
||||||
'authMethods' => [
|
|
||||||
HttpBearerAuth::class,
|
|
||||||
],
|
|
||||||
]
|
|
||||||
];
|
|
||||||
|
|
||||||
return array_merge($parent, $b);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function actionIndex(): array
|
|
||||||
{
|
{
|
||||||
$reportsModel = new ReportSearchForm();
|
$reportsModel = new ReportSearchForm();
|
||||||
|
|
||||||
$params = Yii::$app->request->get();
|
$params = Yii::$app->request->get();
|
||||||
if(!isset($params['user_card_id'])){
|
|
||||||
$userCard = UserCard::find()->where(['id_user' => Yii::$app->user->id])->one();
|
|
||||||
if($userCard){
|
|
||||||
$params['user_card_id'] = $userCard->id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$reportsModel->attributes = $params;
|
$reportsModel->attributes = $params;
|
||||||
|
|
||||||
|
$reportsModel->limit = $params['limit'] ?? $reportsModel->limit;
|
||||||
|
$reportsModel->offset = $params['offset'] ?? $reportsModel->offset;
|
||||||
|
|
||||||
if(!$reportsModel->validate()){
|
if(!$reportsModel->validate()){
|
||||||
return $reportsModel->errors;
|
return $reportsModel->errors;
|
||||||
}
|
}
|
||||||
return $reportsModel->byParams();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function actionView($id): array{
|
return $reportsModel->byParams();
|
||||||
$report = Reports::findOne($id);
|
|
||||||
return array_merge($report->toArray(), ['tasks' => $report->_task]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -88,9 +50,6 @@ class ReportsController extends ApiController
|
|||||||
throw new BadRequestHttpException('Wrong date format');
|
throw new BadRequestHttpException('Wrong date format');
|
||||||
}
|
}
|
||||||
|
|
||||||
// return $reportsModel->date;
|
|
||||||
// return $reportsModel->user_card_id;
|
|
||||||
|
|
||||||
if(!$reportsModel->validate()){
|
if(!$reportsModel->validate()){
|
||||||
return $reportsModel->errors;
|
return $reportsModel->errors;
|
||||||
}
|
}
|
||||||
@ -145,7 +104,7 @@ class ReportsController extends ApiController
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(false === ($report->delete())) {
|
if(false === ($report->delete())) {
|
||||||
throw new JsonException('Report not deleted');
|
throw new \RuntimeException('Report not deleted');
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -161,7 +120,7 @@ class ReportsController extends ApiController
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(isset($params['user_card_id'])) {
|
if(isset($params['user_card_id'])) {
|
||||||
throw new JsonException('constraint by user_card_id');
|
throw new \RuntimeException('constraint by user_card_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
$reportsModel->attributes = $params;
|
$reportsModel->attributes = $params;
|
||||||
@ -197,7 +156,9 @@ class ReportsController extends ApiController
|
|||||||
$reportsModel->attributes = $params;
|
$reportsModel->attributes = $params;
|
||||||
$reportsModel->user_card_id = $userCard->id;
|
$reportsModel->user_card_id = $userCard->id;
|
||||||
|
|
||||||
$reports = $reportsModel->findByDate();
|
// return $reportsModel;
|
||||||
|
|
||||||
|
$reports = $reportsModel->reportsByDate();
|
||||||
return ArrayHelper::toArray($reports , [
|
return ArrayHelper::toArray($reports , [
|
||||||
'common\models\Reports' => [
|
'common\models\Reports' => [
|
||||||
'date' => 'created_at',
|
'date' => 'created_at',
|
||||||
|
@ -4,21 +4,18 @@
|
|||||||
namespace frontend\modules\api\models;
|
namespace frontend\modules\api\models;
|
||||||
|
|
||||||
use common\models\Reports;
|
use common\models\Reports;
|
||||||
use common\models\ReportsTask;
|
|
||||||
use yii\base\Model;
|
use yii\base\Model;
|
||||||
|
|
||||||
|
/** */
|
||||||
class ReportSearchForm extends Model
|
class ReportSearchForm extends Model
|
||||||
{
|
{
|
||||||
|
public $user_card_id;
|
||||||
public $limit;
|
public $limit;
|
||||||
public $offset;
|
public $offset;
|
||||||
|
/** @var string */
|
||||||
|
public $date;
|
||||||
public $fromDate;
|
public $fromDate;
|
||||||
public $toDate;
|
public $toDate;
|
||||||
public $user_card_id;
|
|
||||||
/**
|
|
||||||
* @var false
|
|
||||||
*/
|
|
||||||
public $byDate;
|
|
||||||
public $date;
|
|
||||||
|
|
||||||
public function __construct($config = [])
|
public function __construct($config = [])
|
||||||
{
|
{
|
||||||
@ -26,10 +23,9 @@ class ReportSearchForm extends Model
|
|||||||
$this->offset = 0;
|
$this->offset = 0;
|
||||||
$this->user_card_id = null;
|
$this->user_card_id = null;
|
||||||
|
|
||||||
$this->toDate = date('Y-m-d', time());
|
$this->toDate = date('Y-m-d');
|
||||||
$this->fromDate = date('Y-m-d', time());
|
$this->fromDate = date('Y-m-d');
|
||||||
$this->date = date('Y-m-d');
|
$this->date = date('Y-m-d');
|
||||||
$this->byDate = false;
|
|
||||||
|
|
||||||
parent::__construct($config);
|
parent::__construct($config);
|
||||||
}
|
}
|
||||||
@ -38,41 +34,44 @@ class ReportSearchForm extends Model
|
|||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
[['byDate'], 'safe'],
|
[['byDate'], 'safe'],
|
||||||
// [['fromDate', 'toDate', 'date'], 'date', 'format' => 'Y-m-d'],
|
[['fromDate', 'toDate', 'date'], 'string'],
|
||||||
[['limit', 'offset', 'user_card_id'], 'integer', 'min' => 0],
|
// [['fromDate', 'toDate', 'date'], 'date', 'format' => 'php:Y-m-d'],
|
||||||
|
[[ 'user_card_id'], 'integer', 'min' => 0],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function byParams()
|
public function byParams()
|
||||||
{
|
{
|
||||||
$queryBuilder = Reports::find()
|
$queryBuilder = Reports::find()->with('task');
|
||||||
->with('task');
|
|
||||||
|
|
||||||
if ($this->byDate) {
|
if ($this->fromDate && $this->toDate) {
|
||||||
$queryBuilder->andWhere(['reports.created_at' => $this->date]);
|
|
||||||
} else {
|
|
||||||
$queryBuilder->andWhere(['between', 'reports.created_at', $this->fromDate, $this->toDate]);
|
$queryBuilder->andWhere(['between', 'reports.created_at', $this->fromDate, $this->toDate]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isset($this->user_card_id)) {
|
||||||
|
$queryBuilder->andWhere(['reports.user_card_id' => $this->user_card_id]);
|
||||||
|
}
|
||||||
|
|
||||||
$queryBuilder->limit($this->limit)
|
$queryBuilder->limit($this->limit)
|
||||||
->offset($this->offset);
|
->offset($this->offset);
|
||||||
|
|
||||||
if (isset($this->user_card_id)) {
|
return $queryBuilder->asArray()->all();
|
||||||
$queryBuilder->andWhere(['user_card_id' => $this->user_card_id]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = $queryBuilder->asArray()->all();
|
public function findByDate()
|
||||||
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function findByDate(): array
|
|
||||||
{
|
{
|
||||||
return Reports::find()
|
return Reports::find()->with('task')
|
||||||
// ->joinWith('task')
|
->where(['reports.user_card_id' => $this->user_card_id])
|
||||||
->with('task')
|
// ->where(['between', 'reports.created_at', $this->fromDate, $this->toDate])
|
||||||
->where(['user_card_id' => $this->user_card_id])
|
->andWhere(['reports.created_at' => $this->date])
|
||||||
->andWhere(['created_at' => $this->date])
|
->asArray()->all();
|
||||||
->all();
|
}
|
||||||
|
|
||||||
|
public function reportsByDate()
|
||||||
|
{
|
||||||
|
return Reports::find()->with('task')
|
||||||
|
->where(['reports.user_card_id' => $this->user_card_id])
|
||||||
|
->where(['between', 'reports.created_at', $this->fromDate, $this->toDate])
|
||||||
|
->asArray()->all();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,83 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace frontend\modules\api\models;
|
|
||||||
|
|
||||||
use yii\db\ActiveQuery;
|
|
||||||
use yii\helpers\Url;
|
|
||||||
use yii\web\Linkable;
|
|
||||||
|
|
||||||
class News extends \common\models\Reports implements Linkable
|
|
||||||
{
|
|
||||||
public function fields()
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'id',
|
|
||||||
'created_at',
|
|
||||||
'today',
|
|
||||||
'difficulties',
|
|
||||||
'tomorrow',
|
|
||||||
'status',
|
|
||||||
'user_card_id'
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function extraFields()
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'tags',
|
|
||||||
'comments',
|
|
||||||
'comments_count' => function () {
|
|
||||||
return (int)$this->getCommentsCount();
|
|
||||||
},
|
|
||||||
'photo' => function () {
|
|
||||||
return $this->getPhotoLink();
|
|
||||||
},
|
|
||||||
'news_body',
|
|
||||||
'like' => function () {
|
|
||||||
return (int)$this->getLikesCount();
|
|
||||||
},
|
|
||||||
'category' => function () {
|
|
||||||
return $this->category;
|
|
||||||
},
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getPhotoLink()
|
|
||||||
{
|
|
||||||
if (empty($this->photo)) {
|
|
||||||
return 'N/A';
|
|
||||||
}
|
|
||||||
return '/uploads/news-image/' . $this->photo;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getTags(): ActiveQuery
|
|
||||||
{
|
|
||||||
return $this->hasMany(Tag::className(), ['id' => 'tag_id'])
|
|
||||||
->via('newsTags');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getCategory()
|
|
||||||
{
|
|
||||||
return $this->hasMany(Category::className(), ['id' => 'category_id'])
|
|
||||||
->via('categoryNews');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getComments(): ActiveQuery
|
|
||||||
{
|
|
||||||
return $this->hasMany(Comment::className(), ['news_id' => 'id']);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getLinks(): array
|
|
||||||
{
|
|
||||||
$string = str_replace('+', ',', Url::to(['news/news', 'expand' => 'tags comments photo news_body like', 'news_id' => $this->id], true));
|
|
||||||
|
|
||||||
return [
|
|
||||||
'self' => $string,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getEvent()
|
|
||||||
{
|
|
||||||
return $this->hasOne(EventType::class, ['id' => 'event_type_id']);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user