From 30d58286a5ae8dd94fa698582109a21789929ec3 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Thu, 23 Apr 2015 20:56:11 -0500
Subject: [PATCH] Fix and add direct download attribute to image hover

---
 assets/default/css/theme.css |  6 ++++-
 include/class.format.php     |  2 +-
 include/client/view.inc.php  | 46 ++++++++++++++++++++++++++++--------
 js/osticket.js               |  4 +++-
 4 files changed, 45 insertions(+), 13 deletions(-)

diff --git a/assets/default/css/theme.css b/assets/default/css/theme.css
index e1cf19907..97703547a 100644
--- a/assets/default/css/theme.css
+++ b/assets/default/css/theme.css
@@ -885,7 +885,6 @@ a.refresh {
 #ticketThread .info a {
   display: inline-block;
   margin: 5px 10px 5px 0;
-  padding-left: 24px;
   height: 16px;
   line-height: 16px;
   background-position: 0 50%;
@@ -1048,3 +1047,8 @@ img.sign-in-image {
     overflow: hidden;
     text-overflow: ellipsis;
 }
+.image-hover a.action-button:hover,
+.image-hover a.action-button {
+    color: initial !important;
+    text-decoration: none;
+}
diff --git a/include/class.format.php b/include/class.format.php
index 06a07f506..b184b26cf 100644
--- a/include/class.format.php
+++ b/include/class.format.php
@@ -366,7 +366,7 @@ class Format {
             },
             'schemes' => 'href: aim, feed, file, ftp, gopher, http, https, irc, mailto, news, nntp, sftp, ssh, telnet; *:file, http, https; src: cid, http, https, data',
             'elements' => '*+iframe',
-            'spec' => 'span=data-src,width,height',
+            'spec' => 'span=data-src,width,height;img=data-cid',
         );
         return Format::html($text, $config);
     }
diff --git a/include/client/view.inc.php b/include/client/view.inc.php
index 249f3058b..3b55a0fb6 100644
--- a/include/client/view.inc.php
+++ b/include/client/view.inc.php
@@ -128,16 +128,27 @@ if($ticket->getThreadCount() && ($thread=$ticket->getClientThread())) {
             </th></tr>
             <tr><td class="thread-body"><div><?php echo Format::clickableurls($entry->getBody()->toHtml()); ?></div></td></tr>
             <?php
-            if($entry->has_attachments
-                    && ($urls = $entry->getAttachmentUrls())
-                    && ($links = $entry->getAttachmentsLinks())) { ?>
-                <tr><td class="info"><?php echo $links; ?></td></tr>
-<?php       }
-            if ($urls) { ?>
-                <script type="text/javascript">
-                    $(function() { showImagesInline(<?php echo
-                        JsonDataEncoder::encode($urls); ?>); });
-                </script>
+            $urls = null;
+            if ($entry->has_attachments
+                && ($urls = $entry->getAttachmentUrls())) { ?>
+            <tr>
+                <td class="info"><?php
+                    foreach ($entry->attachments as $A) {
+                        if ($A->inline) continue;
+                        $size = '';
+                        if ($A->file->size)
+                            $size = sprintf('<em>(%s)</em>',
+                                Format::file_size($A->file->size));
+?>
+                &nbsp; <i class="icon-paperclip"></i>
+                <a class="no-pjax" href="<?php echo $A->file->getDownloadUrl();
+                    ?>" download="<?php echo Format::htmlchars($A->file->name); ?>"
+                        target="_blank">
+                <?php echo Format::htmlchars($A->file->name);
+                ?></a><?php echo $size;?>&nbsp;
+<?php               } ?>
+                </td>
+            </tr>
 <?php       } ?>
         </table>
     <?php
@@ -199,3 +210,18 @@ if (!$ticket->isClosed() || $ticket->isReopenable()) { ?>
 </form>
 <?php
 } ?>
+<script type="text/javascript">
+<?php
+// Hover support for all inline images
+$urls = array();
+foreach (AttachmentFile::objects()->filter(array(
+    'attachments__thread_entry__thread__id' => $ticket->getThreadId(),
+    'attachments__inline' => true,
+)) as $file) {
+    $urls[strtolower($file->getKey())] = array(
+        'download_url' => $file->getDownloadUrl(),
+        'filename' => $file->name,
+    );
+} ?>
+showImagesInline(<?php echo JsonDataEncoder::encode($urls); ?>);
+</script>
diff --git a/js/osticket.js b/js/osticket.js
index 911cbf4e7..28fd56fc8 100644
--- a/js/osticket.js
+++ b/js/osticket.js
@@ -161,7 +161,9 @@ showImagesInline = function(urls, thread_id) {
                     }
                 ).append($('<div class="caption">')
                     .append('<span class="filename">'+info.filename+'</span>')
-                    .append('<a href="'+info.download_url+'" class="action-button pull-right"><i class="icon-download-alt"></i> ' + __('Download') + '</a>')
+                    .append($('<a href="'+info.download_url+'" class="action-button pull-right"><i class="icon-download-alt"></i> ' + __('Download') + '</a>')
+                      .attr('download', info.filename)
+                    )
                 );
             e.data('wrapped', true);
         }
-- 
GitLab