contributions/models.searchindex/lib/helpers/searchindexrepository.cls.php
Go to the documentation of this file.00001 <?php 00002 /** 00003 * Repository for search index. Used to register models and implementation 00004 * 00005 * @author Gerd Riesselmann 00006 * @ingroup SearchIndex 00007 */ 00008 class SearchIndexRepository { 00009 private static $index = null; 00010 private static $model_rules = array(); 00011 00012 /** 00013 * Set the index implementation 00014 */ 00015 public static function set_index_implementation(ISearchIndex $index) { 00016 self::$index = $index; 00017 } 00018 00019 /** 00020 * Get the index implementation 00021 * 00022 * @return ISearchIndex 00023 */ 00024 public static function get_index_implementation() { 00025 if (self::$index) { 00026 return clone(self::$index); 00027 } else { 00028 $model = Config::get_value(ConfigSearchIndex::TABLE_NAME); 00029 return DB::create($model); 00030 } 00031 } 00032 00033 /** 00034 * Add a model rule 00035 */ 00036 public static function add_model_rule(SearchIndexModelRule $rule) { 00037 self::$model_rules[$rule->model] = $rule; 00038 } 00039 00040 /** 00041 * Add a model 00042 */ 00043 public static function add_model($model, $id = false, $weight = 1) { 00044 if (empty($id)) { 00045 $id = count(self::$model_rules) + 1; 00046 } 00047 self::add_model_rule(new SearchIndexModelRule($model, $id, $weight)); 00048 } 00049 00050 public static function get_model_rules() { 00051 return self::$model_rules; 00052 } 00053 00054 /** 00055 * Retrieve rule for given model 00056 * 00057 * @return SearchIndexModelRule 00058 */ 00059 public static function get_model_rule($model) { 00060 return Arr::get_item(self::$model_rules, $model, false); 00061 } 00062 00063 /** 00064 * Return model id 00065 */ 00066 public static function get_model_id($model) { 00067 $ret = false; 00068 $rule = self::get_model_rule($model); 00069 if ($rule) { 00070 $ret = $rule->model_id; 00071 } 00072 return $ret; 00073 } 00074 00075 public static function get_model_for_id($id) { 00076 $ret = false; 00077 foreach(self::$model_rules as $model => $rule) { 00078 if ($rule->model_id == $id) { 00079 $ret = $model; 00080 break; 00081 } 00082 } 00083 return $ret; 00084 } 00085 00086 public static function get_model_rule_for_id($id) { 00087 $model = self::get_model_for_id($id); 00088 if ($model) { 00089 return self::get_model_rule($model); 00090 } 00091 return false; 00092 } 00093 }