SearchContext   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 46
c 2
b 0
f 0
dl 0
loc 98
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getSearchFields() 0 61 9
A getConvertToAutocomplete() 0 3 1
A setConvertToAutocomplete() 0 4 1
1
<?php
2
3
namespace ilateral\SilverStripe\ModelAdminPlus;
4
5
use SilverStripe\Core\ClassInfo;
6
use SilverStripe\Forms\TextField;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\ORM\Search\SearchContext as SSSearchContext;
9
10
class SearchContext extends SSSearchContext
11
{
12
    /**
13
     * Use autocomplete fields instead of text fields
14
     * 
15
     * @var boolean
16
     */
17
    protected $convert_to_autocomplete = true;
18
19
    /**
20
     * Extend default search fields and add extra functionality.
21
     * 
22
     * @return \SilverStripe\Forms\FieldList
23
     */
24
    public function getSearchFields()
25
    {
26
        $fields = parent::getSearchFields();
27
        $class = $this->modelClass;
28
        $use_autocomplete = $this->getConvertToAutocomplete();
29
30
        $db = Config::inst()->get($class, "db");
31
        $has_one = Config::inst()->get($class, "has_one");
32
        $has_many = Config::inst()->get($class, "has_many");
33
        $many_many = Config::inst()->get($class, "many_many");
34
        $belongs_many_many = Config::inst()->get($class, "belongs_many_many");
35
        $associations = array_merge(
36
            $has_one,
37
            $has_many,
38
            $many_many,
39
            $belongs_many_many
40
        );
41
42
        // Update search fields to autocomplete
43
        foreach ($fields as $field) {
44
            $field_class = $this->modelClass;
45
            $name = $field->getName();
46
            $title = $field->Title();
47
            $db_field = $name;
48
            $in_db = false;
49
50
            // Find any text fields an replace with autocomplete fields
51
            if (ClassInfo::class_name($field) == TextField::class && $use_autocomplete) {
52
                // If this is a relation, switch class name
53
                if (strpos($name, "__")) {
54
                    $parts = explode("__", $db_field);
55
                    $field_class = isset($associations[$parts[0]]) ? $associations[$parts[0]] : null;
56
                    $db_field = $parts[1];
57
                    $in_db = ($field_class) ? true : false;
58
                }
59
60
                // If this is in the DB (not casted)
61
                if (in_array($db_field, array_keys($db))) {
62
                    $in_db = true;
63
                }
64
65
                if ($in_db) {
66
                    $fields->replaceField(
67
                        $name,
68
                        $field = AutoCompleteField::create(
69
                            $name,
70
                            $title,
71
                            $field->Value(),
72
                            $field_class,
73
                            $db_field
74
                        )->setDisplayField($db_field)
75
                        ->setLabelField($db_field)
76
                        ->setStoredField($db_field)
77
                    );
78
                }
79
            }
80
81
            $field->setName($name);
82
        }
83
84
        return $fields;
85
    }
86
87
    /**
88
     * Get use autocomplete fields instead of text fields
89
     *
90
     * @return  boolean
91
     */ 
92
    public function getConvertToAutocomplete()
93
    {
94
        return $this->convert_to_autocomplete;
95
    }
96
97
    /**
98
     * Set use autocomplete fields instead of text fields
99
     *
100
     * @param boolean $convert Convert?
101
     *
102
     * @return self
103
     */ 
104
    public function setConvertToAutocomplete(boolean $convert)
0 ignored issues
show
Bug introduced by
The type ilateral\SilverStripe\ModelAdminPlus\boolean was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
105
    {
106
        $this->convert_to_autocomplete = $convert;
0 ignored issues
show
Documentation Bug introduced by
It seems like $convert of type ilateral\SilverStripe\ModelAdminPlus\boolean is incompatible with the declared type boolean of property $convert_to_autocomplete.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
107
        return $this;
108
    }
109
}
110