Fixed warning message about an array value being undefined.

This commit is contained in:
paul 2006-09-01 08:26:32 +00:00
parent 8374bea772
commit 3be0939b41

View file

@ -6,10 +6,10 @@
/** /**
* Abstract interface for the localizer to access data from different sources. * Abstract interface for the localizer to access data from different sources.
* @package Campware * @package Campware
* @abstract * @abstract
*/ */
class LocalizerFileFormat { class LocalizerFileFormat {
function load(&$p_localizerLanguage) { } function load(&$p_localizerLanguage) { }
function save(&$p_localizerLanguage) { } function save(&$p_localizerLanguage) { }
function getFilePath($p_localizerLanguage) { } function getFilePath($p_localizerLanguage) { }
} // class LocalizerFileFormat } // class LocalizerFileFormat
@ -27,17 +27,17 @@ class LocalizerFileFormat_GS extends LocalizerFileFormat {
* *
* @return boolean * @return boolean
*/ */
function load(&$p_localizerLanguage) function load(&$p_localizerLanguage)
{ {
global $g_localizerConfig; global $g_localizerConfig;
$p_localizerLanguage->setMode('gs'); $p_localizerLanguage->setMode('gs');
$filePath = LocalizerFileFormat_GS::GetFilePath($p_localizerLanguage); $filePath = LocalizerFileFormat_GS::GetFilePath($p_localizerLanguage);
//echo "Loading $filePath<br>"; //echo "Loading $filePath<br>";
if (file_exists($filePath)) { if (file_exists($filePath)) {
$lines = file($filePath); $lines = file($filePath);
foreach ($lines as $line) { foreach ($lines as $line) {
if (strstr($line, "regGS")) { if (strstr($line, "regGS")) {
$line = preg_replace('/regGS/', '$p_localizerLanguage->registerString', $line); $line = preg_replace('/regGS/', '$p_localizerLanguage->registerString', $line);
$success = eval($line); $success = eval($line);
if ($success === FALSE) { if ($success === FALSE) {
echo "Error evaluating: ".htmlspecialchars($line)."<br>"; echo "Error evaluating: ".htmlspecialchars($line)."<br>";
@ -48,10 +48,10 @@ class LocalizerFileFormat_GS extends LocalizerFileFormat {
} }
else { else {
return false; return false;
} }
} // fn load } // fn load
/** /**
* Save the translation table to a PHP-GS file. * Save the translation table to a PHP-GS file.
* *
@ -61,7 +61,7 @@ class LocalizerFileFormat_GS extends LocalizerFileFormat {
* @return string * @return string
* File contents * File contents
*/ */
function save(&$p_localizerLanguage) function save(&$p_localizerLanguage)
{ {
global $g_localizerConfig; global $g_localizerConfig;
$data = "<?php\n"; $data = "<?php\n";
@ -76,42 +76,42 @@ class LocalizerFileFormat_GS extends LocalizerFileFormat {
$filePath = LocalizerFileFormat_GS::GetFilePath($p_localizerLanguage); $filePath = LocalizerFileFormat_GS::GetFilePath($p_localizerLanguage);
//echo $filePath; //echo $filePath;
$p_localizerLanguage->_setSourceFile($filePath); $p_localizerLanguage->_setSourceFile($filePath);
// Create the language directory if it doesnt exist. // Create the language directory if it doesnt exist.
$dirName = $g_localizerConfig['TRANSLATION_DIR'].'/'.$p_localizerLanguage->getLanguageCode(); $dirName = $g_localizerConfig['TRANSLATION_DIR'].'/'.$p_localizerLanguage->getLanguageCode();
if (!file_exists($dirName)) { if (!file_exists($dirName)) {
mkdir($dirName); mkdir($dirName);
} }
// write data to file // write data to file
if (PEAR::isError(File::write($filePath, $data, FILE_MODE_WRITE))) { if (PEAR::isError(File::write($filePath, $data, FILE_MODE_WRITE))) {
echo "<br>error writing file<br>"; echo "<br>error writing file<br>";
return FALSE; return FALSE;
} }
File::close($filePath, FILE_MODE_WRITE); File::close($filePath, FILE_MODE_WRITE);
return $data; return $data;
} // fn save } // fn save
/** /**
* Get the full path to the translation file. * Get the full path to the translation file.
* @param LocalizerLanguage $p_localizerLanguage * @param LocalizerLanguage $p_localizerLanguage
* @return string * @return string
*/ */
function getFilePath($p_localizerLanguage) function getFilePath($p_localizerLanguage)
{ {
global $g_localizerConfig; global $g_localizerConfig;
return $g_localizerConfig['TRANSLATION_DIR'].'/'.$p_localizerLanguage->getLanguageCode() return $g_localizerConfig['TRANSLATION_DIR'].'/'.$p_localizerLanguage->getLanguageCode()
.'/'.$p_localizerLanguage->getPrefix().'.php'; .'/'.$p_localizerLanguage->getPrefix().'.php';
} // fn getFilePath } // fn getFilePath
/** /**
* Get all supported languages as an array of LanguageMetadata objects. * Get all supported languages as an array of LanguageMetadata objects.
* @return array * @return array
* An array of LanguageMetadata * An array of LanguageMetadata
*/ */
function getLanguages() function getLanguages()
{ {
/* /*
global $Campsite; global $Campsite;
@ -125,7 +125,7 @@ class LocalizerFileFormat_GS extends LocalizerFileFormat {
} }
*/ */
global $languages; global $languages;
$metadata = array(); $metadata = array();
foreach ($languages as $language) { foreach ($languages as $language) {
$tmpMetadata =& new LanguageMetadata(); $tmpMetadata =& new LanguageMetadata();
@ -138,7 +138,7 @@ class LocalizerFileFormat_GS extends LocalizerFileFormat {
} }
return $metadata; return $metadata;
} // fn getLanguages } // fn getLanguages
} // class LocalizerFileFormat_GS } // class LocalizerFileFormat_GS
@ -150,46 +150,46 @@ class LocalizerFileFormat_XML extends LocalizerFileFormat {
var $m_unserializeOptions = array(); var $m_unserializeOptions = array();
var $l_serializeOptions = array(); var $l_serializeOptions = array();
var $l_unserializeOptions = array(); var $l_unserializeOptions = array();
function LocalizerFileFormat_XML() function LocalizerFileFormat_XML()
{ {
global $g_localizerConfig; global $g_localizerConfig;
$this->m_serializeOptions = array( $this->m_serializeOptions = array(
// indent with tabs // indent with tabs
"indent" => "\t", "indent" => "\t",
// root tag // root tag
"rootName" => "translations", "rootName" => "translations",
// tag for values with numeric keys // tag for values with numeric keys
"defaultTagName" => "item", "defaultTagName" => "item",
"keyAttribute" => "position", "keyAttribute" => "position",
"addDecl" => true, "addDecl" => true,
"encoding" => $g_localizerConfig['FILE_ENCODING'], "encoding" => $g_localizerConfig['FILE_ENCODING'],
"indentAttributes" => true "indentAttributes" => true
); );
$this->l_serializeOptions = array( $this->l_serializeOptions = array(
// indent with tabs // indent with tabs
"indent" => "\t", "indent" => "\t",
// root tag // root tag
"rootName" => "languages", "rootName" => "languages",
// tag for values with numeric keys // tag for values with numeric keys
"defaultTagName" => "item", "defaultTagName" => "item",
"keyAttribute" => "position", "keyAttribute" => "position",
"addDecl" => true, "addDecl" => true,
"encoding" => $g_localizerConfig['FILE_ENCODING'], "encoding" => $g_localizerConfig['FILE_ENCODING'],
"indentAttributes" => true "indentAttributes" => true
); );
} }
/** /**
* Read an XML-format translation file into the translation table. * Read an XML-format translation file into the translation table.
* @param LocalizerLanguage $p_localizerLanguage * @param LocalizerLanguage $p_localizerLanguage
* @return boolean * @return boolean
*/ */
function load(&$p_localizerLanguage) function load(&$p_localizerLanguage)
{ {
global $g_localizerConfig; global $g_localizerConfig;
$p_localizerLanguage->setMode('xml'); $p_localizerLanguage->setMode('xml');
$filePath = LocalizerFileFormat_XML::GetFilePath($p_localizerLanguage); $filePath = LocalizerFileFormat_XML::GetFilePath($p_localizerLanguage);
if (file_exists($filePath)) { if (file_exists($filePath)) {
@ -200,7 +200,7 @@ class LocalizerFileFormat_XML extends LocalizerFileFormat {
$translationArray = $unserializer->getUnserializedData(); $translationArray = $unserializer->getUnserializedData();
if ($g_localizerConfig['ORDER_KEYS']) $translationArray['item'] = $this->_xSortArray($translationArray['item'], 'key'); if ($g_localizerConfig['ORDER_KEYS']) $translationArray['item'] = $this->_xSortArray($translationArray['item'], 'key');
$p_localizerLanguage->clearValues(); $p_localizerLanguage->clearValues();
if (isset($translationArray['item'])) { if (isset($translationArray['item'])) {
foreach ($translationArray['item'] as $translationPair) { foreach ($translationArray['item'] as $translationPair) {
@ -208,13 +208,13 @@ class LocalizerFileFormat_XML extends LocalizerFileFormat {
} }
} }
return true; return true;
} }
else { else {
return false; return false;
} }
} // fn load } // fn load
/** /**
* Write a XML-format translation file. * Write a XML-format translation file.
* @param LocalizerLanguage $p_localizerLanguage * @param LocalizerLanguage $p_localizerLanguage
@ -222,7 +222,7 @@ class LocalizerFileFormat_XML extends LocalizerFileFormat {
* The XML that was written on success, * The XML that was written on success,
* FALSE on error. * FALSE on error.
*/ */
function save(&$p_localizerLanguage) function save(&$p_localizerLanguage)
{ {
$saveData = array(); $saveData = array();
$saveData["Id"] = $p_localizerLanguage->getLanguageId(); $saveData["Id"] = $p_localizerLanguage->getLanguageId();
@ -232,36 +232,36 @@ class LocalizerFileFormat_XML extends LocalizerFileFormat {
$saveTranslationTable[] = array('key' => $key, 'value' => $value); $saveTranslationTable[] = array('key' => $key, 'value' => $value);
} }
$saveData = array_merge($saveData, $saveTranslationTable); $saveData = array_merge($saveData, $saveTranslationTable);
$serializer =& new XML_Serializer($this->m_serializeOptions); $serializer =& new XML_Serializer($this->m_serializeOptions);
$serializer->serialize($saveData); $serializer->serialize($saveData);
$xml = $serializer->getSerializedData(); $xml = $serializer->getSerializedData();
if (PEAR::isError($xml)) { if (PEAR::isError($xml)) {
echo "<br>error serializing data<br>"; echo "<br>error serializing data<br>";
return FALSE; return FALSE;
} }
$filePath = LocalizerFileFormat_XML::GetFilePath($p_localizerLanguage); $filePath = LocalizerFileFormat_XML::GetFilePath($p_localizerLanguage);
//echo "Saving as ".$this->m_filePath."<Br>"; //echo "Saving as ".$this->m_filePath."<Br>";
// write data to file // write data to file
if (PEAR::isError(File::write($filePath, $xml, FILE_MODE_WRITE))) { if (PEAR::isError(File::write($filePath, $xml, FILE_MODE_WRITE))) {
echo "<br>error writing file $filePath<br>"; echo "<br>error writing file $filePath<br>";
return FALSE; return FALSE;
} }
File::close($filePath, FILE_MODE_WRITE); File::close($filePath, FILE_MODE_WRITE);
return $xml; return $xml;
} // fn save } // fn save
/** /**
* Get the full path to the translation file. * Get the full path to the translation file.
* @param LocalizerLanguage $p_localizerLanguage * @param LocalizerLanguage $p_localizerLanguage
* @return string * @return string
*/ */
function getFilePath($p_localizerLanguage) function getFilePath($p_localizerLanguage)
{ {
global $g_localizerConfig; global $g_localizerConfig;
return $g_localizerConfig['TRANSLATION_DIR'].'/' return $g_localizerConfig['TRANSLATION_DIR'].'/'
@ -270,36 +270,36 @@ class LocalizerFileFormat_XML extends LocalizerFileFormat {
.'/'.$p_localizerLanguage->getPrefix().'.xml'; .'/'.$p_localizerLanguage->getPrefix().'.xml';
} // fn getFilePath } // fn getFilePath
/** /**
* Get all supported languages as an array of LanguageMetadata objects. * Get all supported languages as an array of LanguageMetadata objects.
* @return array * @return array
*/ */
function getLanguages($p_default=TRUE, $p_completed_only=FALSE) function getLanguages($p_default=TRUE, $p_completed_only=FALSE)
{ {
global $g_localizerConfig; global $g_localizerConfig;
$fileName = $g_localizerConfig['TRANSLATION_DIR'] $fileName = $g_localizerConfig['TRANSLATION_DIR']
.$g_localizerConfig['LANGUAGE_METADATA_FILENAME']; .$g_localizerConfig['LANGUAGE_METADATA_FILENAME'];
if (!file_exists($fileName)) { if (!file_exists($fileName)) {
echo "Connot read ".$g_localizerConfig['LANGUAGE_METADATA_FILENAME']; echo "Connot read ".$g_localizerConfig['LANGUAGE_METADATA_FILENAME'];
return FALSE; return FALSE;
} }
$xml = File::readAll($fileName); $xml = File::readAll($fileName);
File::rewind($fileName, FILE_MODE_READ); File::rewind($fileName, FILE_MODE_READ);
$handle =& new XML_Unserializer($this->l_unserializeOptions); $handle =& new XML_Unserializer($this->l_unserializeOptions);
$handle->unserialize($xml); $handle->unserialize($xml);
$arr = $handle->getUnserializedData(); $arr = $handle->getUnserializedData();
if (array_key_exists(0, $arr['item'])) { if (array_key_exists(0, $arr['item'])) {
$languages = $arr['item']; $languages = $arr['item'];
} else { } else {
$languages[0] = $arr['item']; $languages[0] = $arr['item'];
} }
foreach ($languages as $language) { foreach ($languages as $language) {
if(!$p_completed_only || $language['Completed']) { if (!$p_completed_only || (isset($language['Completed']) && $language['Completed'])) {
// just display default language in maintainance mode // just display default language in maintainance mode
if ($p_default || $language['Id'] !== $g_localizerConfig['DEFAULT_LANGUAGE']) { if ($p_default || $language['Id'] !== $g_localizerConfig['DEFAULT_LANGUAGE']) {
list ($langCode, $countryCode) = explode('_', $language['Id']); list ($langCode, $countryCode) = explode('_', $language['Id']);
@ -315,77 +315,77 @@ class LocalizerFileFormat_XML extends LocalizerFileFormat {
} }
return $return; return $return;
} // fn getLanguages } // fn getLanguages
function addLanguage($new) function addLanguage($new)
{ {
global $g_localizerConfig; global $g_localizerConfig;
$fileName = $g_localizerConfig['TRANSLATION_DIR'] $fileName = $g_localizerConfig['TRANSLATION_DIR']
.$g_localizerConfig['LANGUAGE_METADATA_FILENAME']; .$g_localizerConfig['LANGUAGE_METADATA_FILENAME'];
if (!file_exists($fileName)) { if (!file_exists($fileName)) {
echo "$fileName not found"; echo "$fileName not found";
return FALSE; return FALSE;
} }
$xml = File::readAll($fileName); $xml = File::readAll($fileName);
File::rewind($fileName, FILE_MODE_READ); File::rewind($fileName, FILE_MODE_READ);
$handle =& new XML_Unserializer($this->l_unserializeOptions); $handle =& new XML_Unserializer($this->l_unserializeOptions);
$handle->unserialize($xml); $handle->unserialize($xml);
$arr = $handle->getUnserializedData(); $arr = $handle->getUnserializedData();
if (array_key_exists(0, $arr['item'])) { if (array_key_exists(0, $arr['item'])) {
$languages = $arr['item']; $languages = $arr['item'];
} else { } else {
$languages[0] = $arr['item']; $languages[0] = $arr['item'];
} }
$languages[] = array( $languages[] = array(
'Id' => $new['Id'], 'Id' => $new['Id'],
'Name' => $new['Name'], 'Name' => $new['Name'],
'NativeName' => $new['NativeName'] 'NativeName' => $new['NativeName']
); );
$languages = $this->_xSortArray($languages, 'Id'); $languages = $this->_xSortArray($languages, 'Id');
$handle =& new XML_Serializer($this->l_serializeOptions); $handle =& new XML_Serializer($this->l_serializeOptions);
$handle->serialize($languages); $handle->serialize($languages);
if (!$xml = $handle->getSerializedData()) { if (!$xml = $handle->getSerializedData()) {
echo "Cannot serialize date"; echo "Cannot serialize date";
return FALSE; return FALSE;
} }
if (!File::write($fileName, $xml, FILE_MODE_WRITE)) { if (!File::write($fileName, $xml, FILE_MODE_WRITE)) {
echo "Cannot add langauge to file $fileName"; echo "Cannot add langauge to file $fileName";
return FALSE; return FALSE;
} }
// create the path/file to stor translations in // create the path/file to stor translations in
if (!mkdir($g_localizerConfig['TRANSLATION_DIR'].'/'.$new['Id'])) { if (!mkdir($g_localizerConfig['TRANSLATION_DIR'].'/'.$new['Id'])) {
echo "Cannot create path ".$g_localizerConfig['TRANSLATION_DIR'].'/'.$new['Id']; echo "Cannot create path ".$g_localizerConfig['TRANSLATION_DIR'].'/'.$new['Id'];
return FALSE; return FALSE;
} }
return TRUE; return TRUE;
} }
function _xSortArray($array, $key) function _xSortArray($array, $key)
{ {
if (!is_array($array) || !count($array)) { if (!is_array($array) || !count($array)) {
return; return;
} }
foreach($array as $k=>$v) { foreach($array as $k=>$v) {
$trans[$v[$key]] = $v; $trans[$v[$key]] = $v;
} }
ksort($trans); ksort($trans);
foreach ($trans as $v) { foreach ($trans as $v) {
$ret[] = $v; $ret[] = $v;
} }
return $ret; return $ret;
} }
} // class LocalizerFileFormat_XML } // class LocalizerFileFormat_XML
?> ?>