Ejemplo n.º 1
0
 public function search($query, $comparison = 'like')
 {
     $ageChange = $this->model->cache()->get(sha1(jdb($this->model->db, $this->model->table)->dir));
     $keyCache = 'cache::index::' . $this->model->db . '_' . APPLICATION_ENV . '::' . $this->model->table . '::' . sha1(serialize($this->fields)) . '::' . sha1($query . $comparison);
     $keyCacheData = $keyCache . '::data';
     $keyCacheAge = $keyCache . '::age';
     $age = $this->model->cache()->get($keyCacheAge);
     if (!strlen($age) || $age < $ageChange) {
         $keys = $this->keys($query);
         $collection = [];
         $tuples = [];
         if (count($keys)) {
             foreach ($this->fields as $field) {
                 $rows = $this->model->cache()->keys('indexes::' . $this->model->db . '_' . APPLICATION_ENV . '::' . $this->model->table . '::*');
                 if (count($rows)) {
                     foreach ($rows as $row) {
                         list($rowDummy, $rowDb, $rowTable, $rowValue) = explode('::', $row, 3);
                         $subRows = $this->model->cache()->hgetall($row);
                         if (count($subRows)) {
                             foreach ($subRows as $index => $val) {
                                 list($ind, $rowField) = explode('::', $index, 2);
                                 if ($rowField == $field) {
                                     $dbRow = jdb($this->model->db, $this->model->table)->find($ind, false);
                                     if ($dbRow) {
                                         $compare = isAke($dbRow, $field, false);
                                         if (false !== $compare) {
                                             foreach ($keys as $compareKey) {
                                                 if ('like' === $comparison) {
                                                     $check = fnmatch("*{$compareKey}*", $rowValue);
                                                 } elseif ('strict' == $comparison) {
                                                     $check = sha1($compareKey) == sha1($rowValue);
                                                 } elseif ('phonetic' == $comparison) {
                                                     $phonetic = Phonetic::instance();
                                                     $similarity = $phonetic->similarity($rowValue, $compareKey);
                                                     $check = $similarity <= $phonetic->getTolerance();
                                                 }
                                                 if (true === $check && !Arrays::in($ind, $tuples)) {
                                                     array_push($collection, $dbRow);
                                                     array_push($tuples, $ind);
                                                     break;
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         $this->model->cache()->set($keyCacheData, serialize($collection));
         $this->model->cache()->set($keyCacheAge, time());
     } else {
         $collection = unserialize($this->model->cache()->get($keyCacheData));
     }
     return new Collection($collection);
 }
Ejemplo n.º 2
0
 function searchSegmentsByPhonetic($universe, $word, $limit = 0, $tuples = [])
 {
     $collection = [];
     $keyCacheData = 'phonetics.data.' . sha1(serialize(func_get_args()));
     $keyCacheAge = 'phonetics.age.' . sha1(serialize(func_get_args()));
     $segType = Model::Segmenttype()->where(['name', '=', $universe])->first(true);
     if ($segType) {
         $segments = Model::Segment()->where(['segmenttype_id', '=', $segType->id])->exec();
         $age = Model::Segment()->getAge();
         $ageCache = Model::Segment()->cache()->get($keyCacheAge);
         if (strlen($ageCache)) {
             if ($ageCache > $age) {
                 $data = Model::Segment()->cache()->get($keyCacheData);
                 if (strlen($data)) {
                     return unserialize($data);
                 }
             }
         }
         foreach ($segments as $segment) {
             $data = repo('segment')->getData($segment['id']);
             $option_fondamentale = isAke($data, 'option_fondamentale', false);
             $option_suggeree = isAke($data, 'option_suggeree', false);
             $icon = isAke($data, 'icon', null);
             $img = isAke($data, 'img', null);
             $description = isAke($data, 'description', null);
             $is_item = isAke($data, 'is_item', false);
             if (is_string($is_item)) {
                 $is_item = 'oui' == strtolower($is_item) ? true : false;
             }
             if (!$option_fondamentale && !$option_suggeree) {
                 $score = Phonetik::proximity($segment['name'], $word);
                 $score = $score['score'];
                 if (60 <= $score) {
                     unset($segment['hash']);
                     $segment['is_item'] = $is_item;
                     $segment['icon'] = $icon;
                     $segment['img'] = $img;
                     $segment['description'] = $description;
                     $segment['score'] = $score;
                     if (true === $is_item) {
                         $father = Model::Segment()->find($segment['segment_id']);
                         if ($father) {
                             $father = Model::Segment()->find($father->segment_id);
                             if ($father) {
                                 $segment['cat'] = $father->name;
                             }
                         }
                     }
                     if (!Arrays::in($segment['id'], $tuples)) {
                         array_push($collection, $segment);
                         array_push($tuples, $segment['id']);
                     }
                     if ($limit > 0) {
                         if (count($collection) == $limit) {
                             break;
                         }
                     }
                 }
             }
         }
     }
     if (fnmatch('* *', $word)) {
         $words = explode(' ', $word);
         foreach ($words as $wd) {
             if (strlen($wd) > 1) {
                 $collection = array_merge($collection, searchSegmentsByPhonetic($universe, $wd, 0, $tuples));
             }
         }
     }
     if (!empty($collection)) {
         $collection = arrayOrderBy($collection, 'score', SORT_DESC);
     }
     Model::Segment()->cache()->set($keyCacheAge, time());
     Model::Segment()->cache()->set($keyCacheData, serialize($collection));
     return $collection;
 }