public function get_root_category()
 {
     $root = new RichRootCategory();
     $root->set_authorizations(FaqConfig::load()->get_authorizations());
     $root->set_description(FaqConfig::load()->get_root_category_description());
     return $root;
 }
    public function build_view(HTTPRequestCustom $request)
    {
        $authorized_categories = FaqService::get_authorized_categories(Category::ROOT_CATEGORY);
        $mode = $request->get_getstring('sort', FaqUrlBuilder::DEFAULT_SORT_MODE);
        $field = $request->get_getstring('field', FaqUrlBuilder::DEFAULT_SORT_FIELD);
        $sort_mode = $mode == 'asc' ? 'ASC' : 'DESC';
        switch ($field) {
            case 'question':
                $sort_field = FaqQuestion::SORT_ALPHABETIC;
                break;
            default:
                $sort_field = FaqQuestion::SORT_DATE;
                break;
        }
        $result = PersistenceContext::get_querier()->select('SELECT *
		FROM ' . FaqSetup::$faq_table . ' faq
		LEFT JOIN ' . DB_TABLE_MEMBER . ' member ON member.user_id = faq.author_user_id
		WHERE approved = 0
		AND faq.id_category IN :authorized_categories
		' . (!FaqAuthorizationsService::check_authorizations()->moderation() ? ' AND faq.author_user_id = :user_id' : '') . '
		ORDER BY ' . $sort_field . ' ' . $sort_mode, array('authorized_categories' => $authorized_categories, 'user_id' => AppContext::get_current_user()->get_id()));
        $this->tpl->put_all(array('C_QUESTIONS' => $result->get_rows_count() > 0, 'C_PENDING' => true, 'C_MORE_THAN_ONE_QUESTION' => $result->get_rows_count() > 1, 'C_DISPLAY_TYPE_ANSWERS_HIDDEN' => FaqConfig::load()->is_display_type_answers_hidden(), 'QUESTIONS_NUMBER' => $result->get_rows_count()));
        while ($row = $result->fetch()) {
            $faq_question = new FaqQuestion();
            $faq_question->set_properties($row);
            $this->tpl->assign_block_vars('questions', $faq_question->get_array_tpl_vars());
        }
        $result->dispose();
        $this->build_sorting_form($field, $mode);
    }
 private function save()
 {
     $this->config->set_categories_number_per_page($this->form->get_value('categories_number_per_page'));
     $this->config->set_columns_number_per_line($this->form->get_value('columns_number_per_line'));
     $this->config->set_display_type($this->form->get_value('display_type')->get_raw_value());
     $this->config->set_root_category_description($this->form->get_value('root_category_description'));
     $this->config->set_authorizations($this->form->get_value('authorizations')->build_auth_array());
     FaqConfig::save();
     FaqService::get_categories_manager()->regenerate_cache();
     FaqCache::invalidate();
 }
    private function build_view(HTTPRequestCustom $request)
    {
        $config = FaqConfig::load();
        $authorized_categories = FaqService::get_authorized_categories($this->get_category()->get_id());
        $subcategories_page = AppContext::get_request()->get_getint('subcategories_page', 1);
        $subcategories_number = count(FaqService::get_categories_manager()->get_categories_cache()->get_childrens($this->get_category()->get_id()));
        $pagination = $this->get_subcategories_pagination($subcategories_number, $config->get_categories_number_per_page(), $subcategories_page);
        //Children categories
        $result = PersistenceContext::get_querier()->select('SELECT @id_cat:= faq_cats.id, faq_cats.*,
		(SELECT COUNT(*) FROM ' . FaqSetup::$faq_table . '
			WHERE id_category IN (
				@id_cat,
				(SELECT GROUP_CONCAT(id SEPARATOR \',\') FROM ' . FaqSetup::$faq_cats_table . ' WHERE id_parent = @id_cat), 
				(SELECT GROUP_CONCAT(childs.id SEPARATOR \',\') FROM ' . FaqSetup::$faq_cats_table . ' parents
				INNER JOIN ' . FaqSetup::$faq_cats_table . ' childs ON parents.id = childs.id_parent
				WHERE parents.id_parent = @id_cat)
			)
			AND approved = 1
		) AS questions_number
		FROM ' . FaqSetup::$faq_cats_table . ' faq_cats
		WHERE id_parent = :id_category
		AND id IN :authorized_categories
		ORDER BY id_parent, c_order
		LIMIT :number_items_per_page OFFSET :display_from', array('id_category' => $this->category->get_id(), 'authorized_categories' => $authorized_categories, 'number_items_per_page' => $pagination->get_number_items_per_page(), 'display_from' => $pagination->get_display_from()));
        $nbr_cat_displayed = 0;
        while ($row = $result->fetch()) {
            $category_image = new Url($row['image']);
            $this->tpl->assign_block_vars('sub_categories_list', array('C_CATEGORY_IMAGE' => !empty($row['image']), 'C_MORE_THAN_ONE_QUESTION' => $row['questions_number'] > 1, 'CATEGORY_NAME' => $row['name'], 'CATEGORY_IMAGE' => $category_image->rel(), 'QUESTIONS_NUMBER' => $row['questions_number'], 'U_CATEGORY' => FaqUrlBuilder::display_category($row['id'], $row['rewrited_name'])->rel()));
            $nbr_cat_displayed++;
        }
        $result->dispose();
        $nbr_column_cats = $nbr_cat_displayed > $config->get_columns_number_per_line() ? $config->get_columns_number_per_line() : $nbr_cat_displayed;
        $nbr_column_cats = !empty($nbr_column_cats) ? $nbr_column_cats : 1;
        $cats_columns_width = floor(100 / $nbr_column_cats);
        $result = PersistenceContext::get_querier()->select('SELECT *
		FROM ' . FaqSetup::$faq_table . ' faq
		LEFT JOIN ' . DB_TABLE_MEMBER . ' member ON member.user_id = faq.author_user_id
		WHERE approved = 1
		AND faq.id_category = :id_category
		ORDER BY q_order ASC', array('id_category' => $this->get_category()->get_id()));
        $category_description = FormatingHelper::second_parse($this->get_category()->get_description());
        $this->tpl->put_all(array('C_CATEGORY' => true, 'C_ROOT_CATEGORY' => $this->get_category()->get_id() == Category::ROOT_CATEGORY, 'C_HIDE_NO_ITEM_MESSAGE' => $this->get_category()->get_id() == Category::ROOT_CATEGORY && ($nbr_cat_displayed != 0 || !empty($category_description)), 'C_CATEGORY_DESCRIPTION' => !empty($category_description), 'C_SUB_CATEGORIES' => $nbr_cat_displayed > 0, 'C_QUESTIONS' => $result->get_rows_count() > 0, 'C_MORE_THAN_ONE_QUESTION' => $result->get_rows_count() > 1, 'C_DISPLAY_TYPE_ANSWERS_HIDDEN' => $config->is_display_type_answers_hidden(), 'C_MODERATION' => FaqAuthorizationsService::check_authorizations($this->get_category()->get_id())->moderation(), 'C_SUBCATEGORIES_PAGINATION' => $pagination->has_several_pages(), 'SUBCATEGORIES_PAGINATION' => $pagination->display(), 'CATS_COLUMNS_WIDTH' => $cats_columns_width, 'ID_CAT' => $this->get_category()->get_id(), 'CATEGORY_NAME' => $this->get_category()->get_name(), 'CATEGORY_IMAGE' => $this->get_category()->get_image()->rel(), 'CATEGORY_DESCRIPTION' => $category_description, 'U_EDIT_CATEGORY' => $this->get_category()->get_id() == Category::ROOT_CATEGORY ? FaqUrlBuilder::configuration()->rel() : FaqUrlBuilder::edit_category($this->get_category()->get_id())->rel(), 'QUESTIONS_NUMBER' => $result->get_rows_count()));
        while ($row = $result->fetch()) {
            $faq_question = new FaqQuestion();
            $faq_question->set_properties($row);
            $this->tpl->assign_block_vars('questions', $faq_question->get_array_tpl_vars());
        }
        $result->dispose();
    }