Changeset 806 for trunk/app/controllers

Show
Ignore:
Timestamp:
10/10/08 21:03:41 (6 weeks ago)
Author:
aarkerio
Message:

Little changes

Location:
trunk/app/controllers
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/app/controllers/components/search.php

    r404 r806  
    11<?php 
    22/** 
    3 * 
    4 * GPLv3 Chipotle Software 2002-2008  
    5 * 
     3*  Manuel Montoya 
     4*  GPLv3 Chipotle Software 2002-2008  
     5*  Full Text Search Engine   --   Postgresql 8.3 
    66**/ 
    77App::import('Model', 'Entry'); 
     
    1212App::import('Model', 'Lesson'); 
    1313 
    14 class SearchComponent extends Object 
    15 { 
     14class SearchComponent extends Object { 
    1615 
    17 public $controller = true;  
     16 public $controller = true;  
    1817 
    19 /** 
     18 private $Keywords = null; 
     19 
     20 private $lang     = ''; 
     21  
     22 public $Data      = array(); 
     23 
     24 /** 
    2025 * Startup - Link the component to the controller. 
    2126 * 
     
    2429 public function startup(&$controller) 
    2530 { 
    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; 
    2738 } 
    2839  
     
    3041 * Build and execute search query 
    3142 */ 
    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   
    4544 public function getRows($keywords)  
    46  {     
    47       //at least three letters 
    48       if (strlen($keywords) < 3) 
    49       { 
     45 { 
     46  if (strlen($keywords) < 3): 
    5047         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 } 
    13088} 
    13189?> 
  • trunk/app/controllers/entries_controller.php

    r785 r806  
    110110  $this->Sanitize = new Sanitize; 
    111111       
    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   
    117114  $this->set('data', $this->Search->getRows($this->data['Entry']['terms'])); 
    118115 }