Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.0] Added Save2Copy in Media Manager #30313

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
173412b
Added Save2Copy in Media Manager
vlaucht Aug 7, 2020
1ee870c
Added Save2Copy in Media Manager
vlaucht Aug 7, 2020
f3c3232
Swapped toolbar buttons
vlaucht Aug 10, 2020
1d2c02c
Added messages + file name + fixed redirect
vlaucht Aug 12, 2020
60a545f
Fixed file directory on close
vlaucht Aug 12, 2020
59b64d6
Removed image name translation from toolbar
vlaucht Aug 12, 2020
591591c
Added LTR support for toolbar title
vlaucht Aug 13, 2020
464beff
Fixed linting errors
vlaucht Aug 13, 2020
062282b
Modified toolbar title
vlaucht Aug 13, 2020
6c320fb
Fixed save error in subfolder and added error message on save
vlaucht Aug 18, 2020
a0cd0c7
Changed save2copy to use adapter and fixed error messages for exceptions
vlaucht Aug 18, 2020
7a29822
removed console.log from edit-images.js
vlaucht Aug 18, 2020
90ff4f6
Update administrator/components/com_media/src/Controller/ApiControlle…
vlaucht Aug 19, 2020
0ee94ae
Merge remote-tracking branch 'upstream/4.0-dev' into save2copy_mm_btn
richard67 Aug 21, 2020
6f73ff3
Fixed edit after rename issue
vlaucht Aug 22, 2020
16cd912
Merge remote-tracking branch 'origin/save2copy_mm_btn' into save2copy…
vlaucht Aug 22, 2020
034a191
Approach for displaying file name in modal
vlaucht Aug 23, 2020
1c2f1cc
Merge remote-tracking branch 'upstream/4.0-dev' into save2copy_mm_btn
vlaucht Aug 24, 2020
e4dc71f
Test image name in modal title
vlaucht Aug 24, 2020
cbdd1d9
Merge remote-tracking branch 'upstream/4.0-dev' into save2copy_mm_btn
vlaucht Aug 25, 2020
c889ca5
resolved conflicts
vlaucht Aug 25, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class Api {
headers: {'Content-Type': 'application/json'},
onSuccess: (response) => {
notifications.success('COM_MEDIA_RENAME_SUCCESS');
resolve(this._normalizeItem(JSON.parse(response).data))
resolve(this._normalizeItem(JSON.parse(response).data.file))
},
onError: (xhr) => {
notifications.error('COM_MEDIA_RENAME_ERROR');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Filesystem\File;
use Joomla\CMS\Filesystem\Path;
use Joomla\CMS\Helper\MediaHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\BaseController;
Expand All @@ -22,6 +24,7 @@
use Joomla\Component\Media\Administrator\Exception\FileExistsException;
use Joomla\Component\Media\Administrator\Exception\FileNotFoundException;
use Joomla\Component\Media\Administrator\Exception\InvalidPathException;
use Joomla\String\StringHelper;

/**
* Api Media Controller
Expand Down Expand Up @@ -252,7 +255,7 @@ public function postFiles()
* "move" : "0"
* }
*
* @return array The data to send with the response
* @return \stdClass The data to send with the response
*
* @since 4.0.0
* @throws \Exception
Expand All @@ -261,18 +264,31 @@ public function putFiles()
{
$adapter = $this->getAdapter();
$path = $this->getPath();
$model = $this->getModel();

$content = $this->input->json;
$name = basename($path);
$mediaContent = base64_decode($content->get('content', '', 'raw'));
$newPath = $content->getString('newPath', null);
$move = $content->get('move', true);
$resp = new \stdClass;

if ($mediaContent != null)
{
$this->checkContent();
$resp->isClose = $content->get('isClose');
$resp->isCopy = $content->get('isCopy');

$this->getModel()->updateFile($adapter, $name, str_replace($name, '', $path), $mediaContent);
if ($content->get('isCopy'))
{
$name = $this->generateNewName($name, $path, $model, $adapter);
$path = dirname($path) . '/' . $name;
$this->getModel()->createFile($adapter, $name, str_replace($name, '', $path), $mediaContent, false);
}
else
{
$this->getModel()->updateFile($adapter, $name, str_replace($name, '', $path), $mediaContent);
}
}

if ($newPath != null && $newPath !== $adapter . ':' . $path)
Expand All @@ -291,9 +307,43 @@ public function putFiles()
$path = $destinationPath;
}

return $this->getModel()->getFile($adapter, $path);
$resp->file = $this->getModel()->getFile($adapter, $path);

return $resp;
}

/**
* Method to change the name of an image
*
* @param string $name The current name.
* @param string $path The path.
*
* @param mixed $model The model
* @param string $adapter The adapter
* @return string Contains the modified name.
*
* @since 4.0
*/
protected function generateNewName($name, $path, $model, $adapter)
{
$extension = File::getExt($name);

try
{
while ($model->getFile($adapter, $path))
{
$base = File::stripExt($name);
$name = StringHelper::increment($base, 'dash') . '.' . $extension;
$path = dirname($path) . '/' . $name;
}
}
catch (FileNotFoundException $e)
{
return $name;
}

return $name;
}
/**
* Send the given data as JSON response in the following format:
*
Expand Down
15 changes: 10 additions & 5 deletions administrator/components/com_media/src/View/File/HtmlView.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function display($tpl = null)
throw new \Exception('No content available!');
}

$this->addToolbar();
$this->addToolbar($this->file->name);

return parent::display($tpl);
}
Expand All @@ -64,12 +64,17 @@ public function display($tpl = null)
*
* @since 4.0.0
*/
protected function addToolbar()
protected function addToolbar($name)
{
ToolbarHelper::title(Text::_('COM_MEDIA_EDIT'), 'images mediamanager');

Text::script('JLIB_APPLICATION_SAVE_SUCCESS');
Text::script('JLIB_APPLICATION_ERROR_SAVE_FAILED_FILE');
ToolbarHelper::title(Text::_('COM_MEDIA_EDIT') . ' - ' . $name, 'images mediamanager');
ToolbarHelper::apply('apply');
ToolbarHelper::save('save');
$toolbarButtons = [['save', 'save'], ['save2copy', 'save2copy']];
ToolbarHelper::saveGroup(
$toolbarButtons,
'btn-success'
);
ToolbarHelper::custom('reset', 'refresh', '', 'COM_MEDIA_RESET', false);

ToolbarHelper::cancel('cancel');
Expand Down
2 changes: 1 addition & 1 deletion administrator/language/en-GB/com_media.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ COM_MEDIA_DECREASE_GRID="Decrease grid size"
COM_MEDIA_DELETE_ERROR="Error deleting the item."
COM_MEDIA_DELETE_SUCCESS="Item deleted."
COM_MEDIA_DROP_FILE="Drop file(s) to Upload"
COM_MEDIA_EDIT="Media Edit"
COM_MEDIA_EDIT="Media: Edit"
COM_MEDIA_ERROR="An error occurred."
COM_MEDIA_ERROR_NO_PROVIDERS="No filesystem providers have been found. Please enable at least one <a href=\"%s\">filesystem plugin</a>."
COM_MEDIA_ERROR_NOT_AUTHENTICATED="You are not authenticated. Please login."
Expand Down
1 change: 1 addition & 0 deletions administrator/language/en-GB/lib_joomla.ini
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ JLIB_APPLICATION_ERROR_REORDER_FAILED="Reorder failed. Error: %s"
JLIB_APPLICATION_ERROR_ROUTER_LOAD="Unable to load router: %s"
JLIB_APPLICATION_ERROR_RUN_TRANSITION="Unable to run transition."
JLIB_APPLICATION_ERROR_SAVE_FAILED="Save failed with the following error: %s"
JLIB_APPLICATION_ERROR_SAVE_FAILED_FILE="An error has occurred while saving the file."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add the same string in frontend similar file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vlaucht Has been solved with commit 034a191 , right?

JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED="Save not permitted."
JLIB_APPLICATION_ERROR_SERVER="Internal server error."
JLIB_APPLICATION_ERROR_TABLE_NAME_NOT_SUPPORTED="Table %s not supported. File not found."
Expand Down
50 changes: 39 additions & 11 deletions build/media_source/com_media/js/edit-images.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ Joomla.MediaManager = Joomla.MediaManager || {};
const forUpload = {
name,
content: Joomla.MediaManager.Edit.current.contents.replace(`data:image/${format};base64,`, ''),
isCopy: false,
isClose: false,
};

// eslint-disable-next-line prefer-destructuring
Expand All @@ -142,23 +144,20 @@ Joomla.MediaManager = Joomla.MediaManager || {};

forUpload[options.csrfToken] = '1';

let fileDirectory = uploadPath.split('/');
fileDirectory.pop();
fileDirectory = fileDirectory.join('/');

// If we are in root add a backslash
if (fileDirectory.endsWith(':')) {
fileDirectory = `${fileDirectory}/`;
}

const fileDirectory = Joomla.MediaManager.GetDirectory(uploadPath);
switch (task) {
case 'apply':
Joomla.UploadFile.exec(name, JSON.stringify(forUpload), uploadPath, url, type);
Joomla.MediaManager.Edit.Reset(true);
break;
case 'save2copy':
forUpload.isCopy = true;
Joomla.UploadFile.exec(name, JSON.stringify(forUpload), uploadPath, url, type);
Joomla.MediaManager.Edit.Reset(true);
break;
case 'save':
forUpload.isClose = true;
Joomla.UploadFile.exec(name, JSON.stringify(forUpload), uploadPath, url, type);
window.location = `${pathName}?option=com_media&path=${fileDirectory}`;
break;
case 'cancel':
if (window.self !== window.top) {
Expand Down Expand Up @@ -187,6 +186,17 @@ Joomla.MediaManager = Joomla.MediaManager || {};
// The upload object
Joomla.UploadFile = {};

Joomla.MediaManager.GetDirectory = (path) => {
let fileDirectory = path.split('/');
fileDirectory.pop();
fileDirectory = fileDirectory.join('/');
// If we are in root add a backslash
if (fileDirectory.endsWith(':')) {
fileDirectory = `${fileDirectory}/`;
}
return fileDirectory;
};

/**
* @TODO Extend Joomla.request and drop this code!!!!
*/
Expand All @@ -204,24 +214,42 @@ Joomla.MediaManager = Joomla.MediaManager || {};
} catch (er) {
resp = null;
}

if (resp) {
if (xhr.status === 200) {
if (resp.success === true) {
Joomla.MediaManager.Edit.removeProgressBar();
if (resp.data.isClose) {
const fileDirectory = Joomla.MediaManager.GetDirectory(resp.data.file.path);
const pathName = window.location.pathname.replace(/&view=file.*/g, '');
window.location = `${pathName}?option=com_media&path=${fileDirectory}`;
}
if (resp.data.isCopy) {
const fileBaseUrl = `${Joomla.getOptions('com_media').editViewUrl}&path=`;
window.location.href = fileBaseUrl + resp.data.file.path;
}
Joomla.renderMessages({
message: [Joomla.JText._('JLIB_APPLICATION_SAVE_SUCCESS')],
});
}

if (resp.status === '1') {
Joomla.renderMessages({ success: [resp.message] }, 'true');
Joomla.MediaManager.Edit.removeProgressBar();
}
} else {
Joomla.renderMessages({
error: [Joomla.JText._('JLIB_APPLICATION_ERROR_SAVE_FAILED_FILE')],
});
}
} else {
Joomla.MediaManager.Edit.removeProgressBar();
}
};

xhr.onerror = () => {
Joomla.renderMessages({
error: [Joomla.JText._('JLIB_APPLICATION_ERROR_SAVE_FAILED_FILE')],
});
Joomla.MediaManager.Edit.removeProgressBar();
};

Expand Down