| 51 | | } |
| 52 | | |
| 53 | | $this->Models = array('Entry', 'News', 'Podcast', 'Faq', 'Glossary', 'Lesson'); // models to search in |
| 54 | | |
| 55 | | $this->Keywords = explode(' ', $keywords); // convert to array |
| 56 | | |
| 57 | | //exit(print_r($this->Keywords)); |
| 58 | | |
| 59 | | foreach ($this->Models as $model) |
| 60 | | { |
| 61 | | $conditions = "("; |
| 62 | | |
| 63 | | foreach ($this->Keywords as $f) |
| 64 | | { |
| 65 | | if ( strlen($conditions) > 5) |
| 66 | | { |
| 67 | | $conditions .= " OR "; |
| 68 | | } |
| 69 | | |
| 70 | | switch ($model) |
| 71 | | { |
| 72 | | case 'Entry': |
| 73 | | |
| 74 | | $conditions .= "Entry.body ~* '$f' OR Entry.title ~* '$f' AND Entry.status=1"; |
| 75 | | |
| 76 | | $this->Fields = array('Entry.id', 'Entry.title'); |
| 77 | | $this->Limit = 20; |
| 78 | | $this->Order = "Entry.id DESC"; |
| 79 | | break; |
| 80 | | |
| 81 | | case 'News': |
| 82 | | $conditions .= "News.body ~* '$f' OR News.title ~* '$f' AND News.status=1"; |
| 83 | | $this->Fields = array('News.id', 'News.title'); |
| 84 | | $this->Limit = 20; |
| 85 | | $this->Order = "News.id DESC"; |
| 86 | | break; |
| 87 | | |
| 88 | | case 'Faq': |
| 89 | | $conditions .= "Faq.question ~* '$f' OR Faq.answer ~* '$f'"; |
| 90 | | $this->Fields = array('Faq.id', 'Faq.question'); |
| 91 | | $this->Limit = 20; |
| 92 | | $this->Order = "Faq.id DESC"; |
| 93 | | break; |
| 94 | | |
| 95 | | case 'Podcast': |
| 96 | | $conditions .= "Podcast.title ~* '$f' OR Podcast.description ~* '$f' AND Podcast.status=1"; |
| 97 | | $this->Fields = array('Podcast.id', 'Podcast.title'); |
| 98 | | $this->Limit = 20; |
| 99 | | $this->Order = "Podcast.id DESC"; |
| 100 | | break; |
| 101 | | |
| 102 | | case 'Glossary': |
| 103 | | $conditions .= "Glossary.item ~* '$f' OR Glossary.definition ~* '$f'"; |
| 104 | | $this->Fields = array('Glossary.id', 'Glossary.item'); |
| 105 | | $this->Limit = 20; |
| 106 | | $this->Order = "Glossary.id DESC"; |
| 107 | | break; |
| 108 | | |
| 109 | | case 'Lesson': |
| 110 | | $conditions .= "Lesson.title ~* '$f' OR Lesson.body ~* '$f'"; |
| 111 | | $this->Fields = array('Lesson.id', 'Lesson.title'); |
| 112 | | $this->Limit = 20; |
| 113 | | $this->Order = "Lesson.id DESC"; |
| 114 | | break; |
| 115 | | } |
| 116 | | } |
| 117 | | |
| 118 | | $conditions .= ")"; |
| 119 | | |
| 120 | | $Model =& new $model; |
| 121 | | |
| 122 | | $this->Data[$model] = $Model->findAll($conditions, $this->Fields, $this->Order, $this->Limit); |
| 123 | | |
| 124 | | $conditions = ''; |
| 125 | | } |
| 126 | | |
| 127 | | return $this->Data; |
| 128 | | |
| 129 | | } |
| | 48 | endif; |
| | 49 | |
| | 50 | $this->Keywords = explode(' ', $keywords); // convert to array |
| | 51 | // build terms |
| | 52 | if ( count($this->Keywords) === 1): |
| | 53 | $t = $keywords; |
| | 54 | else: |
| | 55 | $t = (string) ''; |
| | 56 | for($i=0;count($this->Keywords)<=$i;$i++): |
| | 57 | $t .= $this->Keywords[$i]; |
| | 58 | if (count($this->Keywords) < $i): |
| | 59 | $t .= ' | '; |
| | 60 | endif; |
| | 61 | endfor; |
| | 62 | endif; |
| | 63 | |
| | 64 | foreach ($this->Models as $model): |
| | 65 | switch ($model) |
| | 66 | { |
| | 67 | case 'News': |
| | 68 | $q = "SELECT id, title, ts_headline('karamelo_".$this->lang."', body, to_tsquery('karamelo_".$this->lang."','".$t."')), "; |
| | 69 | $q .= "rank FROM ( SELECT id, title, substr(body,0,260) as body, ts_rank_cd(to_tsvector('karamelo_".$this->lang."', body), "; |
| | 70 | $q .= "to_tsquery('karamelo_".$this->lang."','".$t."')) AS rank FROM news, to_tsquery('karamelo_".$this->lang."','".$t."') "; |
| | 71 | $q .= "query WHERE to_tsquery('karamelo_".$this->lang."','".$t."') @@ to_tsvector('karamelo_".$this->lang."', body) "; |
| | 72 | $q .= "ORDER BY rank DESC LIMIT 20) AS foo"; |
| | 73 | break; |
| | 74 | case 'Entry': |
| | 75 | $q = "SELECT id, title, ts_headline('karamelo_".$this->lang."', body, to_tsquery('karamelo_".$this->lang."','".$t."')), "; |
| | 76 | $q .= "rank FROM ( SELECT id, title, substr(body,0,260) as body, ts_rank_cd(to_tsvector('karamelo_".$this->lang."', body), "; |
| | 77 | $q .= "to_tsquery('karamelo_".$this->lang."','".$t."')) AS rank FROM entries, to_tsquery('karamelo_".$this->lang."','".$t."') "; |
| | 78 | $q .= "query WHERE to_tsquery('karamelo_".$this->lang."','".$t."') @@ to_tsvector('karamelo_".$this->lang."', body) "; |
| | 79 | $q .= "ORDER BY rank DESC LIMIT 20) AS foo"; |
| | 80 | break; |
| | 81 | |
| | 82 | } |
| | 83 | $Model =& new $model; |
| | 84 | $this->Data[$model] = $Model->query($q); |
| | 85 | endforeach; |
| | 86 | return $this->Data; |
| | 87 | } |