Page 1 of 1

Customizing csv export: labels as columns titles ...

PostPosted: Tue Sep 29, 2009 5:50 am
by zabelle_motte
Hello,

I would like to adapt the csv export functionnality to present exported datas with column titles coming from the label instruction rather than the one from the database.

Suppose to have the following lines in your fields.ini file :
Code: Select all

[inscription_fin]
widget:label = "Date de fin d'inscription"


In the csv export, how to make this column title to be "Date de fin d'inscription" rather than "inscription_fin" ?

I have been copying the export_csv.php action in my application actions folder and I found that the manipulation should be done in this section (for the non related records) :

Code: Select all
function rec2data(&$record){
      $out = array();
      $columns = array_merge(array_keys($record->_table->fields()), array_keys($record->_table->graftedFields()));
      
      foreach ($columns as $key)
                        $f =& $record->_table->getField($key);
         if ( @$f['visibility']['csv'] == 'hidden' ){
            unset($f);
            continue;
         }
         $out[] = $record->display($key);
         unset($f);
      }
      return $out;
   }


Thanks in advance for your help ...

Zabelle

PostPosted: Wed Sep 30, 2009 10:58 am
by shannah
Actually you'll want to change the following section:

Code: Select all
$headings = array();
         foreach ( $relationship->_schema['short_columns'] as $colhead ){
            $f =& $relationship->getField($colhead);
            if ( @$f['visibility']['csv']  == 'hidden' ){
               unset($f);
               continue;
            }
            $headings[] = $colhead;
            unset($f);
         }


Change the line that says
Code: Select all
$headings[] = $colhead

to
Code: Select all
$headings[] = $f['widget']['label'];


There are 2 places where you need to make this change.

The final solution ...

PostPosted: Thu Oct 01, 2009 1:06 am
by zabelle_motte
Thanks a lot for your help, it works fine !

For the solution to work in every case, even if no label is given for the concerned field and even if the label contains accents (I am French speaking ...), I finally replaced the two occurance of the line
Code: Select all
$headings[] = $colhead;

by the following commands
Code: Select all
if (isset($f['widget']['label']))
{   
    $label = $f['widget']['label'];
    $label=html_entity_decode($label);
    $label=utf8_encode($label);
    $headings[] = $label;
}
else
{
    $headings[] = $colhead;
}


With that solution, even if your fields.ini contains
Code: Select all
[inscription_debut]
widget:label = "Date de début d'inscription"

the accents will be OK in the csv export file while reading the file with UTF8 charset.

Much thanks for your rapid help ...

Zabelle

Re: Customizing csv export: labels as columns titles ...

PostPosted: Fri Dec 09, 2011 9:03 am
by Jean
Hello Steve and all,


This is a smart solution for the columns, thank you.

To complete it, I need to transform htmlarea type fields with strip_tags and html_entities_decode functions.

So I did several tries to add some code in the export_csv.php in the actions directory of Xataface that begins like this :
Code: Select all
if (@$f['widget']['type']=='htmlarea')

But without any result.

Besides I tried to put the export_csv.php in the actions directory of the site but I got an error.

Thank you for any hunch

Jean

Re: Customizing csv export: labels as columns titles ...

PostPosted: Thu Feb 02, 2012 8:39 am
by Jean
Hello Steve,

This problem is recurrent. In the csv export, I'd like to have plain text for the htmlarea fields, like I explained before, how can I do that ?
Thank you

Jean

Re: Customizing csv export: labels as columns titles ...

PostPosted: Thu Feb 02, 2012 9:59 am
by shannah
You could:
1. Override the display() of the field to strip the tags... but this would affect all parts of the application where the HTML fields are displayed.
2. Create a grafted field, then override its display to show the stripped contents of the HTML area field.

e.g. in the fields.ini:
Code: Select all
__sql__ = "select t.*, null as stripped_field from mytable t"

[original_field]
   widget:type=htmlarea
   visibility:csv=hidden

[stripped_field]
    visibility:csv = visible
    visibility:list=hidden
    visibility:browse=hidden
    visibility:find=hidden


Then in the delegate class:
Code: Select all
function stripped_field__display($record){
    return html_entities_decode(strip_tags($record->val('original_field'));
}

Re: Customizing csv export: labels as columns titles ...

PostPosted: Fri Feb 03, 2012 12:57 am
by Jean
GREAT!

Re: Customizing csv export: labels as columns titles ...

PostPosted: Tue Feb 07, 2012 3:19 am
by Jean
Beware entities/entity :wink:
Code: Select all
function stripped_field__display($record){
    return html_entity_decode(strip_tags($record->val('original_field'));
}

Re: Customizing csv export: labels as columns titles ...

PostPosted: Tue Feb 07, 2012 1:54 pm
by Jean
Well Steve I have added your answer in the wiki.
Jean