<?php
/*
============================================================================
Title : CSV-Import
Version : 1.0.2
Operating Systems : All
Plug and Play : yes
License : GPL
Homepage of this plugin : http://www.iteria.de/phpcms/plugins/csvimport/
Author : Patrick Müller ( hide@address.com )
Homepage of the author : http://www.iteria.de/phpcms/plugins/
Filename : csvimport_1_0_2.zip
Description : Import data from the CSV-format (e.g. Excel)
============================================================================
csvImport dient dem automatischen und konfigurierbaren Import von Daten im
CSV-Format aus einer externen Datei in phpCMS.
Die Daten können anhand zuvor definierter Templates (ähnlich der Menü-
Templates von phpCMS) beliebig dargestellt werden. Alle Einstellungen
erfolgen in den Content-Dateien von phpCMS, in denen externe Daten mit
Hilfe von csvImport integriert werden sollen.
----------------------------------------------------------------------------
Vorteile:
- Leichte Einbindung von tabellarischen Daten.
- Darstellung der Daten durch Templates leicht konfigurierbar.
- Dynamisch erzeugte Inhalte können durch dieses Plugin in den Inhalt der
Website miteinbezogen werden.
- Jedes Feld der Tabelle kann per phpCMS-Tag leicht im weiteren Content
benutzt werden
- Mehrere Zeilen können gruppiert und zusammengefasst
werden.
============================================================================
Version 1.0.2:
- New: use different template every x line (Alternate-Line)
(basic idea from Carsten Seiffert)
----------------------------------------------------------------------------
Version 1.0.1:
- Fix: don't supress number "0"
- Fix: display possible rest of lines if GROUPLINES is used
- New: HTML Code formatted output (formatting of the template)
============================================================================
*/
// to avoid errors in the EDIT-mode
if( !isset( $phpCMSedit ) )
{
/* <-- Default-Konfiguration START --> */
if (!isset($default_text) || strlen($default_text)==0) $default_text = ' ' ; // Default-Text if empty content
if (!isset($doReplTags) || strlen($doReplTags)==0) $doReplTags = "OFF"; // generate phpCMS-tags for each field in the dta-file
if (!isset($separator) || strlen($separator)==0) $separator = ";"; // characters-combination between the extracted words in the meta-tags
if (!isset($headlines) || strlen($headlines)==0) $headlines = "1"; // no. of headlines
if (!isset($grouplines) || strlen($grouplines)==0) $grouplines = "1"; // no. of lines for each group
if (!isset($csvtemp) || strlen($csvtemp)==0) $csvtemp = ""; // name of template
if (!isset($maxLineSize) || strlen($maxLineSize)==0) $maxLineSize = "1000"; // maximum number of characters in a csv-line
if (!isset($alternate) || strlen($alternate)==0) $alternate = "1"; // use alternative template every x line (if greater than "1")
/* <-- Default-Konfiguration END --> */
/*
* Eine Funktion zum Parsen von Template-Files
* Nicht perfekt, aber sehr hilfreich.
*
* Gibt ein Array zurück, in dem zu dem CONTENT-TAG
* der Inhalt zugewiesen wurde ($x[TAG] = INHALT)
*/
if (!function_exists(SimpleParse))
{
function SimpleParse($file, $csvtemp = "")
{
global $DEFAULTS;
// Lese Content-File ein
$cofi = file("$file");
// einen DUMMY für ersten Text übernehmen
$vol = "DUMMY";
//durch die Zeilen iterieren und zuordung aus
//geklammerten anweisungen lesen
while( list($key,$val) = each($cofi))
{
if (strlen(trim($val)) == 0) $val = trim($val);
//Start gefunden ?
if ( substr($val,0,1) == $DEFAULTS->START_FIELD )
{
//Das Ende der Klammer finden
$end = strpos($val,$DEFAULTS->STOP_FIELD,1);
//Den Inhalt der Klammer als Key übernehmen
$vol = trim(substr($val,1, $end-1));
//Den Rest der Zeile dem Key zuordnen
$stack[$vol] .= trim(substr($val,$end+1));
}
else
{
//Zeile dem aktuellen Key zuordnen
if (strlen(trim($val)) > 0) $stack[$vol] .= $val;
}
}
//Array zurückgeben
return $stack;
}
}
/*
* Eine Funktion zum Replacen von Content in Template-Files
* $text = Template
* $replaces = Array of Replacements
*/
if (!function_exists(simpleReplace))
{
function simpleReplace($text, $replaceArray)
{
global $DEFAULTS;
//$text = "";
$startTag = $DEFAULTS->START_FIELD;
$endTag = $DEFAULTS->STOP_FIELD;
$count = 0;
$startPos = strpos($text, $startTag, $count);
while( $startPos > 0)
{
$startPos = $startPos + strlen($startTag);
$endPos = strpos($text,$endTag,$startPos);
if ( $endPos > 0)
{
$tag = trim(substr($text, $startPos, ($endPos-$startPos) ) );
if ($replaceArray[$tag])
{
// if tag found then replace it with content of this tag
$text = substr($text,0,$startPos-1) . $replaceArray[$tag] . substr($text,$endPos+1);
$count = $startPos + strlen($replaceArray[$tag]);
}
else
{
// if tag NOT found then delete it
//$text = substr($text,0,$startPos) . substr($text,$endPos);
$count = $endPos;
}
}
else
{
break;
}
$startPos = strpos($text,$startTag,$count);
}
return $text;
}
}
if (!function_exists(concatConfig))
{
function concatConfig($PageContent, $linenumbers)
{
// paste config-lines to one string
$temp = '';
for ($i = 0; $i < $linenumbers; $i++)
{
$temp .= $PageContent->CSVIMPORT[$i];
}
// remove all whitespace-characters like blank, tabs, crlf, ...
$temp = eregi_replace("[\n\r\t]+", " ", $temp);
return($temp);
}
}
if (!function_exists(getLineNumbers))
{
function getLineNumbers($PageContent)
{
// count number of config-lines
if (isset($PageContent->CSVIMPORT))
{
$linenumbers = count($PageContent->CSVIMPORT);
}
else
{
$linenumbers = 0;
}
return($linenumbers);
}
}
if (!function_exists(SplitLine))
{
function SplitLine($Line, $StartValue, $EndValue)
{
$PosStartValue = strpos ( $Line, $StartValue );
$Result[0] = substr ( $Line, 0, $PosStartValue );
$temp = substr ( $Line, $PosStartValue + strlen ($StartValue));
$PosEndValue = strpos ( $temp, $EndValue);
$Result[1] = substr ( $temp, 0, $PosEndValue);
$Result[2] = substr ( $temp, $PosEndValue + strlen ( $EndValue ) );
return $Result;
}
}
if (!function_exists(getPara))
{
function getPara($paraname, $string, $default="")
{
$paraname .= '="';
if ( stristr( $string, $paraname ))
{
$temp = trim ( substr ( $string, strpos ( $string, $paraname ) + strlen($paraname) ) );
$temp = trim ( substr ($temp, 0, strpos ( $temp, '"' ) ) );
}
else
{
$temp = $default;
}
return($temp);
}
}
if (!function_exists(readSection))
{
function readSection($name, $tplArray, $section)
{
$section = $name . '.'. $section ;
// read section-area
if ($tplArray[$section])
{
$temp = simpleReplace($tplArray[$section], $tplArray);
}
return($temp);
}
}
//=========================================================
// read configuration out of content-file
$linenumbers = getLineNumbers($PageContent);
// if there is a config-line for the csv-import.plugin, then do something
If ($linenumbers > 0)
{
$configstring = concatConfig($PageContent, $linenumbers);
// isolate the config-part and do not care about the rest of the string
list ($PartOne, $configstring, $PartTwo) = SplitLine($configstring, '<CSVIMPORT', '>');
// parse configuration
$name = getPara('NAME', $configstring, $name);
$datafile = getPara('CONTENT', $configstring, $datafile);
$templatefile = getPara('TEMPLATE', $configstring, $templatefile);
$fields = getPara('FIELDS', $configstring, $fields);
$doReplTags = getPara('DOTAGS', $configstring, $doReplTags);
$headlines = getPara('HEADLINES', $configstring, $headlines);
$grouplines = getPara('GROUPLINES', $configstring, $grouplines);
$csvtemp = getPara('CSVTEMP', $configstring, $csvtemp);
$separator = getPara('SEPERATOR', $configstring, $separator);
$separator = getPara('SEPARATOR', $configstring, $separator);
$alternate = getPara('ALTERNATE', $configstring, $alternate);
// extract fields
$field = explode($separator, $fields);
// add DocRoot to file, because file has to be with full path
$templatefile = $PHP->GetDocRoot() . $templatefile;
//echo "templatefile: #" . $templatefile . "#<BR>\n";
// read csv-temlate-file and parse sections into array
if (file_exists("$templatefile")) $tplArray = SimpleParse("$templatefile", $csvtemp);
// if csvtemp is defined then use this special template
if (strlen($csvtemp) > 0)
{
$section = $csvtemp;
}
else
{
$section = $name;
}
$tempOutputPre = readSection($section, $tplArray, 'PRE');
$tempOutputHead = readSection($section, $tplArray, 'HEAD');
$tempOutputNormal = readSection($section, $tplArray, 'NORMAL');
$tempOutputAlternate = readSection($section, $tplArray, 'ALTERNATE'); // Version 1.0.2: 30.12.2001 by Carsten Seiffert
$tempOutputPast = readSection($section, $tplArray, 'PAST');
// number of lines in csv-files
$y = 0;
$inHeading = TRUE;
$tempGroupLines = 0;
$temp= "";
$inputfile = $PHP->GetDocRoot() . $datafile;
//echo "inputfile: #" . $inputfile . "#<BR>\n";
if ($datafile && file_exists($inputfile))
{
// open csv-file
$fp = fopen ($inputfile, "r");
$output = $tempOutputPre;
// read data from csv-file into array $data
while (($loopdata = fgetcsv ($fp, $maxLineSize, $separator)) || (!$loopdata && strlen($temp) > 0))
{
// increment line-number
$y++;
// copy template into temporary string
If ($y <= $headlines)
{
// if still in heading-area
$temp = $tempOutputHead;
$inHeading = TRUE;
}
Else
{
// in content-area
// Version 1.0.2: 30.12.2001 Carsten Seiffert:
// differ between odd and even lines
$inHeading = FALSE;
If (($alternate > 1) && !($y % $alternate))
{
$temp = $tempOutputAlternate;
}
Else
{
$temp = $tempOutputNormal;
}
}
if (!$inHeading)
{
$tempGroupLines++;
// reset temporary number of group-lines, if greater than group-lines
if ($tempGroupLines > $grouplines)
{
$tempGroupLines = 1;
}
}
// count number of fields in array field
$num = count ($field);
// FOR-Schleife, um Felder des Arrays auszugeben
for ($x=0; $x<$num; $x++)
{
$data[$x] = trim($loopdata[$x]);
// if field is empty, then use default string
// 08.06.2001: "0"-Bugfix
if ((!$data[$x] && $data[$x] == NULL) || (strlen($data[$x]) == 0 ))
{
$data[$x] = trim($default_text);
}
if ($loopdata)
if (strlen($tempdata[$x]) > 0) $tempdata[$x] .= "<BR>" . $data[$x];
else $tempdata[$x] = $data[$x];
//if (!$inHeading) echo "tempdata[$x]: #" . $tempdata[$x] . "#<BR>\n";
// generate field-name
$replacetag = $field[$x];
if ((!$replacetag) || (strlen($replacetag) == 0))
{
$replacetag = 'FIELD.' . ($x + 1) ;
$templatetag = $data[$x];
}
else
{
$templatetag = $replacetag;
$replacetag .= '.' . $y ;
}
if ($inHeading || $tempGroupLines >= $grouplines || (!$loopdata && strlen($temp) > 0))
{
$replacetag = '<' . $name . '.' . $replacetag . '>' ;
$templatetag = $DEFAULTS->START_FIELD . $templatetag . $DEFAULTS->STOP_FIELD ;
// replace template-tags with the values
$temp = str_replace($templatetag, $tempdata[$x], $temp);
// add new tag to tag-list
If (strtoupper($doReplTags) == "ON")
{
// get number of tags
$tagCount = count($Tags);
$Tags[$tagCount][0] = $replacetag;
$Tags[$tagCount][1] = $tempdata[$x];
$tagCount++;
}
}
}
if ($inHeading || $tempGroupLines >= $grouplines || (!$loopdata && strlen($temp) > 0))
{
$output .= $temp;
$temp = "";
// reset temporary data-strings
//echo "DEL: tempdata: ";
for ($x=0; $x<count($tempdata); $x++)
{
//echo "#" . $tempdata[$x] . "# ";
$tempdata[$x] = "";
}
//echo "<BR>\n";
}
}
fclose ($fp);
$output .= $temp;
$output .= $tempOutputPast;
}
// if content is empty then assume the content to be the default-text
if (strlen(trim($output)) == 0 )
{
$output = trim($default_text);
}
// add placeholder to tag-list
$current=count($Tags);
$Tags[$current][0]='<CSVIMPORT_' . $name . '>';
$Tags[$current][1]=$output;
// add string to content-field
$Current = count($PageContent->CSVIMPORT);
$PageContent->CSVIMPORT[$Current] = $output;
}
}
?>