]> saetta.ns0.it Git - zakform/libzakform/commitdiff
ZakFormElementValidatorDate can now compare the value.
authorAndrea Zagli <azagli@libero.it>
Mon, 1 May 2017 10:44:09 +0000 (12:44 +0200)
committerAndrea Zagli <azagli@libero.it>
Mon, 1 May 2017 10:44:09 +0000 (12:44 +0200)
po/it.po
src/commons.c
src/commons.h
src/formelementvalidatordate.c
src/formelementvalidatordate.h
src/formvalidatorcompare.c
src/formvalidatorcomparedate.c

index aa1eb2ab683289efd01f8a0b11c36d4ecf0b389f..f249455fcc89a7320af4d8d11a51b31f45a648e1 100644 (file)
--- a/po/it.po
+++ b/po/it.po
@@ -7,7 +7,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: libzakform 0.0.1\n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2017-05-01 09:58+0200\n"
+"POT-Creation-Date: 2017-05-01 11:19+0200\n"
 "PO-Revision-Date: 2016-01-02 12:44+0200\n"
 "Last-Translator: Andrea Zagli <azagli@libero.it>\n"
 "Language-Team: Italian <tp@lists.linux.it>\n"
@@ -17,31 +17,31 @@ msgstr ""
 "Content-Transfer-Encoding: 8bit\n"
 "Plural-Forms: nplurals=2; plural=(n != 1);\n"
 
-#: ../src/commons.c:89
+#: ../src/commons.c:90
 msgid "lesser than"
 msgstr "minore di"
 
-#: ../src/commons.c:93
+#: ../src/commons.c:94
 msgid "lesser or equal to"
 msgstr "minore o uguale a"
 
-#: ../src/commons.c:97
+#: ../src/commons.c:98
 msgid "equal to"
 msgstr "uguale a"
 
-#: ../src/commons.c:101
+#: ../src/commons.c:102
 msgid "different from"
 msgstr "diverso da"
 
-#: ../src/commons.c:105
+#: ../src/commons.c:106
 msgid "greater than"
 msgstr "maggiore di"
 
-#: ../src/commons.c:109
+#: ../src/commons.c:110
 msgid "greater or equal to"
 msgstr "maggiore o uguale a"
 
-#: ../src/commons.c:113
+#: ../src/commons.c:114
 msgid "NOT SUPPORTED"
 msgstr "NON SUPPORTATO"
 
@@ -80,6 +80,11 @@ msgstr "I moduli non sono supportati in questo sistema operativo"
 msgid "Invalid value"
 msgstr "Valore non valido"
 
+#: ../src/formelementvalidatordate.c:364
+#, c-format
+msgid "Value must be %s %s"
+msgstr "Il valore deve essere %s %s"
+
 #: ../src/formvalidatorcompare.c:253 ../src/formvalidatorcomparedate.c:266
 #, c-format
 msgid "«%s» must be %s «%s»"
index f1f604e6a3ae6f5fa0c6625287536bed0540276c..a1d2892d7594f66bdbcfdfe1b080b5fd46dc340f 100644 (file)
@@ -67,6 +67,7 @@ zak_form_get_compare_type_from_string (const gchar *str)
        else
                {
                        g_warning ("Compare type «%s» not supported.", str);
+                       ret = ZAK_FORM_COMPARE_TYPE_NONE;
                }
 
        return ret;
index 18e0b66572c239fef06bdae19c335944b7b9959c..87f7452faae2e9cb7618feead937041ea01a9b2f 100644 (file)
@@ -29,6 +29,7 @@ G_BEGIN_DECLS
 
 typedef enum
        {
+               ZAK_FORM_COMPARE_TYPE_NONE,
                ZAK_FORM_COMPARE_TYPE_LESSER,
                ZAK_FORM_COMPARE_TYPE_LESSER_EQUAL,
                ZAK_FORM_COMPARE_TYPE_EQUAL,
index 072ab4e7184270868db1b90e2330b36355f3bf5f..b4130cb0f98371eeaf2261628076b8e50ea8d372 100644 (file)
@@ -20,6 +20,8 @@
        #include <config.h>
 #endif
 
+#include <glib/gi18n-lib.h>
+
 #include <libzakutils/libzakutils.h>
 
 #include "formelementvalidator.h"
@@ -55,6 +57,8 @@ struct _ZakFormElementValidatorDate
 typedef struct _ZakFormElementValidatorDatePrivate ZakFormElementValidatorDatePrivate;
 struct _ZakFormElementValidatorDatePrivate
        {
+               ZakFormCompareType compare_type;
+               gchar *compare_value;
                gchar *format;
        };
 
@@ -100,6 +104,80 @@ ZakFormElementValidatorDate
        return zak_form_element_validator_date;
 }
 
