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