Version: current

     1 <?php
     2 /**
     3  * Provides a simple search engine for your site based on the MySQL FULLTEXT index
     4  *
     5  * @package sapphire
     6  * @subpackage search
     7  */
     8 class FulltextSearchable extends DataObjectDecorator {
     9     protected $searchFields;
    10
    11     /**
    12      * Enable the default configuration of MySQL full-text searching on the given data classes.
    13      */
    14     static function enable($searchableClasses = array('SiteTree', 'File')) {
    15         $defaultColumns = array(
    16             'SiteTree' => 'Title,MenuTitle,Content,MetaTitle,MetaDescription,MetaKeywords',
    17             'File' => 'Filename,Title,Content'
    18         );
    19
    20         if(!is_array($searchableClasses)) $searchableClasses = array($searchableClasses);
    21         foreach($searchableClasses as $class) {
    22                         if(method_exists($class, 'defaultSearchColumns')){
    23                             $c = singleton($class);
    24                             $defaultColumnsContex = $c->defaultSearchColumns();
    25                             Object::add_extension($class, "FulltextSearchable('{$defaultColumnsContex}')");
    26                         }else{
    27                             if(isset($defaultColumns[$class])) {
    28                                     Object::add_extension($class, "FulltextSearchable('{$defaultColumns[$class]}')");
    29                             } else {
    30                                     throw new Exception("FulltextSearchable::enable() I don't know the default search columns for class '$class'");
    31                             }
    32                         }
    33         }
    34
    35         Object::add_extension("ContentController", "ContentControllerSearchExtension");
    36     }
    37
    38     function __construct($searchFields) {
    39         if(is_array($searchFields)) $this->searchFields = implode(',', $searchFields);
    40         else $this->searchFields = $searchFields;
    41         parent::__construct();
    42     }
    43
    44     function extraStatics($class = null, $extension = null) {
    45         if($extension && preg_match('/\([\'"](.*)[\'"]\)/', $extension, $matches)) {
    46             $searchFields = $matches[1];
    47
    48             return array(
    49                 'indexes' => array(
    50                     "SearchFields" => Array(
    51                         'type'=>'fulltext',
    52                         'name'=>'SearchFields',
    53                         'value'=> $searchFields
    54                     ),
    55                 )
    56             );
    57         }
    58     }
    59 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -