]> saetta.ns0.it Git - bcity/fe/commitdiff
Saving on db of ticket category.
authorAndrea Zagli <azagli@libero.it>
Sun, 31 Jan 2016 10:00:38 +0000 (11:00 +0100)
committerAndrea Zagli <azagli@libero.it>
Sun, 31 Jan 2016 10:00:38 +0000 (11:00 +0100)
doc/bcity_fe/db/bcity_fe.sql
src/index.c
src/main.c

index 6457010c898d4d458dc16cb9efb58753f70eb70b..9c5737de66169129daff3a0216ece5df42a384dc 100644 (file)
@@ -37,7 +37,9 @@ CREATE TABLE segnalazioni (
     id integer NOT NULL,
     utente character varying(255) DEFAULT ''::character varying,
     latitudine double precision,
-    longitudine double precision
+    longitudine double precision,
+    id_categorie integer DEFAULT 0,
+    status character varying(1) DEFAULT ''::character varying
 );
 
 
index 7ee30470432af27075f169cbe36d150acf3c2a2a..c1197f408077000885a2a1f73bd3b0c266161595 100644 (file)
@@ -114,6 +114,7 @@ index_category (GMatchInfo *minfo, gpointer user_data)
        gchar *filename;
        gchar *content;
 
+       gchar *str_id_categorie;
        guint id_categorie;
        gchar *sql;
        GdaDataModel *dm;
@@ -128,64 +129,104 @@ index_category (GMatchInfo *minfo, gpointer user_data)
                        return;
                }
 
