Skip to content

Commit

Permalink
Extract HTML from SqlQueryForm::getHtmlForInsert
Browse files Browse the repository at this point in the history
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Sep 3, 2019
1 parent 1a14b3e commit 732236c
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 174 deletions.
195 changes: 25 additions & 170 deletions libraries/classes/SqlQueryForm.php
Expand Up @@ -20,6 +20,19 @@
*/
class SqlQueryForm
{
/**
* @var Template
*/
private $template;

/**
* @param Template $template Template object
*/
public function __construct(Template $template)
{
$this->template = $template;
}

/**
* return HTML for the sql query boxes
*
Expand Down Expand Up @@ -203,179 +216,21 @@ public function getHtmlForInsert(
$query = '',
$delimiter = ';'
) {
// enable auto select text in textarea
if ($GLOBALS['cfg']['TextareaAutoSelect']) {
$auto_sel = ' onclick="Functions.selectContent(this, sqlBoxLocked, true);"';
} else {
$auto_sel = '';
}

$locking = '';
$height = $GLOBALS['cfg']['TextareaRows'] * 2;

list($legend, $query, $columns_list) = $this->init($query);

if (! empty($columns_list)) {
$sqlquerycontainer_id = 'sqlquerycontainer';
} else {
$sqlquerycontainer_id = 'sqlquerycontainerfull';
}

$html = '<a id="querybox"></a>'
. '<div id="queryboxcontainer">'
. '<fieldset id="queryboxf">';
$html .= '<legend>' . $legend . '</legend>';
$html .= '<div id="queryfieldscontainer">';
$html .= '<div id="' . $sqlquerycontainer_id . '">'
. '<textarea tabindex="100" name="sql_query" id="sqlquery"'
. ' cols="' . $GLOBALS['cfg']['TextareaCols'] . '"'
. ' rows="' . $height . '"'
. $auto_sel . $locking . '>'
. htmlspecialchars($query)
. '</textarea>';
$html .= '<div id="querymessage"></div>';
// Add buttons to generate query easily for
// select all, single select, insert, update and delete
if (! empty($columns_list)) {
$html .= '<input type="button" value="SELECT *" id="selectall"'
. ' class="btn btn-secondary button sqlbutton">';
$html .= '<input type="button" value="SELECT" id="select"'
. ' class="btn btn-secondary button sqlbutton">';
$html .= '<input type="button" value="INSERT" id="insert"'
. ' class="btn btn-secondary button sqlbutton">';
$html .= '<input type="button" value="UPDATE" id="update"'
. ' class="btn btn-secondary button sqlbutton">';
$html .= '<input type="button" value="DELETE" id="delete"'
. ' class="btn btn-secondary button sqlbutton">';
}
$html .= '<input type="button" value="' . __('Clear') . '" id="clear"'
. ' class="btn btn-secondary button sqlbutton">';
if ($GLOBALS['cfg']['CodemirrorEnable']) {
$html .= '<input type="button" value="' . __('Format') . '" id="format"'
. ' class="btn btn-secondary button sqlbutton">';
}
$html .= '<input type="button" value="' . __('Get auto-saved query')
. '" id="saved" class="btn btn-secondary button sqlbutton">';

// parameter binding
$html .= '<div>';
$html .= '<input type="checkbox" name="parameterized" id="parameterized">';
$html .= '<label for="parameterized">' . __('Bind parameters') . '</label>';
$html .= Util::showDocu('faq', 'faq6-40');
$html .= '<div id="parametersDiv"></div>';
$html .= '</div>';

$html .= '</div>' . "\n";

if (! empty($columns_list)) {
$html .= '<div id="tablefieldscontainer">'
. '<label>' . __('Columns') . '</label>'
. '<select id="tablefields" name="dummy" '
. 'size="' . ($GLOBALS['cfg']['TextareaRows'] - 2) . '" '
. 'multiple="multiple" ondblclick="Functions.insertValueQuery()">';
foreach ($columns_list as $field) {
$html .= '<option value="'
. Util::backquote(htmlspecialchars($field['Field']))
. '"';
if (isset($field['Field'], $field['Comment']) && strlen($field['Field']) > 0) {
$html .= ' title="' . htmlspecialchars($field['Comment']) . '"';
}
$html .= '>' . htmlspecialchars($field['Field']) . '</option>' . "\n";
}
$html .= '</select>'
. '<div id="tablefieldinsertbuttoncontainer">';
if (Util::showIcons('ActionLinksMode')) {
$html .= '<input type="button" class="btn btn-secondary button" name="insert"'
. ' value="&lt;&lt;" onclick="Functions.insertValueQuery()"'
. ' title="' . __('Insert') . '">';
} else {
$html .= '<input type="button" class="btn btn-secondary button" name="insert"'
. ' value="' . __('Insert') . '"'
. ' onclick="Functions.insertValueQuery()">';
}
$html .= '</div>' . "\n"
. '</div>' . "\n";
}

$html .= '<div class="clearfloat"></div>' . "\n";
$html .= '</div>' . "\n";

$cfgBookmark = Bookmark::getParams($GLOBALS['cfg']['Server']['user']);
if ($cfgBookmark) {
$html .= '<div id="bookmarkoptions">';
$html .= '<div class="formelement">';
$html .= '<label for="bkm_label">'
. __('Bookmark this SQL query:') . '</label>';
$html .= '<input type="text" name="bkm_label" id="bkm_label"'
. ' tabindex="110" value="">';
$html .= '</div>';
$html .= '<div class="formelement">';
$html .= '<input type="checkbox" name="bkm_all_users" tabindex="111"'
. ' id="id_bkm_all_users" value="true">';
$html .= '<label for="id_bkm_all_users">'
. __('Let every user access this bookmark') . '</label>';
$html .= '</div>';
$html .= '<div class="formelement">';
$html .= '<input type="checkbox" name="bkm_replace" tabindex="112"'
. ' id="id_bkm_replace" value="true">';
$html .= '<label for="id_bkm_replace">'
. __('Replace existing bookmark of same name') . '</label>';
$html .= '</div>';
$html .= '</div>';
}

$html .= '<div class="clearfloat"></div>' . "\n";
$html .= '</fieldset>' . "\n"
. '</div>' . "\n";

$html .= '<fieldset id="queryboxfooter" class="tblFooters">' . "\n";
$html .= '<div class="formelement">' . "\n";
$html .= '</div>' . "\n";

$html .= '<div class="formelement">';
$html .= '<label for="id_sql_delimiter">[ ' . __('Delimiter')
. '</label>' . "\n";
$html .= '<input type="text" name="sql_delimiter" tabindex="131" size="3" '
. 'value="' . $delimiter . '" '
. 'id="id_sql_delimiter"> ]';
$html .= '</div>';

$html .= '<div class="formelement">';
$html .= '<input type="checkbox" name="show_query" value="1" '
. 'id="checkbox_show_query" tabindex="132">'
. '<label for="checkbox_show_query">' . __('Show this query here again')
. '</label>';
$html .= '</div>';

$html .= '<div class="formelement">';
$html .= '<input type="checkbox" name="retain_query_box" value="1" '
. 'id="retain_query_box" tabindex="133" '
. ($GLOBALS['cfg']['RetainQueryBox'] === false
? '' : ' checked="checked"')
. '>'
. '<label for="retain_query_box">' . __('Retain query box')
. '</label>';
$html .= '</div>';

$html .= '<div class="formelement">';
$html .= '<input type="checkbox" name="rollback_query" value="1" '
. 'id="rollback_query" tabindex="134">'
. '<label for="rollback_query">' . __('Rollback when finished')
. '</label>';
$html .= '</div>';

// Disable/Enable foreign key checks
$html .= '<div class="formelement">';
$html .= Util::getFKCheckbox();
$html .= '</div>';

$html .= '<input class="btn btn-primary" type="submit" id="button_submit_query" name="SQL"';

$html .= ' tabindex="200" value="' . __('Go') . '">' . "\n";
$html .= '<div class="clearfloat"></div>' . "\n";
$html .= '</fieldset>' . "\n";

return $html;
return $this->template->render('sql/query/insert', [
'legend' => $legend,
'textarea_cols' => $GLOBALS['cfg']['TextareaCols'],
'textarea_rows' => $GLOBALS['cfg']['TextareaRows'],
'textarea_auto_select' => $GLOBALS['cfg']['TextareaAutoSelect'],
'query' => $query,
'columns_list' => $columns_list,
'codemirror_enable' => $GLOBALS['cfg']['CodemirrorEnable'],
'has_bookmark' => $cfgBookmark,
'delimiter' => $delimiter,
'retain_query_box' => $GLOBALS['cfg']['RetainQueryBox'] !== false,
]);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions services.yml
Expand Up @@ -116,6 +116,8 @@ services:

sql_query_form:
class: 'PhpMyAdmin\SqlQueryForm'
arguments:
template: '@template'

status_data:
class: 'PhpMyAdmin\Server\Status\Data'
Expand Down
115 changes: 115 additions & 0 deletions templates/sql/query/insert.twig
@@ -0,0 +1,115 @@
<a id="querybox"></a>
<div id="queryboxcontainer">
<fieldset id="queryboxf">
<legend>{{ legend|raw }}</legend>
<div id="queryfieldscontainer">
<div id="{{ columns_list is not empty ? 'sqlquerycontainer' : 'sqlquerycontainerfull' }}">
<textarea tabindex="100" name="sql_query" id="sqlquery" cols="{{ textarea_cols }}" rows="{{ textarea_rows * 2 }}"
{{- textarea_auto_select ? ' onclick="Functions.selectContent(this, sqlBoxLocked, true);"' }}>
{{- query -}}
</textarea>
<div id="querymessage"></div>

{% if columns_list is not empty %}
<input type="button" value="SELECT *" id="selectall" class="btn btn-secondary button sqlbutton">
<input type="button" value="SELECT" id="select" class="btn btn-secondary button sqlbutton">
<input type="button" value="INSERT" id="insert" class="btn btn-secondary button sqlbutton">
<input type="button" value="UPDATE" id="update" class="btn btn-secondary button sqlbutton">
<input type="button" value="DELETE" id="delete" class="btn btn-secondary button sqlbutton">
{% endif %}
<input type="button" value="{% trans 'Clear' %}" id="clear" class="btn btn-secondary button sqlbutton">
{% if codemirror_enable %}
<input type="button" value="{% trans 'Format' %}" id="format" class="btn btn-secondary button sqlbutton">
{% endif %}
<input type="button" value="{% trans 'Get auto-saved query' %}" id="saved" class="btn btn-secondary button sqlbutton">

<div>
<input type="checkbox" name="parameterized" id="parameterized">
<label for="parameterized">{% trans 'Bind parameters' %}</label>
{{ show_docu('faq', 'faq6-40') }}
<div id="parametersDiv"></div>
</div>
</div>

{% if columns_list is not empty %}
<div id="tablefieldscontainer">
<label>{% trans 'Columns' %}</label>
<select id="tablefields" name="dummy" size="{{ textarea_rows - 2 }}" ondblclick="Functions.insertValueQuery()" multiple>
{% for field in columns_list %}
<option value="{{ backquote(field['Field']) }}"
{{- field['Field'] is not null and field['Comment'] is not null and field['Field']|length > 0 ? ' title="' ~ field['Comment'] ~ '"' }}>
{{ field['Field'] }}
</option>
{% endfor %}
</select>

<div id="tablefieldinsertbuttoncontainer">
{% if show_icons('ActionLinksMode') %}
<input type="button" class="btn btn-secondary button" name="insert" value="{{ '<<' }}" onclick="Functions.insertValueQuery()" title="{% trans 'Insert' %}">
{% else %}
<input type="button" class="btn btn-secondary button" name="insert" value="{% trans 'Insert' %}" onclick="Functions.insertValueQuery()">
{% endif %}
</div>
</div>
{% endif %}

<div class="clearfloat"></div>
</div>

{% if has_bookmark %}
<div id="bookmarkoptions">
<div class="formelement">
<label for="bkm_label">{% trans 'Bookmark this SQL query:' %}</label>
<input type="text" name="bkm_label" id="bkm_label" tabindex="110" value="">
</div>

<div class="formelement">
<input type="checkbox" name="bkm_all_users" tabindex="111" id="id_bkm_all_users" value="true">
<label for="id_bkm_all_users">{% trans 'Let every user access this bookmark' %}</label>
</div>

<div class="formelement">
<input type="checkbox" name="bkm_replace" tabindex="112" id="id_bkm_replace" value="true">
<label for="id_bkm_replace">{% trans 'Replace existing bookmark of same name' %}</label>
</div>
</div>
{% endif %}

<div class="clearfloat"></div>
</fieldset>
</div>

<fieldset id="queryboxfooter" class="tblFooters">
<div class="formelement"></div>

<div class="formelement">
[
<label for="id_sql_delimiter">{% trans 'Delimiter' %}</label>
<input type="text" name="sql_delimiter" tabindex="131" size="3" value="{{ delimiter }}" id="id_sql_delimiter">
]
</div>

<div class="formelement">
<input type="checkbox" name="show_query" value="1" id="checkbox_show_query" tabindex="132">
<label for="checkbox_show_query">{% trans 'Show this query here again' %}</label>
</div>

<div class="formelement">
<input type="checkbox" name="retain_query_box" value="1" id="retain_query_box" tabindex="133"
{{- retain_query_box ? ' checked' }}>
<label for="retain_query_box">{% trans 'Retain query box' %}</label>
</div>

<div class="formelement">
<input type="checkbox" name="rollback_query" value="1" id="rollback_query" tabindex="134">
<label for="rollback_query">{% trans 'Rollback when finished' %}</label>
</div>

<div class="formelement">
{{ get_fk_checkbox() }}
</div>

<input class="btn btn-primary" type="submit" id="button_submit_query" name="SQL" tabindex="200" value="{% trans 'Go' %}">

<div class="clearfloat"></div>
</fieldset>
4 changes: 2 additions & 2 deletions test/classes/SqlQueryFormTest.php
Expand Up @@ -11,7 +11,7 @@
use PhpMyAdmin\Core;
use PhpMyAdmin\Encoding;
use PhpMyAdmin\SqlQueryForm;
use PhpMyAdmin\Theme;
use PhpMyAdmin\Template;
use PhpMyAdmin\Url;
use PhpMyAdmin\Util;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -40,7 +40,7 @@ class SqlQueryFormTest extends TestCase
*/
protected function setUp(): void
{
$this->sqlQueryForm = new SqlQueryForm();
$this->sqlQueryForm = new SqlQueryForm(new Template());

//$GLOBALS
$GLOBALS['max_upload_size'] = 100;
Expand Down
6 changes: 4 additions & 2 deletions test/classes/TrackingTest.php
Expand Up @@ -73,7 +73,8 @@ protected function setUp(): void

$GLOBALS['dbi'] = $dbi;

$this->tracking = new Tracking(new SqlQueryForm(), new Template(), new Relation($dbi));
$template = new Template();
$this->tracking = new Tracking(new SqlQueryForm($template), $template, new Relation($dbi));
}

/**
Expand Down Expand Up @@ -208,7 +209,8 @@ public function testGetHtmlForMain()

/* Here, we need to overwrite the object written in the setUp function because $dbi object is not the one mocked
at the beginning. */
$this->tracking = new Tracking(new SqlQueryForm(), new Template(), new Relation($dbi));
$template = new Template();
$this->tracking = new Tracking(new SqlQueryForm($template), $template, new Relation($dbi));

$html = $this->tracking->getHtmlForMainPage(
$url_params,
Expand Down

0 comments on commit 732236c

Please sign in to comment.