From c9a2fbbbb34c06814357c5f0ac756fb12baabe7c Mon Sep 17 00:00:00 2001 From: Evg Date: Tue, 30 Nov 2021 08:00:54 +0300 Subject: [PATCH] Removing jQuery from uploading photos --- .../Answer/EditAnswerController.php | 1 - .../Facets/EditFacetController.php | 5 +- app/Controllers/Post/AddPostController.php | 5 +- app/Controllers/Post/EditPostController.php | 15 +- app/Controllers/User/SettingController.php | 7 +- app/Libraries/UploadImage.php | 15 +- public/assets/css/image-uploader.css | 166 -------- public/assets/js/image-uploader.js | 379 ------------------ public/assets/js/uploads.js | 120 ++++++ .../views/default/content/facets/edit.php | 15 +- resources/views/default/content/post/add.php | 29 +- resources/views/default/content/post/edit.php | 38 +- resources/views/default/content/post/view.php | 9 +- .../default/content/user/setting/avatar.php | 57 ++- 14 files changed, 246 insertions(+), 615 deletions(-) delete mode 100644 public/assets/css/image-uploader.css delete mode 100644 public/assets/js/image-uploader.js create mode 100644 public/assets/js/uploads.js diff --git a/app/Controllers/Answer/EditAnswerController.php b/app/Controllers/Answer/EditAnswerController.php index 061418e6..ae27ac72 100644 --- a/app/Controllers/Answer/EditAnswerController.php +++ b/app/Controllers/Answer/EditAnswerController.php @@ -31,7 +31,6 @@ class EditAnswerController extends MainController Request::getResources()->addBottomStyles('/assets/js/editor/toastui-editor.min.css'); Request::getResources()->addBottomScript('/assets/js/editor/toastui-editor-all.min.js'); - Request::getResources()->addBottomStyles('/assets/js/editor/dark.css'); return view( '/answer/edit-form-answer', diff --git a/app/Controllers/Facets/EditFacetController.php b/app/Controllers/Facets/EditFacetController.php index ce5d0314..4a807b4e 100644 --- a/app/Controllers/Facets/EditFacetController.php +++ b/app/Controllers/Facets/EditFacetController.php @@ -38,8 +38,7 @@ class EditFacetController extends MainController ); Request::getResources()->addBottomStyles('/assets/css/select2.css'); - Request::getHead()->addStyles('/assets/css/image-uploader.css'); - Request::getResources()->addBottomScript('/assets/js/image-uploader.js'); + Request::getResources()->addBottomScript('/assets/js/uploads.js'); Request::getResources()->addBottomScript('/assets/js/select2.min.js'); return view( @@ -111,7 +110,7 @@ class EditFacetController extends MainController // Запишем img $img = $_FILES['images']; - $check_img = $_FILES['images']['name'][0]; + $check_img = $_FILES['images']['name']; if ($check_img) { UploadImage::img($img, $facet['facet_id'], 'topic'); } diff --git a/app/Controllers/Post/AddPostController.php b/app/Controllers/Post/AddPostController.php index 5ee4fdd3..ad49d3e5 100644 --- a/app/Controllers/Post/AddPostController.php +++ b/app/Controllers/Post/AddPostController.php @@ -20,8 +20,7 @@ class AddPostController extends MainController // Форма добавление поста public function index() { - Request::getHead()->addStyles('/assets/css/image-uploader.css'); - Request::getResources()->addBottomScript('/assets/js/image-uploader.js'); + Request::getResources()->addBottomScript('/assets/js/uploads.js'); Request::getResources()->addBottomStyles('/assets/css/select2.css'); Request::getResources()->addBottomScript('/assets/js/select2.min.js'); Request::getResources()->addBottomStyles('/assets/js/editor/toastui-editor.min.css'); @@ -126,7 +125,7 @@ class AddPostController extends MainController // Обложка поста $cover = $_FILES['images']; - if ($_FILES['images']['name'][0]) { + if ($_FILES['images']['name']) { $post_img = UploadImage::cover_post($cover, 0, $redirect, $this->uid['user_id']); } diff --git a/app/Controllers/Post/EditPostController.php b/app/Controllers/Post/EditPostController.php index f644d32c..57841348 100644 --- a/app/Controllers/Post/EditPostController.php +++ b/app/Controllers/Post/EditPostController.php @@ -26,14 +26,13 @@ class EditPostController extends MainController redirect('/'); } - Request::getHead()->addStyles('/assets/css/image-uploader.css'); + Request::getResources()->addBottomScript('/assets/js/uploads.js'); Request::getResources()->addBottomStyles('/assets/css/select2.css'); - Request::getResources()->addBottomScript('/assets/js/image-uploader.js'); Request::getResources()->addBottomScript('/assets/js/select2.min.js'); Request::getResources()->addBottomStyles('/assets/js/editor/toastui-editor.min.css'); Request::getResources()->addBottomStyles('/assets/js/editor/dark.css'); Request::getResources()->addBottomScript('/assets/js/editor/toastui-editor-all.min.js'); - + return view( '/post/edit', [ @@ -65,7 +64,7 @@ class EditPostController extends MainController $post_user_new = Request::getPost('user_select'); $post_merged_id = Request::getPostInt('post_merged_id'); $post_tl = Request::getPostInt('content_tl'); - $blog_id = Request::getPostInt('blog_id'); + $blog_id = Request::getPostInt('blog_id'); // Связанные посты и темы $post_fields = Request::getPost() ?? []; @@ -100,6 +99,10 @@ class EditPostController extends MainController } Validation::Limits($post_title, Translate::get('title'), '6', '250', $redirect); + + if ($post_content == '') { + $post_content = $post['post_content']; + } Validation::Limits($post_content, Translate::get('the post'), '6', '25000', $redirect); // Проверим хакинг формы @@ -114,12 +117,12 @@ class EditPostController extends MainController // Обложка поста $cover = $_FILES['images']; - if ($_FILES['images']['name'][0]) { + if ($_FILES['images']['name']) { $post_img = UploadImage::cover_post($cover, $post, $redirect, $this->uid['user_id']); } $post_img = $post_img ?? $post['post_content_img']; - + $data = [ 'post_id' => $post_id, 'post_title' => $post_title, diff --git a/app/Controllers/User/SettingController.php b/app/Controllers/User/SettingController.php index 0dc6abfe..03c20138 100644 --- a/app/Controllers/User/SettingController.php +++ b/app/Controllers/User/SettingController.php @@ -106,8 +106,7 @@ class SettingController extends MainController redirect(getUrlByName('setting.avatar', ['login' => $this->uid['user_login']])); } - Request::getHead()->addStyles('/assets/css/image-uploader.css'); - Request::getResources()->addBottomScript('/assets/js/image-uploader.js'); + Request::getResources()->addBottomScript('/assets/js/uploads.js'); return view( '/user/setting/avatar', @@ -129,7 +128,7 @@ class SettingController extends MainController // Запишем img $img = $_FILES['images']; - $check_img = $_FILES['images']['name'][0]; + $check_img = $_FILES['images']['name']; if ($check_img) { $new_img = UploadImage::img($img, $this->uid['user_id'], 'user'); $_SESSION['account']['user_avatar'] = $new_img; @@ -137,7 +136,7 @@ class SettingController extends MainController // Баннер $cover = $_FILES['cover']; - $check_cover = $_FILES['cover']['name'][0]; + $check_cover = $_FILES['cover']['name']; if ($check_cover) { UploadImage::cover($cover, $this->uid['user_id'], 'user'); } diff --git a/app/Libraries/UploadImage.php b/app/Libraries/UploadImage.php index 6805e18e..9a4a882a 100644 --- a/app/Libraries/UploadImage.php +++ b/app/Libraries/UploadImage.php @@ -22,11 +22,11 @@ class UploadImage $default_img = 'noavatar.png'; } - $name = $img['name'][0]; + $name = $img['name']; if ($name) { $filename = $pref . $content_id . '-' . time(); - $file = $img['tmp_name'][0]; + $file = $img['tmp_name']; $image = new SimpleImage(); $image @@ -73,7 +73,8 @@ class UploadImage $month = date('n') . '/'; $file = $img['tmp_name']; $filename = 'post-' . time(); - + + self::createDir($path_img . $year . $month); $image = new SimpleImage(); @@ -119,7 +120,7 @@ class UploadImage if ($cover) { $filename = $pref . $content_id . '-' . time(); - $file_cover = $cover['tmp_name'][0]; + $file_cover = $cover['tmp_name']; $image = new SimpleImage(); $image @@ -155,8 +156,8 @@ class UploadImage public static function cover_post($cover, $post, $redirect, $user_id) { // Проверка ширину - $width_h = getimagesize($cover['tmp_name'][0]); - if ($width_h['0'] < 500) { + $width_h = getimagesize($cover['tmp_name']); + if ($width_h < 500) { $valid = false; addMsg('Ширина меньше 500 пикселей', 'error'); redirect($redirect); @@ -165,7 +166,7 @@ class UploadImage $image = new SimpleImage(); $path = HLEB_PUBLIC_DIR . AG_PATH_POSTS_COVER; $year = date('Y') . '/'; - $file = $cover['tmp_name'][0]; + $file = $cover['tmp_name']; $filename = 'c-' . time(); self::createDir($path . $year); diff --git a/public/assets/css/image-uploader.css b/public/assets/css/image-uploader.css deleted file mode 100644 index 68197885..00000000 --- a/public/assets/css/image-uploader.css +++ /dev/null @@ -1,166 +0,0 @@ -/*! Image Uploader - v1.2.3 - 26/11/2019 - * Copyright (c) 2019 Christian Bayer; Licensed MIT - * + modification (c) Evg 2021 - * Icons Font Face */ - -.upload-text i.icon, -.delete-image i.icon { - margin-right: 0; -} - -.image-uploader { - border: 1px solid #d9d9d9; - position: relative; - width: 95%; - height: 225px; -} - -.create .image-uploader, -.edit-post .image-uploader { - height: 125px; - max-width: 450px; -} - -.post .input-images img { - max-width: 400px; - padding: 20px; - max-height: 130px; -} - -.create .post .input-images img { - max-height: 140px; -} - -.square .image-uploader { - border: 1px solid #d9d9d9; - position: relative; - width: 145px; - height: 141px; - margin-left: 150px; -} - -.square .image-uploader img { - width: 142px; - padding: 8px; - height: 140px; - margin-left: 0; -} - -.img-post-edit img.img-post, -.box.setting.avatar .input-images-cover .image-uploader { - width: 328px; -} - -.img-post-edit { - width: 388px; -} - -a.img-remove { - margin-bottom: 15px; - display: table; -} - -.img-post-edit img.img-post { - border: 1px solid #eee; -} - -.image-uploader.drag-over { - background-color: #f3f3f3; -} - -.img-topic-edit, -.edit-topic .uploaded-image img { - float: left; - border: 1px solid #d9d9d9; - padding: 7px; - width: 141px; -} - -.image-uploader input[type="file"] { - width: 0; - height: 0; - position: absolute; - z-index: -1; - opacity: 0; -} - -.image-uploader .upload-text { - position: absolute; - top: 0; - right: 0; - left: 0; - bottom: 0; - display: flex; - justify-content: center; - align-items: center; - flex-direction: column; -} - -.image-uploader .upload-text i { - display: block; - font-size: 3rem; -} - -.image-uploader .upload-text span { - display: block; -} - -/* Hide the text if there is at least one uploaded image */ -.image-uploader.has-files .upload-text { - display: none; -} - -/* Delete image button */ -.image-uploader .uploaded .uploaded-image .delete-image { - display: none; - cursor: pointer; - position: absolute; - top: 0.2rem; - right: 0.2rem; - padding: 0.3rem; - line-height: 1; - border: none; -} - -.box.setting.avatar img.ava, -.box.setting.space img.ava { - float: left; - border: 1px solid #d9d9d9; - padding: 7px; - width: 140px; -} - -.box.setting.avatar .input-images-cover .uploaded-image img { - width: 312px; -} - -.box-form-img { - float: left; - margin-left: 30px; - width: 150px; -} - -.image-uploader .uploaded .uploaded-image:hover .delete-image { - display: block; -} - -.image-uploader .uploaded .uploaded-image .delete-image i { - display: block; - color: #333; - width: 1.4rem; - height: 1.4rem; - font-size: 1.4rem; - line-height: 1.4rem; -} - -@media screen and (max-width: 786px) { - .image-uploader .uploaded { - padding: 0.3rem; - } - - .image-uploader .uploaded .uploaded-image { - width: calc(33.3333333333% - 0.6rem); - padding-bottom: calc(33.3333333333% - 0.3rem); - margin: 0.3rem; - } -} diff --git a/public/assets/js/image-uploader.js b/public/assets/js/image-uploader.js deleted file mode 100644 index 1720760f..00000000 --- a/public/assets/js/image-uploader.js +++ /dev/null @@ -1,379 +0,0 @@ -/*! Image Uploader - v1.2.3 - 26/11/2019 - * https://github.com/christianbayer/image-uploader - * Copyright (c) 2019 Christian Bayer; Licensed MIT */ - -(function ($) { - - $.fn.imageUploader = function (options) { - - // Default settings - let defaults = { - preloaded: [], - imagesInputName: 'images', - preloadedInputName: 'preloaded', - label: 'Фото', - extensions: ['.jpg', '.jpeg', '.png', '.gif'], - mimes: ['image/jpeg', 'image/png', 'image/gif'], - maxSize: 2100000, // 2мб - maxFiles: 1, - }; - - // Get instance - let plugin = this; - - // Will keep the files - let dataTransfer = new DataTransfer(); - - // The file input - let $input; - - // Set empty settings - plugin.settings = {}; - - // Plugin constructor - plugin.init = function () { - - // Define settings - plugin.settings = $.extend(plugin.settings, defaults, options); - - // Run through the elements - plugin.each(function (i, wrapper) { - - // Create the container - let $container = createContainer(); - - // Append the container to the wrapper - $(wrapper).append($container); - - // Set some bindings - $container.on("dragover", fileDragHover.bind($container)); - $container.on("dragleave", fileDragHover.bind($container)); - $container.on("drop", fileSelectHandler.bind($container)); - - // If there are preloaded images - if (plugin.settings.preloaded.length) { - - // Change style - $container.addClass('has-files'); - - // Get the upload images container - let $uploadedContainer = $container.find('.uploaded'); - - // Set preloaded images preview - for (let i = 0; i < plugin.settings.preloaded.length; i++) { - $uploadedContainer.append(createImg(plugin.settings.preloaded[i].src, plugin.settings.preloaded[i].id, true)); - } - - } - - }); - - }; - - let createContainer = function () { - - // Create the image uploader container - let $container = $('
', { class: 'image-uploader' }); - - // Create the input type file and append it to the container - $input = $('', { - type: 'file', - id: plugin.settings.imagesInputName + '-' + random(), - name: plugin.settings.imagesInputName + '[]', - accept: plugin.settings.extensions.join(','), - multiple: '' - }).appendTo($container); - - // Create the uploaded images container and append it to the container - let $uploadedContainer = $('
', { class: 'uploaded' }).appendTo($container), - - // Create the text container and append it to the container - $textContainer = $('
', { - class: 'upload-text' - }).appendTo($container), - - // Create the icon and append it to the text container - $i = $('', { class: 'bi bi-cloud-arrow-down gray-light' }).appendTo($textContainer), - - // Create the text and append it to the text container - $span = $('', { text: plugin.settings.label }).appendTo($textContainer); - - - // Listen to container click and trigger input file click - $container.on('click', function (e) { - // Prevent browser default event and stop propagation - prevent(e); - - // Trigger input click - $input.trigger('click'); - }); - - // Stop propagation on input click - $input.on("click", function (e) { - e.stopPropagation(); - }); - - // Listen to input files changed - $input.on('change', fileSelectHandler.bind($container)); - - return $container; - }; - - - let prevent = function (e) { - // Prevent browser default event and stop propagation - e.preventDefault(); - e.stopPropagation(); - }; - - let createImg = function (src, id, preloaded) { - - // Create the upladed image container - let $container = $('
', { class: 'uploaded-image' }), - - // Create the img tag - $img = $('', { src: src }).appendTo($container), - - // Create the delete button - $button = $('