1
|
|
|
<?php namespace Arcanesoft\Blog\ViewComposers\Admin\Dashboard; |
2
|
|
|
|
3
|
|
|
use Arcanesoft\Blog\Models\Category; |
4
|
|
|
use Arcanesoft\Blog\ViewComposers\AbstractComposer; |
5
|
|
|
use Illuminate\Contracts\View\View; |
6
|
|
|
use Illuminate\Database\Eloquent\Collection; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class CategoriesRatiosComposer |
10
|
|
|
* |
11
|
|
|
* @package Arcanesoft\Blog\ViewComposers\Dashboard |
12
|
|
|
* @author ARCANEDEV <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class CategoriesRatiosComposer extends AbstractComposer |
15
|
|
|
{ |
16
|
|
|
/* ----------------------------------------------------------------- |
17
|
|
|
| Constants |
18
|
|
|
| ----------------------------------------------------------------- |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
const VIEW = 'blog::admin._composers.dashboard.categories-ratios-chart'; |
22
|
|
|
|
23
|
|
|
/* ----------------------------------------------------------------- |
24
|
|
|
| Main Methods |
25
|
|
|
| ----------------------------------------------------------------- |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
public function compose(View $view) |
29
|
|
|
{ |
30
|
|
|
$view->with('categoriesRatios', $this->prepareRatios( |
31
|
|
|
$this->cachedCategories()->load(['posts']) |
32
|
|
|
)); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/* ----------------------------------------------------------------- |
36
|
|
|
| Other Methods |
37
|
|
|
| ----------------------------------------------------------------- |
38
|
|
|
*/ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Prepare the categories ratios. |
42
|
|
|
* |
43
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $categories |
44
|
|
|
* |
45
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
46
|
|
|
*/ |
47
|
|
|
private function prepareRatios(Collection $categories) |
48
|
|
|
{ |
49
|
|
|
$ratios = $categories->filter(function (Category $category) { |
50
|
|
|
return $category->hasPosts(); |
51
|
|
|
}) |
52
|
|
|
->transform(function (Category $category) { |
53
|
|
|
return [ |
54
|
|
|
'label' => $category->name, |
55
|
|
|
'posts' => $category->posts->count(), |
56
|
|
|
]; |
57
|
|
|
}) |
58
|
|
|
->sortByDesc('posts'); |
59
|
|
|
|
60
|
|
|
$chunk = $ratios->splice(5); |
61
|
|
|
$ratios = $this->colorizeRatios($ratios); |
62
|
|
|
|
63
|
|
|
return $chunk->isEmpty() ? $ratios : $ratios->push([ |
64
|
|
|
'label' => 'Other Categories', |
65
|
|
|
'posts' => $chunk->sum('posts'), |
66
|
|
|
'color' => '#D2D6DE', |
67
|
|
|
]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Colorize the ratios. |
72
|
|
|
* |
73
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $ratios |
74
|
|
|
* |
75
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
76
|
|
|
*/ |
77
|
|
|
private function colorizeRatios(Collection $ratios) |
78
|
|
|
{ |
79
|
|
|
$colors = ['#F56954', '#00A65A', '#F39C12', '#00C0EF', '#3C8DBC']; |
80
|
|
|
|
81
|
|
|
return $ratios->values()->transform(function (array $values, $key) use ($colors) { |
82
|
|
|
return $values + ['color' => $colors[$key]]; |
83
|
|
|
}); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|