<?php
/**
 * An implementation of the search for the blog trackback.
 *
 * @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 trackbacks.
 *
 * @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_BlogTrackBack 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_TrackBack::TABLE_NAME);
        parent::__construct(); 
    }

    /**
     * Search items.
     * @return array array of found trackback and their properties.
     */
    public function searchItems()
    {
        $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) {
            switch ($param) {
            case "title":
                if (empty($value)) continue;
                $select->where("$param regexp('$value')");
                break;
            case "excerpt":
                if (empty($value)) continue;
                $select->where("$param regexp('$value')");
                break;
            case "blogName":
                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;
            case "isSpam":
                if ($value == "undef") continue;
                if ($value) {
                    $select->where("isSpam = 1");
                } else {
                    $select->where("isSpam = 0");
                }
                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;
        }
    }
}
