1 | <?php namespace Arcanesoft\Blog\ViewComposers\Admin\Dashboard; |
||
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) |
||
85 | } |
||
86 |