From: Florian Forster Date: Fri, 9 Jul 2010 16:37:31 +0000 (+0200) Subject: "search" action: If no search string is given, display an "advanced search" form. X-Git-Tag: v4.0.0~115 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=a179bbd8ce683ab581b1adb7eb0cfc5360c5308e;p=collection4.git "search" action: If no search string is given, display an "advanced search" form. It's not advanced yet, but that will be coming soon. --- diff --git a/src/action_search.c b/src/action_search.c index 7782d77..9687460 100644 --- a/src/action_search.c +++ b/src/action_search.c @@ -21,10 +21,13 @@ * Florian octo Forster **/ +#include "config.h" + #include #include #include #include +#include #include "action_search.h" #include "common.h" @@ -52,6 +55,17 @@ struct callback_data_s }; typedef struct callback_data_s callback_data_t; +static int left_menu (__attribute__((unused)) void *user_data) /* {{{ */ +{ + printf ("\n\n", + script_name (), script_name ()); + + return (0); +} /* }}} int left_menu */ + static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */ graph_instance_t *inst, void *user_data) @@ -129,30 +143,6 @@ static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */ return (0); } /* }}} int print_graph_inst_html */ -static int print_graph_html (graph_config_t *cfg, /* {{{ */ - __attribute__((unused)) void *user_data) -{ - char params[1024]; - char title[1024]; - - if (graph_num_instances (cfg) < 1) - return (0); - - memset (title, 0, sizeof (title)); - graph_get_title (cfg, title, sizeof (title)); - html_escape_buffer (title, sizeof (title)); - - memset (params, 0, sizeof (params)); - graph_get_params (cfg, params, sizeof (params)); - html_escape_buffer (params, sizeof (params)); - - printf ("
  • " - "%s
  • \n", - script_name (), params, title); - - return (0); -} /* }}} int print_graph_html */ - struct page_data_s { const char *search_term; @@ -166,45 +156,40 @@ static int print_search_result (void *user_data) /* {{{ */ /* graph_index = */ -1, /* graph_limit = */ 20, /* graph_more = */ 0, /* inst_index = */ -1, /* inst_limit = */ 5, /* inst more = */ 0, /* search_term = */ pg_data->search_term }; + char *term_lc; + char *search_term_html; - if (pg_data->search_term != NULL) - { - char *search_term_html = html_escape (pg_data->search_term); - printf ("

    Search results for "%s"

    \n", - search_term_html); - free (search_term_html); - } + assert (pg_data->search_term != NULL); + + search_term_html = html_escape (pg_data->search_term); + printf ("

    Search results for "%s"

    \n", + search_term_html); + free (search_term_html); printf ("
      \n"); - if (pg_data->search_term == NULL) - { - gl_graph_get_all (print_graph_html, /* user_data = */ &cb_data); - } + + term_lc = strtolower_copy (pg_data->search_term); + + if (strncmp ("host:", term_lc, strlen ("host:")) == 0) + gl_search_field (GIF_HOST, term_lc + strlen ("host:"), + print_graph_inst_html, /* user_data = */ &cb_data); + else if (strncmp ("plugin:", term_lc, strlen ("plugin:")) == 0) + gl_search_field (GIF_PLUGIN, term_lc + strlen ("plugin:"), + print_graph_inst_html, /* user_data = */ &cb_data); + else if (strncmp ("plugin_instance:", term_lc, strlen ("plugin_instance:")) == 0) + gl_search_field (GIF_PLUGIN_INSTANCE, term_lc + strlen ("plugin_instance:"), + print_graph_inst_html, /* user_data = */ &cb_data); + else if (strncmp ("type:", term_lc, strlen ("type:")) == 0) + gl_search_field (GIF_TYPE, term_lc + strlen ("type:"), + print_graph_inst_html, /* user_data = */ &cb_data); + else if (strncmp ("type_instance:", term_lc, strlen ("type_instance:")) == 0) + gl_search_field (GIF_TYPE_INSTANCE, term_lc + strlen ("type_instance:"), + print_graph_inst_html, /* user_data = */ &cb_data); else - { - char *term_lc = strtolower_copy (pg_data->search_term); - - if (strncmp ("host:", term_lc, strlen ("host:")) == 0) - gl_search_field (GIF_HOST, term_lc + strlen ("host:"), - print_graph_inst_html, /* user_data = */ &cb_data); - else if (strncmp ("plugin:", term_lc, strlen ("plugin:")) == 0) - gl_search_field (GIF_PLUGIN, term_lc + strlen ("plugin:"), - print_graph_inst_html, /* user_data = */ &cb_data); - else if (strncmp ("plugin_instance:", term_lc, strlen ("plugin_instance:")) == 0) - gl_search_field (GIF_PLUGIN_INSTANCE, term_lc + strlen ("plugin_instance:"), - print_graph_inst_html, /* user_data = */ &cb_data); - else if (strncmp ("type:", term_lc, strlen ("type:")) == 0) - gl_search_field (GIF_TYPE, term_lc + strlen ("type:"), - print_graph_inst_html, /* user_data = */ &cb_data); - else if (strncmp ("type_instance:", term_lc, strlen ("type_instance:")) == 0) - gl_search_field (GIF_TYPE_INSTANCE, term_lc + strlen ("type_instance:"), - print_graph_inst_html, /* user_data = */ &cb_data); - else - gl_search (term_lc, - print_graph_inst_html, /* user_data = */ &cb_data); - - free (term_lc); - } + gl_search (term_lc, + print_graph_inst_html, /* user_data = */ &cb_data); + + free (term_lc); if (cb_data.cfg != NULL) printf ("
    \n"); @@ -219,37 +204,55 @@ static int print_search_result (void *user_data) /* {{{ */ return (0); } /* }}} int print_search_result */ -static int print_host_list_callback (const char *host, void *user_data) /* {{{ */ +static int print_search_form (void *user_data) /* {{{ */ { - char *host_html; - - /* Make compiler happy */ - user_data = NULL; - - if (host == NULL) - return (EINVAL); - - host_html = html_escape (host); - if (host_html == NULL) - return (ENOMEM); - - printf ("
  • " - "%s
  • \n", - script_name (), host_html, host_html); + page_data_t *pg_data = user_data; + char search_term_html[1024]; - return (0); -} /* }}} int print_host_list_callback */ + if (pg_data->search_term != NULL) + { + html_escape_copy (search_term_html, pg_data->search_term, + sizeof (search_term_html)); + } + else + { + search_term_html[0] = 0; + } -static int print_host_list (__attribute__((unused)) void *user_data) /* {{{ */ -{ - return (0); - printf ("

    List of hosts

    \n" - "
      \n"); - gl_foreach_host (print_host_list_callback, /* user data = */ NULL); - printf ("
    \n"); + printf ("
    \n" + " \n" + "
    \n" + " Advanced search\n" +#if 0 + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" +#endif + "
    Search for
    \n" + " " + "
    \n" + "
    \n", + script_name (), search_term_html); return (0); -} /* }}} int print_host_list */ +} /* }}} int print_search_form */ static int search_html (const char *term) /* {{{ */ { @@ -261,15 +264,18 @@ static int search_html (const char *term) /* {{{ */ snprintf (title, sizeof (title), "Graphs matching \"%s\"", term); else - strncpy (title, "List of all graphs", sizeof (title)); + strncpy (title, "Search", sizeof (title)); title[sizeof (title) - 1] = 0; memset (&pg_data, 0, sizeof (pg_data)); pg_data.search_term = term; pg_callbacks.top_right = html_print_search_box; - pg_callbacks.middle_left = print_host_list; - pg_callbacks.middle_center = print_search_result; + pg_callbacks.middle_left = left_menu; + if (term != NULL) + pg_callbacks.middle_center = print_search_result; + else + pg_callbacks.middle_center = print_search_form; html_print_page (title, &pg_callbacks, &pg_data); @@ -284,6 +290,11 @@ int action_search (void) /* {{{ */ gl_update (); search = strtolower_copy (param ("q")); + if ((search != NULL) && (search[0] == 0)) + { + free (search); + search = NULL; + } status = search_html (search); free (search);