* @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 (sizeof($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'))
{
$requestSql = new RequestSql();
$parser = $requestSql->parsingSql($sql);
$validate = $requestSql->validateSql($parser, false, $sql);
if (!$validate || !empty($requestSql->errorSql))
$this->_DisplayError($requestSql->errorSql);
}
}
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 following tables: ').$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 following tables: ').$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 following tables: ').$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 following tables: ').$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 following tables: ').$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 following tables: ').$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 '
';
}
if ($this->edit OR $this->delete OR ($this->view AND $this->view !== 'noActionColumn'))
{
echo '
';
if ($this->export)
$this->_displayExportLink($token, $id);
if ($this->view)
$this->_displayViewLink($token, $id);
if ($this->edit)
$this->_displayEditLink($token, $id);
if ($this->delete AND (!isset($this->_listSkipDelete) OR !in_array($id, $this->_listSkipDelete)))
$this->_displayDeleteLink($token, $id);
if ($this->duplicate)
$this->_displayDuplicate($token, $id);
echo '