Passed
Push — 1 ( f9fc50...c5209d )
by Morven
05:06
created

SearchContext::setConvertToAutocomplete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ilateral\SilverStripe\ModelAdminPlus;
4
5
use SilverStripe\Forms\TextField;
6
use SilverStripe\Core\Config\Config;
7
use ilateral\SilverStripe\ModelAdminPlus\AutoCompleteField;
0 ignored issues
show
Bug introduced by
The type ilateral\SilverStripe\Mo...nPlus\AutoCompleteField 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...
8
use SilverStripe\ORM\Search\SearchContext as SSSearchContext;
9
use SilverStripe\Core\ClassInfo;
10
11
class SearchContext extends SSSearchContext
12
{
13
    /**
14
     * Use autocomplete fields instead of text fields
15
     * 
16
     * @var boolean
17
     */
18
    protected $convert_to_autocomplete = true;
19
20
    /**
21
     * Extend default search fields and add extra functionality.
22
     * 
23
     * @return \SilverStripe\Forms\FieldList
24
     */
25
    public function getSearchFields()
26
    {
27
        $fields = parent::getSearchFields();
28
        $class = $this->modelClass;
29
        $use_autocomplete = $this->getConvertToAutocomplete();
0 ignored issues
show
Unused Code introduced by
The assignment to $use_autocomplete is dead and can be removed.
Loading history...
30
31
        $db = Config::inst()->get($class, "db");
0 ignored issues
show
Unused Code introduced by
The assignment to $db is dead and can be removed.
Loading history...
32
        $has_one = Config::inst()->get($class, "has_one");
33
        $has_many = Config::inst()->get($class, "has_many");
34
        $many_many = Config::inst()->get($class, "many_many");
35
        $belongs_many_many = Config::inst()->get($class, "belongs_many_many");
36
        $associations = array_merge(
0 ignored issues
show
Unused Code introduced by
The assignment to $associations is dead and can be removed.
Loading history...
37
            $has_one,
38
            $has_many,
39
            $many_many,
40
            $belongs_many_many
41
        );
42
43
        // Change currently scaffolded query fields to use conventional
44
        // field names
45
        /*foreach ($fields as $field) {
46
            $field_class = $this->modelClass;
47
            $name = $field->getName();
48
            $title = $field->Title();
49
            $db_field = $name;
50
            $in_db = false;
51
52
            // Find any text fields an replace with autocomplete fields
53
            if (ClassInfo::class_name($field) == TextField::class && $use_autocomplete) {
54
                // If this is a relation, switch class name
55
                if (strpos($name, "__")) {
56
                    $parts = explode("__", $db_field);
57
                    $field_class = isset($associations[$parts[0]]) ? $associations[$parts[0]] : null;
58
                    $db_field = $parts[1];
59
                    $in_db = ($field_class) ? true : false;
60
                }
61
62
                // If this is in the DB (not casted)
63
                if (in_array($db_field, array_keys($db))) {
64
                    $in_db = true;
65
                }
66
67
                if ($in_db) {
68
                    $fields->replaceField(
69
                        $name,
70
                        $field = AutoCompleteField::create(
71
                            $name,
72
                            $title,
73
                            $field->Value(),
74
                            $field_class,
75
                            $db_field
76
                        )->setDisplayField($db_field)
77
                        ->setLabelField($db_field)
78
                        ->setStoredField($db_field)
79
                    );
80
                }
81
            }
82
83
            $field->setName($name);
84
        }*/
85
86
        return $fields;
87
    }
88
89
    /**
90
     * Get use autocomplete fields instead of text fields
91
     *
92
     * @return  boolean
93
     */ 
94
    public function getConvertToAutocomplete()
95
    {
96
        return $this->convert_to_autocomplete;
97
    }
98
99
    /**
100
     * Set use autocomplete fields instead of text fields
101
     *
102
     * @param boolean $convert Convert?
103
     *
104
     * @return self
105
     */ 
106
    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...
107
    {
108
        $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...
109
        return $this;
110
    }
111
}
112