From eb74a7f09d25e8f3b86e6b2100e595b125f9acf5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come@opensides.be>
Date: Mon, 5 Sep 2016 16:23:02 +0200
Subject: [PATCH] Fixes #5137 Improving error messages

---
 html/themes/breezy/dialog.css                 |  4 +++
 include/class_msgPool.inc                     | 28 +++++++++++++++----
 .../attributes/class_SetAttribute.inc         |  8 +++++-
 include/simpleplugin/class_Attribute.inc      |  4 +--
 4 files changed, 36 insertions(+), 8 deletions(-)

diff --git a/html/themes/breezy/dialog.css b/html/themes/breezy/dialog.css
index 3b8b0f712..026270dbb 100644
--- a/html/themes/breezy/dialog.css
+++ b/html/themes/breezy/dialog.css
@@ -23,6 +23,10 @@ top: 200px;
   }
 }
 
+div.msgdialog .description {
+font-style:italic;
+}
+
 /* Small screens (i.e. phones) */
 @media (max-width: 640px) {
   div.msgdialog {
diff --git a/include/class_msgPool.inc b/include/class_msgPool.inc
index 1d617951e..5bbbb9ebf 100644
--- a/include/class_msgPool.inc
+++ b/include/class_msgPool.inc
@@ -398,13 +398,22 @@ class msgPool {
    *
    * \param string $name The entry duplicated
    */
-  public static function duplicated($name, $dn = NULL)
+  public static function duplicated($name, $dn = NULL, $ldapName = '', $description = '')
   {
+    if (empty($name)) {
+      $fieldName = sprintf('<em>%s</em>', $ldapName);
+    } else {
+      $fieldName = sprintf(_('"%s"'), $name);
+    }
     if ($dn == NULL) {
-      return sprintf(_("There is already an entry with this '%s' attribute in the system!"), $name);
+      $msg = sprintf(_('There is already an entry with this %s attribute in the system!'), $fieldName);
     } else {
-      return sprintf(_("The entry '%s' already use this '%s' attribute!"), $dn, $name);
+      $msg = sprintf(_('The entry "%s" already use this %s attribute!'), $dn, $fieldName);
+    }
+    if (!empty($description)) {
+      $msg .= sprintf('<p class="description">%s</p>', $description);
     }
+    return $msg;
   }
 
   /*!
@@ -412,9 +421,18 @@ class msgPool {
    *
    * \param string $name Name of the field
    */
-  public static function required($name)
+  public static function required($name, $ldapName = '', $description = '')
   {
-    return sprintf(_("The required field '%s' is empty!"), $name);
+    if (empty($name)) {
+      $fieldName = sprintf('<em>%s</em>', $ldapName);
+    } else {
+      $fieldName = sprintf(_('"%s"'), $name);
+    }
+    $msg = sprintf(_('The required field %s is empty!'), $fieldName);
+    if (!empty($description)) {
+      $msg .= sprintf('<p class="description">%s</p>', $description);
+    }
+    return $msg;
   }
 
   /*!
diff --git a/include/simpleplugin/attributes/class_SetAttribute.inc b/include/simpleplugin/attributes/class_SetAttribute.inc
index 8f93933bd..5478f47ba 100644
--- a/include/simpleplugin/attributes/class_SetAttribute.inc
+++ b/include/simpleplugin/attributes/class_SetAttribute.inc
@@ -579,7 +579,13 @@ class OrderedArrayAttribute extends SetAttribute
       $this->attribute->loadPostValue();
       $this->attribute->applyPostValue();
       if ($error = $this->attribute->check()) {
-        msg_dialog::display(sprintf(_('Invalid value for %s'), $this->getLabel()), $error);
+        $label = $this->getLabel();
+        if (empty($label)) {
+          $label = '<em>'.$this->getLdapName().'</em>';
+        } else {
+          $label = sprintf(_('"%s"'), $label);
+        }
+        msg_dialog::display(sprintf(_('Invalid value for %s'), $label), $error);
       } else {
         $this->addPostValue($this->attribute->getValue());
       }
diff --git a/include/simpleplugin/class_Attribute.inc b/include/simpleplugin/class_Attribute.inc
index 14bf31c40..44586cd8f 100644
--- a/include/simpleplugin/class_Attribute.inc
+++ b/include/simpleplugin/class_Attribute.inc
@@ -431,7 +431,7 @@ class Attribute
     global $config;
     $value = $this->getValue();
     if ($this->isRequired() && !$this->disabled && (($value === "") || ($value === array()))) {
-      return msgPool::required($this->getLabel());
+      return msgPool::required($this->getLabel(), $this->getLdapName(), $this->getDescription());
     } elseif (($this->unique !== FALSE) && !$this->disabled) {
       $value = $this->computeLdapValue();
       if (($value === "") || ($value === array())) {
@@ -527,7 +527,7 @@ class Attribute
             continue;
           }
 
-          return msgPool::duplicated($this->getLabel(), $attrs['dn']);
+          return msgPool::duplicated($this->getLabel(), $attrs['dn'], $this->getLdapName(), $this->getDescription());
         }
       }
     }
-- 
GitLab