| 1 | <?php |
||
| 2 | |||
| 3 | namespace LeKoala\SimpleSearch; |
||
| 4 | |||
| 5 | use PageController; |
||
|
0 ignored issues
–
show
|
|||
| 6 | use SilverStripe\ORM\ArrayList; |
||
| 7 | use SilverStripe\Core\ClassInfo; |
||
| 8 | use SilverStripe\ORM\DataObject; |
||
| 9 | use SilverStripe\Core\Extensible; |
||
| 10 | use SilverStripe\ORM\PaginatedList; |
||
| 11 | use SilverStripe\CMS\Model\SiteTree; |
||
| 12 | use SilverStripe\Core\Config\Config; |
||
| 13 | use SilverStripe\Control\HTTPRequest; |
||
| 14 | use SilverStripe\ErrorPage\ErrorPage; |
||
|
0 ignored issues
–
show
The type
SilverStripe\ErrorPage\ErrorPage 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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 15 | use SilverStripe\ORM\FieldType\DBField; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * A search controller |
||
| 19 | */ |
||
| 20 | class SimpleSearchController extends PageController |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Process and render search results. |
||
| 24 | */ |
||
| 25 | public function index(HTTPRequest $request = null) |
||
| 26 | { |
||
| 27 | $request = $this->getRequest(); |
||
| 28 | $Query = $request->getVar('q'); |
||
| 29 | $SearchList = new ArrayList(); |
||
| 30 | if ($Query) { |
||
| 31 | $FullQuery = str_replace(' ', '%', $Query); |
||
| 32 | |||
| 33 | // Search page by title/content |
||
| 34 | $excludedPageClasses = [ |
||
| 35 | ErrorPage::class, |
||
| 36 | ]; |
||
| 37 | $filters = [ |
||
| 38 | "Title:PartialMatch" => $FullQuery, |
||
| 39 | "Content:PartialMatch" => $FullQuery, |
||
| 40 | ]; |
||
| 41 | $Results = SiteTree::get()->filterAny($filters)->exclude('ClassName', $excludedPageClasses); |
||
| 42 | foreach ($Results as $Result) { |
||
| 43 | if ($Result->canView()) { |
||
| 44 | $SearchList->push($Result); |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | // Also search DataObjects in sitemap |
||
| 49 | $dataObjects = self::getSearchableDataObjects(); |
||
| 50 | foreach ($dataObjects as $dataObject) { |
||
| 51 | $sng = singleton($dataObject); |
||
| 52 | |||
| 53 | $filters = []; |
||
| 54 | if ($sng->hasMethod('getSearchFilters')) { |
||
| 55 | // Use dedicated filters |
||
| 56 | $filters = $sng->getSearchFilters(); |
||
| 57 | } else { |
||
| 58 | // Scaffold search based on fields |
||
| 59 | $fields = Config::inst()->get($dataObject, 'db'); |
||
| 60 | if (isset($fields['Title'])) { |
||
| 61 | $filters['Title:PartialMatch'] = $FullQuery; |
||
| 62 | } |
||
| 63 | if (isset($fields['Name'])) { |
||
| 64 | $filters['Name:PartialMatch'] = $FullQuery; |
||
| 65 | } |
||
| 66 | if (isset($fields['Content'])) { |
||
| 67 | $filters['Content:PartialMatch'] = $FullQuery; |
||
| 68 | } |
||
| 69 | if (isset($fields['Description'])) { |
||
| 70 | $filters['Description:PartialMatch'] = $FullQuery; |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | $Results = $dataObject::get()->filterAny($filters); |
||
| 75 | if ($Results) { |
||
| 76 | foreach ($Results as $Result) { |
||
| 77 | if ($Result->canView()) { |
||
| 78 | $SearchList->push($Result); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | } |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | $PaginatedList = new PaginatedList($SearchList, $request); |
||
| 86 | $data = array( |
||
| 87 | 'Results' => $PaginatedList, |
||
| 88 | 'Query' => DBField::create_field('Text', $Query), |
||
| 89 | 'Title' => _t('SimpleSearch.SearchResults', 'Search Results'), |
||
| 90 | 'YouSearchedFor' => _t('SimpleSearch.YouSearchFor', 'You searched for %s', [$Query]), |
||
| 91 | ); |
||
| 92 | return $this->customise($data)->renderWith(array('Page_results', 'Page')); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get all classes using the given extension |
||
| 97 | * |
||
| 98 | * @param boolean $strict |
||
| 99 | * @return array |
||
| 100 | */ |
||
| 101 | public static function getSearchableDataObjects($strict = false) |
||
| 102 | { |
||
| 103 | if (!class_exists(\Wilr\GoogleSitemaps\GoogleSitemap::class)) { |
||
| 104 | return []; |
||
| 105 | } |
||
| 106 | $extension = \Wilr\GoogleSitemaps\Extensions\GoogleSitemapExtension::class; |
||
| 107 | $classes = ClassInfo::getValidSubClasses(DataObject::class); |
||
| 108 | $extendedClasses = []; |
||
| 109 | foreach ($classes as $lc_class => $class) { |
||
| 110 | if ($class === SiteTree::class || is_subclass_of($class, SiteTree::class)) { |
||
| 111 | continue; |
||
| 112 | } |
||
| 113 | if (Extensible::has_extension($class, $extension, $strict)) { |
||
| 114 | $extendedClasses[] = $class; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | return $extendedClasses; |
||
| 118 | } |
||
| 119 | } |
||
| 120 |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths