Skip to content

Commit

Permalink
media: fixup empty selection for upload and empty multiselection issues
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanbrunker committed May 29, 2020
1 parent e6bc1b7 commit b1a8d38
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
28 changes: 14 additions & 14 deletions include/admin/images.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// No echo output here, before the switch, since that matters renaming alerts!

// unset adminAction type to default, if an image was bulkmoved and the origin page reloaded
if (!is_array($serendipity['POST']) && $serendipity['GET']['adminAction'] == 'multidelete') {
if (!is_array($serendipity['POST']) && $serendipity['GET']['adminAction'] == 'multicheck') {
unset($serendipity['GET']['adminAction']);
}
// Listens on toggle_dir STRICT to list items per directory, or include all sub directory items
Expand All @@ -31,10 +31,10 @@
$messages = array();

// submitted media_upload.tpl: check for empty file field and redirect back to media_upload
if ( $serendipity['GET']['adminAction'] == 'add' ) {
$serendipity['POST']['imageurl'] = serendipity_specialchars($serendipity['POST']['imageurl']);
if ( $serendipity['GET']['adminAction'] == 'add' && !$serendipity['POST']['adminSubAction'] == 'properties') {

if (empty($serendipity['POST']['imageurl'])) {
if ((empty($serendipity['POST']['imageurl']) || $serendipity['POST']['imageurl'] == 'http://' )
&& empty($_FILES['serendipity']['name']['userfile'][1])) {
$messages[] = '<span class="msg_error"><span class="icon-attention-circled" aria-hidden="true"></span>' . NO_FILE_SELECTED . "</span>\n";
$serendipity['GET']['adminAction'] = 'addSelect';
}
Expand Down Expand Up @@ -129,22 +129,21 @@
$data['newLoc'] = $newLoc;
break;

case 'multidelete':
case 'multicheck':
if (!serendipity_checkFormToken() || !serendipity_checkPermission('adminImagesDirectories')) {
return; // blank content page, but default token check parameter is presenting a XSRF message when false
}
if (!is_array($serendipity['POST']['multiDelete']) && isset($_POST['toggle_move'])) {

// empty selection
if (!is_array($serendipity['POST']['multicheck'])) {
echo '<div class="msg_notice"><span class="icon-attention-circled" aria-hidden="true"></span> ' . sprintf(MULTICHECK_NO_ITEM, $_SERVER['HTTP_REFERER']) . '</div>'."\n";
break;
}
if (is_array($serendipity['POST']['multiDelete']) && isset($serendipity['POST']['oldDir']) && (! isset($serendipity['POST']['newDir'])) && isset($_POST['toggle_move'])) {
echo '<div class="msg_notice"><span class="icon-attention-circled" aria-hidden="true"></span> ' . sprintf(MULTICHECK_NO_DIR, $_SERVER['HTTP_REFERER']) . '</div>'."\n";
break;
}

// case bulk multimove (leave the fake oldDir being send as an empty dir)
if (!empty($serendipity['POST']['newDir'])) {
$multiMoveImages = $serendipity['POST']['multiDelete']; // The 'multiDelete' key name should better be renamed to 'multiCheck', but this would need to change 2k11/admin/serendipity_editor.js, images.inc.tpl, media_items.tpl, media_pane.tpl and this file
unset($serendipity['POST']['multiDelete']);
$multiMoveImages = $serendipity['POST']['multicheck'];
unset($serendipity['POST']['multicheck']);

$nDir = serendipity_specialchars(serendipity_dirSlash('end', (string)$serendipity['POST']['newDir'])); // relative to Uploads/

Expand All @@ -170,7 +169,7 @@
$ids = '';
$data['rip_image'] = array();
$data['case_multidelete'] = true;
foreach($serendipity['POST']['multiDelete'] AS $idx => $id) {
foreach($serendipity['POST']['multicheck'] AS $idx => $id) {
$ids .= (int)$id . ',';
$image = serendipity_fetchImageFromDatabase($id);
$data['rip_image'][] = sprintf(DELETE_SURE, $image['id'] . ' - ' . serendipity_specialchars($image['realname']));
Expand Down Expand Up @@ -226,9 +225,10 @@

$new_media = array();

$serendipity['POST']['imageurl'] = serendipity_specialchars($serendipity['POST']['imageurl']);

// First find out whether to fetch a hotlink file or accept an upload
if ($serendipity['POST']['imageurl'] != 'http://') {
if ($serendipity['POST']['imageurl'] != '' && $serendipity['POST']['imageurl'] != 'http://') {
if (!empty($serendipity['POST']['target_filename'][2])) {
// Faked hidden form 2 when submitting with JavaScript
$tfile = $serendipity['POST']['target_filename'][2];
Expand Down
4 changes: 2 additions & 2 deletions templates/2k11/admin/media_items.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
{if $media.manage AND $media.multiperm}

<div class="form_check">
<input id="multidelete_image{$file.id}" class="multidelete" name="serendipity[multiDelete][]" type="checkbox" value="{$file.id}" data-multidelid="media_{$file.id}">
<label for="multidelete_image{$file.id}" class="visuallyhidden">{$CONST.TOGGLE_SELECT}</label>
<input id="multicheck_image{$file.id}" class="multicheck" name="serendipity[multicheck][]" type="checkbox" value="{$file.id}" data-multidelid="media_{$file.id}">
<label for="multicheck_image{$file.id}" class="visuallyhidden">{$CONST.TOGGLE_SELECT}</label>
</div>
{else}
{if NOT $media.manage}
Expand Down
4 changes: 2 additions & 2 deletions templates/2k11/admin/media_pane.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,11 @@
{else}
{if $media.manage AND $media.multiperm}

<form id="formMultiDelete" name="formMultiDelete" action="?" method="post">
<form id="formMultiCheck" name="formMultiCheck" action="?" method="post">
{$media.token}
<input name="serendipity[action]" type="hidden" value="admin">
<input name="serendipity[adminModule]" type="hidden" value="media">
<input name="serendipity[adminAction]" type="hidden" value="multidelete">
<input name="serendipity[adminAction]" type="hidden" value="multicheck">
{/if}
{if NOT $media.manage}
<form id="formMultiInsert" name="formMultiInsert" action="?" method="GET">
Expand Down
18 changes: 14 additions & 4 deletions templates/2k11/admin/serendipity_editor.js.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,16 @@
}
$box.trigger('click');
});
$('#formMultiCheck .multicheck').each(function() {
var $box = $(this);
var boxId = $box.attr('id');
if($box.is(':checked')) {
$(boxId).prop('checked', false);
} else {
$(boxId).prop('checked', true);
}
$box.trigger('click');
});
}

// Highlight/dehighlight elements in lists
Expand Down Expand Up @@ -1386,10 +1396,10 @@ $(function() {
});

// Selection for multidelete
$('.multidelete, .multiinsert').click(function() {
$('.multidelete, .multiinsert, .multicheck').click(function() {
var $el = $(this);
serendipity.highlightComment($el.attr('data-multidelid'), $el.attr('checked'));
var selectionAmount = $('.multidelete:checked, .multiinsert:checked').length;
var selectionAmount = $('.multidelete:checked, .multiinsert:checked, .multiselect:checked').length;
if (selectionAmount > 0) {
$('#media_galleryinsert').fadeIn();
} else {
Expand All @@ -1405,7 +1415,7 @@ $(function() {
// When clicking the 'Insert all' button, check whether only one or multiple
// images are selected
$('#media_galleryinsert .image_insert').on('click', function(e) {
var selectionAmount = $('.multidelete:checked, .multiinsert:checked').length;
var selectionAmount = $('.multidelete:checked, .multiinsert:checked, .multicheck:checked').length;
if (selectionAmount == 0) {
e.preventDefault();
return false;
Expand All @@ -1414,7 +1424,7 @@ $(function() {
// Actually do a single insert
if (selectionAmount == 1) {
e.preventDefault();
var actualTargetElement = $('.multidelete:checked, .multiinsert:checked').closest('.media_file').find('.media_file_preview a').first();
var actualTargetElement = $('.multidelete:checked, .multiinsert:checked, .multicheck:checked').closest('.media_file').find('.media_file_preview a').first();
// This below is a hack. Triggering actualTargetElement.click() does not work. Probably because MFP overrides click events within a modal popup.
// So we need to do something dirty and directly access the location href. Don't tell my mom.
location.href = actualTargetElement.attr('href');
Expand Down

0 comments on commit b1a8d38

Please sign in to comment.