/**
  * Genrating a export file
  */
 public function generateExport()
 {
     $id = Tools::getValue($this->identifier);
     $export_dir = defined('_PS_HOST_MODE_') ? _PS_ROOT_DIR_ . '/export/' : _PS_ADMIN_DIR_ . '/export/';
     if (!Validate::isFileName($id)) {
         die(Tools::displayError());
     }
     $file = 'request_sql_' . $id . '.csv';
     if ($csv = fopen($export_dir . $file, 'w')) {
         $sql = RequestSql::getRequestSqlById($id);
         if ($sql) {
             $results = Db::getInstance()->executeS($sql[0]['sql']);
             foreach (array_keys($results[0]) as $key) {
                 $tab_key[] = $key;
                 fputs($csv, $key . ';');
             }
             foreach ($results as $result) {
                 fputs($csv, "\n");
                 foreach ($tab_key as $name) {
                     fputs($csv, '"' . strip_tags($result[$name]) . '";');
                 }
             }
             if (file_exists($export_dir . $file)) {
                 $filesize = filesize($export_dir . $file);
                 $upload_max_filesize = Tools::convertBytes(ini_get('upload_max_filesize'));
                 if ($filesize < $upload_max_filesize) {
                     if (Configuration::get('PS_ENCODING_FILE_MANAGER_SQL')) {
                         $charset = Configuration::get('PS_ENCODING_FILE_MANAGER_SQL');
                     } else {
                         $charset = self::$encoding_file[0]['name'];
                     }
                     header('Content-Type: text/csv; charset=' . $charset);
                     header('Cache-Control: no-store, no-cache');
                     header('Content-Disposition: attachment; filename="' . $file . '"');
                     header('Content-Length: ' . $filesize);
                     readfile($export_dir . $file);
                     die;
                 } else {
                     $this->errors[] = Tools::DisplayError('The file is too large and can not be downloaded. Please use the LIMIT clause in this query.');
                 }
             }
         }
     }
 }