* Object::add_extension('MyObject', "FulltextSearchable('MySearchableField,'MyOtherField')"); * * * Caution: This is a wrapper method that should only be used in _config.php, * and only be called once in your code. * * @param Array $searchableClasses The extension will be applied to all DataObject subclasses * listed here. Default: {@link SiteTree} and {@link File}. */ static function enable($searchableClasses = array('SiteTree', 'File')) { $defaultColumns = array( 'SiteTree' => 'Title,MenuTitle,Content,MetaTitle,MetaDescription,MetaKeywords', 'File' => 'Filename,Title,Content' ); if(!is_array($searchableClasses)) $searchableClasses = array($searchableClasses); foreach($searchableClasses as $class) { if(!class_exists($class)) continue; if(isset($defaultColumns[$class])) { Object::add_extension($class, "FulltextSearchable('{$defaultColumns[$class]}')"); Object::add_static_var($class, 'create_table_options', array('MySQLDatabase' => 'ENGINE=MyISAM')); } else { throw new Exception("FulltextSearchable::enable() I don't know the default search columns for class '$class'"); } } self::$searchable_classes = $searchableClasses; if(class_exists("ContentController")){ Object::add_extension("ContentController", "ContentControllerSearchExtension"); } } /** * @param Array|String $searchFields Comma-separated list (or array) of database column names * that can be searched on. Used for generation of the database index defintions. */ function __construct($searchFields) { if(is_array($searchFields)) $this->searchFields = implode(',', $searchFields); else $this->searchFields = $searchFields; parent::__construct(); } function extraStatics($class = null, $extension = null) { if($extension && preg_match('/\([\'"](.*)[\'"]\)/', $extension, $matches)) { $searchFields = $matches[1]; return array( 'indexes' => array( "SearchFields" => Array( 'type'=>'fulltext', 'name'=>'SearchFields', 'value'=> $searchFields ), ) ); } } /** * Shows all classes that had the {@link FulltextSearchable} extension applied through {@link enable()}. * * @return Array */ function get_searchable_classes() { return self::$searchable_classes; } }