Passed
Push — master ( 382c9c...930971 )
by Nicolaas
03:34
created

QuickSearchBaseClass::getDefaultLists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Sunnysideup\SiteWideSearch\QuickSearches;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Core\ClassInfo;
7
use SilverStripe\Core\Config\Configurable;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Forms\Form;
10
11
abstract class QuickSearchBaseClass
12
{
13
    use Configurable;
14
15
    private static $is_enabled = true;
16
    public static function available_quick_searches()
17
    {
18
        return ClassInfo::subclassesFor(self::class, false);
19
    }
20
    public function IsEnabled(): bool
21
    {
22
        return $this->Config()->get('is_enabled');
23
    }
24
25
    abstract public function getTitle(): string;
26
    abstract public function getClassesToSearch(): array;
27
    abstract public function getFieldsToSearch(): array;
28
    public function getSortOverride(): ?array
29
    {
30
        return null;
31
    }
32
33
    /**
34
     * Should return it like this:
35
     * ```php
36
     * [
37
     *     'ClassName' => [
38
     *          'MyRelation.Title',
39
     *          'MyOtherRelation.Title',
40
     *      ]
41
     *
42
     * ]
43
     * ```
44
     * @return array
45
     */
46
    public function getIncludedClassFieldCombos(): array
47
    {
48
        return [];
49
    }
50
51
    /**
52
     * Should return it like this:
53
     * ```php
54
     * [
55
     *     'MyClass' => MyClass::get()->filter(['MyField' => 'MyValue']),
56
     *
57
     * ]
58
     * ```
59
     * @return array
60
     */
61
    public function getDefaultLists(): array
62
    {
63
        return [];
64
    }
65
    public static function get_list_of_quick_searches(): array
66
    {
67
        $array = [
68
            'limited' => 'Limited search',
69
        ];
70
        $availableSearchClasses = self::available_quick_searches();
71
        if(!empty($availableSearchClasses) > 0) {
72
            foreach($availableSearchClasses as $availableSearchClass) {
73
                $singleton = Injector::inst()->get($availableSearchClass);
74
                if($singleton->isEnabled()) {
75
                    $array[$availableSearchClass] = $singleton->getTitle();
76
                }
77
            }
78
        }
79
        $array['all'] = 'All (careful - may result in memory and time out issues)';
80
        return $array;
81
82
    }
83
84
}
85