public function get_search_request($args)
 {
     $now = new Date();
     $authorized_categories = WebService::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\tw.id AS id_content,\n\t\t\tw.name AS title,\n\t\t\t( 2 * FT_SEARCH_RELEVANCE(w.name, '" . $args['search'] . "') + (FT_SEARCH_RELEVANCE(w.contents, '" . $args['search'] . "') +\n\t\t\tFT_SEARCH_RELEVANCE(w.short_contents, '" . $args['search'] . "')) / 2 ) / 3 * " . $weight . " AS relevance,\n\t\t\tCONCAT('" . PATH_TO_ROOT . "/web/index.php?url=/', id_category, '-', IF(id_category != 0, cat.rewrited_name, 'root'), '/', w.id, '-', w.rewrited_name) AS link\n\t\t\tFROM " . WebSetup::$web_table . " w\n\t\t\tLEFT JOIN " . WebSetup::$web_cats_table . " cat ON w.id_category = cat.id\n\t\t\tWHERE ( FT_SEARCH(w.name, '" . $args['search'] . "') OR FT_SEARCH(w.contents, '" . $args['search'] . "') OR FT_SEARCH_RELEVANCE(w.short_contents, '" . $args['search'] . "') )\n\t\t\tAND id_category IN(" . implode(", ", $authorized_categories) . ")\n\t\t\tAND (approbation_type = 1 OR (approbation_type = 2 AND start_date < '" . $now->get_timestamp() . "' AND (end_date > '" . $now->get_timestamp() . "' OR end_date = 0)))\n\t\t\tORDER BY relevance DESC\n\t\t\tLIMIT 100 OFFSET 0";
 }
    public function build_view(HTTPRequestCustom $request)
    {
        $now = new Date();
        $config = WebConfig::load();
        $authorized_categories = WebService::get_authorized_categories(Category::ROOT_CATEGORY);
        $mode = $request->get_getstring('sort', WebUrlBuilder::DEFAULT_SORT_MODE);
        $field = $request->get_getstring('field', WebUrlBuilder::DEFAULT_SORT_FIELD);
        $condition = 'WHERE relation.id_keyword = :id_keyword
		AND id_category IN :authorized_categories
		AND (approbation_type = 1 OR (approbation_type = 2 AND start_date < :timestamp_now AND (end_date > :timestamp_now OR end_date = 0)))';
        $parameters = array('id_keyword' => $this->get_keyword()->get_id(), 'authorized_categories' => $authorized_categories, 'timestamp_now' => $now->get_timestamp());
        $page = AppContext::get_request()->get_getint('page', 1);
        $pagination = $this->get_pagination($condition, $parameters, $field, $mode, $page);
        $sort_mode = $mode == 'asc' ? 'ASC' : 'DESC';
        switch ($field) {
            case 'name':
                $sort_field = WebLink::SORT_ALPHABETIC;
                break;
            case 'visits':
                $sort_field = WebLink::SORT_NUMBER_VISITS;
                break;
            case 'com':
                $sort_field = WebLink::SORT_NUMBER_COMMENTS;
                break;
            case 'note':
                $sort_field = WebLink::SORT_NOTATION;
                break;
            default:
                $sort_field = WebLink::SORT_DATE;
                break;
        }
        $result = PersistenceContext::get_querier()->select('SELECT web.*, member.*, com.number_comments, notes.average_notes, notes.number_notes, note.note
		FROM ' . WebSetup::$web_table . ' web
		LEFT JOIN ' . DB_TABLE_KEYWORDS_RELATIONS . ' relation ON relation.module_id = \'web\' AND relation.id_in_module = web.id 
		LEFT JOIN ' . DB_TABLE_MEMBER . ' member ON member.user_id = web.author_user_id
		LEFT JOIN ' . DB_TABLE_COMMENTS_TOPIC . ' com ON com.id_in_module = web.id AND com.module_id = \'web\'
		LEFT JOIN ' . DB_TABLE_AVERAGE_NOTES . ' notes ON notes.id_in_module = web.id AND notes.module_name = \'web\'
		LEFT JOIN ' . DB_TABLE_NOTE . ' note ON note.id_in_module = web.id AND note.module_name = \'web\' AND note.user_id = :user_id
		' . $condition . '
		ORDER BY web.privileged_partner DESC, ' . $sort_field . ' ' . $sort_mode . '
		LIMIT :number_items_per_page OFFSET :display_from', array_merge($parameters, array('user_id' => AppContext::get_current_user()->get_id(), 'number_items_per_page' => $pagination->get_number_items_per_page(), 'display_from' => $pagination->get_display_from())));
        $this->tpl->put_all(array('C_WEBLINKS' => $result->get_rows_count() > 0, 'C_MORE_THAN_ONE_WEBLINK' => $result->get_rows_count() > 1, 'C_CATEGORY_DISPLAYED_SUMMARY' => $config->is_category_displayed_summary(), 'C_CATEGORY_DISPLAYED_TABLE' => $config->is_category_displayed_table(), 'C_COMMENTS_ENABLED' => $config->are_comments_enabled(), 'C_NOTATION_ENABLED' => $config->is_notation_enabled(), 'C_PAGINATION' => $pagination->has_several_pages(), 'PAGINATION' => $pagination->display(), 'TABLE_COLSPAN' => 3 + (int) $config->are_comments_enabled() + (int) $config->is_notation_enabled(), 'CATEGORY_NAME' => $this->get_keyword()->get_name()));
        while ($row = $result->fetch()) {
            $weblink = new WebLink();
            $weblink->set_properties($row);
            $keywords = $weblink->get_keywords();
            $has_keywords = count($keywords) > 0;
            $this->tpl->assign_block_vars('weblinks', array_merge($weblink->get_array_tpl_vars(), array('C_KEYWORDS' => $has_keywords)));
            if ($has_keywords) {
                $this->build_keywords_view($keywords);
            }
        }
        $result->dispose();
        $this->build_sorting_form($field, $mode);
    }
    private function build_view(HTTPRequestCustom $request)
    {
        $now = new Date();
        $authorized_categories = WebService::get_authorized_categories($this->get_category()->get_id());
        $mode = $request->get_getstring('sort', WebUrlBuilder::DEFAULT_SORT_MODE);
        $field = $request->get_getstring('field', WebUrlBuilder::DEFAULT_SORT_FIELD);
        $page = AppContext::get_request()->get_getint('page', 1);
        $subcategories_page = AppContext::get_request()->get_getint('subcategories_page', 1);
        $subcategories_number = count(WebService::get_categories_manager()->get_categories_cache()->get_childrens($this->get_category()->get_id()));
        $subcategories_pagination = $this->get_subcategories_pagination($subcategories_number, $this->config->get_categories_number_per_page(), $field, $mode, $page, $subcategories_page);
        //Children categories
        $result = PersistenceContext::get_querier()->select('SELECT @id_cat:= web_cats.id, web_cats.*,
		(SELECT COUNT(*) FROM ' . WebSetup::$web_table . '
			WHERE id_category IN (
				@id_cat,
				(SELECT GROUP_CONCAT(id SEPARATOR \',\') FROM ' . WebSetup::$web_cats_table . ' WHERE id_parent = @id_cat), 
				(SELECT GROUP_CONCAT(childs.id SEPARATOR \',\') FROM ' . WebSetup::$web_cats_table . ' parents
				INNER JOIN ' . WebSetup::$web_cats_table . ' childs ON parents.id = childs.id_parent
				WHERE parents.id_parent = @id_cat)
			)
			AND (approbation_type = 1 OR (approbation_type = 2 AND start_date < :timestamp_now AND (end_date > :timestamp_now OR end_date = 0)))
		) AS weblinks_number
		FROM ' . WebSetup::$web_cats_table . ' web_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('timestamp_now' => $now->get_timestamp(), 'id_category' => $this->category->get_id(), 'authorized_categories' => $authorized_categories, 'number_items_per_page' => $subcategories_pagination->get_number_items_per_page(), 'display_from' => $subcategories_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_WEBLINK' => $row['weblinks_number'] > 1, 'CATEGORY_NAME' => $row['name'], 'CATEGORY_IMAGE' => $category_image->rel(), 'WEBLINKS_NUMBER' => $row['weblinks_number'], 'U_CATEGORY' => WebUrlBuilder::display_category($row['id'], $row['rewrited_name'])->rel()));
            $nbr_cat_displayed++;
        }
        $result->dispose();
        $nbr_column_cats = $nbr_cat_displayed > $this->config->get_columns_number_per_line() ? $this->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);
        $condition = 'WHERE id_category = :id_category
		AND (approbation_type = 1 OR (approbation_type = 2 AND start_date < :timestamp_now AND (end_date > :timestamp_now OR end_date = 0)))';
        $parameters = array('id_category' => $this->get_category()->get_id(), 'timestamp_now' => $now->get_timestamp());
        $pagination = $this->get_pagination($condition, $parameters, $field, $mode, $page, $subcategories_page);
        $sort_mode = $mode == 'asc' ? 'ASC' : 'DESC';
        switch ($field) {
            case 'name':
                $sort_field = WebLink::SORT_ALPHABETIC;
                break;
            case 'visits':
                $sort_field = WebLink::SORT_NUMBER_VISITS;
                break;
            case 'com':
                $sort_field = WebLink::SORT_NUMBER_COMMENTS;
                break;
            case 'note':
                $sort_field = WebLink::SORT_NOTATION;
                break;
            default:
                $sort_field = WebLink::SORT_DATE;
                break;
        }
        $result = PersistenceContext::get_querier()->select('SELECT web.*, member.*, com.number_comments, notes.average_notes, notes.number_notes, note.note
		FROM ' . WebSetup::$web_table . ' web
		LEFT JOIN ' . DB_TABLE_MEMBER . ' member ON member.user_id = web.author_user_id
		LEFT JOIN ' . DB_TABLE_COMMENTS_TOPIC . ' com ON com.id_in_module = web.id AND com.module_id = \'web\'
		LEFT JOIN ' . DB_TABLE_AVERAGE_NOTES . ' notes ON notes.id_in_module = web.id AND notes.module_name = \'web\'
		LEFT JOIN ' . DB_TABLE_NOTE . ' note ON note.id_in_module = web.id AND note.module_name = \'web\' AND note.user_id = :user_id
		' . $condition . '
		ORDER BY web.privileged_partner DESC, ' . $sort_field . ' ' . $sort_mode . '
		LIMIT :number_items_per_page OFFSET :display_from', array_merge($parameters, array('user_id' => AppContext::get_current_user()->get_id(), 'number_items_per_page' => $pagination->get_number_items_per_page(), 'display_from' => $pagination->get_display_from())));
        $category_description = FormatingHelper::second_parse($this->get_category()->get_description());
        $this->tpl->put_all(array('C_WEBLINKS' => $result->get_rows_count() > 0, 'C_MORE_THAN_ONE_WEBLINK' => $result->get_rows_count() > 1, 'C_CATEGORY_DISPLAYED_SUMMARY' => $this->config->is_category_displayed_summary(), 'C_CATEGORY_DISPLAYED_TABLE' => $this->config->is_category_displayed_table(), 'C_CATEGORY_DESCRIPTION' => !empty($category_description), 'C_COMMENTS_ENABLED' => $this->config->are_comments_enabled(), 'C_NOTATION_ENABLED' => $this->config->is_notation_enabled(), 'C_MODERATE' => WebAuthorizationsService::check_authorizations($this->get_category()->get_id())->moderation(), 'C_PAGINATION' => $pagination->has_several_pages(), '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_SUB_CATEGORIES' => $nbr_cat_displayed > 0, 'C_SUBCATEGORIES_PAGINATION' => $subcategories_pagination->has_several_pages(), 'SUBCATEGORIES_PAGINATION' => $subcategories_pagination->display(), 'CATS_COLUMNS_WIDTH' => $cats_columns_width, 'PAGINATION' => $pagination->display(), 'TABLE_COLSPAN' => 3 + (int) $this->config->are_comments_enabled() + (int) $this->config->is_notation_enabled(), '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 ? WebUrlBuilder::configuration()->rel() : WebUrlBuilder::edit_category($this->get_category()->get_id())->rel()));
        while ($row = $result->fetch()) {
            $weblink = new WebLink();
            $weblink->set_properties($row);
            $keywords = $weblink->get_keywords();
            $has_keywords = count($keywords) > 0;
            $this->tpl->assign_block_vars('weblinks', array_merge($weblink->get_array_tpl_vars(), array('C_KEYWORDS' => $has_keywords)));
            if ($has_keywords) {
                $this->build_keywords_view($keywords);
            }
        }
        $result->dispose();
        $this->build_sorting_form($field, $mode);
    }