<?php

Yii::import('bootstrap.widgets.TbDataColumn');

/**
 * Bootstrap grid data column.
 */
class PlDataColumn extends TbDataColumn
{

    public $enableCustomDropdownFilter = false;
    public $enableCustomDateTimeSearchFilter = false;
    public $disableSearchField = false;

    /**
     * Renders the filter cell.
     */
    public function renderFilterCell()
    {
        if ($this->disableSearchField) {
            $this->getSearchFieldDisabledOpenTag();
        } else {
            $this->getFilterContainerOpenTag();
            $this->getCustomFilterCssClass();
        }
        $this->getFilterCloseTag();
    }

    protected function getSearchFieldDisabledOpenTag()
    {
        echo '<td class="searchFieldBackgroundDisabled"><div class="filter-container searchFieldDisabled">';
    }

    protected function getFilterContainerOpenTag()
    {
        echo '<td><div class="filter-container">';
    }

    protected function getCustomFilterCssClass()
    {
        if ($this->filter !== false && $this->grid->filter !== null && $this->name !== null && strpos($this->name, '.') === false) {
            if ($this->enableCustomDropdownFilter && is_array($this->filter)) {
                $this->getChtmlDropdownList();
            } else if ($this->enableCustomDateTimeSearchFilter) {
                $this->getDateTimeSearchField();
            } else {
                $this->getCustomInputSearchField();
            }
        }
    }

    protected function getChtmlDropdownList()
    {
        echo CHtml::activeDropDownList($this->grid->filter, $this->name, $this->filter, array('id' => false, 'prompt' => '', 'class' => 'dropdownSearchField'));
    }

    protected function getDateTimeSearchField()
    {
        echo CHtml::activeTextField($this->grid->filter, $this->name, array('id' => false, 'class' => 'dateTimeSearchField', 'maxLength' => 22));
    }

    protected function getCustomInputSearchField()
    {
        echo CHtml::activeTextField($this->grid->filter, $this->name, array('id' => false, 'maxLength' => 18));
    }

    protected function getFilterCloseTag()
    {
        echo '</div></td>';
    }
}
