Keyword

plugin Load Csv datas

  • Hardkiffeur
  • Hardkiffeur's Avatar Topic Author
  • Offline
  • Senior Member
More
11 years 9 months ago #101652 by Hardkiffeur
plugin Load Csv datas was created by Hardkiffeur
Hi,
I looking for loading some data from a Csv file browsh from backend Edit Item and rendering this on AfterItemDisplayContent with this code below Jfolder don't get the File's folder :
Loadcsv.xml :
<?xml version="1.0" encoding="utf-8"?>
<install version="1.5" type="plugin" group="k2" method="upgrade">
	<name>LoadCSV K2 Plugin</name>
	<author>HardKiffeur</author>
	<creationDate>3 Aout, 2012</creationDate>
	<copyright>Copyright (c) 2012  hardkiffeur. All rights reserved.</copyright>
	<authorEmail>[email protected]</authorEmail>
	<authorUrl>www.hardkiffeur.com</authorUrl>
	<version>1</version>
	<license>http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL</license>
	<description>An K2 plugin to extend item, category forms in K2 with a CSV loading for Table outputing in front into item with data.</description>
	<files>
		<filename plugin="loadcsv">loadcsv.php</filename>
	</files>
	
	
		<!-- Standard plugin parameters for Joomla! 1.5 -->
	<params>
	    <param name="mycsvfile" type="filelist" default="" label="Select a csv file" description="" directory="csv" filter="" exclude="" stripext="" />
	</params>
	
	<!-- Standard plugin parameters for Joomla! 1.6+ -->
	<config>
		<fields name="params">
			<fieldset name="basic">
                <field name="mycsvfile" type="filelist" default="" label="Select a csv file" description="" directory="csv" filter="" exclude="" stripext="" />
            </fieldset>
		</fields>
	</config>
	
	<!-- K2 plugin parameters which extend K2 backend forms are defined here. We use the same syntax as Joomla! 1.5 only, yet they are compatible with Joomla! 1.7 as well. These parameters are parsed by K2 only and we decided to just make things easier for developers, instead of opting for double parameters in 'params' and 'fields' blocks. -->
	<params group="item-content">
		<param name="mycsvfile" type="filelist" default="" label="Select a csv file" description="" directory="csv" filter="" exclude="" stripext="" />
	</params>
</install>
I try to add this, butplugin don't install (even if I've this csv folder in my zip plugin's architecture folder
<files>
   <folder>csv</folder>
</files>

here the load.php code (even if the fisrt step find the csv file's information don't work)
<?php
/**
 * @version		1
 * @package		csv loader (K2 plugin)
 * @author    Hardkiffeur http://hardkiffeur.com
 * @copyright	Copyright (c) 2012 Hardkiffeur. All rights reserved.
 * @license		GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
 */

// no direct access
defined('_JEXEC') or die ('Restricted access');

/**
 * Load a csv K2 Plugin, to render a tabla from csv entered in backend K2 forms to table full of data information in the frontend.
 */

// Load the K2 Plugin API
JLoader::register('K2Plugin', JPATH_ADMINISTRATOR.DS.'components'.DS.'com_k2'.DS.'lib'.DS.'k2plugin.php');

// Initiate class to hold plugin events
class plgK2Loadcsv extends K2Plugin {

	// Some params
	var $pluginName = 'LoadCSV';
	var $pluginNameHumanReadable = 'Load CSV data K2 Plugin';

	function plgK2Loadcsv( & $subject, $params) {
		parent::__construct($subject, $params);
	}

	/**
	 * Below we list all available FRONTEND events, to trigger K2 plugins.
	 * Watch the different prefix "onK2" instead of just "on" as used in Joomla! already.
	 * Most functions are empty to showcase what is available to trigger and output. A few are used to actually output some code for example reasons.
	 */

	function onK2PrepareContent( &$item, &$params, $limitstart) {
		$mainframe = &JFactory::getApplication();
		//$item->text = 'It works! '.$item->text;
	}

	function onK2AfterDisplay( &$item, &$params, $limitstart) {
		$mainframe = &JFactory::getApplication();
		return '';
	}

	function onK2BeforeDisplay( &$item, &$params, $limitstart) {
		$mainframe = &JFactory::getApplication();
		return '';
	}