+       str_id_categorie = g_match_info_fetch_named (minfo, "id_categorie");
+       if (str_id_categorie == NULL)
+               {
+                       id_categorie = 0;
+               }
+       else
+               {
+                       id_categorie = strtol (str_id_categorie, NULL, 10);
+               }
+       g_free (str_id_categorie);
+
        if (zak_cgi_main_is_post (commons->zcgi_main))
                {
                        zak_cgi_session_set_value (commons->zcgi_session, "latitudine", g_value_get_string (zak_cgi_main_get_stdin_field (commons->zcgi_main, "lat")));
                        zak_cgi_session_set_value (commons->zcgi_session, "longitudine", g_value_get_string (zak_cgi_main_get_stdin_field (commons->zcgi_main, "lng")));
+               }
+       else /* GET */
+               {
+                       if (g_strcmp0 (zak_cgi_session_get_value (commons->zcgi_session, "latitudine"), "") == 0
+                               || g_strcmp0 (zak_cgi_session_get_value (commons->zcgi_session, "longitudine"), "") == 0)
+                               {
+                                       zak_cgi_main_redirect (commons->zcgi_main, "/bcity/bcity_fe/index/index");
+                                       return;
+                               }
 
-                       id_categorie = 0;
-                       categorie = g_string_new ("");
-
-                       sql = g_strdup_printf ("SELECT c.*,"
-                                                                  " CASE WHEN COALESCE ((SELECT c1.id FROM categorie AS c1 WHERE c1.id_categorie = c.id), 0) != 0 THEN TRUE ELSE FALSE END AS finale"
+                       /* se da categoria "foglia" allora passo al salvataggio */
+                       sql = g_strdup_printf ("SELECT id"
                                                                   " FROM categorie AS c"
-                                                                  " WHERE c.status <> 'E'"
-                                                                  " AND c.id_categorie = %d",
+                                                                  " WHERE id_categorie = %d",
                                                                   id_categorie);
                        dm = gdaex_query (commons->gdaex, sql);
                        g_free (sql);
-                       if (dm != NULL)
+                       if (dm == NULL
+                               || gda_data_model_get_n_rows (dm) < 1)
                                {
-                                       g_string_append_printf (categorie, "<ul>\n");
-
-                                       iter = gda_data_model_create_iter (dm);
-                                       while (gda_data_model_iter_move_next (iter))
+                                       if (dm != NULL)
                                                {
-                                                       g_string_append_printf (categorie,
-                                                                                                       "<li>%s</ul>\n",
-                                                                                                       gdaex_data_model_iter_get_field_value_stringify_at (iter, "descrizione"));
+                                                       g_object_unref (dm);
                                                }
 
-                                       g_string_append_printf (categorie, "</ul>\n");
-
+                                       zak_cgi_session_set_value (commons->zcgi_session, "id_categorie", g_strdup_printf ("%d", id_categorie));
+                                       zak_cgi_main_redirect (commons->zcgi_main, "/bcity/bcity_fe/index/save");
+                                       return;
+                               }
+                       if (dm != NULL)
+                               {
                                        g_object_unref (dm);
                                }
+               }
 
-                       env = ctpl_environ_new ();
-                       ctpl_environ_push_string (env, "categorie", categorie->str);
+       categorie = g_string_new ("");
+
+       sql = g_strdup_printf ("SELECT c.*"
+                                                  " FROM categorie AS c"
+                                                  " WHERE c.status <> 'E'"
+                                                  " AND c.id_categorie = %d"
+                                                  " ORDER BY c.descrizione",
+                                                  id_categorie);
+       dm = gdaex_query (commons->gdaex, sql);
+       g_free (sql);
+       if (dm != NULL)
+               {
+                       g_string_append_printf (categorie, "<ul>\n");
+
+                       iter = gda_data_model_create_iter (dm);
+                       while (gda_data_model_iter_move_next (iter))
+                               {
+                                       g_string_append_printf (categorie,
+                                                                                       "<li><a href=\"/bcity/bcity_fe/index/category/%d\">%s</a></li>\n",
+                                                                                       gdaex_data_model_iter_get_field_value_integer_at (iter, "id"),
+                                                                                       gdaex_data_model_iter_get_field_value_stringify_at (iter, "descrizione"));
+                               }
 
-                       filename = g_build_filename (commons->ctpldir, "index_category.ctpl", NULL);
-                       content = get_ctpl_filled (filename, env);
-                       g_free (filename);
+                       g_string_append_printf (categorie, "</ul>\n");
 
-                       g_string_free (categorie, TRUE);
+                       g_object_unref (dm);
+               }
 
-                       env = ctpl_environ_new ();
-                       ctpl_environ_push_string (env, "head", "");
-                       ctpl_environ_push_string (env, "body_tag", "");
-                       ctpl_environ_push_string (env, "body", content);
+       env = ctpl_environ_new ();
+       ctpl_environ_push_string (env, "categorie", categorie->str);
 
-                       filename = g_build_filename (commons->ctpldir, "template.ctpl", NULL);
-                       g_string_printf (commons->out, "%s",
-                                                        get_ctpl_filled (filename, env));
-                       g_free (filename);
+       filename = g_build_filename (commons->ctpldir, "index_category.ctpl", NULL);
+       content = get_ctpl_filled (filename, env);
+       g_free (filename);
 
-                       g_free (content);
-               }
-       else
-               {
-                       zak_cgi_main_redirect (commons->zcgi_main, "/bcity/bcity_fe/index/index");
-               }
+       g_string_free (categorie, TRUE);
+
+       env = ctpl_environ_new ();
+       ctpl_environ_push_string (env, "head", "");
+       ctpl_environ_push_string (env, "body_tag", "");
+       ctpl_environ_push_string (env, "body", content);
+
+       filename = g_build_filename (commons->ctpldir, "template.ctpl", NULL);
+       g_string_printf (commons->out, "%s",
+                                        get_ctpl_filled (filename, env));
+       g_free (filename);
+
+       g_free (content);
 }
 
 void
@@ -207,38 +248,40 @@ index_save (GMatchInfo *minfo, gpointer user_data)
                        return;
                }
 
