nblgeoweb VIP
Total posts: 178
05 Sep 2014 01:20

I'm exploring the CSV formatter and it looks like it supports templates. Currently there is a csv.php. I'd like to modify the csv output from the default. Is it possible to have multiple templates for the csv formatter that can be specified in the URL?

Last Modified: 08 Sep 2014


nblgeoweb VIP
Total posts: 178
05 Sep 2014 12:15

I answered my own question... No. Easy enough for even a basic hacker like me to add the functionality :). I had to modify a core file so hopefully you include this in your distribution. You'll see the extra code in the onListFormat and onRecordFormat functions. Now I can have a template for the csv formatter. I just specify the url with formatter=csv&template=ssp_csv. Replace "ssp_csv" with whatever the name of the template is (without the .php).

The file is plugins/mint/formatter_csv/formatter_csv.php

<?php
/**
 * Cobalt by MintJoomla
 * a component for Joomla! 1.7 - 2.5 CMS ( http://www.joomla.org )
 * Author Website:  http://www.mintjoomla.com/ 
 * @copyright Copyright (C) 2012 MintJoomla ( http://www.mintjoomla.com ). All rights reserved.
 * @license   GNU/GPL  http://www.gnu.org/copyleft/gpl.html 
 */
defined('_JEXEC') or die('Restricted access');

class plgMintFormatter_csv extends JPlugin
{
        private $tmpl_path;

        public function __construct(& $subject, $config)
        {
                parent::__construct($subject, $config);
                $this->loadLanguage();
                $this->tmpl_path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'tmpl' . DIRECTORY_SEPARATOR;
        }

        function onListFormat($view)
        {
                if(JFactory::getApplication()->input->get('formatter') != 'csv')
                {
                        return;
                }

                $this->sendHeader();
                if(JFactory::getApplication()->input->get('template') != '')
                {
                        $template=JFactory::getApplication()->input->get('template').".php";
                } else {
                        $template="csv.php";
                }
                require $this->tmpl_path . 'list' . DIRECTORY_SEPARATOR . $template;
        }

        function onRecordFormat($view)
        {
                if(JFactory::getApplication()->input->get('formatter') != 'csv')
                {
                        return;
                }

                $this->sendHeader();
                if(JFactory::getApplication()->input->get('template') != '')
                {
                        $template=JFactory::getApplication()->input->get('template').".php";
                } else {
                        $template="csv.php";
                }
                require $this->tmpl_path . 'record' . DIRECTORY_SEPARATOR . $template;
        }

        function sendHeader()
        {
                header('Content-type: text/plain; charset=utf-8');
        }

        function _getVal($val, $glue = ', ')
        {
                if($this->params->get('field_format') == 1 && is_array($val))
                {
                        return json_encode($val);
                }
                elseif(is_array($val))
                {
                        return $this->multi_implode($val, $glue);
                }

                return $val;
        }

        function multi_implode($array, $glue)
        {
                $ret = '';

                foreach($array as $item)
                {
                        if(is_array($item))
                        {
                                $ret .= $this->multi_implode($item, $glue) . $glue;
                        }
                        else
                        {
                                $ret .= $item . $glue;
                        }
                }

                $ret = substr($ret, 0, 0 - strlen($glue));

                return $ret;
        }
}


Sergey
Total posts: 13,748
08 Sep 2014 00:18

I updated plugins and added plugin parameter. But URL like you did will work also in case you need different template per section.


nblgeoweb VIP
Total posts: 178
08 Sep 2014 10:28

Great. Thanks Sergey. It's turned out very helpful to be able to use a template. I am using DPCalendar for a few things and I wanted my Cobalt events to display on the calendar integrated with it's native entries as well as be in Cobalt. DPCalendar has a plugin to read directly from CSV but they unfortunately require a very specific format in the CSV. With the template applied to the formatter I was able to make it all work perfectly and independantly control exactly what my record listing would look like in Cobalt. Yet another great functionality of Cobalt :)


Sergey
Total posts: 13,748
08 Sep 2014 10:36

If you have not yet posted review on JED, you are welcome.

Powered by Cobalt