/*
- * Copyright (C) 2015-2016 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2015-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 Lesser General Public
ZakCgiMain *zakcgimain;
GHashTable *ht_attrs;
GPtrArray *ar_elems;
+ gchar *bootstrap_version;
};
G_DEFINE_TYPE (ZakFormCgiForm, zak_form_cgi_form, ZAK_FORM_TYPE_FORM)
priv->zakcgimain = NULL;
priv->ht_attrs = NULL;
priv->ar_elems = ZAK_FORM_FORM_CLASS (zak_form_cgi_form_parent_class)->get_elements (ZAK_FORM_FORM (zak_form_cgi_form));
+ priv->bootstrap_version = g_strdup ("3");
}
/**
return g_strdup (str->str);
}
+void
+zak_form_cgi_form_set_bootstrap_version (ZakFormCgiForm *zakcgiform, const gchar *version)
+{
+ guint i;
+
+ ZakFormCgiFormPrivate *priv;
+
+ priv = ZAK_FORM_CGI_FORM_GET_PRIVATE (zakcgiform);
+
+ if (priv->bootstrap_version != NULL)
+ {
+ g_free (priv->bootstrap_version);
+ }
+ priv->bootstrap_version = g_strdup (version);
+
+ /* for each element */
+ for (i = 0; i < priv->ar_elems->len; i++)
+ {
+ ZakFormCgiFormElement *element = (ZakFormCgiFormElement *)g_ptr_array_index (priv->ar_elems, i);
+
+ zak_form_cgi_form_element_set_bootstrap_version (element, priv->bootstrap_version);
+ }
+}
+
+gchar
+*zak_form_cgi_form_get_bootstrap_version (ZakFormCgiForm *zakcgiform)
+{
+ ZakFormCgiFormPrivate *priv;
+
+ priv = ZAK_FORM_CGI_FORM_GET_PRIVATE (zakcgiform);
+
+ return g_strdup (priv->bootstrap_version);
+}
+
/* PRIVATE */
static void
zak_form_cgi_form_set_property (GObject *object,
/*
- * Copyright (C) 2015-2016 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2015-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 Lesser General Public
gchar *zak_form_cgi_form_render_start (ZakFormCgiForm *zakcgiform);
gchar *zak_form_cgi_form_render (ZakFormCgiForm *zakcgiform);
+void zak_form_cgi_form_set_bootstrap_version (ZakFormCgiForm *zakcgiform, const gchar *version);
+gchar *zak_form_cgi_form_get_bootstrap_version (ZakFormCgiForm *zakcgiform);
+
G_END_DECLS
/*
- * Copyright (C) 2015-2017 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2015-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 Lesser General Public
#include <libzakcgi/libzakcgi.h>
#include "formelement.h"
+#include "formelementcheck.h"
+#include "formelementradio.h"
enum
{
GHashTable *ht_attrs;
GHashTable *ht_label_attrs;
GValue *value;
+ gchar *bootstrap_version;
};
G_DEFINE_TYPE (ZakFormCgiFormElement, zak_form_cgi_form_element, ZAK_FORM_TYPE_ELEMENT)
priv->ht_attrs = NULL;
priv->ht_label_attrs = NULL;
+ priv->bootstrap_version = g_strdup ("3");
}
gchar
if (messages != NULL
&& messages->len > 0)
{
- g_string_append (str, " has-error");
+ if (g_strcmp0 (priv->bootstrap_version, "3") == 0)
+ {
+ g_string_append (str, " has-error");
+ }
}
g_string_append (str, "\">\n");
g_hash_table_insert (priv->ht_attrs, "class", g_strdup ("form-control"));
}
+ if (messages != NULL
+ && messages->len > 0)
+ {
+ if (g_strcmp0 (priv->bootstrap_version, "4") == 0)
+ {
+ g_hash_table_insert (priv->ht_attrs, "class", g_strdup_printf ("%s is-invalid", g_hash_table_lookup (priv->ht_attrs, "class")));
+ }
+ }
+
if (!zak_form_element_get_editable (ZAK_FORM_ELEMENT (element)))
{
g_hash_table_insert (priv->ht_attrs, (gpointer)"readonly", (gpointer)"readonly");
}
+ if ((ZAK_FORM_CGI_IS_FORM_ELEMENT_CHECK (element)
+ || ZAK_FORM_CGI_IS_FORM_ELEMENT_RADIO (element))
+ && g_strcmp0 (zak_form_cgi_form_element_get_bootstrap_version (element), "4") == 0)
+ {
+ g_string_append (str, "<div class=\"form-check");
+ if ((ZAK_FORM_CGI_IS_FORM_ELEMENT_CHECK (element)
+ && zak_form_cgi_form_element_check_get_in_line (ZAK_FORM_CGI_FORM_ELEMENT_CHECK (element)))
+ || (ZAK_FORM_CGI_IS_FORM_ELEMENT_RADIO (element)
+ && zak_form_cgi_form_element_radio_get_in_line (ZAK_FORM_CGI_FORM_ELEMENT_RADIO (element))))
+ {
+ g_string_append (str, " form-check-inline");
+ }
+ g_string_append (str, "\">\n");
+ }
+
g_string_append (str, ZAK_FORM_CGI_FORM_ELEMENT_GET_CLASS (element)->render (element));
}
for (i = 0; i < messages->len; i++)
{
g_string_append_printf (str,
- "\n<span id=\"helpBox_%s_%d\" class=\"help-block\">%s</span>",
+ "\n<span id=\"helpBox_%s_%d\" class=\"%s\">%s</span>",
priv->id,
i + 1,
+ g_strcmp0 (priv->bootstrap_version, "4") == 0 ? "invalid-feedback" : "help-block",
(gchar *)g_ptr_array_index (messages, i));
}
}
+ if ((ZAK_FORM_CGI_IS_FORM_ELEMENT_CHECK (element)
+ || ZAK_FORM_CGI_IS_FORM_ELEMENT_RADIO (element))
+ && g_strcmp0 (zak_form_cgi_form_element_get_bootstrap_version (element), "4") == 0)
+ {
+ g_string_append (str, "</div>\n");
+ }
+
g_string_append (str, "\n</div>\n");
ret = g_strdup (str->str);
return ret;
}
+void
+zak_form_cgi_form_element_set_bootstrap_version (ZakFormCgiFormElement *element, const gchar *version)
+{
+ guint i;
+
+ ZakFormCgiFormElementPrivate *priv;
+
+ priv = ZAK_FORM_CGI_FORM_ELEMENT_GET_PRIVATE (element);
+
+ if (priv->bootstrap_version != NULL)
+ {
+ g_free (priv->bootstrap_version);
+ }
+ priv->bootstrap_version = g_strdup (version);
+}
+
+gchar
+*zak_form_cgi_form_element_get_bootstrap_version (ZakFormCgiFormElement *element)
+{
+ ZakFormCgiFormElementPrivate *priv;
+
+ priv = ZAK_FORM_CGI_FORM_ELEMENT_GET_PRIVATE (element);
+
+ return g_strdup (priv->bootstrap_version);
+}
+
/* PRIVATE */
static void
zak_form_cgi_form_element_construct (ZakFormCgiFormElement *element,
/*
- * Copyright (C) 2015-2017 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2015-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 Lesser General Public
gchar *zak_form_cgi_form_element_render (ZakFormCgiFormElement *element);
gchar *zak_form_cgi_form_element_render_label (ZakFormCgiFormElement *element);
+void zak_form_cgi_form_element_set_bootstrap_version (ZakFormCgiFormElement *element, const gchar *version);
+gchar *zak_form_cgi_form_element_get_bootstrap_version (ZakFormCgiFormElement *element);
+
G_END_DECLS
/*
- * Copyright (C) 2015-2017 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2015-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 Lesser General Public
g_free (attr_class);
}
- g_string_append_printf (str, "\n%s<label%s>\n%s %s</label>%s",
- priv->in_line ? "" : "<div class=\"checkbox\">\n",
- priv->in_line ? " class=\"checkbox-inline\"" : "",
- zak_cgi_tag_tag_ht ("input", zak_form_cgi_form_element_get_id (element), ht_attrs),
- zak_form_cgi_form_element_check_get_text (ZAK_FORM_CGI_FORM_ELEMENT_CHECK (element)),
- priv->in_line ? "" : "\n</div><br/>");
+ if (g_strcmp0 (zak_form_cgi_form_element_get_bootstrap_version (element), "3") == 0)
+ {
+ g_string_append_printf (str, "\n%s<label%s>\n%s %s</label>%s",
+ priv->in_line ? "" : "<div class=\"checkbox\">\n",
+ priv->in_line ? " class=\"checkbox-inline\"" : "",
+ zak_cgi_tag_tag_ht ("input", zak_form_cgi_form_element_get_id (element), ht_attrs),
+ zak_form_cgi_form_element_check_get_text (ZAK_FORM_CGI_FORM_ELEMENT_CHECK (element)),
+ priv->in_line ? "" : "\n</div><br/>");
+ }
+ else
+ {
+ g_hash_table_insert (ht_attrs, "class", g_strdup_printf ("%s form-check-input", g_hash_table_lookup (ht_attrs, "class")));
+
+ g_string_append_printf (str, "\n%s<label class=\"form-check-label\">%s</label>",
+ zak_cgi_tag_tag_ht ("input", zak_form_cgi_form_element_get_id (element), ht_attrs),
+ zak_form_cgi_form_element_check_get_text (ZAK_FORM_CGI_FORM_ELEMENT_CHECK (element)));
+ }
if (ht_label_attrs != NULL)
{
g_hash_table_insert (ht_attrs_option, (gpointer)"checked", (gpointer)"checked");
}
- g_string_append_printf (ret, "\n%s<label%s>\n%s %s</label>%s",
- priv->in_line ? "" : "<div class=\"radio\">\n",
- priv->in_line ? " class=\"radio-inline\"" : "",
- zak_cgi_tag_tag_ht ("input",
- g_strdup_printf ("%s_%d",
- zak_form_cgi_form_element_get_id (element),
- idx + 1),
- ht_attrs_option),
- opt->content,
- priv->in_line ? "" : "\n</div>");
+ if (g_strcmp0 (zak_form_cgi_form_element_get_bootstrap_version (element), "3") == 0)
+ {
+ g_string_append_printf (ret, "\n%s<label%s>\n%s %s</label>%s",
+ priv->in_line ? "" : "<div class=\"radio\">\n",
+ priv->in_line ? " class=\"radio-inline\"" : "",
+ zak_cgi_tag_tag_ht ("input",
+ g_strdup_printf ("%s_%d",
+ zak_form_cgi_form_element_get_id (element),
+ idx + 1),
+ ht_attrs_option),
+ opt->content,
+ priv->in_line ? "" : "\n</div>");
+ }
+ else
+ {
+ /* g_hash_table_insert (ht_attrs_option, "class", g_strdup_printf ("%s form-check-input", g_hash_table_lookup (ht_attrs_option, "class"))); */
+
+ g_string_append_printf (ret, "\n%s<label class=\"form-check-label\"> %s</label>",
+ zak_cgi_tag_tag_ht ("input",
+ g_strdup_printf ("%s_%d",
+ zak_form_cgi_form_element_get_id (element),
+ idx + 1),
+ ht_attrs_option),
+ opt->content);
+ }
g_hash_table_unref (ht_attrs_option);
/*
- * Copyright (C) 2015-2017 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2015-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 Lesser General Public
g_string_erase (str_attr_class, form_control - str_attr_class->str, 12);
}
- if (g_strstr_len (attr_class, -1, "btn btn-default") == NULL)
+ if (g_strcmp0 (zak_form_cgi_form_element_get_bootstrap_version (element), "3") == 0)
{
- g_hash_table_insert (ht_attrs, "class", g_strdup_printf ("%s btn btn-default", str_attr_class->str));
+ if (g_strstr_len (attr_class, -1, "btn btn-default") == NULL)
+ {
+ g_hash_table_insert (ht_attrs, "class", g_strdup_printf ("%s btn btn-default", str_attr_class->str));
+ }
+ }
+ else
+ {
+ g_hash_table_insert (ht_attrs, "class", g_strdup_printf ("%s btn", str_attr_class->str));
}
g_free (attr_class);
}
else
{
- g_hash_table_replace (ht_attrs, g_strdup ("class"), g_strdup ("btn btn-default"));
+ g_hash_table_replace (ht_attrs, g_strdup ("class"),
+ g_strdup_printf ("btn%s",
+ g_strcmp0 (zak_form_cgi_form_element_get_bootstrap_version (element), "3") == 0 ? " btn-default" : ""));
}
ret = zak_cgi_tag_submit_ht (zak_form_cgi_form_element_get_id (element), ht_attrs);