From 744e5f23382b36b8e7a9697a763c57295f8daa80 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come@opensides.be>
Date: Thu, 4 Aug 2016 16:38:31 +0200
Subject: [PATCH] Fixes #4832 Allow removal of ImageAttribute

---
 include/class_IconTheme.inc                   |  4 ++
 include/simpleplugin/class_attribute.inc      | 37 +++++++++++++++++--
 .../simpleplugin/class_dialogAttributes.inc   |  2 +-
 3 files changed, 39 insertions(+), 4 deletions(-)

diff --git a/include/class_IconTheme.inc b/include/class_IconTheme.inc
index 8e15b3b40..b7874729d 100644
--- a/include/class_IconTheme.inc
+++ b/include/class_IconTheme.inc
@@ -309,6 +309,10 @@ class IconTheme
       array('actions','go-up'),
       array('actions','arrow-up'),
     ),
+    'actions/upload'                => array(
+      array('actions','document-import'),
+      array('actions','up'),
+    ),
     'actions/down'                  => array(
       array('actions','go-down'),
       array('actions','arrow-down'),
diff --git a/include/simpleplugin/class_attribute.inc b/include/simpleplugin/class_attribute.inc
index 8836f0f52..0ca9001ca 100644
--- a/include/simpleplugin/class_attribute.inc
+++ b/include/simpleplugin/class_attribute.inc
@@ -1782,8 +1782,20 @@ class ImageAttribute extends FileAttribute
   function loadPostValue ()
   {
     $this->postValue = $this->value;
-    if (isset($_POST['upload'.$this->getHtmlId()])) {
-      parent::loadPostValue();
+    $id = $this->getHtmlId();
+    if (!$this->disabled && $this->isVisible()) {
+      foreach (array_keys($_POST) as $name) {
+        if (!$this->isRequired()) {
+          if (preg_match('/^'.$id.'_remove_/', $name)) {
+            $this->setPostValue('');
+            break;
+          }
+        }
+        if (preg_match('/^'.$id.'_upload_/', $name)) {
+          parent::loadPostValue();
+          break;
+        }
+      }
     }
   }
 
@@ -1846,7 +1858,26 @@ class ImageAttribute extends FileAttribute
                 ' title="'.$this->getDescription().'"'.
                 ' /><br/>';
     $display  .= $this->renderInputField('file', $id);
-    $display  .= $this->renderInputField('submit', 'upload'.$id, array('value' => _('Upload')));
+    $display .= $this->renderInputField(
+      'image', $id.'_upload',
+      array(
+        'class' => 'center',
+        'src'   => 'geticon.php?context=actions&amp;icon=upload&amp;size=16',
+        'title' => _('Upload'),
+        'alt'   => _('Upload')
+      )
+    );
+    if (!$this->isRequired()) {
+      $display .= $this->renderInputField(
+        'image', $id.'_remove',
+        array(
+          'class' => 'center',
+          'src'   => 'geticon.php?context=actions&amp;icon=remove&amp;size=16',
+          'title' => _('Remove'),
+          'alt'   => _('Remove')
+        )
+      );
+    }
     if (($this->getValue() == '') && ($this->placeholder != '')) {
       session::set('binary', $this->placeholder);
     } else {
diff --git a/include/simpleplugin/class_dialogAttributes.inc b/include/simpleplugin/class_dialogAttributes.inc
index 947e26115..9e00e7ff8 100644
--- a/include/simpleplugin/class_dialogAttributes.inc
+++ b/include/simpleplugin/class_dialogAttributes.inc
@@ -641,7 +641,7 @@ class UserAttribute extends DialogButtonAttribute
         'image', $id.'_remove',
         array(
           'class' => 'center',
-          'src'   => 'geticon.php?context=actions&amp;icon=edit-delete&amp;size=16',
+          'src'   => 'geticon.php?context=actions&amp;icon=remove&amp;size=16',
           'title' => _('Remove'),
           'alt'   => _('Remove')
         )
-- 
GitLab