// added list of tables and attributes in AdminRequestSqlController + change export request

This commit is contained in:
lLefevre
2011-11-30 13:24:43 +00:00
parent 885701b27a
commit db3feb8df4
3 changed files with 98 additions and 21 deletions
+9 -5
View File
@@ -1,6 +1,6 @@
<?php <?php
/* /*
* 2007-2011 PrestaShop * 2007-2011 PrestaShop
* *
* NOTICE OF LICENSE * NOTICE OF LICENSE
* *
@@ -138,14 +138,13 @@ class RequestSqlCore extends ObjectModel
if (!$this->checkedLimit($tab['LIMIT'])) if (!$this->checkedLimit($tab['LIMIT']))
return false; return false;
} }
if (empty($this->_errors)) if (empty($this->_errors))
if (!Db::getInstance()->executeS($sql)) if (!Db::getInstance()->executeS($sql))
return false; return false;
return true; return true;
} }
public function showTables() public function getTables()
{ {
$results = Db::getInstance()->executeS('SHOW TABLES'); $results = Db::getInstance()->executeS('SHOW TABLES');
foreach ($results as $result) foreach ($results as $result)
@@ -156,6 +155,11 @@ class RequestSqlCore extends ObjectModel
return $tables; return $tables;
} }
public function getAttributesByTable($table)
{
return Db::getInstance()->executeS(sprintf('DESCRIBE `%s`', $table));
}
public function cutJoin($attrs, $from) public function cutJoin($attrs, $from)
{ {
$attrs = explode('=', str_replace(' ', '', $attrs)); $attrs = explode('=', str_replace(' ', '', $attrs));
@@ -223,7 +227,7 @@ class RequestSqlCore extends ObjectModel
{ {
if (is_array($table) && (count($table) == 1)) if (is_array($table) && (count($table) == 1))
$table = $table[0]; $table = $table[0];
$attributs = Db::getInstance()->executeS(sprintf('DESCRIBE `%s`', $table)); $attributs = $this->getAttributesByTable($table);
foreach ($attributs as $attribut) foreach ($attributs as $attribut)
if ($attribut['Field'] == trim($attr)) if ($attribut['Field'] == trim($attr))
return true; return true;
@@ -258,7 +262,7 @@ class RequestSqlCore extends ObjectModel
for ($i = 0; $i < $nb; $i++) for ($i = 0; $i < $nb; $i++)
{ {
$table = $from[$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']; $this->error_sql['checkedFrom']['table'] = $table['table'];
return false; return false;
+2 -2
View File
@@ -401,10 +401,10 @@ abstract class DbCore
$sql = (string)$sql; $sql = (string)$sql;
// This methode must be used only with queries which display results // 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_) 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); return $this->execute($sql, $use_cache);
} }
+87 -14
View File
@@ -60,18 +60,11 @@ class AdminRequestSqlControllerCore extends AdminController
</ul> </ul>
'); ');
$this->addRowAction('export');
$this->addRowAction('view'); $this->addRowAction('view');
$this->addRowAction('edit'); $this->addRowAction('edit');
$this->addRowAction('delete'); $this->addRowAction('delete');
$this->bulk_actions = array( $this->bulk_actions = array('delete' => array('text' => $this->l('Delete selected'),'confirm' => $this->l('Delete selected items?')));
'delete' => array(
'text' => $this->l('Delete selected'),
'confirm' => $this->l('Delete selected items?')
),
'export' => array(
'text' => $this->l('Export selected')
)
);
return parent::renderList(); 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(); 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() public function renderView()
{ {
if (!($obj = $this->loadObject(true))) 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) $tpl = $this->context->smarty->createTemplate('request_sql/list_action_export.tpl');
$this->_errors[] = Tools::DisplayError('You must select a query to export the results.');
$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'; $file = 'request_sql_'.$id.'.csv';
if ($csv = fopen(_PS_ADMIN_DIR_.'/export/'.$file, 'w')) if ($csv = fopen(_PS_ADMIN_DIR_.'/export/'.$file, 'w'))
@@ -183,7 +256,7 @@ class AdminRequestSqlControllerCore extends AdminController
{ {
header('Content-type: text/csv'); header('Content-type: text/csv');
header('Cache-Control: no-store, no-cache'); header('Cache-Control: no-store, no-cache');
header('Content-Disposition: attachment; filename="$file"'); header('Content-Disposition: attachment; filename="'.$file.'"');
header('Content-Length: '.$filesize); header('Content-Length: '.$filesize);
readfile(_PS_ADMIN_DIR_.'/export/'.$file); readfile(_PS_ADMIN_DIR_.'/export/'.$file);
die(); die();