<?php
/**
 * An implementation of the search for the blog article.
 *
 * @package koubel homepage
 * @author Miroslav Kubelik (koubel@volny.cz)
 * @copyright Copyright (c) 2007-2009 Miroslav Kubelik
 * @license http://opensource.org/licenses/gpl-license.php GNU GPL
 */

/**
 * Search class for the blog article.
 *
 * @package koubel homepage
 * @author Miroslav Kubelik (koubel@volny.cz)
 * @copyright Copyright (c) 2007-2009 Miroslav Kubelik
 * @license http://opensource.org/licenses/gpl-license.php GNU GPL
 */
class Blog_Model_Search_Article extends Blog_Model_Search
{
    /**
     * order constant
     */
    const ORDER_POSTED = 1;

    /**
     * Sets dao, source db table, order constant. 
     */
    protected function __construct()
    {
        $this->_order = self::ORDER_POSTED;
        $this->_fromTables = array(Blog_Model_Dao_Article::TABLE_NAME);
        parent::__construct();
    }

    /**
     * Sets search parameters 
     */
    public function setParams(array $arr)
    {
        //remove empty parameters, these are not important
        MKLib_Utils::arrayFilterEmpty($arr);

        parent::setParams($arr);
        //set text as searchText parametr and unset text 
        if (isset($this->_searchParams["text"])) {
            $this->_searchParams["searchText"]=$this->_searchParams["text"];
            unset($this->_searchParams["text"]);
        } 
    }

    /**
     * Search items.
     * @return array array of found articles and their properties.
     */
    public function searchItems()
    {
     /*
      If we want search in a article text, we cannot use classic SQL method, because
      we have no method how strip tags as DB function (maybe it is is possible in MySQL 5.x
      via stored procedure)
      but our hosting provider has 4.1 :-(.
      */
        if (isset($this->_searchParams["searchText"]))
            $searchedText=$this->_searchParams["searchText"];

        if (!empty($searchedText)) {
            return $this->_selectRowsByCode();
        }

        $select = $this->_buildSQL();

        if (!$this->_usePagination()) {
            return $select->query()->fetchAll();
        }

        $this->_paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
        $this->_paginator->setCurrentPageNumber($this->_pageNumber);
        $this->_paginator->setItemCountPerPage($this->_perPageItems);

        return $this->_paginator;
    }

    /**
     * Number of items in the search result.
     * @return string
     */
    public function itemsCount()
    {
        if (!isset($this->_searchParams["searchText"])) {
            $select = $this->_buildSQL(true);
            $this->_foundCount = $select->query()->fetchColumn();
        }
        return parent::itemsCount();
    }

    protected function _sqlWhereCond(Zend_Db_Select $select)
    {
        foreach ($this->_searchParams as $param=>$value) {
            switch ($param) {
            case "searchText":
                if (empty($value)) continue;
                $select->where("LENGTH('text')>0");
                break;
            case "title":
                if (empty($value)) continue;
                $select->where("$param regexp('$value')");
                break;
            case "author_id":
                if (empty($value)) continue;
                $select->where("$param = $value");
                break;
            case "from":
                if(empty($value)) continue;
                $date=new Zend_Date($value);    
                $select->where("posted >= '" . $date->get(Zend_Date::ISO_8601) . "'");
                break;
            case "to":
                if(empty($value)) continue;
                $date=new Zend_Date($value);    
                $select->where("posted <= '" . $date->get(Zend_Date::ISO_8601) . "'");
                break;
            default:
                break;
            }
        }
    }

    protected function _buildOrderSQL(Zend_Db_Select $select)
    {
        switch ($this->_order) {
        case self::ORDER_POSTED :
            $select->order("posted desc");
            break;
        default:
            break;
        }
    }

    /**
     * Row is matched, when searched text is in a title or
     * in a the article plain text.   
     */
    protected function _rowMatch($row)
    {
        $pattern="/{$this->_searchParams["searchText"]}/i";
        return ( preg_match($pattern,$row["plainText"]) ||
            preg_match($pattern,strip_tags($row["title"]))
        );
    }
}
