<?php
/**
 * @version 7.9 [rev.7.9.01]
 */
/* @var string $htmlHeader */
/* @var \Ppb\Db\Table\Rowset\Categories $categories */

echo $this->renderHtml($htmlHeader, true); ?>
<div class="masonry">
    <?php
    /** @var \Ppb\Db\Table\Row\Category $category */
    foreach ($categories as $category) {
        $counter = $category->getCounter();

        if ($counter > 0 || !$this->settings['hide_empty_categories']) {
            $children = $category->findDependentRowset('\Ppb\Db\Table\Categories');
            $hasChildren = count($children);
            $urlParams = ($hasChildren > 0) ?
                array('category_name' => $category['name'], 'parent_id' => $category['id']) :
                array('module' => 'listings', 'controller' => 'browse', 'action' => 'index', 'category_name' => $category['name'], 'parent_id' => $category['id']);
            $urlParams = ($hasChildren > 0) ? $category->browseLink() : $category->link();

            ?>

            <div class="col-sm-3 col-xs-6 item">
                <h2 <?php echo (!empty($category['logo_path'])) ?
                    "class='category-title' style='background: #fff url(\"" . $this->thumbnail()->generateLink($category['logo_path'],
                        150, true) . "\") no-repeat center center;' " : ''; ?>>
                    <a href="<?php echo $this->url($urlParams); ?>">
                        <?php echo $this->_($category['name']); ?>
                        <?php
                        if ($this->settings['category_counters']) {
                            echo '<small>(' . $counter . ')</small>';
                        }
                        ?></a>
                </h2>
                <?php if ($hasChildren > 0) {
                    /** @var \Ppb\Db\Table\Row\Category $subcategory */
                    foreach ($children as $subcategory) {
                        $subcategoryCounter = $subcategory->getCounter();

                        if ($subcategoryCounter > 0 || !$this->settings['hide_empty_categories']) {
                            ?>
                            <div>
                                <a href="<?php echo $this->url($subcategory->link()); ?>">
                                    <?php echo $this->_($subcategory['name']); ?>


                                    <?php
                                    if ($this->settings['category_counters']) {
                                        echo '<small>(' . $subcategoryCounter . ')</small>';
                                    }
                                    ?></a>
                            </div>
                            <?php
                        }
                    }
                } ?>
            </div>
            <?php
        }
    } ?>
</div>

<?php if ($parentId) { ?>
    <?php $featuredItems = $this->action('listings', 'browse', 'listings',
        array('type' => 'categories', 'parent_id' => $parentId)); ?>
    <?php if (!empty($featuredItems)) { ?>
        <div>
            <h3 class="headline">
                <span>
                    <?php echo $this->_('Featured Items from'); ?><?php echo $this->_($this->headline); ?></span>
            </h3>
            <?php echo $featuredItems; ?>
        </div>
    <?php } ?>
<?php } ?>