	function onK2AfterDisplayTitle( &$item, &$params, $limitstart) {
		$mainframe = &JFactory::getApplication();
		return '';
	}

	function onK2BeforeDisplayContent( &$item, &$params, $limitstart) {
		$mainframe = &JFactory::getApplication();
		return '';
	}

	// Event to display (in the frontend) the YouTube URL as entered in the item form
	function onK2AfterDisplayContent( &$item, &$params, $limitstart) {
		$mainframe = &JFactory::getApplication();

		// Get the K2 plugin params (the stuff you see when you edit the plugin in the plugin manager)
		$plugin = &JPluginHelper::getPlugin('k2', $this->pluginName);
		$pluginParams = new JParameter($plugin->params);

		// Get the output of the K2 plugin fields (the data entered by your site maintainers)
		$plugins = new K2Parameter($item->plugins, '', $this->pluginName);
		
		$mycsvfile = $plugins->get('mycsvfile_item');

		// Check if we have a value entered
		if ( empty($mcsvfile)) return;

		// Output

		$output = '
		<?php 
	echo "<table>\n";
$row = 0;
$handle = fopen("$mycsvfile", "r");

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    if ($row == 0) {
        // this is the first line of the csv file
        // it usually contains titles of columns
        $num = count($data);
        echo "<thead>\n<tr>";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo "<th>" . $data[$c] . "</th>";
        }
        echo "</tr>\n</thead>\n\n<tbody>";
    } else {
        // this handles the rest of the lines of the csv file
        $num = count($data);
        echo "<tr>";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo "<td>" . $data[$c] . "</td>";
        }
        echo "</tr>\n";
    }
}
fclose($handle);

echo "</tbody>\n</table>";

?>

		';

		return $output;
	}

} // END CLASS

Any idea the right rules for select Folder by plugins and so broswche csv files available fron ItemForm ?
Thanks for the time ...

Please Log in or Create an account to join the conversation.

  • Hardkiffeur
  • Hardkiffeur's Avatar Topic Author
  • Offline
  • Senior Member
More
11 years 9 months ago - 11 years 9 months ago #101653 by Hardkiffeur
Replied by Hardkiffeur on topic Re: plugin Load Csv datas
Day's return,
Install plugin's without error, but seems php (ok with real value : plugins/k2/loadcsv/csv/test.csv) work on the K2Template override Item.php but issue with the output and if &mycsvfile empty
here the loadcsv.xml file :
<?xml version="1.0" encoding="utf-8"?>
<install version="1.5" type="plugin" group="k2" method="upgrade">
        <name>LoadCSV K2 Plugin</name>
        <author>HardKiffeur</author>
        <creationDate>3 Aout, 2012</creationDate>
        <copyright>Copyright (c) 2012  hardkiffeur. All rights reserved.</copyright>
        <authorEmail>[email protected]</authorEmail>
        <authorUrl>www.hardkiffeur.com</authorUrl>
        <version>1</version>
        <license>http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL</license>
        <description>An K2 plugin to extend item, category forms in K2 with a CSV loading for Table outputing in front into item page, with data.</description>
        <files>
                <filename plugin="loadcsv">loadcsv.php</filename>
                <folder>csv</folder>
        </files>
        
        
                <!-- Standard plugin parameters for Joomla! 1.5 -->
        <params>
            <param name="mycsvfolder" type="folderlist" default="plugins/k2/loadcsv/csv" label="Select a csv folder" description="" directory="plugins/k2/loadcsv/csv" filter="" exclude="" stripext="" />
        </params>
        
        <!-- Standard plugin parameters for Joomla! 1.6+ -->
        <config>
                <fields name="params">
                        <fieldset name="basic">
                <field name="mycsvfolder" type="folderlist" default="plugins/k2/loadcsv/csv" label="Select a csv folder" description="" directory="plugins/k2/loadcsv/csv" filter="" exclude="" stripext="" />
            </fieldset>
                </fields>
        </config>
        
        <!-- K2 plugin parameters which extend K2 backend forms are defined here. We use the same syntax as Joomla! 1.5 only, yet they are compatible with Joomla! 1.7 as well. These parameters are parsed by K2 only and we decided to just make things easier for developers, instead of opting for double parameters in 'params' and 'fields' blocks. -->
        <params group="item-content">
                <param name="mycsvfile" type="filelist" default="test.csv" label="Select a csv file" description="" directory="plugins/k2/loadcsv/csv/" filter="" exclude="" stripext="" />
        </params>
