From afe821a220208bedac1e61203c09c76fef9bdf0f Mon Sep 17 00:00:00 2001 From: merdan Date: Thu, 15 Apr 2021 14:23:36 +0500 Subject: [PATCH] statistika completed --- .../controllers/StatisticsController.php | 80 ++++++++++++++---- .../statisticscontroller/_barchart.htm | 35 ++++++++ .../statisticscontroller/_linechart.htm | 26 ++++++ .../statisticscontroller/config_filter.yaml | 28 +++++++ .../statisticscontroller/index.htm | 82 ++----------------- plugins/tps/reklama/models/Statistika.php | 11 +++ .../tps/reklama/models/statistika/fields.yaml | 2 +- 7 files changed, 170 insertions(+), 94 deletions(-) create mode 100644 plugins/tps/reklama/controllers/statisticscontroller/_barchart.htm create mode 100644 plugins/tps/reklama/controllers/statisticscontroller/_linechart.htm create mode 100644 plugins/tps/reklama/controllers/statisticscontroller/config_filter.yaml diff --git a/plugins/tps/reklama/controllers/StatisticsController.php b/plugins/tps/reklama/controllers/StatisticsController.php index d450b4273..035ba3c09 100644 --- a/plugins/tps/reklama/controllers/StatisticsController.php +++ b/plugins/tps/reklama/controllers/StatisticsController.php @@ -4,7 +4,9 @@ namespace Tps\Reklama\Controllers; use Backend\Classes\Controller; use Backend\Facades\BackendMenu; +use Carbon\Carbon; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Log; use Tps\Reklama\Models\Reklama; use Tps\Reklama\Models\Statistika; use Tps\Reklama\Widgets\Stats; @@ -19,11 +21,21 @@ class StatisticsController extends Controller BackendMenu::setContext('Tps.Reklama', 'main-menu-item', 'side-menu-item3'); + $config = $this->makeConfig('$/tps/reklama/controllers/statisticscontroller/config_filter.yaml'); + + $widget = $this->makeWidget('Backend\Widgets\Filter',$config); + + $this->vars['widget'] = $widget; + + $widget->bindEvent('filter.update', function () use($widget){ + return $this->onFilteer(); + }); + + $widget->bindToController(); } public function index() { -// $config = $this->makeConfig('$/tps/reklama/models/statistika/fields.yaml'); $this->pageTitle = 'Statistics'; $this->vars['stats'] = Statistika::groupBy('item_id') @@ -32,26 +44,58 @@ class StatisticsController extends Controller ->take(20) ->get() ->toArray(); - } - public function onLoad(){ - $item_id = input('item_id'); - - if($item_id){ - $stats = Db::table('tps_reklama_statistika') - ->select(Db::raw("item_id,sum(view) as v, sum(click) as c, extract(year from date) as y, extract(month from date) as m")) - ->where('item_id',$item_id) - ->whereNotNull('date') - ->groupBy(Db::raw("item_id, extract(year from date), extract(month from date)")) - ->orderBy('y') - ->orderBy('m') - ->get() - ->toArray(); - return [ - 'result' => $stats - ]; + protected function getCurrentFilters() { + $filters = []; + foreach (\Session::get('widget', []) as $name => $item) { + if (str_contains($name, 'Filter')) { + $filter = @unserialize(@base64_decode($item)); + if ($filter) { + //$filters[] = $filter; + array_push($filters, $filter); + } + } } + + return $filters[0]; + } + + public function onFilteer(){ + + $this->vars['reklama'] = 'Reklama'; + + $filters = $this->getCurrentFilters(); + + $reklamas = $filters['scope-reklama']; + + $dates = $filters['scope-date']; + + if($reklamas) + { + $ids = array_keys($reklamas); + + $stats = Db::table('tps_reklama_statistika') + ->select(Db::raw("item_id,view, click, date")) + ->whereIn('item_id',$ids); + + if($dates){ + $stats->where('date','>=',$dates[0]) + ->where('date','<=',$dates[1]); + } + else + { + $stats->where('date','<=',Carbon::today()->startOfMonth()); + } + + $this->vars['reklams'] = $reklamas; + + $this->vars['reklam_stats'] = $stats->get(); + + return ['#linechart' => $this->makePartial('linechart')]; + } + + } } diff --git a/plugins/tps/reklama/controllers/statisticscontroller/_barchart.htm b/plugins/tps/reklama/controllers/statisticscontroller/_barchart.htm new file mode 100644 index 000000000..4ed85097c --- /dev/null +++ b/plugins/tps/reklama/controllers/statisticscontroller/_barchart.htm @@ -0,0 +1,35 @@ +
+ +
+ diff --git a/plugins/tps/reklama/controllers/statisticscontroller/_linechart.htm b/plugins/tps/reklama/controllers/statisticscontroller/_linechart.htm new file mode 100644 index 000000000..63908d331 --- /dev/null +++ b/plugins/tps/reklama/controllers/statisticscontroller/_linechart.htm @@ -0,0 +1,26 @@ + $reklam):?> +
+
+ + diff --git a/plugins/tps/reklama/controllers/statisticscontroller/config_filter.yaml b/plugins/tps/reklama/controllers/statisticscontroller/config_filter.yaml new file mode 100644 index 000000000..3ed4579f3 --- /dev/null +++ b/plugins/tps/reklama/controllers/statisticscontroller/config_filter.yaml @@ -0,0 +1,28 @@ +# =================================== +# Filter Scope Definitions +# =================================== + +scopes: + + reklama: + label: Reklama + type: group + conditions: item_id = (:filtered) + modelClass: Tps\Reklama\Models\Statistika + options: filterCategory + + date: + label: Date + type: daterange + conditions: date >= ':afterDate' AND published_at <= ':beforeDate' + minDate: '2021-04-01' + yearRange: 1 + +# status: +# label: Active +# type: switch +# default: 1 +# conditions: +# - active <> true +# - active = true + diff --git a/plugins/tps/reklama/controllers/statisticscontroller/index.htm b/plugins/tps/reklama/controllers/statisticscontroller/index.htm index 3858bd8d8..4b267c152 100644 --- a/plugins/tps/reklama/controllers/statisticscontroller/index.htm +++ b/plugins/tps/reklama/controllers/statisticscontroller/index.htm @@ -1,4 +1,4 @@ - +
-
+ makePartial('barchart')?> +
+
+
+ render()?>
-
- -
-
+
- - - diff --git a/plugins/tps/reklama/models/Statistika.php b/plugins/tps/reklama/models/Statistika.php index a8bb69f5f..f2369e4f5 100644 --- a/plugins/tps/reklama/models/Statistika.php +++ b/plugins/tps/reklama/models/Statistika.php @@ -33,4 +33,15 @@ class Statistika extends Model 'order' => 'name' ], ]; + + public function filterCategory() + { + $result = []; + + foreach (Reklama::where('active', 1)->orderBy('title', 'asc')->get()->all() as $item) { + $result[$item->id] = $item->title; + } + + return $result; + } } diff --git a/plugins/tps/reklama/models/statistika/fields.yaml b/plugins/tps/reklama/models/statistika/fields.yaml index f6532c3d3..0d89e37fc 100644 --- a/plugins/tps/reklama/models/statistika/fields.yaml +++ b/plugins/tps/reklama/models/statistika/fields.yaml @@ -1,4 +1,4 @@ -secondaryTabs: +tabs: stretch: true fields: general_stats: