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 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();
    }
 public function get_menu_content()
 {
     //Create file template
     $tpl = new FileTemplate('faq/FaqModuleMiniMenu.tpl');
     //Assign the lang file to the tpl
     $tpl->add_lang(LangLoader::get('common', 'faq'));
     //Load module cache
     $faq_cache = FaqCache::load();
     //Get authorized categories for the current user
     $authorized_categories = FaqService::get_authorized_categories(Category::ROOT_CATEGORY);
     $categories = array_intersect($faq_cache->get_categories(), $authorized_categories);
     if (!empty($categories)) {
         $id_category = $categories[array_rand($categories)];
         $category_questions = $faq_cache->get_category_questions($id_category);
         $random_question = $category_questions[array_rand($category_questions)];
         if (!empty($random_question)) {
             $category = FaqService::get_categories_manager()->get_categories_cache()->get_category($id_category);
             $tpl->put_all(array('C_QUESTION' => true, 'QUESTION' => $random_question['question'], 'U_LINK' => FaqUrlBuilder::display($category->get_id(), $category->get_rewrited_name(), $random_question['id'])->rel()));
         }
     }
     return $tpl->render();
 }
 public function get_search_request($args)
 {
     $authorized_categories = FaqService::get_authorized_categories(Category::ROOT_CATEGORY);
     $weight = isset($args['weight']) && is_numeric($args['weight']) ? $args['weight'] : 1;
     return "SELECT " . $args['id_search'] . " AS id_search,\n\t\t\tf.id AS id_content,\n\t\t\tf.question AS title,\n\t\t\t( 2 * FT_SEARCH_RELEVANCE(f.question, '" . $args['search'] . "') + FT_SEARCH_RELEVANCE(f.answer, '" . $args['search'] . "' )) / 3 * " . $weight . " AS relevance,\n\t\t\tCONCAT('" . PATH_TO_ROOT . "/faq/index.php?url=/', id_category, '-', IF(id_category != 0, cat.rewrited_name, 'root'), '/#question', f.id) AS link\n\t\t\tFROM " . FaqSetup::$faq_table . " f\n\t\t\tLEFT JOIN " . FaqSetup::$faq_cats_table . " cat ON f.id_category = cat.id\n\t\t\tWHERE ( FT_SEARCH(f.question, '" . $args['search'] . "') OR FT_SEARCH(f.answer, '" . $args['search'] . "') )\n\t\t\tAND approved = 1\n\t\t\tAND id_category IN (" . implode(", ", $authorized_categories) . ")\n\t\t\tORDER BY relevance DESC\n\t\t\tLIMIT 100 OFFSET 0";
 }