Remove the DropDownUploadFileBlock HTML generator

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
This commit is contained in:
Maurício Meneghini Fauth 2020-04-01 20:06:09 -03:00
parent ef994c3398
commit 487591badd
5 changed files with 65 additions and 114 deletions

View File

@ -10,6 +10,7 @@ use PhpMyAdmin\Charsets;
use PhpMyAdmin\Charsets\Charset;
use PhpMyAdmin\Core;
use PhpMyAdmin\Encoding;
use PhpMyAdmin\FileListing;
use PhpMyAdmin\Message;
use PhpMyAdmin\Plugins;
use PhpMyAdmin\Plugins\ImportPlugin;
@ -120,6 +121,38 @@ class Import
'can_convert_kanji' => Encoding::canConvertKanji(),
'charsets' => $charsets,
'is_foreign_key_check' => Util::isForeignKeyCheck(),
'user_upload_dir' => Util::userDir($cfg['UploadDir'] ?? ''),
'local_files' => self::getLocalFiles($importList),
]);
}
/**
* @param array $importList List of plugin instances.
*
* @return false|string
*/
private static function getLocalFiles(array $importList)
{
$fileListing = new FileListing();
$extensions = '';
foreach ($importList as $importPlugin) {
if (! empty($extensions)) {
$extensions .= '|';
}
$extensions .= $importPlugin->getProperties()->getExtension();
}
$matcher = '@\.(' . $extensions . ')(\.(' . $fileListing->supportedDecompressions() . '))?$@';
$active = isset($GLOBALS['timeout_passed'], $GLOBALS['local_import_file']) && $GLOBALS['timeout_passed']
? $GLOBALS['local_import_file']
: '';
return $fileListing->getFileSelectOptions(
Util::userDir($GLOBALS['cfg']['UploadDir'] ?? ''),
$matcher,
$active
);
}
}

View File

@ -1,81 +0,0 @@
<?php
/**
* HTML Generator for drop down for file upload
*/
declare(strict_types=1);
namespace PhpMyAdmin\Html\Forms\Fields;
use PhpMyAdmin\FileListing;
use PhpMyAdmin\Message;
use PhpMyAdmin\Plugins\ImportPlugin;
use PhpMyAdmin\Util;
use function htmlspecialchars;
use function sprintf;
/**
* * HTML Generator for drop down for file upload
*/
class DropDownUploadFileBlock implements FieldGenerator
{
/**
* Prepare the form used to select a file to import from the server upload
* directory
*
* @param ImportPlugin[] $import_list array of import plugins
* @param string $uploaddir upload directory
*/
public static function generate($import_list, $uploaddir): string
{
$fileListing = new FileListing();
$block_html = '';
$block_html .= '<label for="radio_local_import_file">'
. sprintf(
__('Select from the web server upload directory <b>%s</b>:'),
htmlspecialchars(Util::userDir($uploaddir))
)
. '</label>';
$extensions = '';
foreach ($import_list as $import_plugin) {
if (! empty($extensions)) {
$extensions .= '|';
}
$extensions .= $import_plugin->getProperties()
->getExtension();
}
$matcher = '@\.(' . $extensions . ')(\.('
. $fileListing->supportedDecompressions() . '))?$@';
$active = isset($GLOBALS['timeout_passed'], $GLOBALS['local_import_file']) && $GLOBALS['timeout_passed']
? $GLOBALS['local_import_file']
: '';
$files = $fileListing->getFileSelectOptions(
Util::userDir($uploaddir),
$matcher,
$active
);
if ($files === false) {
Message::error(
__('The directory you set for upload work cannot be reached.')
)
->display();
} elseif (! empty($files)) {
$block_html .= "\n"
. ' <select style="margin: 5px" size="1" '
. 'name="local_import_file" '
. 'id="select_local_import_file">' . "\n"
. ' <option value="">&nbsp;</option>' . "\n"
. $files
. ' </select>' . "\n";
} elseif (empty($files)) {
$block_html .= '<i>' . __('There are no files to upload!') . '</i>';
}
return $block_html;
}
}

View File

@ -1,14 +0,0 @@
<?php
/**
* Field generator interface
*/
declare(strict_types=1);
namespace PhpMyAdmin\Html\Forms\Fields;
/**
* Field generator interface
*/
interface FieldGenerator
{
}

View File

@ -83,11 +83,6 @@ class UtilExtension extends AbstractExtension
'\PhpMyAdmin\Html\Generator::getImage',
['is_safe' => ['html']]
),
new TwigFunction(
'get_select_upload_file_block',
'\PhpMyAdmin\Html\Forms\Fields\DropDownUploadFileBlock::generate',
['is_safe' => ['html']]
),
new TwigFunction(
'get_start_and_number_of_rows_panel',
'PhpMyAdmin\Html\Generator::getStartAndNumberOfRowsPanel',

View File

@ -79,14 +79,22 @@
<p>{% trans 'You may also drag and drop a file on any page.' %}</p>
</li>
<li>
<input type="radio" name="file_location" id="radio_local_import_file"
{%- if timeout_passed_global is not empty and local_import_file is not empty %}
checked="checked"
{%- endif %}>
{{ get_select_upload_file_block(
import_list,
upload_dir
) }}
<input type="radio" name="file_location" id="radio_local_import_file"
{{- timeout_passed_global is not empty and local_import_file is not empty ? ' checked' }}>
<label for="radio_local_import_file">
{{ 'Select from the web server upload directory [strong]%s[/strong]:'|trans|format(user_upload_dir)|sanitize }}
</label>
{% if local_files is same as(false) %}
{{ 'The directory you set for upload work cannot be reached.'|trans|error }}
{% elseif local_files is not empty %}
<select size="1" name="local_import_file" id="select_local_import_file" aria-label="{% trans 'Select file to import' %}">
<option value=""></option>
{{ local_files|raw }}
</select>
{% else %}
<em>{% trans 'There are no files to upload!' %}</em>
{% endif %}
</li>
</ul>
{% elseif is_upload %}
@ -102,13 +110,23 @@
<input type="hidden" name="MAX_FILE_SIZE" value="{{ max_upload_size }}">
<p>{% trans 'You may also drag and drop a file on any page.' %}</p>
{% elseif not is_upload %}
{{ 'File uploads are not allowed on this server.'|trans|notice }}
{% elseif upload_dir is not empty %}
{{ get_select_upload_file_block(
import_list,
upload_dir
) }}
<label for="select_local_import_file">
{{ 'Select from the web server upload directory [strong]%s[/strong]:'|trans|format(user_upload_dir)|sanitize }}
</label>
{% if local_files is same as(false) %}
{{ 'The directory you set for upload work cannot be reached.'|trans|error }}
{% elseif local_files is not empty %}
<select size="1" name="local_import_file" id="select_local_import_file">
<option value=""></option>
{{ local_files|raw }}
</select>
{% else %}
<em>{% trans 'There are no files to upload!' %}</em>
{% endif %}
{% else %}
{{ 'File uploads are not allowed on this server.'|trans|notice }}
{% endif %}
</div>