+/**
+ * zak_form_element_validator_date_set_compare_type:
+ * @validator:
+ * @type:
+ *
+ */
+void
+zak_form_element_validator_date_set_compare_type (ZakFormElementValidatorDate *validator, ZakFormCompareType type)
+{
+       ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (validator);
+
+       priv->compare_type = type;
+}
+
+/**
+ * zak_form_element_validator_date_get_compare_type:
+ * @validator:
+ *
+ */
+ZakFormCompareType
+zak_form_element_validator_date_get_compare_type (ZakFormElementValidatorDate *validator)
+{
+       ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (validator);
+
+       return priv->compare_type;
+}
+
+/**
+ * zak_form_element_validator_date_set_compare_value:
+ * @validator:
+ * @value:
+ *
+ */
+void
+zak_form_element_validator_date_set_compare_value (ZakFormElementValidatorDate *validator, const gchar *value)
+{
+       ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (validator);
+
+       if (priv->compare_value != NULL)
+               {
+                       g_free (priv->compare_value);
+                       priv->compare_value = NULL;
+               }
+
+       if (value != NULL)
+               {
+                       priv->compare_value = g_strdup (value);
+               }
+}
+
+/**
+ * zak_form_element_validator_date_get_compare_value:
+ * @validator:
+ *
+ */
+gchar
+*zak_form_element_validator_date_get_compare_value (ZakFormElementValidatorDate *validator)
+{
+       gchar *ret;
+
+       ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (validator);
+
+       if (priv->compare_value == NULL)
+               {
+                       ret = NULL;
+               }
+       else
+               {
+                       ret = g_strdup (priv->compare_value);
+               }
+
+       return ret;
+}
+
 /**
  * zak_form_element_validator_date_set_format:
  * @validator:
@@ -140,8 +218,18 @@ gchar
 static gboolean
 zak_form_element_validator_date_xml_parsing (ZakFormElementValidator *validator, xmlNode *xnode)
 {
+       gchar *prop;
+
        ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (ZAK_FORM_ELEMENT_VALIDATOR_DATE (validator));
 
+       prop = (gchar *)xmlGetProp (xnode, (const xmlChar *)"type_comp");
+       zak_form_element_validator_date_set_compare_type (ZAK_FORM_ELEMENT_VALIDATOR_DATE (validator), zak_form_get_compare_type_from_string (prop));
+       g_free (prop);
+
+       prop = (gchar *)xmlGetProp (xnode, (const xmlChar *)"value_comp");
+       zak_form_element_validator_date_set_compare_value (ZAK_FORM_ELEMENT_VALIDATOR_DATE (validator), prop);
+       g_free (prop);
+
        zak_form_element_validator_date_set_format (ZAK_FORM_ELEMENT_VALIDATOR_DATE (validator), (gchar *)xmlNodeGetContent (xnode));
 
        return TRUE;
@@ -213,11 +301,18 @@ zak_form_element_validator_date_validate (ZakFormElementValidator *validator_dat
        gboolean ret;
        gchar *_value;
        GDateTime *gdt;
+       GDateTime *gdt_compare;
+
+       gint comp;
+       gchar *msg;
 
        ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (validator_date);
 
        g_return_val_if_fail (value != NULL, FALSE);
 
+       gdt = NULL;
+       gdt_compare = NULL;
+
        _value = g_strstrip (g_strdup (value));
        if (g_strcmp0 (_value, "") == 0)
                {
@@ -228,6 +323,62 @@ zak_form_element_validator_date_validate (ZakFormElementValidator *validator_dat
                        gdt = zak_utils_get_gdatetime_from_string (_value, priv->format);
                        ret = (gdt != NULL);
 
+                       if (ret
+                           && priv->compare_type > ZAK_FORM_COMPARE_TYPE_NONE)
+                               {
+                                       if (g_strcmp0 (priv->compare_value, "@now") == 0)
+                                               {
+                                                       GDateTime *gdt_tmp;
+                                                       gchar *gdt_str;
+
+                                                       gdt_tmp = g_date_time_new_now_local ();
+                                                       gdt_str = zak_utils_gdatetime_format (gdt_tmp, priv->format);
+                                                       gdt_compare = zak_utils_get_gdatetime_from_string (gdt_str, priv->format);
+
+                                                       g_free (gdt_str);
+                                                       g_date_time_unref (gdt_tmp);
+                                               }
+                                       else
+                                               {
+                                                       gdt_compare = zak_utils_get_gdatetime_from_string (priv->compare_value, priv->format);
+                                               }
+
+                                       if (gdt_compare != NULL)
+                                               {
+                                                       comp = g_date_time_compare (gdt, gdt_compare);
+                                                       switch (comp)
+                                                               {
+                                                               case -1:
+                                                                       ret = (priv->compare_type == ZAK_FORM_COMPARE_TYPE_LESSER
+                                                                              || priv->compare_type == ZAK_FORM_COMPARE_TYPE_NOT_EQUAL);
+                                                                       break;
+
+                                                               case 0:
+                                                                       ret = (priv->compare_type == ZAK_FORM_COMPARE_TYPE_LESSER_EQUAL
+                                                                              || priv->compare_type == ZAK_FORM_COMPARE_TYPE_EQUAL
+                                                                              || priv->compare_type == ZAK_FORM_COMPARE_TYPE_GREATER_EQUAL);
+                                                                       break;
+
+                                                               case 1:
+                                                                       ret = (priv->compare_type == ZAK_FORM_COMPARE_TYPE_GREATER
+                                                                              || priv->compare_type == ZAK_FORM_COMPARE_TYPE_NOT_EQUAL);
+                                                                       break;
+                                                               };
+
+                                                       if (!ret)
+                                                               {
+                                                                       msg = g_strdup_printf (_("Value must be %s %s"),
+                                                                                              zak_form_get_compare_type_stringify (priv->compare_type),
+                                                                                              zak_utils_gdatetime_format (gdt_compare, priv->format));
+                                                                       zak_form_element_validator_set_message (validator_date, msg);
+                                                                       g_free (msg);
+                                                               }
+
+                                                       g_date_time_unref (gdt_compare);
+                                               }
+
+                               }
+
                        if (gdt != NULL)
                                {
                                        g_date_time_unref (gdt);
index 84cf21ceb92f9c3d61fc936e18ff686fde7489be..d5d489d0393b798620c5e2792db1961ed9fbd168 100644 (file)
@@ -24,6 +24,7 @@
 
 #include <libxml/tree.h>
 
+#include "commons.h"
 #include "formelementvalidator.h"
 
 
@@ -35,6 +36,12 @@ G_DECLARE_FINAL_TYPE (ZakFormElementValidatorDate, zak_form_element_validator_da
 
 ZakFormElementValidatorDate *zak_form_element_validator_date_new (void);
 
+void zak_form_element_validator_date_set_compare_type (ZakFormElementValidatorDate *validator, ZakFormCompareType type);
+ZakFormCompareType zak_form_element_validator_date_get_compare_type (ZakFormElementValidatorDate *validator);
+
+void zak_form_element_validator_date_set_compare_value (ZakFormElementValidatorDate *validator, const gchar *value);
+gchar *zak_form_element_validator_date_get_compare_value (ZakFormElementValidatorDate *validator);
+
 void zak_form_element_validator_date_set_format (ZakFormElementValidatorDate *validator, const gchar *format);
 gchar *zak_form_element_validator_date_get_format (ZakFormElementValidatorDate *validator);
 
index c18b2de8c2623b2d9fac2713b094fe4bcc930894..68a4e5ebf27b7f9b7ebe795389496929da6cea08 100644 (file)
@@ -232,7 +232,6 @@ zak_form_validator_compare_validate (ZakFormValidator *validator)
                        if (comp < 0)
                                {
                                        ret = (priv->type == ZAK_FORM_COMPARE_TYPE_LESSER
-                                              || priv->type == ZAK_FORM_COMPARE_TYPE_LESSER_EQUAL
                                               || priv->type == ZAK_FORM_COMPARE_TYPE_NOT_EQUAL);
                                }
                        else if (comp == 0)
@@ -244,7 +243,6 @@ zak_form_validator_compare_validate (ZakFormValidator *validator)
                        else if (comp > 0)
                                {
                                        ret = (priv->type == ZAK_FORM_COMPARE_TYPE_GREATER
-                                              || priv->type == ZAK_FORM_COMPARE_TYPE_GREATER_EQUAL
                                               || priv->type == ZAK_FORM_COMPARE_TYPE_NOT_EQUAL);
                                };
 
index 2205a805942ba10061c6fa487ac15abc37e656be..ab7dc8291d3b296b7f9ffaed032c21398739960c 100644 (file)
@@ -243,7 +243,6 @@ zak_form_validator_compare_date_validate (ZakFormValidator *validator)
                                                {
                                                case -1:
                                                        ret = (priv->type == ZAK_FORM_COMPARE_TYPE_LESSER
-                                                              || priv->type == ZAK_FORM_COMPARE_TYPE_LESSER_EQUAL
                                                               || priv->type == ZAK_FORM_COMPARE_TYPE_NOT_EQUAL);
                                                        break;
 
@@ -255,7 +254,6 @@ zak_form_validator_compare_date_validate (ZakFormValidator *validator)
 
                                                case 1:
                                                        ret = (priv->type == ZAK_FORM_COMPARE_TYPE_GREATER
-                                                              || priv->type == ZAK_FORM_COMPARE_TYPE_GREATER_EQUAL
                                                               || priv->type == ZAK_FORM_COMPARE_TYPE_NOT_EQUAL);
                                                        break;
                                                };