Classes

File content/category/FormFieldCategoriesSelect.class.php

File content/category/FormFieldCategoriesSelect.class.php

 1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 
<?php
/**
 * @package     Content
 * @subpackage  Category
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Kevin MASSY <reidlos@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2014 12 22
 * @since       PHPBoost 3.0 - 2011 09 26
*/

class FormFieldCategoriesSelect extends FormFieldSimpleSelectChoice
{
    private $categories_cache;
    private $search_category_children_options;
    private $options = array();

    /**
     * Constructs a FormFieldCategoriesSelect.
     * @param string $id Field id
     * @param string $label Field label
     * @param mixed $value Default value (either a FormFieldEnumOption object or a string corresponding to the FormFieldEnumOption's raw value)
     * @param string[] $field_options Map of the field options (this field has no specific option, there are only the inherited ones)
     */
    public function __construct($id, $label, $value = 0, SearchCategoryChildrensOptions $search_category_children_options, $field_options = array(), CategoriesCache $categories_cache)
    {
        $this->categories_cache = $categories_cache;
        $this->search_category_children_options = $search_category_children_options;
        parent::__construct($id, $label, $value, $this->generate_options($value), $field_options);
    }

    private function generate_options($id_category)
    {
        $categories = $this->categories_cache->get_categories();
        $root_category = $categories[Category::ROOT_CATEGORY];

        if (($this->search_category_children_options->is_excluded_categories_recursive() && $this->search_category_children_options->category_is_excluded($root_category)) || !$this->search_category_children_options->check_authorizations($root_category))
        {
            return array();
        }

        if (!$this->search_category_children_options->category_is_excluded($root_category))
        {
            $this->options[] = new FormFieldSelectChoiceOption($root_category->get_name(), $root_category->get_id());
        }

        return $this->build_children_map($id_category, $categories, Category::ROOT_CATEGORY);
    }

    private function build_children_map($id_category, $categories, $id_parent, $node = 1)
    {
        foreach ($categories as $id => $category)
        {
            if ($category->get_id_parent() == $id_parent && $id != Category::ROOT_CATEGORY)
            {
                if ($this->search_category_children_options->check_authorizations($category) && !$this->search_category_children_options->category_is_excluded($category))
                    $this->options[] = new FormFieldSelectChoiceOption(str_repeat('--', $node) . ' ' . $category->get_name(), $id);

                if ($this->search_category_children_options->check_authorizations($category) && ($this->search_category_children_options->is_excluded_categories_recursive() ? !$this->search_category_children_options->category_is_excluded($category) : true) && $this->search_category_children_options->is_enabled_recursive_exploration())
                    $this->build_children_map($id_category, $categories, $id, ($node+1));
            }
        }
        return $this->options;
    }
}
?>