215 Posts
iliil
3 years ago
Topic

Hi all,

I need to export a custom search query list (including joined tables)into CSV. As far as I know, SEBLOD Exporter just exports data from a selected content type so it won't probably help with this.  But maybe there is some hidden trick that will allow the Exporter component to do it.

Thanks for sharing your experience

Michal

Get a Book for SEBLOD
3 years ago
1
Level 1

Hi Michal,   

Currently it's not possible to do that with the Exporter.
The workaround is to use the Toolbox addon: Toolbox Addon

You can create a Manual Processing with those settings: 

Then in your Search Form, add a Submit button which is calling your Process:


You can also push some fields to the process and get them with a JFactory::getApplication()->input->get( '..' );

You can use this base for your process :
We are using the JCckContent class to get all items and extend this class with some specifics functions.
This exemple is working a Free Object.
Feel free to modify it if your are working on another object.

if ( !isset( $options ) ) {
    return false;
}

// Init
$app            =    JFactory::getApplication();
$format         =    $app->input->getString( 'my_field_name', 'default_string' );
$content_free   =    new JCckContentFree;
$path           =    JPATH_SITE.'/tmp/'.$config['uniqid'].'.csv';
$rows           =    array();

$content_free->setTable( '#__cck_store_form_my_free_table' );
$content_free->extend( __DIR__.'/extend/mixin.php' );

if ( $content_free->load( $config['pk'] )->isSuccessful() ) {
    $rows[]        =    $content_free->_getValues();
}
if ( JCckDevProcessing::isFirstItem( $config ) ) {
    if ( count( $rows ) ) {
        $header        =    array(
                            'id',
                            'title',
                            'date',
                            ....
                        );
        
        $content_free->_writeCsv( $path, array( 0=>$header ) );
    }
}
if ( count( $rows ) ) {
    $content_free->_writeCsv( $path, $rows );
}
if ( JCckDevProcessing::isLastItem( $config ) ) {
    $config['path']    =    $content_free->_getPath( $path, $format ); // The path of the CSV file
}

Here the base of a Mixin:

$mixin    =    new class() {
    use JCckContentTraitMixin;
    // _getPath
    protected function _getPath()
    {
        return function( $path, $format = 'csv' ) {
            if ( !JFile::exists( $path ) ) {
                return;
            }
            $info        =    pathinfo( $path );
            $new_path    =    $info['dirname'].'/my-export-'.JFactory::getDate()->format( 'Y-m-d' );
            $new_path    .=    '.csv';
            JFile::move( $path, $new_path );
            return $new_path;
        };
    }
    // _getValues
    protected function _getValues()
    {
        return function() {
            // Customize your query here
            $data    =    $this->getData();            
            $result    =    array();
            foreach ( $data as $key => $value ) {
                $result['o_'.$key]    =    $this->_get( $key, $value );
            }
            return $result;
        };
    }
    // _writeCsv
    protected function _writeCsv()
    {
        return function( $file, $rows ) {
            $handle    =    fopen( $file, 'a+' );
            foreach ( $rows as $row ) {
                fputcsv( $handle, $row, ';' );
            }
            
            fclose( $handle );
            return true;
        };
    }    
};

Hopping it will help.

Regards

215 Posts
iliil
3 years ago
0
Level 2

Thanks so much, Lionel.

This is a fantastic example and makes perfect sense!

Best Regards

Michal

Get a VIP membership