Post and PM Inline Attachments

Started by dougiefresh, July 26, 2014, 09:14:08 PM

Previous topic - Next topic

@rjen

Updated, looking forward to next version...  ;)
Running SMF 2.1 with latest TinyPortal at www.fjr-club.nl

jakes_t

Great mod!

I've read through most of the thread but couldn't find anything related to the 2 bugs(?) I encountered:

1. Clicking (more attachments) to add another attachment, but leaving the browse box empty (ie, not adding another file) then clicking post gives a blank screen.


2. Feature or bug, I'm not sure - When editing a post to add more inline pics it will attach the next picture on the bottom as an attachment again. You have to the edit again a second time then go "insert attachment" then only does it go inline?

Installed mods:

1.    Avatars on Board/MessageIndex    
2.    Show JDALLAUI in Post and PM    
3.    Post and PM Inline Attachments    
4.    Resize Attached Images    2.4.1

Thanks!

dougiefresh

@jakes_t: I will look into bug #1, as nobody else has reported that one.  Bug #2 is known and no solution has been found yet.

GL700Wing

Quote from: dougiefresh on November 13, 2017, 11:25:40 AM
Bug #2 is known and no solution has been found yet.
I found a solution for this just now when I discovered that copying ./Sources/Subs-InlineAttachments.php from v5.6 fixed the problem.

I then discovered that making the following change in ./Sources/Subs-InlineAttachments.php from v5.11 also fixed the problem:
Find:
// Fetch attachments for use in "parse_bbc" function...
if (!isset($attachments[$msg_id]))
{

Replace With:
// Fetch attachments for use in "parse_bbc" function...

Find:
}

// Load the attachment context even if there are no attachments:

Replace With:


// Load the attachment context even if there are no attachments:


This change effectively reinstates the same code that was used in this section in v5.6 (although "unset($attachments[$msg_id])" was used in v5.6 and "$attachments[$msg_id] = array();" is used in v5.11) but obviously you will need to determine if this solution is appropriate or if it breaks something else ... :)
Life doesn't have to be perfect to be wonderful ...

GL700Wing

I think I've found another bug ...

I have an SMF 2.0.14 forum with no other mods installed except forv5.11 of this mod (without any code changes made) and whenever I select a 'Download link and counter setting' of either 'Filename only', 'Filename and Size' or 'Filename, Size and Dimensions' (ie, either the second, third or fourth option from the drop-down list) all of the image details are displayed (ie, Filename, Size, Dimensions and Count) below the image.
Life doesn't have to be perfect to be wonderful ...

GL700Wing

Quote from: GL700Wing on November 14, 2017, 06:18:39 AM
I think I've found another bug ...

I have an SMF 2.0.14 forum with no other mods installed except forv5.11 of this mod (without any code changes made) and whenever I select a 'Download link and counter setting' of either 'Filename only', 'Filename and Size' or 'Filename, Size and Dimensions' (ie, either the second, third or fourth option from the drop-down list) all of the image details are displayed (ie, Filename, Size, Dimensions and Count) below the image.

I've think I've worked out the fix for this bug too ... :)

In ./Sources/Subs-InlineAttachments.php
Find:
($download_count ? ($download_count >= 5 ? '<br/>' : ' ') . '(' . $attachment['size'] . ($attachment['is_image'] && !empty($dimensions['width']) ? ' - ' . $dimensions['width'] . 'x' . $dimensions['height'] . ($download_count == 6 ? ')<br/>(' : ' - ') . $viewed : ' - ' . $downloaded) . ')' : '').
Replace With:
($download_count ? (($download_count >= 5 ? '<br/>' : ' ') . ($download_count >= 2 ? '(' . $attachment['size'] : '') . ($download_count >= 3 && $attachment['is_image'] && !empty($dimensions['width']) ? ', ' . $dimensions['width'] . 'x' . $dimensions['height'] : '') . ($download_count >= 4 ? (($download_count == 6 ? ')<br/>(' : ' - ') . ($attachment['is_image'] ? $viewed : ' - ' . $downloaded)) : '') . ($download_count >= 2 ? ')' : '')) : '') .

Life doesn't have to be perfect to be wonderful ...

dougiefresh

#686
Uploaded v5.12 - November 14th, 2017
o Modified code to enclose ILA tag with "display: inline-block" CSS style, based on @rjen's code snipet..
o Fixed attachment not inline after editing post, per GL700Wing's instructions.
o Fixed download link/counter code for display, per GL700Wing's instructions.




@rjen and GL700Wing:  Thank you for finding and correcting the code fragments that were the source of the bugs that you found.  I apologize for not being around lately.  Life has been kicking me lately, but I'm fighting back (like normal)....

