<?php
/**
 * Category layered navigation
 *
 * @see Mage_Catalog_Block_Layer_View
 * http://docs.magentocommerce.com/Mage_Catalog/Mage_Catalog_Block_Layer_View.html
 *
 * customized layered navigation
 * ./app/design/frontend/default/default/template/catalog/layer/my.view.phtml
 * based on
 * ./app/design/frontend/base/default/template/catalog/layer/view.phtml
 *
 * v2011-02-08 by panticz
 */
?>

<?php
function is_filter($_filters, $_content = "") {
    if(strlen($_content) > 0) {
       return true;
    } else {
        foreach ($_filters as $_filter) {
            if($_filter->getItemsCount() > 1) {
                return true;    
            }
        }
    }

    return false;
}

/* overide this function to disable filter on some category */
function checkAttribute($_filter) {
    return true;

    /* example: show this filter only in this category
    $category = array(
        "diagonal" => array("TV", "LCD", "Plasma"),
        "resolution" => array("Beamer")
    );

    $currentCategory = Mage::registry('current_category');

    if($_filter->getAttributeModel() != null) {
        $attributeCode = $_filter->getAttributeModel()->getAttributeCode();

        // check if filter for attribute exists
        if (array_key_exists($attributeCode, $filter)) {
            // find top category name
            while($currentCategory->getLevel() > 3) {
                $currentCategory = $currentCategory->getParentCategory();
            }

            // check if top category in filter
            if(in_array($currentCategory->getName(), $filter[$attributeCode])) {
                return true;
            } else {
                return false;
            }
        }
    }
        
    return true;
    */
}

/* overide this function to disable filter on some pages */
function showFilter() {
    return true;

    /* example: dont show filter in root category
    if(Mage::registry('current_category')->getUrlPath() == "shop.html") {
        return false;
    } else {
        return true;
    }
    */
}
?>

<?php if($this->canShowBlock() && showFilter()): ?>
    <?php $_filters = $this->getFilters() ?>
    <?php if(is_filter($_filters, $this->getStateHtml())): ?>
        <div class="block block-layered-nav">
            <div class="block-content">
            <?php echo $this->getStateHtml() ?>
            <?php if($this->canShowOptions()): ?>
                <?php if(is_filter($_filters)): ?>
                    <p class="block-subtitle"><?php echo $this->__('Shopping Options') ?></p>
                    <?php foreach ($_filters as $_filter): ?>
                        <?php if($_filter->getItemsCount() > 1 && checkAttribute($_filter)): ?>
                            <div class="block-layered-nav-filter">
                                <div class="block-layered-nav-filter-name">
                                    <ol class="currently">
                                        <li><span class="label"><?php echo $this->__($_filter->getName()) ?>:</span></li>
                                    </ol>
                                </div>
                                <div class="block-layered-nav-filter-value"><?php echo $_filter->getHtml() ?></div>
                            </div>
                        <?php endif; ?>
                    <?php endforeach; ?>
                <?php endif; ?>
            <?php endif; ?>
            </div>
        </div>
    <?php endif; ?>
<?php endif; ?>
