Changeset 806
- Timestamp:
- 10/10/08 21:03:41 (8 weeks ago)
- Location:
- trunk/app
- Files:
-
- 6 modified
-
controllers/components/search.php (modified) (4 diffs)
-
controllers/entries_controller.php (modified) (1 diff)
-
locale/spa/LC_MESSAGES/default.po (modified) (1 diff)
-
views/elements/search.ctp (modified) (1 diff)
-
views/entries/search.ctp (modified) (1 diff)
-
views/layouts/portal.ctp (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/app/controllers/components/search.php
r404 r806 1 1 <?php 2 2 /** 3 * 4 * GPLv3 Chipotle Software 2002-20085 * 3 * Manuel Montoya 4 * GPLv3 Chipotle Software 2002-2008 5 * Full Text Search Engine -- Postgresql 8.3 6 6 **/ 7 7 App::import('Model', 'Entry'); … … 12 12 App::import('Model', 'Lesson'); 13 13 14 class SearchComponent extends Object 15 { 14 class SearchComponent extends Object { 16 15 17 public $controller = true;16 public $controller = true; 18 17 19 /** 18 private $Keywords = null; 19 20 private $lang = ''; 21 22 public $Data = array(); 23 24 /** 20 25 * Startup - Link the component to the controller. 21 26 * … … 24 29 public function startup(&$controller) 25 30 { 26 $this->controller =& $controller; 31 $this->controller =& $controller; 32 $this->Models = array('Entry', 'News'); // models to search in 33 if ($this->controller->Auth->user('lang')): 34 $this->lang = 'es'; //$this->controller->Auth->user('lang'); 35 else: 36 $this->lang = 'en'; 37 endif; 27 38 } 28 39 … … 30 41 * Build and execute search query 31 42 */ 32 33 private $Models = null; // fields to search 34 35 private $Keywords = null; 36 37 private $Fields = null; 38 39 private $Limit = null; 40 41 private $Order = null; 42 43 public $Data = array(); 44 43 45 44 public function getRows($keywords) 46 { 47 //at least three letters 48 if (strlen($keywords) < 3) 49 { 45 { 46 if (strlen($keywords) < 3): 50 47 return null; 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 } 130 88 } 131 89 ?> -
trunk/app/controllers/entries_controller.php
r785 r806 110 110 $this->Sanitize = new Sanitize; 111 111 112 $this->Sanitize->paranoid($this->data['Entry']['terms']); 113 $q = 'SELECT id, title FROM entries WHERE '; 114 $q .= ' to_tsvector(title || body) @@ to_tsquery(\''.$this->data['Entry']['terms'].'\') '; 115 $q .= ' ORDER BY created DESC LIMIT 10'; 116 112 $this->Sanitize->paranoid($this->data['Entry']['terms']); 113 117 114 $this->set('data', $this->Search->getRows($this->data['Entry']['terms'])); 118 115 } -
trunk/app/locale/spa/LC_MESSAGES/default.po
r758 r806 4 4 msgid "Yes" 5 5 msgstr "Si" 6 7 msgid "Search Results" 8 msgstr "Resultados de búsqueda" 6 9 7 10 msgid "Virtual Class" -
trunk/app/views/elements/search.ctp
r404 r806 25 25 <?php 26 26 echo $form->create('Entry', array('action'=>'search', 'onsubmit'=>"return chkSearch()")); 27 echo $form->input('Entry.terms', array('label'=>'', 'size' =>12, 'value' => "Search...", "maxlength"=> 40,27 echo $form->input('Entry.terms', array('label'=>'', 'size' =>12, 'value' => 'Search...', 'maxlength' => 40, 28 28 "onblur" => "if(this.value=='') this.value='Search...';", 29 29 "onfocus"=>"if(this.value=='Search...') this.value='';")); -
trunk/app/views/entries/search.ctp
r541 r806 1 1 <?php 2 //die(debug($data)); 3 echo $html->div('title_portal', __('Search Results', true)); 2 4 3 echo $html->div('title_section','Results'); 5 if ( count($data) < 1 ): 6 echo $html->div(null, 'Sorry, no search results'); 7 else: 8 $num = (count($data['News']) + count($data['Entry'])); 9 echo $html->div(null, $num.' results found'); 10 endif; 4 11 5 echo '<div style="padding:4px;margin:4px;">'; 12 if (count($data['Entry']) > 0 ): 13 echo $html->div(null, 'Edublogs', array('style'=>'margin-top:10px;padding:4px;background-color:#ecebeb;font-size:13pt;')); 14 foreach($data['Entry'] as $e): 15 $tmp = $html->link($e[0]['title'], '/entries/view/'.$e[0]['id']).'<br />'; 16 $tmp .= 'Rank:'.$e[0]['rank'] .'<br /> '. $e[0]['ts_headline']; 17 echo $html->div(null, $tmp, array('style'=>'padding:4px;font-size:10pt;')); 18 endforeach; 19 endif; 6 20 7 //die(print_r($data)); 21 if (count($data['News']) > 0 ): 22 echo $html->div(null, __('News', true), array('style'=>'margin-top:10px;padding:4px;background-color:#ecebeb;font-size:13pt;')); 23 foreach($data['News'] as $n): 24 $tmp = $html->link($n[0]['title'], '/news/view/'.$n[0]['id']).'<br />'; 25 $tmp .= 'Rank:'.$n[0]['rank'] .'<br /> '. $n[0]['ts_headline']; 26 echo $html->div(null, $tmp, array('style'=>'padding:4px;font-size:10pt;')); 27 endforeach; 28 endif; 8 29 9 $Models = array('Entry'=>'entries', 'News'=>'news', 'Podcast'=>'podcasts', 'Faq'=>'faqs', 'Glossary'=>'glossaries', 'Lesson'=>'lessons');10 11 $i = 0;12 13 foreach( $Models as $model => $controller)14 {15 if (count($data[$model]) > 1 )16 {17 $i++;18 echo '<div style="padding:4px;background-color:#ecebeb;font-size:14pt;">' . $model . '</div>';19 echo "<p>" .count($data[$model]) . " results found</p>";20 21 foreach ($data[$model] as $val)22 {23 switch($model)24 {25 case 'Entry':26 echo '<p>';27 echo $html->link($val[$model]['title'], '/users/entry/' .$val[$model]['id'].'/'.$val[$model]['id']);28 echo '</p>';29 break;30 31 case 'News':32 echo '<p><a href="/users/entry/' .$val[$model]['id'].'/'.$val[$model]['id'].'">' . $val[$model]['title'] . '</a></p>';33 break;34 35 case 'Podcast':36 echo '<p><a href="/users/entry/' .$val[$model]['id'].'/'.$val[$model]['id'].'">' . $val[$model]['title'] . '</a></p>';37 break;38 39 case 'Faq':40 echo '<p><a href="/users/entry/' .$val[$model]['id'].'/'.$val[$model]['id'].'">' . $val[$model]['question'] . '</a></p>';41 break;42 43 case 'Glossary':44 echo '<p><a href="/users/entry/' .$val[$model]['id'].'/'.$val[$model]['id'].'">' . $val[$model]['item'] . '</a></p>';45 break;46 47 case 'Lesson':48 echo '<p><a href="/users/entry/' .$val[$model]['id'].'/'.$val[$model]['id'].'">' . $val[$model]['title'] . '</a></p>';49 break;50 }51 }52 }53 }54 55 if ( $i == 0):56 echo "<h3>Sorry, no results found.</h3>";57 endif;58 30 ?> 59 </div> -
trunk/app/views/layouts/portal.ctp
r682 r806 96 96 97 97 // search 98 //$list = array('<h2 class="bg3">'.__('Search', true).'</h2>'.$this->element('search'));99 //echo $html->nestedList($list);98 $list = array('<h2 class="bg3">'.__('Search', true).'</h2>'.$this->element('search')); 99 echo $html->nestedList($list); 100 100 101 101 $tmp = $html->para(null,$html->link($html->image('static/valid_xhtml10_80x15_22.png'), 'http://www.w3.org/TR/xhtml1/', null, null, false));
