<?php
//****************************************************************************
//
// Copyright (C) 2001 Eric SEIGNE <hide@address.com>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
//****************************************************************************
//
// For more informations, get to the project's main source file.
//
// Description Main header for some useful functions
// Global constants and variables definitions.
//
//****************************************************************************
if(!defined("__functions_arch_php__")):
define("__functions_arch_php__", "1");
//****************************************************************************
//****************************************************************************
//
// XML archives of Bradabra related functions (nomenclature specific functions)
//
//****************************************************************************
//****************************************************************************
//----------------------------------------------------------------------------
// Bradabra XML file example
//----------------------------------------------------------------------------
/*
<?xml version="1.0" standalone="yes" ?>
<document reference="E20010903_000" date_year="2001" date_month="09" date_day="03" city="Gradignan" currency="FF" n_items="2">
<client>
LASTNAME Firstname
Address
postal_code City
COUNTRY
tél 00 00 00 00 00
fax 22 22 22 22 22
hide@address.com
</client>
<items>
<item_1 quantity="3" reference="ref_article" unit_price="100">
Description of item :
You can drop some item information here.
Accept multiline text.
</item_1>
<item_2 quantity="1" reference="tshirt1" unit_price="100">
T-Shirt "Foo"
</item_2>
</items>
</document>
*/
//----------------------------------------------------------------------------
// Constants
//----------------------------------------------------------------------------
$ArchXML_Nomenclature = array(
"document" => array(
array("reference", "attribute"),
array("date_year", "attribute"),
array("date_month", "attribute"),
array("date_day", "attribute"),
array("city", "attribute"),
array("currency", "attribute"),
array("n_items", "attribute")
),
"client" => array(
array("description", "section")
),
"items" => array(
array("quantity", "attribute"),
array("reference", "attribute"),
array("unit_price", "attribute"),
array("description", "section")
)
);
//----------------------------------------------------------------------------
// ArchXML_GetFileInfo
//----------------------------------------------------------------------------
function ArchXML_GetFileInfo ($file_content)
{
global $lang;
$fileinfo_array = FALSE;
$tag_header = MyXML_GetTagHeader($file_content, "document");
if($tag_header)
{
$reference = trim(MyXML_GetFieldValueInTagHeader($tag_header, "reference"));
$date_year = trim(MyXML_GetFieldValueInTagHeader($tag_header, "date_year"));
$date_month = trim(MyXML_GetFieldValueInTagHeader($tag_header, "date_month"));
$date_day = trim(MyXML_GetFieldValueInTagHeader($tag_header, "date_day"));
$type = strtoupper($reference[0]); //strtoupper(substr($reference, 0, 1));
if($type == ARCHIVEPREFIX_QUOTATION)
$type = $lang["Quotation"];
else if($type == ARCHIVEPREFIX_INVOICE)
$type = $lang["Invoice"];
$fileinfo_array = array(
"type" => $type,
"reference" => $reference,
"date_year" => $date_year,
"date_month" => $date_month,
"date_day" => $date_day
);
}
return $fileinfo_array;
}
//----------------------------------------------------------------------------
// ArchXML_GetItemsNumber
//----------------------------------------------------------------------------
function ArchXML_GetItemsNumber ($file_content)
{
$tag_header = MyXML_GetTagHeader($file_content, "document");
if(!$tag_header)
return FALSE;
$value = GetFieldValueInTagHeader($tag_header, "n_items");
return $value;
}
//****************************************************************************
//****************************************************************************
//
// Mini XML parser
//
//****************************************************************************
//****************************************************************************
//----------------------------------------------------------------------------
// MyXML_GetFieldValueInTagHeader - Typically called after a
// MyXML_GetTagHeader() call.
//
// " param1=\"value1\" param2=\"value2\">text</tag>"
// In the above string, and if $field_name is
// equal to "param2", this function will
// return : "value2".
//
// Returns FALSE on error.
//----------------------------------------------------------------------------
function MyXML_GetFieldValueInTagHeader ($tag_header, $field_name)
{
// get the field name and value : field_name="field_value (without the last " character)
if(!eregi($field_name."=\"[^\"]*", $tag_header, $result))
return FALSE;
// get the field value
$value = strstr($result[0], "\"");
$value = substr($value, 1);
return $value;
}
//----------------------------------------------------------------------------
// MyXML_GetTagHeader - If string is :
// "<tag param1=\"value1\" param2=\"value2\">text</tag>"
// this function will return :
// " param1=\"value1\" param2=\"value2\""
//
// If string is :
// "<tag>text</tag>"
// this function will return :
// ""
//
// Returns FALSE otherwise.
//----------------------------------------------------------------------------
function MyXML_GetTagHeader ($text, $tag_name)
{
if(eregi("<".$tag_name."[^>]*", $text, $result))
{
$ret = eregi_replace("<[^ ]*", "", $result[0]);
return $ret;
}
return FALSE;
}
//----------------------------------------------------------------------------
// MyXML_GetTagSection - "<tag>text</tag>"
// "<tag param=\"value\">text</tag>"
// In the two above strings, this function will return
// "text", for each one.
//
// Returns FALSE on error.
//----------------------------------------------------------------------------
function MyXML_GetTagSection ($text, $tag_name)
{
/*
// The cool regexp method
// [JC] - Is this method faster ?
//--------------------------------
$regexp_tag_header = "(<".$tag_name."[^>]*>)";
$regexp_tag_closer = "(</".$tag_name.">)";
if(!eregi($regexp_tag_header.".*?".$regexp_tag_closer, $text, $result))
return FALSE;
$section = eregi_replace("(".$regexp_tag_header."|".$regexp_tag_closer.")", "", $result[0]);
return $section;
*/
// The boring old method (C/C++ style)
//-------------------------------------
// remove the tag header : "<tag param=\"value\">"
$section = strstr($text, "<".$tag_name);
$section = strstr($section, ">");
$section = substr($section, 1);
// remove the tag closer : "</tag>"
$section = substr($section, 0, strpos($section, "</".$tag_name.">"));
return $section;
}
endif;
?>