<?php
/**
 * An implementation of the search for the blog comment.
 *
 * @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 comment.
 *
 * @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 Search_BLogComment extends Search_Blog
{
    /**
     * order constant
     */
    const ORDER_POSTED = 1;

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

    /**
     * Search items.
     * @return array array of found comments and their properties.
     */
    public function searchItems()
    {

/*
  If we want search in a comment 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["message"])) {
            $searchedText = $this->_searchParams["message"];
        }

        $searchedItems = array();

        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 (empty($this->_searchParams["message"])) {
            $select = $this->_buildSQL(false,true);
            $this->_foundCount = $select->query()->fetchColumn();
        }
        return parent::itemsCount();
    }

    protected function _sqlWhereCond(Zend_Db_Select $select)
    {
        //array with condition strings for every parameters in
        //searchParams array
        $conditions=array();

        foreach ($this->_searchParams as $param=>$value) {
            if ($param=="message") {
                $select->where("LENGTH('message')>0");
                continue;
            }
            switch ($param) {
            case "name":
                if (empty($value)) continue;
                $select->where("$param regexp('$value')");
                break;
            case "email":
                if (empty($value)) continue;
                $select->where("$param regexp('$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;
            }
        }

        return $conditions;
    }

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

    protected function _rowMatch($row)
    {
        $pattern="/{$this->_searchParams["message"]}/";
        return preg_match($pattern,strip_tags($row["message"]));
    }
}
