QDataGridExporter plugin prerelease - v0.1 + v0.2 + v0.3 now v0.4

Login or register to post comments
17 replies [Last post]
grossini's picture
Offline
Joined: 01/05/2009

Final name of plugin will be QDataGridExporterButton - see attached v 0.4 version

How can i share this plugin zip.
In my test Plugin manager digest well the package and also example is added to plugin examples and seemingly it works.

Today (01-16) - added little enhancement so two version 0.2 are present (different date)
From Plugin example: Setting blnDowload_all you can instruct the button to

- DOWNLOAD_ENTIRE_GRID (true) or

- DOWNLOAD_CURRENT_PAGE_ONLY (false).

and now you can export also in XLS
The structure is a simplified XLS version that works in my office 2000 - started from format
used to export in Excel by PHP Report Maker v.3 with my simple modification to display grid lines on excel Worksheet ( without grid the worksheets look unpleasant !! - why not HKVSTORE)

Final name of plugin will be QDataGridExporterButton - see attached v 0.4 version

Note on Excel
Excel is confused by cell with code number >16 (e.g. IBAN ).
Last digit (at right) are lost due to conversion in float. Added in plugin function to prefix it by 'c:'

alex94040's picture
Offline
Joined: 11/06/2008

Gianni - great to see you making progress.

A few notes for you: info on how to check in and distribute your plugin is here: http://examples.qcu.be/assets/_core/php/examples/plugins/packaging.php.

I was not able to install the plugin because the .zip file does not contain an install.php script at its root (it has a folder inside, which is not allowed). Please follow the instructions in the example I linked above to package the plugin.

Also, you might want to clean up your code a little bit further - tabs vs spaces (we have a convention to use tabs everywhere), remove any sort of testing code (QFirebug logs, print_r statements, etc).

grossini's picture
Offline
Joined: 01/05/2009

Inside zip I uploaded was directory QDataGridExporter....with the correct content.
Of course this was not that one that I tested. I discovered the problem but I not applied (me stupid) the correction to that one.
I took out install.php, includes and example dir from the now deleted QDataGridExporter dir and placed in root of zip.
Sorry.

To jump on higher summit....

Creating a simple example cloning Basic Qdatagrid and adding my button to it
I discovered that the my actual plugin is dependent of 'MetaDataBinder'
due the presence of:
$this->datasource->SetDataBinder('MetaDataBinder', $this->datasource);
$this->datasource->DataBind();

The resultant example is able to export all rows of table.
......::LoadAll($objClauses)

How can I use the datasource binded in form/panel so the exported data reflect
the filtered set.

Perhaps the solution is behind the corner and I am not able to see it.

Tank's for hour help and sorry about my malformed zip, my spaces insted of tabs and left comments. I promise an hour committed to cleaning.

Can You help me to make the plugin indipendent from 'MetaDataBinder'.
Before a long study, how can I have the datasource of form (panel).

Offline
Joined: 04/22/2009

Hello grossini,

I was quite happy with that exporter. However I have changed the export function. I think it is more elegant that way. Anyway, this will still need some testing.

You can even make the code shorter by not buffering the result into an array.

Cheers

Helge

<?php
       
// Added by hzdierz
   
protected function getCsvRowFromArray($arrRow){
       
$result = "";
        if(
is_array($arrRow)){
           
$first=true;
           
            foreach(
$arrRow as $item){
                if(
$first) $result.=$item;
                else
$result.=','.$item;
               
$first=false;
            }
           
        }
       
        return
$result;
    }
   
    function 
buttonCVS_clicked ($strFormId, $strControlId, $strParameter)
    {
       
// Dta bind. What will happen if the grid has got a paginator?
       
$this->datasource->DataBind();
       
       
// Get the data
       
$data = $this->datasource->DataSource;
       
       
// Get the columns for the header
       
$columns = $this->datasource-> GetAllColumns();
       
       
// Get header names
       
$header = array();
        foreach(
$columns as $column){
           
// Get the column names but strip off any html tags in case we have got a sort ref.
           
$header[] = strip_tags($column->Name);
        }
       
//QFirebug::log($header);
        // get thge data rows
       
$rows = array();
        foreach(
$data as $item){
           
$values = array();
            foreach(
$columns as $column){
               
// Get the values but strip off any html tags in case we have got a button or so.
               
$values[] = strip_tags(QDataGrid::ParseHtml($column->Html,$this->datasource,$column,$item));
               
            }
           
$rows[] = $values;
        }
       
//QFirebug::log($rows);       
       
        // Change heaser info
       
session_cache_limiter('must-revalidate');        // Blaine's fix for SSL & PHP Sessions
       
header("Pragma: hack"); // IE chokes on "no cache", so set to something, anything, else.
       
$ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time()) . " GMT";
       
header($ExpStr);

       
header("Content-type: text/csv");
       
header("Content-disposition: csv; filename=" . date("Y-m-d") ."_datagrid_export.csv");
       
       
// Spit out header
       
echo $this->getCsvRowFromArray($header);
        echo
"\n";
       
// Spit out rows
       
foreach($rows as $row){
            echo
$this->getCsvRowFromArray($row);
            echo
"\n";
        }
        exit();
       
    }
?>

grossini's picture
Offline
Joined: 01/05/2009

Now I replaced version 0.1 with the version with advices from hdzierz.
Also I tried a cleaning activity of code as per Alex kind reproof.

I added two lines (see comment) to confuse paginator and to have all pages downloaded.

If required, one can create (easily) two buttons - download all - download this page.....

Gianni

alex94040's picture
Offline
Joined: 11/06/2008

