| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Sunnysideup\SiteWideSearch\QuickSearches; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverStripe\Core\ClassInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\Core\Config\Configurable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\Core\Injector\Injector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | abstract class QuickSearchBaseClass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     use Configurable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     private static $is_enabled = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     public static function available_quick_searches() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         return ClassInfo::subclassesFor(self::class, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public function IsEnabled(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         return $this->Config()->get('is_enabled'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     abstract public function getTitle(): string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     abstract public function getClassesToSearch(): array; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     abstract public function getFieldsToSearch(): array; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function getSortOverride(): ?array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * Should return it like this: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * ```php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      *     'ClassName' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      *          'MyRelation.Title', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      *          'MyOtherRelation.Title', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      *      ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * ``` | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     public function getIncludedClassFieldCombos(): array | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * Should return it like this: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * ```php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      *     'MyClass' => MyClass::get()->filter(['MyField' => 'MyValue']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     public function getDefaultLists(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     public static function get_list_of_quick_searches(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $array = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             'limited' => 'Quick search (limited search)', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $availableSearchClasses = self::available_quick_searches(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         if (! empty($availableSearchClasses) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             foreach ($availableSearchClasses as $availableSearchClass) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 $singleton = Injector::inst()->get($availableSearchClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 if ($singleton->isEnabled()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                     $array[$availableSearchClass] = $singleton->getTitle(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $array['all'] = 'Full Search (please use with care - this will put a lot of strain on the server)'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         return $array; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |  |