]> saetta.ns0.it Git - zakform/tests/commitdiff
Test bootstrap 4.
authorAndrea Zagli <azagli@libero.it>
Mon, 20 Jul 2020 06:47:06 +0000 (08:47 +0200)
committerAndrea Zagli <azagli@libero.it>
Mon, 20 Jul 2020 06:47:06 +0000 (08:47 +0200)
.gitignore
src/Makefile.am
src/cgi_ini_b4.c [new file with mode: 0644]

index 0c23471b23e87b80152a444d03e3dcff2c738181..17b9837e4dee73b328f7bf688ec2a26bac3cadc0 100644 (file)
@@ -50,7 +50,7 @@ intltool-*
 Rules-quot
 *.exe
 *.csv
-cgi_ini
+cgi_ini*
 cgi_render_each_element
 get_elements
 get_element_filters
index e2299e9c0ced36aa40ddeefe7a1ee8429d42bde7..7abcd18f286006c48f82af0d10aad5ce921a146b 100644 (file)
@@ -9,6 +9,7 @@ LIBS = $(ZAKFORMTESTS_LIBS)
 
 noinst_PROGRAMS = \
                   cgi_ini \
+                  cgi_ini_b4 \
                   cgi_render_each_element \
                   get_elements \
                   get_element_filters \
diff --git a/src/cgi_ini_b4.c b/src/cgi_ini_b4.c
new file mode 100644 (file)
index 0000000..91a8d64
--- /dev/null
@@ -0,0 +1,158 @@
+/*
+ * Copyright (C) 2016-2020 Andrea Zagli <azagli@libero.it>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifdef HAVE_CONFIG_H
+       #include <config.h>
+#endif
+
+#include <unistd.h>
+#include <locale.h>
+
+#include <libzakcgi/libzakcgi.h>
+#include <libzakform/libzakform.h>
+#include <libzakformcgi/libzakformcgi.h>
+#include <libzakformini/libzakformini.h>
+
+int
+main (int argc, char *argv[])
+{
+       ZakCgiMain *zakcgimain;
+
+       GString *str;
+
+       GValue *val;
+       gchar *filename;
+       gint id;
+
+       ZakFormCgiForm *form;
+
+       ZakFormIniProvider *provider;
+
+       setlocale (LC_ALL, "");
+
+       zakcgimain = zak_cgi_main_new ();
+
+       str = g_string_new ("<!DOCTYPE html>\n"
+                           "<html lang=\"it\">\n"
+                           "<head>\n"
+                           "<meta charset=\"utf-8\">\n"
+                           "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrik-to-fit=no\">\n"
+                           "<title>Form test</title>\n"
+                           "<!-- Bootstrap -->\n"
+                           "<link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css\">\n"
+                           "</head>\n"
+                           "<body>\n"
+                           "<div class=\"container\">\n");
+
+       val = zak_cgi_main_get_parameter (zakcgimain, "filename");
+       if (val != NULL)
+               {
+                       filename = (gchar *)g_value_get_string (val);
+               }
+       else
+               {
+                       filename = g_strdup ("");
+               }
+
+       val = zak_cgi_main_get_parameter (zakcgimain, "id");
+       if (val != NULL)
+               {
+                       id = strtol (g_value_get_string (val), NULL, 10);
+               }
+       else
+               {
+                       id = 0;
+               }
+
+       form = zak_form_cgi_form_new (zakcgimain,
+                                     "method", "post",
+                                     "action", g_strdup_printf ("cgi_ini_b4?filename=%s", filename),
+                                     NULL);
+
+       if (zak_form_form_load_from_file (ZAK_FORM_FORM (form), XMLDIR "/cgi.xml"))
+               {
+                       zak_form_cgi_form_set_bootstrap_version (form, "4");
+
+                       if (zak_cgi_main_is_post (zakcgimain))
+                               {
+                                       /* validating the form */
+                                       zak_form_cgi_form_bind (form);
+                                       if (zak_form_form_is_valid (ZAK_FORM_FORM (form)))
+                                               {
+                                                       g_string_append (str, "<div class=\"alert alert-success\" role=\"alert\">Form is valid!!!</div>");
+                                                       provider = zak_form_ini_provider_new_from_file (filename);
+                                                       if (provider != NULL)
+                                                               {
+                                                                       zak_form_form_insert (ZAK_FORM_FORM (form), ZAK_FORM_IPROVIDER (provider));
+                                                                       g_object_unref (provider);
+                                                               }
+                                               }
+                                       else
+                                               {
+                                                       g_string_append (str, "<div class=\"alert alert-danger\" role=\"alert\">Form is not valid!!!");
+
+                                                       guint m;
+                                                       GPtrArray *ar_messages = zak_form_form_get_messages (ZAK_FORM_FORM (form));
+                                                       if (ar_messages != NULL)
+                                                               {
+                                                                       if (ar_messages->len > 0)
+                                                                               {
+                                                                                       g_string_append (str, "\n<ul>\n");
+                                                                                       for (m = 0; m < ar_messages->len; m++)
+                                                                                               {
+                                                                                                       g_string_append_printf (str, "\n<li>%s</li>", (gchar *)g_ptr_array_index (ar_messages, m));
+                                                                                               }
+                                                                                       g_string_append (str, "\n</ul>\n");
+                                                                               }
+                                                               }
+
+                                                       g_string_append (str, "</div>");
+
+                                                       g_string_append (str, zak_form_cgi_form_render (form));
+                                               }
+                               }
+                       else
+                               {
+                                       zak_form_form_clear (ZAK_FORM_FORM (form));
+                                       if (id > 0)
+                                               {
+                                                       zak_form_element_set_value (zak_form_form_get_element_by_id (ZAK_FORM_FORM (form), "id"), g_strdup_printf ("%d", id));
+
+                                                       provider = zak_form_ini_provider_new_from_file (filename);
+                                                       if (provider != NULL)
+                                                               {
+                                                                       zak_form_form_load (ZAK_FORM_FORM (form), ZAK_FORM_IPROVIDER (provider));
+                                                                       g_object_unref (provider);
+                                                               }
+                                               }
+                                       g_string_append (str, zak_form_cgi_form_render (form));
+                               }
+               }
+
+       g_string_append (str,
+                        "\n</div>\n"
+                        "<script src=\"https://code.jquery.com/jquery-3.5.1.slim.min.js\"></script>\n"
+                        "<script src=\"https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js\"></script>\n"
+                        "<script src=\"https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js\"></script>\n"
+                        "</body>\n"
+                        "</html>");
+
+       zak_cgi_main_out (NULL, str->str);
+
+       return 0;
+}