Quote from: GL700Wing on November 14, 2017, 04:12:50 AM
This change effectively reinstates the same code that was used in this section in v5.6 (although "unset($attachments[$msg_id])" was used in v5.6 and "$attachments[$msg_id] = array();" is used in v5.11) but obviously you will need to determine if this solution is appropriate or if it breaks something else ... :)
I was trying to prevent an unnecessary database call with that if...then statement.  It looks like it really was a necessary call to make...

Quote from: jakes_t on November 03, 2017, 06:24:09 AM
1. Clicking (more attachments) to add another attachment, but leaving the browse box empty (ie, not adding another file) then clicking post gives a blank screen.
I'm still trying to find the time to look into this bug report.  I apologize for any inconvenience....

GL700Wing

Quote from: dougiefresh on November 14, 2017, 11:02:36 AM
@rjen and GL700Wing:  Thank you for finding and correcting the code fragments that were the source of the bugs that you found.  I apologize for not being around lately.  Life has been kicking me lately, but I'm fighting back (like normal)....
You're welcome - happy to help when I can.
Life doesn't have to be perfect to be wonderful ...

pellet_newb

Any luck with Tapatalk support?  Thanks

dougiefresh

Uploaded v5.13 - November 26th, 2017
o Added support for the Tapatalk mod.
o Fixed undefined variable error when HS4SMF mod is installed and enabled.




@pellet_newb:  Sorry about the wait.  It's been frustrating trying to figure out why Tapatalk doesn't seem to respond to my changes, but I discovered that Tapatalk uses a modified set of source files instead of the core source files.  I am able to modify those so that Tapatalk support works (I think...).  Let me know if more needs to be done.  Thanks!

GL700Wing

After installing this mod trying to insert a PM attachment inline doesn't work - clicking on 'Insert Attachment X' does nothing.

In order for inline PM attachments to work the installation of the 'Post and PM Inline Attachments' mod needs to replace of the some code in PersonalMessage.template.php that was added by the 'PM Attachments' mod.  However, and even though the operation fails for two blocks of code the installation for PersonalMessage.template.php shows as 'Test successful' because the two failed operations are set to 'Ignore Errors' (you don't see these messages if all other tests pass and you don't expand the twistie for PersonalMessage.template.php).

