Jump to content

CSV export


cikcak

Recommended Posts

This is for 1.6.0.9 -

 

Add a new file

./override/controllers/admin/AdminRequestSqlController.php




<?php

class AdminRequestSqlController extends AdminRequestSqlControllerCore
{

        public function generateExport()
        {
                $id = Tools::getValue($this->identifier);
                if (!Validate::isFileName($id))
                        die(Tools::displayError());

                if (Configuration::get('PS_CSV_SEPARATOR'))
                        $csvsep = stripslashes(Configuration::get('PS_CSV_SEPARATOR'));
                else
                        $csvsep = ';';
                if (Configuration::get('PS_CSV_STRIPTAGS'))
                        $csvstrip = Configuration::get('PS_CSV_STRIPTAGS');
                else
                        $csvstrip = true;
                $file = 'request_sql_'.$id.'.csv';
                if ($csv = fopen(_PS_ADMIN_DIR_.'/export/'.$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.$csvsep);
                                }
                                foreach ($results as $result)
                                {
                                        fputs($csv, "\n");
                                        foreach ($tab_key as $name)
                                                fputs($csv, '"'.($csvstrip ? strip_tags($result[$name]) : $result[$name]).'"'.$csvsep);
                                }
                                if (file_exists(_PS_ADMIN_DIR_.'/export/'.$file))
                                {
                                        $filesize = filesize(_PS_ADMIN_DIR_.'/export/'.$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(_PS_ADMIN_DIR_.'/export/'.$file);
                                                die();
                                        }
                                        else
                                                $this->errors[] = Tools::DisplayError('The file is too large and can not be downloaded. Please use the clause "LIMIT" in this query.');
                                }
                        }
                }
        }

}
Now use phpmyadmin, mysqlcc or whatever to add a new record to ps_configuration, PS_CSV_STRIPTAGS value of "0" (i.e.: off!)

 

(You could also add PS_CSV_SEPARATOR in case you need something other than ";" between fields!)

 

 

PS: Being an override, this should for the most part survive prestashop updates...

Edited by irrelevant (see edit history)
Link to comment
Share on other sites

This is for 1.6.0.9 -

 

Add a new file

./override/controllers/admin/AdminRequestSqlController.php




<?php

class AdminRequestSqlController extends AdminRequestSqlControllerCore
{

        public function generateExport()
        {
                $id = Tools::getValue($this->identifier);
                if (!Validate::isFileName($id))
                        die(Tools::displayError());

                if (Configuration::get('PS_CSV_SEPARATOR'))
                        $csvsep = stripslashes(Configuration::get('PS_CSV_SEPARATOR'));
                else
                        $csvsep = ';';
                if (Configuration::get('PS_CSV_STRIPTAGS'))
                        $csvstrip = Configuration::get('PS_CSV_STRIPTAGS');
                else
                        $csvstrip = true;
                $file = 'request_sql_'.$id.'.csv';
                if ($csv = fopen(_PS_ADMIN_DIR_.'/export/'.$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.$csvsep);
                                }
                                foreach ($results as $result)
                                {
                                        fputs($csv, "\n");
                                        foreach ($tab_key as $name)
                                                fputs($csv, '"'.($csvstrip ? strip_tags($result[$name]) : $result[$name]).'"'.$csvsep);
                                }
                                if (file_exists(_PS_ADMIN_DIR_.'/export/'.$file))
                                {
                                        $filesize = filesize(_PS_ADMIN_DIR_.'/export/'.$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(_PS_ADMIN_DIR_.'/export/'.$file);
                                                die();
                                        }
                                        else
                                                $this->errors[] = Tools::DisplayError('The file is too large and can not be downloaded. Please use the clause "LIMIT" in this query.');
                                }
                        }
                }
        }

}
Now use phpmyadmin, mysqlcc or whatever to add a new record to ps_configuration, PS_CSV_STRIPTAGS value of "0" (i.e.: off!)

 

(You could also add PS_CSV_SEPARATOR in case you need something other than ";" between fields!)

 

 

PS: Being an override, this should for the most part survive prestashop updates...

 

I tryed for 1.5 PS version, doesnt work.. But thank you for help, maybe it will be usefull for somebody. Any ideas for 1.5 PS?

Link to comment
Share on other sites

I tryed for 1.5 PS version, doesnt work.. But thank you for help, maybe it will be usefull for somebody. Any ideas for 1.5 PS?

 

Just remove the word "strip_tags" from AdminRequestSqlController.php ....

 

@musicmaster .. not my code, that's prestashop core code with a few tweaks..  Yep, I'd probably look at fputcsv if I was writing from scratch.  

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...