]> git.mxchange.org Git - friendica.git/commitdiff
attachment preview: some backend work in the item module:
authorrabuzarus <trebor@central-unit>
Fri, 1 Feb 2019 22:42:36 +0000 (23:42 +0100)
committerrabuzarus <trebor@central-unit>
Fri, 1 Feb 2019 22:42:36 +0000 (23:42 +0100)
- convert attachment to bbcode and
- add the attachment bbcode to the item body

mod/item.php
view/js/linkPreview.js

index 7cb2849a89e137e54a85fbcecbb4434b2dcc47dd..40c01da20f48d5a83d489c676e2a5b30770d4a71 100644 (file)
@@ -40,6 +40,8 @@ use Friendica\Util\Emailer;
 use Friendica\Util\Security;
 use Friendica\Util\Strings;
 
+require_once 'include/items.php';
+
 function item_post(App $a) {
        if (!local_user() && !remote_user()) {
                return 0;
@@ -188,6 +190,40 @@ function item_post(App $a) {
        $categories = '';
        $postopts = '';
        $emailcc = '';
+       $body = defaults($_REQUEST, 'body', '');
+       $has_attachment = defaults($_REQUEST, 'has_attachment', 0);
+
+       // If we have a speparate attachment, we need to add it to the body.
+       if (!empty($has_attachment)) {
+               $attachment_type  = defaults($_REQUEST, 'attachment_type',  '');
+               $attachment_title = defaults($_REQUEST, 'attachment_title', '');
+               $attachment_text  = defaults($_REQUEST, 'attachment_text',  '');
+
+               $attachment_url     = hex2bin(defaults($_REQUEST, 'attachment_url',     ''));
+               $attachment_img_src = hex2bin(defaults($_REQUEST, 'attachment_img_src', ''));
+
+               $attachment_img_width  = defaults($_REQUEST, 'attachment_img_width',  0);
+               $attachment_img_height = defaults($_REQUEST, 'attachment_img_height', 0);
+               $attachment = [
+                       'type'   => $attachment_type,
+                       'title'  => $attachment_title,
+                       'text'   => $attachment_text,
+                       'url'    => $attachment_url,
+               ];
+
+               if (!empty($attachment_img_src)) {
+                       $attachment['images'] = [
+                               0 => [
+                                       'src'    => $attachment_img_src,
+                                       'width'  => $attachment_img_width,
+                                       'height' => $attachment_img_height
+                               ]
+                       ];
+               }
+
+               $att_bbcode = add_page_info_data($attachment);
+               $body .= $att_bbcode;
+       }
 
        if (!empty($orig_post)) {
                $str_group_allow   = $orig_post['allow_gid'];
@@ -201,7 +237,7 @@ function item_post(App $a) {
                $app               = $orig_post['app'];
                $categories        = $orig_post['file'];
                $title             = Strings::escapeTags(trim($_REQUEST['title']));
-               $body              = Strings::escapeHtml(trim($_REQUEST['body']));
+               $body              = Strings::escapeHtml(trim($body));
                $private           = $orig_post['private'];
                $pubmail_enabled   = $orig_post['pubmail'];
                $network           = $orig_post['network'];
@@ -237,7 +273,7 @@ function item_post(App $a) {
                $coord             = Strings::escapeTags(trim(defaults($_REQUEST, 'coord'   , '')));
                $verb              = Strings::escapeTags(trim(defaults($_REQUEST, 'verb'    , '')));
                $emailcc           = Strings::escapeTags(trim(defaults($_REQUEST, 'emailcc' , '')));
-               $body              = Strings::escapeHtml(trim(defaults($_REQUEST, 'body'    , '')));
+               $body              = Strings::escapeHtml(trim($body));
                $network           = Strings::escapeTags(trim(defaults($_REQUEST, 'network' , Protocol::DFRN)));
                $guid              = System::createUUID();
 
index dcc1677cc57d7f91fa78c79a5cdb1416290455fb..ab5091c7e750893427e232761ae7d826686afe8b 100644 (file)
                selector = selector.substr(1);\r
 \r
                var previewTpl = '\\r
-                       <div id="preview_' + selector + '" class="preview {0}">{1}</div>\\r
-                       <div style="clear: both"></div>';\r
+                       <div id="preview_' + selector + '" class="preview {0}">\\r
+                               {1}\\r
+                               <input type="hidden" name="has_attachment" id="hasAttachment_' + selector + '" value="{2}" />\\r
+                               <input type="hidden" name="attachment_url" id="attachmentUrl_' + selector + '" value="{3}" />\\r
+                               <input type="hidden" name="attachment_type" id="attachmentType_' + selector + '" value="{4}" />\\r
+                       </div>';\r
+\r
                var attachmentTpl = '\\r
                        <hr class="previewseparator">\\r
                        <div id="closePreview_' + selector + '" title="Remove" class="closePreview" >\\r
@@ -32,6 +37,9 @@
                                <div id="previewImage_' + selector + '" class="previewImage">\\r
                                </div>\\r
                                <input type="hidden" id="photoNumber_' + selector + '" class="photoNumber" value="0" />\\r
+                               <input type="hidden" name="attachment_img_src" id="attachmentImageSrc_' + selector + '" value="" />\\r
+                               <input type="hidden" name="attachment_img_width" id="attachmentImageWidth_' + selector + '" value="0" />\\r
+                               <input type="hidden" name="attachment_img_height" id="attachmentImageHeight_' + selector + '" value="0" />\\r
                        </div>\\r
                        <div id="previewContent_' + selector + '" class="previewContent">\\r
                                <h4 id="previewTitle_' + selector + '" class="previewTitle"></h4>\\r
@@ -73,8 +81,7 @@
                        });\r
                };\r
                var resetPreview = function() {\r
-                       $('#previewChangeImg_' + selector).removeClass('buttonChangeActive');\r
-                       $('#previewChangeImg_' + selector).addClass('buttonChangeDeactive');\r
+                       $('#hasAttachment_' + selector).val(0);\r
                        photoNumber = 0;\r
                        images = "";\r
                }\r
                                isExtern = true;\r
                        }\r
 \r
+                       // Don't procces the textarea input if we have already\r
+                       // an attachment preview.\r
+                       if (!isExtern && isActive) {\r
+                               return;\r
+                       }\r
+\r
                        if (trim(text) !== "") {\r
                                if (block === false && urlRegex.test(text)) {\r
                                        binurl = bin2hex(text);\r
                        // Note: if we finish the Preview of other media content type,\r
                        // we can move this condition to the beggining of crawlText();\r
                        if (isActive) {\r
+                               $('#profile-rotator').hide();\r
                                return;\r
                        }\r
 \r
                        if (json.type != 'link' && json.type != 'video' && json.type != 'photo' || json.url == json.title) {\r
+                               $('#profile-rotator').hide();\r
                                return;\r
                        }\r
 \r
 \r
                        // Load and add the template if it isn't allready loaded.\r
                        if ($('#preview_' + selector).length == 0) {\r
-                               var tpl = previewTpl.format(typeClass, attachmentTpl);\r
+                               var tpl = previewTpl.format(\r
+                                       typeClass,\r
+                                       attachmentTpl,\r
+                                       1,\r
+                                       bin2hex(json.url),\r
+                                       json.type\r
+                               );\r
                                $('#' + selector).after(tpl);\r
                        }\r
 \r
 \r
                        $('#previewTitle_' + selector).html("\\r
                                <span id='previewSpanTitle_" + selector + "' class='previewSpanTitle' >" + escapeHTML(json.title) + "</span>\\r
-                               <input type='text' value='" + escapeHTML(json.title) + "' id='previewInputTitle_" + selector + "' class='previewInputTitle inputPreview' style='display: none;'/>"\r
+                               <input type='text' name='attachment_title' value='" + escapeHTML(json.title) + "' id='previewInputTitle_" + selector + "' class='previewInputTitle inputPreview' style='display: none;'/>"\r
                        );\r
 \r
-\r
                        $('#previewDescription_' + selector).html("\\r
                                <span id='previewSpanDescription_" + selector + "' class='previewSpanDescription' >" + escapeHTML(description) + "</span>\n\\r
-                               <textarea id='previewInputDescription_" + selector + "' class='previewInputDescription' style='display: none;' class='inputPreview' >" + escapeHTML(json.text) + "</textarea>"\r
+                               <textarea id='previewInputDescription_" + selector + "' name='attachment_text' class='previewInputDescription' style='display: none;' class='inputPreview' >" + escapeHTML(json.text) + "</textarea>"\r
                        );\r
 \r
                        if (json.url) {\r
 \r
                        if (Array.isArray(images)) {\r
                                $('#previewImages_' + selector).show();\r
+                               $('#attachmentImageSrc_' + selector).val(bin2hex(images[photoNumber].src));\r
+                               $('#attachmentImageWidth_' + selector).val(images[photoNumber].width);\r
+                               $('#attachmentImageHeight_' + selector).val(images[photoNumber].height);\r
                        } else {\r
                                $('#previewImages_' + selector).hide();\r
                        }\r
 \r
                        $('#previewImage_' + selector).html(appendImage + "<div id='whiteImage' style='color: transparent; display:none;'>...</div>");\r
 \r
-                       // more than just one image.\r
+                       // More than just one image.\r
                        if (images.length > 1) {\r
                                // Enable the the button to change the preview pictures.\r
                                $('#previewChangeImg_' + selector).show();\r
                                        $('#previewChangeImg_' + selector).unbind('click').click(function (e) {\r
                                                e.stopPropagation();\r
                                                if (images.length > 1) {\r
-//                                                     photoNumber = parseInt($('#photoNumber_' + selector).val());\r
                                                        $('#imagePreview_' + selector + '_' + photoNumber).css({\r
                                                                'display': 'none'\r
                                                        });\r
                                                                'display': 'block'\r
                                                        });\r
                                                        $('#photoNumber_' + selector).val(photoNumber);\r
+                                                       $('#attachmentImageSrc_' + selector).val(bin2hex(images[photoNumber].src));\r
+                                                       $('#attachmentImageWidth_' + selector).val(images[photoNumber].width);\r
+                                                       $('#attachmentImageHeight_' + selector).val(images[photoNumber].height);\r
                                                }\r
                                        });\r
                                }\r