<?php

namespace common\modules\product\widgets;

use common\modules\product\models\Category;
use yii\base\Widget;

class catalogSubmenuWidget extends Widget {
    public $root_id;
    public $rootClass = '';

    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
    }

    public function run() {
        /** @var Category $rootCategory */
        $rootCategory = Category::findOne($this->root_id);

        $categories = $rootCategory->getAllChildren(2, [], 'categoryName')->all();
        $populary = [];
        foreach($categories as $category) {
            if ($category->populary) {
                $populary[] = $category;
            }
        }

        return $this->render('submenu', [
            'rootCategory' => $rootCategory,
            'rootClass' => $this->rootClass,
            'populary' => $populary,
            'items' => $rootCategory->buildTree($categories, $rootCategory->category_id)
        ]);
    }
}