</install>
here the loadcsv.php
<?php
/**
 * @version             1
 * @package             csv loader (K2 plugin)
 * @author    Hardkiffeur http://hardkiffeur.com
 * @copyright   Copyright (c) 2012 Hardkiffeur. All rights reserved.
 * @license             GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
 */

// no direct access
defined('_JEXEC') or die ('Restricted access');

/**
 * Load a csv K2 Plugin, to render a tabla from csv entered in backend K2 forms to table full of data information in the frontend.
 */

// Load the K2 Plugin API
JLoader::register('K2Plugin', JPATH_ADMINISTRATOR.DS.'components'.DS.'com_k2'.DS.'lib'.DS.'k2plugin.php');

// Initiate class to hold plugin events
class plgK2loadcsv extends K2Plugin {

        // Some params
        var $pluginName = 'loadcsv';
        var $pluginNameHumanReadable = 'Load CSV data K2 Plugin';

        function plgK2loadcsv( & $subject, $params) {
                parent::__construct($subject, $params);
        }

        /**
         * Below we list all available FRONTEND events, to trigger K2 plugins.
         * Watch the different prefix "onK2" instead of just "on" as used in Joomla! already.
         * Most functions are empty to showcase what is available to trigger and output. A few are used to actually output some code for example reasons.
         */

        function onK2PrepareContent( &$item, &$params, $limitstart) {
                $mainframe = &JFactory::getApplication();
                //$item->text = 'It works! '.$item->text;
        }

        function onK2AfterDisplay( &$item, &$params, $limitstart) {
                $mainframe = &JFactory::getApplication();
                return '';
        }

        function onK2BeforeDisplay( &$item, &$params, $limitstart) {
                $mainframe = &JFactory::getApplication();
                return '';
        }

        function onK2AfterDisplayTitle( &$item, &$params, $limitstart) {
                $mainframe = &JFactory::getApplication();
                return '';
        }

        function onK2BeforeDisplayContent( &$item, &$params, $limitstart) {
                $mainframe = &JFactory::getApplication();
                return '';
        }

        // Event to display (in the frontend) the YouTube URL as entered in the item form
        function onK2AfterDisplayContent( &$item, &$params, $limitstart) {
                $mainframe = &JFactory::getApplication();

                // Get the K2 plugin params (the stuff you see when you edit the plugin in the plugin manager)
                $plugin = &JPluginHelper::getPlugin('k2', $this->pluginName);
                $pluginParams = new JParameter($plugin->params);

                // Get the output of the K2 plugin fields (the data entered by your site maintainers)
                $plugins = new K2Parameter($item->plugins, '', $this->pluginName);        
                $mycsvname = $plugins->get('mycsvfile_item');

                // Check if we have a value entered
                if ( empty($mycsvfile)) return;

                // Output

                $output = '
                <?php

echo "<table>\n";
$row = 0;
$handle = fopen("'.$mycsvfile.'", "r");

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    if ($row == 0) {
        // this is the first line of the csv file
        // it usually contains titles of columns
        $num = count($data);
        echo "<thead>\n<tr>";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo "<th>" . $data[$c] . "</th>";
        }
        echo "</tr>\n</thead>\n\n<tbody>";
    } else {
        // this handles the rest of the lines of the csv file
        $num = count($data);
        echo "<tr>";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo "<td>" . $data[$c] . "</td>";
        }
        echo "</tr>\n";
    }
}
fclose($handle);

echo "</tbody>\n</table>";

?>
                ';

                return $output;
        }

} // END CLASS

For me the issue is due to a not reall good match of the :
mycsvfolder & mycsvfile and the php code. I couldn't find at this moment how solve
Good to have a default folder under Plugin's Manager and have a unique file under the item's form

Any help could be very nice.
I attach the plugins Zip file too if someone want to investigate with me....
Attachments:

Please Log in or Create an account to join the conversation.


Powered by Kunena Forum