* @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('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.'). '
'.($obj->id ? '' : '').'
'.$this->l('Request').'
* '.$this->l('Required field').'
'; } 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 ''; echo ' '; $request_sql = new RequestSql(); $attributes = $request_sql->attributes; foreach ($results as $result) { echo ''; foreach ($tab_key as $name) { if (!isset($attributes[$name])) echo ''; else echo ''; } echo ''; } echo '
'.$key_name.'
'.Tools::safeOutput($result[$name]).''.$attributes[$name].'
'; } 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 'identifier,$this->identifiersDnd) ? ' id="tr_'.(($id_category = (int)(Tools::getValue('id_'.($isCms ? 'cms_' : '').'category', '1'))) ? $id_category : '').'_'.$id.'_'.$tr['position'].'"' : '').($irow++ % 2 ? ' class="alt_row"' : '').' '.((isset($tr['color']) AND $this->colorOnBackground) ? 'style="background-color: '.$tr['color'].'"' : '').'> '; if ($this->delete AND (!isset($this->_listSkipDelete) OR !in_array($id, $this->_listSkipDelete))) echo ''; echo ''; foreach ($this->fieldsDisplay as $key => $params) { $tmp = explode('!', $key); $key = isset($tmp[1]) ? $tmp[1] : $tmp[0]; echo ' noLink) || !$this->noLink)) echo ' onclick="document.location = \''.self::$currentIndex.'&'.$this->identifier.'='.$id.($this->view? '&view' : '&update').$this->table.'&token='.($token!=NULL ? $token : $this->token).'\'">'.(isset($params['prefix']) ? $params['prefix'] : ''); else echo '>'; if (isset($params['active']) && isset($tr[$key])) $this->_displayEnableLink($token, $id, $tr[$key], $params['active'], Tools::getValue('id_category'), Tools::getValue('id_product')); else if (isset($params['activeVisu']) && isset($tr[$key])) echo ''.($tr[$key] ? $this->l('Enabled') : $this->l('Disabled')).''; else if (isset($params['position'])) { if ($this->_orderBy == 'position' && $this->_orderWay != 'DESC') { echo ' '.$this->l('Down').''; echo ' '.$this->l('Up').''; } else echo (int)($tr[$key] + 1); } else if (isset($tr[$key])) { $echo = $tr[$key]; echo isset($params['callback']) ? call_user_func_array(array((isset($params['callback_object'])) ? $params['callback_object'] : $this->className, $params['callback']), array($echo, $tr)) : $echo; } else echo '--'; echo (isset($params['suffix']) ? $params['suffix'] : ''). ''; } if ($this->shopLinkType) { $name = (Tools::strlen($tr['shop_name']) > 15) ? Tools::substr($tr['shop_name'], 0, 15).'...' : $tr['shop_name']; echo ''.$name.''; } if ($this->edit || $this->delete || ($this->view && $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 && (!isset($this->_listSkipDelete) || !in_array($id, $this->_listSkipDelete))) $this->_displayDeleteLink($token, $id); if ($this->duplicate) $this->_displayDuplicate($token, $id); echo ''; } echo ''; } } } protected function _displayExportLink($token = null, $id) { $_cacheLang['export'] = $this->l('export'); echo ' '.$_cacheLang['export'].''; } }