Test Setup Failed
Push — ft/command-palette ( 94ffe1...ab00c3 )
by
unknown
05:19
created

CommandPaletteController::getAllPageModelIndices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 13
rs 10
1
<?php
2
3
namespace Thinktomorrow\Chief\App\Http\Controllers\Back\CommandPalette;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Str;
7
use Thinktomorrow\Chief\App\Http\Controllers\Controller;
8
use Thinktomorrow\Chief\Managers\Register\Registry;
9
10
class CommandPaletteController extends Controller
11
{
12
    public function __construct()
13
    {
14
        $this->pageModels = $this->getAllPageModels();
0 ignored issues
show
Bug Best Practice introduced by
The property pageModels does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
15
        // dd($this->pageModels);
16
    }
17
18
    public function search(string $term = '')
19
    {
20
        $lowercaseTerm = Str::lower($term);
21
22
        $results = [];
23
24
        foreach ($this->pageModels as $modelGroup) {
25
            $resultGroup = [];
26
27
            foreach ($modelGroup['models'] as $model) {
28
                if (
29
                    Str::contains(Str::lower($model->adminConfig()->getModelName()), $lowercaseTerm) ||
30
                    Str::contains(Str::lower($model->adminConfig()->getIndexTitle()), $lowercaseTerm) ||
31
                    Str::contains(Str::lower($model->adminConfig()->getNavTitle()), $lowercaseTerm) ||
32
                    Str::contains(Str::lower($model->adminConfig()->getPageTitle()), $lowercaseTerm) ||
33
                    Str::contains(Str::lower($model->title ?? ''), $lowercaseTerm)
34
                ) {
35
                    $resultGroup[$model->modelReference()->getShort()] = [
36
                        'label' => $model->title,
37
                        'url' => '/admin/' . $model->managedModelKey() . '/' . $model->id . '/edit',
38
                    ];
39
                }
40
            }
41
42
            if (count($resultGroup) !== 0) {
43
                array_push($results, [
44
                    'label' => $modelGroup['label'],
45
                    'models' => $resultGroup,
46
                ]);
47
            }
48
        }
49
50
        return response()->view('chief::layout.nav.command-palette._result', [
51
            'term' => $term,
52
            'results' => $results,
53
        ]);
54
    }
55
56
    private function getAllPageModels(): Collection
57
    {
58
        return collect(app(Registry::class)->models())
59
            // Filter out fragment models
60
            ->filter(function ($model) {
61
                return ! in_array('Thinktomorrow\Chief\Fragments\Fragmentable', class_implements($model));
62
                // Return all instances of the models
63
            })->map(function ($model) {
64
                $models = $model::all();
65
66
                return [
67
                    'label' => $model::make()->adminConfig()->getModelName(),
68
                    'models' => $models,
69
                ];
70
            });
71
    }
72
73
    public static function getAllPageModelIndices(): Collection
74
    {
75
        return collect(app(Registry::class)->models())
76
            // Filter out fragment models
77
            ->filter(function ($model) {
78
                return ! in_array('Thinktomorrow\Chief\Fragments\Fragmentable', class_implements($model));
79
                // Return all instances of the models
80
            })->map(function ($model) {
81
                $model = $model::make();
82
83
                return [
84
                    'label' => $model->adminConfig()->getNavTitle(),
85
                    'url' => '/admin/' . $model->managedModelKey(),
86
                ];
87
            });
88
    }
89
}
90