Skip to content

Commit

Permalink
Added Save2Copy in Media Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
vlaucht committed Aug 7, 2020
1 parent bc0da36 commit 173412b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
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 @@ -272,7 +275,16 @@ public function putFiles()
{
$this->checkContent();

$this->getModel()->updateFile($adapter, $name, str_replace($name, '', $path), $mediaContent);
if ($content->get('isCopy'))
{
$name = $this->generateNewName($name, $path);
$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 @@ -294,6 +306,29 @@ public function putFiles()
return $this->getModel()->getFile($adapter, $path);
}

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

while (is_file(Path::check(JPATH_ROOT . '/images/' . dirname($path) . $name)))
{
$base = File::stripExt($name);
$name = StringHelper::increment($base, 'dash') . '.' . $extension;
}

return $name;
}

/**
* Send the given data as JSON response in the following format:
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ protected function addToolbar()
ToolbarHelper::title(Text::_('COM_MEDIA_EDIT'), 'images mediamanager');

ToolbarHelper::apply('apply');
ToolbarHelper::save('save');
$toolbarButtons = [['save2copy', 'save2copy'], ['save', 'save']];
ToolbarHelper::saveGroup(
$toolbarButtons,
'btn-success'
);
ToolbarHelper::custom('reset', 'refresh', '', 'COM_MEDIA_RESET', false);

ToolbarHelper::cancel('cancel');
Expand Down
6 changes: 6 additions & 0 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,7 @@ Joomla.MediaManager = Joomla.MediaManager || {};
const forUpload = {
name,
content: Joomla.MediaManager.Edit.current.contents.replace(`data:image/${format};base64,`, ''),
isCopy: false
};


Expand Down Expand Up @@ -159,6 +160,11 @@ Joomla.MediaManager = Joomla.MediaManager || {};
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);
window.location = `${pathName}?option=com_media&path=${fileDirectory}`;
break;
case 'save':
Joomla.UploadFile.exec(name, JSON.stringify(forUpload), uploadPath, url, type);
window.location = `${pathName}?option=com_media&path=${fileDirectory}`;
Expand Down

0 comments on commit 173412b

Please sign in to comment.