Gianni - I'm SUPER impressed. I just downloaded and installed the 0.2 version of the plugin, and everything works as advertised! That's so great!

A few ways to potentially improve the plugin further:
1) Create __set() and __get() methods on your QDataGridExporter class. In these methods, allow the user to set a DownloadContent variable; allow them to set it to QDataGridExporter::DOWNLOAD_ENTIRE_GRID or QDataGridExporter::DOWNLOAD_CURRENT_PAGE_ONLY.

2) You might want to rename QDataGridExporter to QDataGridExporterButton - I'm only now realizing that since it's a child of a button control, it should probably be called a Button itself :-)

3) Additional cleanup of the code.
- There are still some tabs/spaces problems in the files. You might want to use an IDE that shows you tabs and spaces, so that you don't have to guess.
- There is still lots of commented out stuff that probably should not be there.

4) Example file. What you have is a great starting point; you might want to extend the wording further to explain how it works with pagination, etc.

When you are done, I'd love to see your code on the /plugins section of the QCubed subversion repository, where all other plugins are: http://trac.qcu.be/projects/qcubed/browser/plugins. More info on how to put stuff there is this tutorial: http://examples.qcu.be/assets/_core/php/examples/plugins/packaging.php

UPDATE: I see you've checked your stuff into the source control repository already! That's super exciting! Quick ask: can you make sure to provide comments with every checkin you make to the repository? Thank you!

Offline
Joined: 04/22/2009

I have to add that I am very happy with that tool too. I was absolutely missing such a tool. May be you can add some additional features as export to XLS,PDF ... :-)

I had started an extension to QDataGrid, but found your 'button concept' quite intriguing.

Well done!

Helge

Offline
Joined: 01/09/2008

Have a look at:

http://qcu.be/content/export-excel

I have not looked into the code of this plugin here, but is it would be greate if we could extend this with additional export possibilities.

Kristof

alex94040's picture
Offline
Joined: 11/06/2008

I would LOVE it if the datagrid exporter could be generalized to work with either CVS, or XLS, or PDF, or whatever we want. Gianni, would you be up for integrating Kristof's work into your plugin? It shouldn't be too difficult.

grossini's picture
Offline
Joined: 01/05/2009

now plugin QDataGridExporter is at version 0.3. No update to svn jet. Alex, please don't preach me.
Example name changed ... how this can be reflected in tortoise? And how comments on change done?
At moment no time to study it.

Now I hope you LOVE the plugin also if it is without PDF.....
Can I send a regard in Italian language?
Un caro saluto, Gianni

scottux's picture
Offline
Joined: 11/07/2008

I have had to export datagrids to XLS, PDF, DOC, CSV, and tab-delimited text.
I will scrounge up some code to donate to this very worthy project.

alex94040's picture
Offline
Joined: 11/06/2008

Gianni, great to see the new 0.3 release with the ability to do XLS export!

I would encourage you to do a few things:
1) Rename the blnDowload_all property to look more like every other property in QCubed - DownloadMode, and have acceptable values be constants like QDataGridExporter::ENTIRE_DATAGRID and QDataGridExporter::CURRENT_PAGE.

2) You might want to rename QDataGridExporter to QDataGridExporterButton - I'm only now realizing that since it's a child of a button control, it should probably be called a Button itself :-)

3) Move the code to do the export to Excel to one function, and export to CSV to another function. buttonCVS_clicked() function right now is too long.

4) Create a new class property to determine whether CSV or XLS should be downloaded. Depending on the Text property of the button seems inappropriate (what if i want my button text to just say "download"?). That property may be called DownloadFormat, and have values defined in another class (essentially an enum class), QDataGridExporterFormat::XLS or QDataGridExporterFormat::CSV. I can imagine this growing as you incorporate new formats (code from scottux).

5) Update the example file to include:
- Downloading current page only
- Downloading in XLS format.

Thanks for doing this!! I'm very excited about using your control for my own applications, as I'm sure everyone else in this community!

grossini's picture
Offline
Joined: 01/05/2009

I admit that, due to my poor experience in OOP, I tried without look your suggestion
but after some unsuccesfull code effort to put CONSTANT in action (so QDataGridExporter::ENTIRE_DATAGRID is effective) I took a short cut to enable the pluglin to achive desired result.

Can you show me the way (a trace of lines of code) to achive the result in a more polite manner? I learn by example with swifts steps ...

Gianni

alex94040's picture
Offline
Joined: 11/06/2008

Sure, Gianni. Totally my pleasure to help. Here's how you declare constants and use them in PHP:

class MyClass {
public const MY_CONSTANT_A = 1;
public const MY_CONSTANT_B = 2;
}

$myVar = MyClass::MY_CONSTANT_A;

grossini's picture
Offline
Joined: 01/05/2009

Only added to this initial post to test - no update in tortoise yet....

public before constant in my installation give error...!!!

Ciao, Gianni

grossini's picture
Offline
Joined: 01/05/2009

Now QDataGridExporterButton (0.4) works near my expectation.
Hope this is also for you.

Have you see my circunvention approach on Excel confused by numeric > 16 digit?

Ciao, Gianni

alex94040's picture
Offline
Joined: 11/06/2008

Gianni - you're right, sorry! Please remove "public" from before "const". I've done too much C# lately :) :) Forgetting my PHP syntax :)

Tutorial on PHP class constants: http://php.net/manual/en/language.oop5.constants.php.

Let us know when you have an update that has all the comments incorporated! Very much looking forward to this stuff!

alex94040's picture
Offline
Joined: 11/06/2008