The first of the failing 'Replace' operations in ./Themes/default/PersonalMessage.template.php should be as follows (the search was not looking for href="javascript:void(0);" but I don't know if it should still be there after the replace):
Find:
function addAttachment()
{
if (allowed_attachments <= 0)
return alert("', $txt['more_attachments_error'], '");

setOuterHTML(document.getElementById("moreAttachments"), \'<dd class="smalltext"><input type="file" size="38" name="attachment

[]" class="input_file" /><\' + \'/dd><dd class="smalltext" id="moreAttachments"><a href="javascript:void(0);" onclick="addAttachment(); return false;">(', $txt['more_attachments'], ')<\' + \'/a><\' + \'/dd>\');

allowed_attachments = allowed_attachments - 1;

return true;
}

Replace:
var current_attachment = ', (isset($context['current_attachments']) ? count($context['current_attachments']) : 0) + !empty($modSettings['ila_one_based_numbering']), ';

function addAttachment()
{
current_attachment = current_attachment + 1;
if (allowed_attachments <= 0)
return alert("', $txt['more_attachments_error'], '");

var temp = "', $txt['ila_insert'], '";
temp = temp.replace("\%d", current_attachment);
setOuterHTML(document.getElementById("moreAttachments"), \'<dd class="smalltext"><input type="file" size="60" name="attachment

[]" id="attachment\' + current_attachment + \'" class="input_file" /> (<a onclick="insertAttachment(\' + current_attachment + \'); return false;">\' + temp + \'</a>) (<a onclick="cleanFileInput(\\\'attachment\' +

current_attachment + \'\\\');">', $txt['clean_attach'], '</a>)\' + \'</dd><dd class="smalltext" id="moreAttachments"><a onclick="addAttachment(); return false;">(' . $txt['more_attachments'] . ')<\' + \'/a><\' +

\'/dd>\');

allowed_attachments = allowed_attachments - 1;

return true;
}


The second of the failing 'Replace' operations in ./Themes/default/PersonalMessage.template.php should be as follows (it was failing because of the number of tab characters it was searching for and it also added the comment on the last line twice):
Find:
echo '
<dd class="smalltext">';

// Show some useful information such as allowed extensions, maximum size and amount of attachments allowed.


Replace:
$ila_tag = (isset($modSettings['ila_insert_tag']) ? $modSettings['ila_insert_tag'] : 'attachment');
$ila_tag .= (empty($modSettings['ila_insert_format']) ? '=' : ' id=') . '\' + id + \'';
$ila_tag .= (!empty($modSettings['ila_insert_format']) && $modSettings['ila_insert_format'] == 2 ? ' msg=' . (isset($_REQUEST['msg']) ? $_REQUEST['msg'] : 'new') : '');
echo '
<dd class="smalltext">
<script type="text/javascript"><!-- // --><![CDATA[
function insertAttachment(id)
{', (file_exists($sourcedir . '/Subs-SCEditor.php') ? '
$(smfmod_editor).data(\'sceditor\').insertText(\'[' . $ila_tag . ']\');' : '
replaceText(\'[' . $ila_tag . ']\', document.forms.postmodify.' . $context['post_box_name'] . ');'), '
}
// ]]></script>';

// Show some useful information such as allowed extensions, maximum size and amount of attachments allowed.



Also, the operation that always fails in ./Sources/ManageAttachments.php when PM attachments has already been installed could be avoided by changing it from a 'Before' action to an 'After' action as follows:
Find:

'removeall' => 'RemoveAllAttachments'

Add After:
,
'ila' => 'ILA_Admin_Settings'



After I'd made the changes above I had inline PM attachments working but I then discovered images are always displayed as unapproved attachments (eg, with opacity set to 40%) and they are still shown below the message as an attachment even if the option 'Remove attachment image under post after in-post use?' is checked/ticked for posts.  Also, if only the standard SMF viewer is available (ie, neither HS4SMF, Highslide Image Viewer or jQLightbox are installed) clicking on the image does nothing.  To fix all these issues I made the following changes to ./Sources/Subs-InlineAttachments.php

The following code change enables PM attachments to be displayed inline without any opacity.
Find:
$style = (empty($attachment['is_approved']) ? 'opacity: ' . ($style / 100) . '; filter: alpha(opacity=' . $style . ');' : '');
Replace:
$style = (empty($attachment['is_approved']) && !$context['ila']['pm_attach'] ? 'opacity: ' . ($style / 100) . '; filter: alpha(opacity=' . $style . ');' : '');


The following code change stops inline attachments also being displayed below the PM text if the option 'Remove attachment image under post after in-post use?' is checked/ticked for posts.
Find:
// Return to our lord and saviour, our caller! :p
return $html;

Add Before
if (!empty($modSettings['ila_duplicate']) && $context['ila']['pm_attach'])
$context['ila']['dont_show'][$attachment['id']] = true;




The following code change allows inline images in a PM attachment to be downloaded when the image is clicked.
Find:
return '<img src="' . $full . ';image" ' . ' alt="' . $name . '"' . ' class="bbc_img resized' . (!empty($class) ? ' ' . $class : '') . '"' . $style .' />';
Replace:
return '<a href="' . $full . '"><img src="' . $full . ';image" ' . ' alt="' . $name . '"' . ' class="bbc_img resized' . (!empty($class) ? ' ' . $class : '') . '"' . $style .' />';



Finally, I cam across a bit of code that may be redundant in  ./Sources/Subs-InlineAttachments.php.
On line 955 'has_thumb' is set to false when the function is defined:
function ILA_subfunction($id, $full, $thumb, $name, $style = '', $has_thumb = false, $expand = true)

However, further down in same function (line 991) there is a test for 'has_thumb' that may be redundant  because it's doesn't get changed once the function has been called ...
if ($has_thumb)

Life doesn't have to be perfect to be wonderful ...

GL700Wing

#691
I discovered another minor bug with this mod - there is no option to clear/remove the first attachment when composing a new message (the option to clear an attachment is only displayed for the second and subsequent attachments).

In the mod installer file for ./Themes/default/PersonalMessage.template.php:

Find:
<search position="replace"><![CDATA[<input type="file" size="38" name="attachment[]" class="input_file" />';]]></search>
<add><![CDATA[<input type="file" size="60" name="attachment[]" id="attachment1" class="input_file" /> (<a onclick="insertAttachment(', ((empty($context['current_attachments']) ? 0 : count($context['current_attachments'])) + !empty($modSettings['ila_one_based_numbering'])), '); return false;">', sprintf($txt['ila_insert'], ((empty($context['current_attachments']) ? 0 : count($context['current_attachments'])) + !empty($modSettings['ila_one_based_numbering']))),'</a>)';]]></add>


Replace:
<search position="replace"><![CDATA[<input type="file" size="60" name="attachment[]" id="attachment1" class="input_file" /> (<a href="javascript:void(0);" onclick="cleanFileInput(\'attachment1\');">', $txt['clean_attach'], '</a>)';]]></search>
<add><![CDATA[<input type="file" size="60" name="attachment[]" id="attachment1" class="input_file" /> (<a onclick="insertAttachment(', ((empty($context['current_attachments']) ? 0 : count($context['current_attachments'])) + !empty($modSettings['ila_one_based_numbering'])), '); return false;">', sprintf($txt['ila_insert'], ((empty($context['current_attachments']) ? 0 : count($context['current_attachments'])) + !empty($modSettings['ila_one_based_numbering']))),'</a>) (<a href="javascript:void(0);" onclick="cleanFileInput(\'attachment1\');">', $txt['clean_attach'], '</a>)';]]></add>


Note:  This fix assumes the issue of not being to clear any attachments in the 'PM Attachments' mod has also been fixed.
Life doesn't have to be perfect to be wonderful ...

GL700Wing

#692
Quote from: GL700Wing on November 29, 2017, 11:51:49 PM
Finally, I came across a bit of code that may be redundant in  ./Sources/Subs-InlineAttachments.php.
...
Ignore this comment - I figured out what it is for and how it works :)

Last find for today ...

I couldn't get 'attachthumb' to display a non-expandable thumbnail - all it would display was the name of the image and clicking on it just caused a spinning pointer.  I got it working by making the following changes in ./Sources/Subs-InlineAttachments.php:
Find:
// AttachThumb => Show thumbnail ONLY, not expandable
function ILA_tag_attachthumb(&$info, &$dim, $has_thumb, $style)
{
$data = &$info;
if (!empty($info['thumbnail']['has_thumb']))
$dim = array('width' => $info['width'], 'height' => $info['height'], 'img' => $info['href']);
else
$dim = array('width' => $info['real_width'], 'height' => $info['real_height']);
return ILA_subfunction($info['id'], $dim['href'], $dim['href'], $info['name'], $style, $has_thumb);
}


Replace:
// AttachThumb => Show thumbnail ONLY, not expandable
function ILA_tag_attachthumb(&$info, &$dim, $has_thumb, $style)
{
$data = &$info;
$image = ($expand = !empty($info['thumbnail']['has_thumb'])) ? $info['thumbnail']['href'] : $info['href'];
if (!empty($info['thumbnail']['has_thumb']))
$dim = array('width' => $info['width'], 'height' => $info['height'], 'img' => $info['href']);
else
$dim = array('width' => $info['real_width'], 'height' => $info['real_height']);
$expand = false;
return ILA_subfunction($info['id'], $info['href'], $image, $info['name'], $style, $has_thumb, $expand);
}

Life doesn't have to be perfect to be wonderful ...

GL700Wing

Quote from: dougiefresh on November 28, 2017, 12:11:08 PM
Uploaded v5.13 - November 26th, 2017
o Added support for the Tapatalk mod.
o Fixed undefined variable error when HS4SMF mod is installed and enabled.




@pellet_newb:  Sorry about the wait.  It's been frustrating trying to figure out why Tapatalk doesn't seem to respond to my changes, but I discovered that Tapatalk uses a modified set of source files instead of the core source files.  I am able to modify those so that Tapatalk support works (I think...).  Let me know if more needs to be done.  Thanks!
The version of ./Sources/Subs-InlineAttachmentsAdmin.php included with v5.13 still wont allow the option "Allow quoted attachment images from another post?" to be selected if Tapatalk - the option is greyed out which means the checkbox cannot be selected.

The following changes need to be made in ./Sources/Subs-InlineAttachmentsAdmin.php:
Delete the following (lines 67 & 68 in v5.13):
if ($tapatalk = file_exists($sourcedir . '/Subs-Tapatalk.php'))
$modSettings['ila_allow_quoted_images'] = 0;


Find:
array('check', 'ila_allow_quoted_images', ($tapatalk ? 'disabled' : 99) => true),
Replace:
array('check', 'ila_allow_quoted_images'),
Life doesn't have to be perfect to be wonderful ...

GL700Wing

Another couple of changes required in ./Sources/Subs-InlineAttachmentsAdmin.php to enable inline quoted images to be displayed when Tapatalk is displayed:
Find:
Quoteif (empty($modSettings['ila_allow_quoted_images']) || empty($msg) || file_exists($sourcedir . '/Subs-Tapatalk.php'))
Replace:
Quoteif (empty($modSettings['ila_allow_quoted_images']) || empty($msg))

Find:
Quote$allowed = (isset($modSettings['ila_allow_quoted_images']) && !empty($modSettings['ila_allow_quoted_images']) && !file_exists($sourcedir . '/Subs-Tapatalk.php'));
Replace:
Quote$allowed = (isset($modSettings['ila_allow_quoted_images']) && !empty($modSettings['ila_allow_quoted_images']));
Life doesn't have to be perfect to be wonderful ...

dougiefresh

Good Lord, man!  You've been quite the busy beaver, finding and fixing things....  I'm having trouble keeping up!  O:)  I'll incorporate these changes as soon as I can....

GL700Wing

Quote from: dougiefresh on December 05, 2017, 09:41:37 AM
Good Lord, man!  You've been quite the busy beaver, finding and fixing things....  I'm having trouble keeping up!  O:)  I'll incorporate these changes as soon as I can....
I wasn't quite done with this mod but I wasn't sure I could solve the last issue I'd found as it involved another mod (Custom View of Attachments) which is no longer available from this site (although it can still be downloaded from the developer's website).   ;)

The issue was that if only *some* image attachments were inserted inline they were *all* shown as thumbnails in the attachment area at the bottom of the message.

Fortunately the fix is fairly simple to implement and requires two changes to ./Sources/Subs-CustomView.php:
Find:
loadLanguage('CustomView');

if (!empty($modSettings['custom_view_attachments']))


Replace:
loadLanguage('CustomView');

$msg_id = $message['id'];
if (!empty($modSettings['custom_view_attachments']))



Find:
if (!isset($context['ila_dont_show_attach_below']) || !array_key_exists($attachment['id'], $context['ila_dont_show_attach_below']))
{


Replace:
if (empty($context['ila']['dont_show'][$attachment['id']]) && ((!isset($context['ila_dont_show_attach_below']) || !array_key_exists($attachment['id'], $context['ila_dont_show_attach_below']))))
{
// Skip over already displayed inline attachments:
if (!empty($context['ila']['dont_show'][$msg_id][$attachment['id']]))
continue;



Think I'm done now ...  :D
Note:  The Post and PM Inline Attachments mod already makes part of the second change - the bit about skipping over already displayed inline attachments needs to be added.


PS:  I also did a bit of work on PM Attachments
Life doesn't have to be perfect to be wonderful ...

dougiefresh

Uploaded v6.0 - December 8th, 2017
o Per GL700Wing: Fixed issue where inserting a PM attachment inline doesn't work.
o Per GL700Wing: Fixed issue where inline PM attachments were being displayed as unapproved.
o Per GL700Wing: Fixed install issue with PM Attachments mod regarding ManageAttachments.php.
o Per GL700Wing: Fixed issue where inline PM attachments are shown again under PM text.
o Per GL700Wing: Fixed issue where inline PM attachments can't be downloaded.
o Per GL700Wing: Fixed issue where no option to clear/remove the first attachment when composing a new message.
o Per GL700Wing: Fixed issue where [attachthumb] is an expandable thumbnail.
o Per GL700Wing: Fixed issue so that inline attachments can be quoted in Tapatalk.
o Per GL700Wing: Fixed another issue code so that inline attachments can be quoted in Tapatalk.
o Per GL700Wing: Fixed issue with Custom Attachment View mod where all attachments are shown.
o Copied mime detection code from my Play Media Attachments mod.




@GL700Wing:  Thank you for all the bug reports and fixes!  They have been included in this version!

@rjen

Just uninstalled version 5.12 and installed 6.0. Now when I go to the admin section for Inline attachments this error shows..

syntax error, unexpected ''removeall'' (T_CONSTANT_ENCAPSED_STRING), expecting ')'

The error log is quickly filling with errors:

https://test.fjr-club.nl/index.php?action=admin;area=manageattachments;sa=ila;acf1bfd273=7876e56ab0d4bec7eefaf281b85d2dc9
8: Undefined index: description
Bestand: /home/deb77453/domains/fjr-club.nl/public_html/test/Themes/default/GenericMenu.template.php
Regel: 303

Running SMF 2.1 with latest TinyPortal at www.fjr-club.nl

GL700Wing

I think that's my fault - I've just realised that in a suggestion I made in an earlier post I got the 'before' and 'after' positions around the wrong way.

It should have been:
Also, the operation that always fails in ./Sources/ManageAttachments.php when PM attachments has already been installed could be avoided by changing it from a 'after' action to an 'before' action as follows:
Find:

'removeall' => 'RemoveAllAttachments'

Add After:
,
'ila' => 'ILA_Admin_Settings'
Life doesn't have to be perfect to be wonderful ...

Advertisement: