// added list of tables and attributes in AdminRequestSqlController + change export request
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
/*
|
||||
* 2007-2011 PrestaShop
|
||||
* 2007-2011 PrestaShop
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
@@ -138,14 +138,13 @@ class RequestSqlCore extends ObjectModel
|
||||
if (!$this->checkedLimit($tab['LIMIT']))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (empty($this->_errors))
|
||||
if (!Db::getInstance()->executeS($sql))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public function showTables()
|
||||
public function getTables()
|
||||
{
|
||||
$results = Db::getInstance()->executeS('SHOW TABLES');
|
||||
foreach ($results as $result)
|
||||
@@ -156,6 +155,11 @@ class RequestSqlCore extends ObjectModel
|
||||
return $tables;
|
||||
}
|
||||
|
||||
public function getAttributesByTable($table)
|
||||
{
|
||||
return Db::getInstance()->executeS(sprintf('DESCRIBE `%s`', $table));
|
||||
}
|
||||
|
||||
public function cutJoin($attrs, $from)
|
||||
{
|
||||
$attrs = explode('=', str_replace(' ', '', $attrs));
|
||||
@@ -223,7 +227,7 @@ class RequestSqlCore extends ObjectModel
|
||||
{
|
||||
if (is_array($table) && (count($table) == 1))
|
||||
$table = $table[0];
|
||||
$attributs = Db::getInstance()->executeS(sprintf('DESCRIBE `%s`', $table));
|
||||
$attributs = $this->getAttributesByTable($table);
|
||||
foreach ($attributs as $attribut)
|
||||
if ($attribut['Field'] == trim($attr))
|
||||
return true;
|
||||
@@ -258,7 +262,7 @@ class RequestSqlCore extends ObjectModel
|
||||
for ($i = 0; $i < $nb; $i++)
|
||||
{
|
||||
$table = $from[$i];
|
||||
if (!in_array(str_replace('`', '', $table['table']), $this->showTables()))
|
||||
if (!in_array(str_replace('`', '', $table['table']), $this->getTables()))
|
||||
{
|
||||
$this->error_sql['checkedFrom']['table'] = $table['table'];
|
||||
return false;
|
||||
|
||||
+2
-2
@@ -401,10 +401,10 @@ abstract class DbCore
|
||||
$sql = (string)$sql;
|
||||
|
||||
// This methode must be used only with queries which display results
|
||||
if (!preg_match('#^\s*(select|show|explain)\s#i', $sql))
|
||||
if (!preg_match('#^\s*(select|show|explain|describe)\s#i', $sql))
|
||||
{
|
||||
if (defined('_PS_MODE_DEV_') && _PS_MODE_DEV_)
|
||||
throw new PrestashopDatabaseException('Db->executeS() must be used only with select, show or explain queries');
|
||||
throw new PrestashopDatabaseException('Db->executeS() must be used only with select, show, explain or describe queries');
|
||||
return $this->execute($sql, $use_cache);
|
||||
}
|
||||
|
||||
|
||||
@@ -60,18 +60,11 @@ class AdminRequestSqlControllerCore extends AdminController
|
||||
</ul>
|
||||
');
|
||||
|
||||
$this->addRowAction('export');
|
||||
$this->addRowAction('view');
|
||||
$this->addRowAction('edit');
|
||||
$this->addRowAction('delete');
|
||||
$this->bulk_actions = array(
|
||||
'delete' => array(
|
||||
'text' => $this->l('Delete selected'),
|
||||
'confirm' => $this->l('Delete selected items?')
|
||||
),
|
||||
'export' => array(
|
||||
'text' => $this->l('Export selected')
|
||||
)
|
||||
);
|
||||
$this->bulk_actions = array('delete' => array('text' => $this->l('Delete selected'),'confirm' => $this->l('Delete selected items?')));
|
||||
|
||||
return parent::renderList();
|
||||
}
|
||||
@@ -105,9 +98,33 @@ class AdminRequestSqlControllerCore extends AdminController
|
||||
)
|
||||
);
|
||||
|
||||
$request = new RequestSql();
|
||||
$this->tpl_form_vars = array('tables' => $request->getTables());
|
||||
|
||||
return parent::renderForm();
|
||||
}
|
||||
|
||||
/**
|
||||
* method call when ajax request is made with the details row action
|
||||
* @see AdminController::postProcess()
|
||||
*/
|
||||
public function ajaxProcess()
|
||||
{
|
||||
if ($table = Tools::GetValue('table'))
|
||||
{
|
||||
$request_sql = new RequestSql();
|
||||
$attributes = $request_sql->getAttributesByTable($table);
|
||||
foreach ($attributes as $key => $attribute)
|
||||
{
|
||||
unset($attributes[$key]['Null']);
|
||||
unset($attributes[$key]['Key']);
|
||||
unset($attributes[$key]['Default']);
|
||||
unset($attributes[$key]['Extra']);
|
||||
}
|
||||
die(Tools::jsonEncode($attributes));
|
||||
}
|
||||
}
|
||||
|
||||
public function renderView()
|
||||
{
|
||||
if (!($obj = $this->loadObject(true)))
|
||||
@@ -149,12 +166,68 @@ class AdminRequestSqlControllerCore extends AdminController
|
||||
}
|
||||
}
|
||||
|
||||
public function bulkexport($boxes)
|
||||
/**
|
||||
* Display export action link
|
||||
*/
|
||||
public function displayExportLink($token = null, $id)
|
||||
{
|
||||
if (!$boxes || count($boxes) > 1)
|
||||
$this->_errors[] = Tools::DisplayError('You must select a query to export the results.');
|
||||
$tpl = $this->context->smarty->createTemplate('request_sql/list_action_export.tpl');
|
||||
|
||||
$id = (int)$boxes[0];
|
||||
$tpl->assign(array(
|
||||
'href' => self::$currentIndex.'&token='.$this->token.'&'.$this->identifier.'='.$id.'&export'.$this->table.'=1',
|
||||
'action' => $this->l('Export')
|
||||
));
|
||||
|
||||
return $tpl->fetch();
|
||||
}
|
||||
|
||||
public function initProcess()
|
||||
{
|
||||
parent::initProcess();
|
||||
if (Tools::getValue('export'.$this->table))
|
||||
{
|
||||
$this->display = 'export';
|
||||
$this->action = 'export';
|
||||
}
|
||||
}
|
||||
|
||||
public function initContent()
|
||||
{
|
||||
// toolbar (save, cancel, new, ..)
|
||||
$this->initToolbar();
|
||||
if ($this->display == 'edit' || $this->display == 'add')
|
||||
{
|
||||
if (!$this->loadObject(true))
|
||||
return;
|
||||
|
||||
$this->content .= $this->renderForm();
|
||||
}
|
||||
else if ($this->display == 'view')
|
||||
{
|
||||
// Some controllers use the view action without an object
|
||||
if ($this->className)
|
||||
$this->loadObject(true);
|
||||
$this->content .= $this->renderView();
|
||||
}
|
||||
else if ($this->display == 'export')
|
||||
{
|
||||
$this->generateExport();
|
||||
}
|
||||
else if (!$this->ajax)
|
||||
{
|
||||
$this->content .= $this->renderList();
|
||||
$this->content .= $this->renderOptions();
|
||||
}
|
||||
|
||||
$this->context->smarty->assign(array(
|
||||
'content' => $this->content,
|
||||
'url_post' => self::$currentIndex.'&token='.$this->token,
|
||||
));
|
||||
}
|
||||
|
||||
public function generateExport()
|
||||
{
|
||||
$id = Tools::getValue($this->identifier);
|
||||
|
||||
$file = 'request_sql_'.$id.'.csv';
|
||||
if ($csv = fopen(_PS_ADMIN_DIR_.'/export/'.$file, 'w'))
|
||||
@@ -183,7 +256,7 @@ class AdminRequestSqlControllerCore extends AdminController
|
||||
{
|
||||
header('Content-type: text/csv');
|
||||
header('Cache-Control: no-store, no-cache');
|
||||
header('Content-Disposition: attachment; filename="$file"');
|
||||
header('Content-Disposition: attachment; filename="'.$file.'"');
|
||||
header('Content-Length: '.$filesize);
|
||||
readfile(_PS_ADMIN_DIR_.'/export/'.$file);
|
||||
die();
|
||||
|
||||
Reference in New Issue
Block a user