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
class FormFieldCategoriesSelect extends FormFieldSimpleSelectChoice
{
private $categories_cache;
private $search_category_children_options;
private $options = array();
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;
}
}
?>