SearchRelationHandlerTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 69
ccs 0
cts 30
cp 0
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A searchRelation() 0 51 5
1
<?php
2
3
namespace Yaro\Jarboe\Http\Controllers\Traits\Handlers;
4
5
use Illuminate\Http\Request;
6
use Yaro\Jarboe\Table\CRUD;
7
use Yaro\Jarboe\Table\Fields\Select;
8
use Yaro\Jarboe\Table\Fields\Tags;
9
10
trait SearchRelationHandlerTrait
11
{
12
    /**
13
     * Handle relation search action.
14
     * Currently used for Select field with type `select2` and `ajax = true`, and Tags field with `ajax = true`.
15
     *
16
     * @param Request $request
17
     * @return \Illuminate\Http\JsonResponse
18
     */
19
    public function searchRelation(Request $request)
20
    {
21
        $this->beforeInit();
22
        $this->init();
23
        $this->bound();
24
25
        // TODO:
26
        $perPage = 10;
27
28
        $query = $request->get('term');
29
        $fieldName = $request->get('field');
30
        $page = (int) $request->get('page');
31
32
        /** @var Select|Tags $field */
33
        $field = $this->crud()->getFieldByName($fieldName);
34
        if (!$field) {
35
            throw new \RuntimeException(sprintf('Field [%s] not setted to crud', $fieldName));
36
        }
37
38
        $total = 0;
39
        $results = [];
40
        if ($field->isGroupedRelation()) {
41
            foreach ($field->getRelations() as $index => $group) {
42
                $options = $field->getOptions($page, $perPage, $query, $total, $index);
43
                array_walk($options, function (&$item, $key) use ($group) {
44
                    $item = [
45
                        'id'   => crc32($group['group']) .'~~~'. $key,
46
                        'text' => $item,
47
                    ];
48
                });
49
                if ($options) {
50
                    $results[] = [
51
                        'text'     => $group['group'],
52
                        'children' => array_values($options),
53
                    ];
54
                }
55
            }
56
        } else {
57
            $results = $field->getOptions($page, $perPage, $query, $total);
58
            array_walk($results, function (&$item, $key) {
59
                $item = [
60
                    'id'   => $key,
61
                    'text' => $item,
62
                ];
63
            });
64
        }
65
66
        return response()->json([
67
            'results' => array_values($results),
68
            'pagination' => [
69
                'more' => $total > $page * $perPage,
70
            ]
71
        ]);
72
    }
73
74
    abstract protected function beforeInit();
75
    abstract protected function init();
76
    abstract protected function bound();
77
    abstract protected function crud(): CRUD;
78
    abstract protected function can($action): bool;
79
}
80