-       if (zak_cgi_main_is_post (commons->zcgi_main))
+       if (g_strcmp0 (zak_cgi_session_get_value (commons->zcgi_session, "latitudine"), "") == 0
+               || g_strcmp0 (zak_cgi_session_get_value (commons->zcgi_session, "longitudine"), "") == 0
+               || strtol (zak_cgi_session_get_value (commons->zcgi_session, "id_categorie"), NULL, 10) == 0)
                {
-                       newid = gdaex_get_new_id(commons->gdaex, "segnalazioni", "id", NULL);
-
-                       sql = g_strdup_printf ("INSERT INTO segnalazioni"
-                                                                  " (id, utente, latitudine, longitudine)"
-                                                                  " VALUES (%d, '%s', %s, %s)",
-                                                                  newid,
-                                                                  zak_cgi_session_get_value (commons->zcgi_session, "username"),
-                                                                  zak_cgi_session_get_value (commons->zcgi_session, "latitudine"),
-                                                                  zak_cgi_session_get_value (commons->zcgi_session, "longitudine"));
-                       gdaex_execute (commons->gdaex, sql);
-                       g_free (sql);
+                       zak_cgi_main_redirect (commons->zcgi_main, "/bcity/bcity_fe/index/index");
+                       return;
+               }
 
-                       filename = g_build_filename (commons->ctpldir, "index_save.ctpl", NULL);
-                       content = get_ctpl_filled (filename, NULL);
-                       g_free (filename);
+       newid = gdaex_get_new_id(commons->gdaex, "segnalazioni", "id", NULL);
 
-                       env = ctpl_environ_new ();
-                       ctpl_environ_push_string (env, "head", "");
-                       ctpl_environ_push_string (env, "body_tag", "");
-                       ctpl_environ_push_string (env, "body", content);
+       sql = g_strdup_printf ("INSERT INTO segnalazioni"
+                                                  " (id, utente, latitudine, longitudine, id_categorie)"
+                                                  " VALUES (%d, '%s', %s, %s, %d)",
+                                                  newid,
+                                                  zak_cgi_session_get_value (commons->zcgi_session, "username"),
+                                                  zak_cgi_session_get_value (commons->zcgi_session, "latitudine"),
+                                                  zak_cgi_session_get_value (commons->zcgi_session, "longitudine"),
+                                                  strtol (zak_cgi_session_get_value (commons->zcgi_session, "id_categorie"), NULL, 10));
+       gdaex_execute (commons->gdaex, sql);
+       g_free (sql);
 
-                       filename = g_build_filename (commons->ctpldir, "template.ctpl", NULL);
-                       g_string_printf (commons->out, "%s",
-                                                        get_ctpl_filled (filename, env));
-                       g_free (filename);
+       filename = g_build_filename (commons->ctpldir, "index_save.ctpl", NULL);
+       content = get_ctpl_filled (filename, NULL);
+       g_free (filename);
 
-                       g_free (content);
-               }
-       else
-               {
-                       zak_cgi_main_redirect (commons->zcgi_main, "/bcity/bcity_fe/index/index");
-               }
+       env = ctpl_environ_new ();
+       ctpl_environ_push_string (env, "head", "");
+       ctpl_environ_push_string (env, "body_tag", "");
+       ctpl_environ_push_string (env, "body", content);
+
+       filename = g_build_filename (commons->ctpldir, "template.ctpl", NULL);
+       g_string_printf (commons->out, "%s",
+                                        get_ctpl_filled (filename, env));
+       g_free (filename);
+
+       g_free (content);
 }
index d0bc0dd9a2591d31c8339b5ede93096925860b3f..397770f2422e3a56e8dd0885bc71d4cfe9c2913c 100644 (file)
@@ -97,6 +97,7 @@ main (int argc, char *argv[])
        zak_cgi_url_connect (zcgi_url, "/index[/]?", (ZakCgiUrlConnectedFunction)index_index, commons);
        zak_cgi_url_connect (zcgi_url, "/index/index", (ZakCgiUrlConnectedFunction)index_index, commons);
        zak_cgi_url_connect (zcgi_url, "/index/category", (ZakCgiUrlConnectedFunction)index_category, commons);
+       zak_cgi_url_connect (zcgi_url, "/index/category/(?<id_categorie>[1-9][0-9]*)", (ZakCgiUrlConnectedFunction)index_category, commons);
        zak_cgi_url_connect (zcgi_url, "/index/save", (ZakCgiUrlConnectedFunction)index_save, commons);
 
        zak_cgi_url_connect (zcgi_url, "/login[/]?", (ZakCgiUrlConnectedFunction)login_index, commons);