<?php
namespace front\factory;

class Search
{
  public static function search_results( $search_txt, $lang_id )
  {
    global $mdb, $cache;
    
    if ( strlen( $search_txt ) < 3 )
      return false;
    
    if ( !$articles = \Cache::fetch( "search_results:$search_txt:$lang_id" ) )
    {
      $results = $mdb -> query( 'SELECT * FROM ( '
                                      . 'SELECT '
                                          . 'a.id, '
                                          . '( CASE '
                                            . 'WHEN copy_from IS NULL THEN title '
                                            . 'WHEN copy_from IS NOT NULL THEN ( '
                                              . 'SELECT '
                                                . 'title '
                                              . 'FROM '
                                                . 'pp_articles_langs '
                                              . 'WHERE '
                                                . 'lang_id = al.copy_from AND article_id = a.id '
                                              . ') '
                                           . 'END ) AS title, '
                                          . '( CASE '
                                            . 'WHEN copy_from IS NULL THEN entry '
                                            . 'WHEN copy_from IS NOT NULL THEN ( '
                                              . 'SELECT '
                                                . 'entry '
                                              . 'FROM '
                                                . 'pp_articles_langs '
                                              . 'WHERE '
                                                . 'lang_id = al.copy_from AND article_id = a.id '
                                              . ') '
                                           . 'END ) AS entry, '
                                          . '( CASE '
                                            . 'WHEN copy_from IS NULL THEN text '
                                            . 'WHEN copy_from IS NOT NULL THEN ( '
                                              . 'SELECT '
                                                . 'text '
                                              . 'FROM '
                                                . 'pp_articles_langs '
                                              . 'WHERE '
                                                . 'lang_id = al.copy_from AND article_id = a.id '
                                              . ') '
                                           . 'END ) AS text '
                                      . 'FROM '
                                        . 'pp_articles AS a '
                                        . 'INNER JOIN pp_articles_langs AS al ON a.id = al.article_id '
                                      . 'WHERE '
                                        . 'status = 1 AND lang_id = \'' . $lang_id . '\' '
                                  . ') AS q1 '
                                  . 'WHERE '
                                      . 'q1.title IS NOT NULL '
                                    . 'AND '
                                      . '( '
                                            . 'LOWER( title ) LIKE \'%' . \S::escape( strtolower( $search_txt ) ) . '%\' '
                                          . 'OR '
                                            . 'LOWER( entry ) LIKE \'%' . \S::escape( strtolower( $search_txt ) ) . '%\' '
                                          . 'OR '
                                            . 'LOWER( text ) LIKE \'%' . \S::escape( strtolower( $search_txt ) ) . '%\' '
                                      . ')' ) -> fetchAll();
      if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row )
        $articles[] = \front\factory\Articles::article_details( $row['id'], $lang_id );
      
      \Cache::store( "search_results:$search_txt:$lang_id", $articles );      
    }
    return $articles;
  }
}
