* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision: 6844 $
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
class AdminRequestSql extends AdminTab
{
public function __construct()
{
$this->table = 'request_sql';
$this->className = 'RequestSql';
$this->edit = true;
$this->delete = true;
$this->view = true;
$this->export = true;
$this->fieldsDisplay = array(
'id_request_sql' => array('title' => $this->l('ID'), 'width' => 25),
'name' => array('title' => $this->l('Name'), 'width' => 300),
'sql' => array('title' => $this->l('Request'), 'width' => 500)
);
parent::__construct();
}
public function displayList()
{
return parent::displayList();
}
public function displayTop()
{
echo '
'.$this->l('How to create a new sql query?').'
'.$this->l('Click "Add new".').'
'.$this->l('Fill in the fields and click "Save".').'
'.$this->l('You can then view the query results by clicking on the tab: ').'
'.$this->l('You can then export the query results as a file. Csv file by clicking on the tab: ').'
'.$this->l('Warning: when saving the query, only the request type "SELECT" are allowed.').
'
';
if (isset($_GET['maxsize']))
echo '
'.
$this->l('The file is too large and can not be downloaded. Please use the clause "LIMIT" in this query.').'
';
}
public function displayForm($isMainTab = true)
{
parent::displayForm();
if (!($obj = $this->loadObject(true)))
return;
echo '
'.$this->l('Warning: when saving the query, only the request type "SELECT" are allowed.').
'
';
}
public function postProcess()
{
if (!($obj = $this->loadObject(true)))
return;
$result = Db::getInstance()->executeS('
SELECT `id_request_sql`
FROM `'._DB_PREFIX_.'request_sql`
');
if (count($result) === 1)
foreach ($result as $row)
$this->_listSkipDelete = array($row['id_request_sql']);
return parent::postProcess();
}
public function _childValidation()
{
if (Tools::getValue('submitAdd'.$this->table) && $sql = Tools::getValue('sql'))
{
$request_sql = new RequestSql();
$parser = $request_sql->parsingSql($sql);
$validate = $request_sql->validateParser($parser, false, $sql);
if (!$validate || !empty($request_sql->error_sql))
$this->displayError($request_sql->error_sql);
}
}
public function displayError($e)
{
foreach (array_keys($e) as $key)
{
switch ($key)
{
case 'checkedFrom':
if (isset($e[$key]['table']))
$this->_errors[] = Tools::DisplayError($this->l('The Table ').' "'.$e[$key]['table'].'" '.$this->l(' doesn\'t exist.'));
else if (isset($e[$key]['attribut']))
$this->_errors[] = Tools::DisplayError($this->l('The attribute ').' "'.
$e[$key]['attribut'][0].'" '.$this->l(' does not exist in the table: ').$e[$key]['attribut'][1].'.');
else
$this->_errors[] = Tools::DisplayError($this->l('Error'));
break;
case 'checkedSelect':
if (isset($e[$key]['table']))
$this->_errors[] = Tools::DisplayError($this->l('The Table ').' "'.$e[$key]['table'].'" '.$this->l(' doesn\'t exist.'));
else if (isset($e[$key]['attribut']))
$this->_errors[] = Tools::DisplayError($this->l('The attribute ').' "'.
$e[$key]['attribut'][0].'" '.$this->l(' does not exist in the table: ').$e[$key]['attribut'][1].'.');
else if (isset($e[$key]['*']))
$this->_errors[] = Tools::DisplayError($this->l('The operand "*" can be used in a nested query.'));
else
$this->_errors[] = Tools::DisplayError($this->l('Error'));
break;
case 'checkedWhere':
if (isset($e[$key]['operator']))
$this->_errors[] = Tools::DisplayError($this->l('The operator ').' "'.$e[$key]['operator'].'" '.$this->l(' used is incorrect.'));
else if (isset($e[$key]['attribut']))
$this->_errors[] = Tools::DisplayError($this->l('The attribute ').' "'.
$e[$key]['attribut'][0].'" '.$this->l(' does not exist in the table: ').$e[$key]['attribut'][1].'.');
else
$this->_errors[] = Tools::DisplayError($this->l('Error'));
break;
case 'checkedHaving':
if (isset($e[$key]['operator']))
$this->_errors[] = Tools::DisplayError($this->l('The operator ').' "'.$e[$key]['operator'].'" '.$this->l(' used is incorrect.'));
else if (isset($e[$key]['attribut']))
$this->_errors[] = Tools::DisplayError($this->l('The attribute ').' "'.
$e[$key]['attribut'][0].'" '.$this->l(' does not exist in the table: ').$e[$key]['attribut'][1].'.');
else
$this->_errors[] = Tools::DisplayError($this->l('Error'));
break;
case 'checkedOrder':
if (isset($e[$key]['attribut']))
$this->_errors[] = Tools::DisplayError($this->l('The attribute ').' "'.
$e[$key]['attribut'][0].'" '.$this->l(' does not exist in the table: ').$e[$key]['attribut'][1].'.');
else
$this->_errors[] = Tools::DisplayError($this->l('Error'));
break;
case 'checkedGroupBy':
if (isset($e[$key]['attribut']))
$this->_errors[] = Tools::DisplayError($this->l('The attribute ').' "'.
$e[$key]['attribut'][0].'" '.$this->l(' does not exist in the table: ').$e[$key]['attribut'][1].'.');
else
$this->_errors[] = Tools::DisplayError($this->l('Error'));
break;
case 'checkedLimit':
$this->_errors[] = Tools::DisplayError($this->l('The LIMIT clause must contain numeric arguments.'));
break;
case 'returnNameTable':
if (isset($e[$key]['reference']))
$this->_errors[] = Tools::DisplayError($this->l('The reference ').'"'.
$e[$key]['reference'][0].'"'.$this->l(' doesn\'t exist in : ').$e[$key]['reference'][1]);
else
$this->_errors[] = Tools::DisplayError($this->l('When multiple tables are used, each attribute must be referenced to a table.'));
break;
case 'testedRequired':
$this->_errors[] = Tools::DisplayError($e[$key].' '.$this->l(' doesn\'t exist.'));
break;
case 'testedUnauthorized':
$this->_errors[] = Tools::DisplayError($e[$key].' '.$this->l(' is a unauthorized keyword.'));
break;
default:
break;
}
}
}
public function viewRequest_sql()
{
if (!($obj = $this->loadObject(true)))
return;
echo '
'.$obj->name.'
';
if ($results = Db::getInstance()->executeS($obj->sql))
{
$tab_key = array();
foreach (array_keys($results[0]) as $key)
$tab_key[] = $key;
echo '
';
foreach ($tab_key as $key_name)
echo '
'.$key_name.'
';
echo '
';
$request_sql = new RequestSql();
$attributes = $request_sql->attributes;
foreach ($results as $result)
{
echo '
';
foreach ($tab_key as $name)
{
if (!isset($attributes[$name]))
echo '
'.Tools::safeOutput($result[$name]).'
';
else
echo '
'.$attributes[$name].'
';
}
echo '
';
}
echo '
';
}
echo '
'.((Tools::getValue('back')) ? $this->l('Back') : $this->l('Back to list')).' ';
}
/**
* Override displayListContent method for add a button "export"
*/
public function displayListContent($token = null)
{
/* Display results in a table
*
* align : determine value alignment
* prefix : displayed before value
* suffix : displayed after value
* image : object image
* icon : icon determined by values
* active : allow to toggle status
*/
$id_category = 1; // default categ
$irow = 0;
if ($this->_list && isset($this->fieldsDisplay['position']))
{
$positions = array_map(create_function('$elem', 'return (int)($elem[\'position\']);'), $this->_list);
sort($positions);
}
if ($this->_list)
{
$isCms = false;
if (preg_match('/cms/Ui', $this->identifier))
$isCms = true;
$keyToGet = 'id_'.($isCms ? 'cms_' : '').'category'.(in_array($this->identifier, array('id_category', 'id_cms_category')) ? '_parent' : '');
foreach ($this->_list as $tr)
{
$id = $tr[$this->identifier];
echo '