cvs: pear /Translation translation.str_mgmt.php

From: Date: Mon, 05 Jan 2004 01:24:34 +0000
Subject: cvs: pear /Translation translation.str_mgmt.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-15354@lists.php.net to get a copy of this message
quipo Sun Jan 4 20:24:34 2004 EDT Modified files: /pear/Translation translation.str_mgmt.php Log: allow custom table definitions in admin class too (work in progress...)

Index: pear/Translation/translation.str_mgmt.php diff -u pear/Translation/translation.str_mgmt.php:1.1 pear/Translation/translation.str_mgmt.php:1.2 --- pear/Translation/translation.str_mgmt.php:1.1 Mon Dec 1 14:37:05 2003 +++ pear/Translation/translation.str_mgmt.php Sun Jan 4 20:24:34 2004 @@ -1,113 +1,250 @@ -<?php -/** -* Additional functions for Translation class -* -* Functions allowing user to create the new language, translation for specific strings and full management of the languages database. -* -* @autor Wojciech Zieliñski <voyteck@caffe.com.pl> -* @version 1.2 -* @access public -* @package Translation -*/ - -require_once 'DB.php'; - -/** - * New language creation - * - * Creates new language in the system. Creates language entry in the languages table and the table for language strings. If other languages has been created before and their tables were filled with strings, function addTranslation should be executed for each of the added strings just after calling this function and before using the Translation class for any purpose. - * @param string $LangID Language identifier - * @param string $LangName Language name - it is preferred so this name should be in language, which it describes. This name can be later retrieved by calling getLangName and getOtherLangs methods and used for hyperlinks changing the site language. - * @param string $METATags Tags that may describe the language codepage etc. These tags can be retrieved by calling getMetaTags method. - * @param string pear_DSN PEAR DSN string for database connection - * @return integer $result 1 if everything went OK or PEAR DB_Error object if something goes wrong. - */ -function createNewLang($LangID, $LangName, $METATags, $pear_DSN) { - $db = DB::connect($pear_DSN); - if (DB::isError($db)) - return $db; - $result = $db->query("INSERT INTO tr_langsavail (lang_id, name, metatags) - VALUES (\"$LangID\", \"$LangName\", \"$METATags\")"); - if (DB::isError($result)) - return $result; - $result = $db->query("CREATE TABLE tr_strings_$LangID - (page_id varchar(16) default NULL, - string_id varchar(32) NOT NULL, - string longtext, - UNIQUE KEY page_id (page_id, string_id))"); - if (DB::isError($result)) { - $delresult = $db->query("DELETE FROM tr_langsavail - WHERE lang_id=\"$LangID\""); - return $result; - } - return 1; -} - -/** - * Language removal - * - * Removes language from system. This function should be used with much carefull - this will permanently remove all the strings that has been added to language table by dropping this table. - * @param string $LangID Language identifier - * @param string $pear_DSN PEAR DSN string for database connection - * @return integer $result 1 if everything went OK or PEAR DB_Error object if something goes wrong. - */ -function removeLang($LangID, $pear_DSN) { - $db = DB::connect($pear_DSN); - if (DB::isError($db)) - return $db; - $result = $db->query("DROP TABLE tr_strings_$LangID"); - if (DB::isError($result)) - return $result; - $result = $db->query("DELETE FROM tr_langsavail WHERE lang_id=\"$langID\""); - if (DB::isError($result)) - return $result; - return 1; -} - -/** - * Translation adding - * - * Adds string to one or more language tables. - * @param string $PageID page identifier. Might be "" if the string is to be available from any page, independendly from translation object creation parameters. - * @param string $StringID string identifier. Must be unique for the same PageID and strings that were created without PageID's. This rule must be used to prevent the situation of 2 same StringID's in one Translation object. - * @param array string $String array of strings - the array keys should be languages id's, the values - the sttrings in these languages - e.g.: ("en"->"English text", "pl"->"Tekst polski", ...) - * @param string $pear_DSN PEAR DSN string for database connection - * @return integer $result 1 if everything went OK or PEAR DB_Error object if something goes wrong - */ -function addTranslation($PageID, $StringID, $String, $pear_DSN) { - $db = DB::connect($pear_DSN); - if (DB::isError($db)) - return $db; - foreach ($String as $LangID => $Text) - $data[] = array("tr_strings_$LangID", $Text); - $result = $db->executeMultiple(($db->prepare("INSERT INTO ! (page_id, string_id, string) VALUES ('$PageID', '$StringID', ?)")), $data); - if (DB::isError($result)) - return $result; - return 1; -} - -/** - * Translation removal - * - * Removes string from all of string tables - * @param string $PageID page identifier. - * @param string $StringID string identifier. - * @param string $pear_DSN PEAR DSN string for database connection - * @return integer $result 1 if everything went OK or PEAR DB_Error object if something goes wrong - */ -function removeTranslation($PageID, $StringID, $pear_DSN) { - $db = DB::connect($pear_DSN); - if (DB::isError($db)) - return $db; - $result = $db->query("SELECT lang_id FROM tr_langsavail"); - if (DB::isError($result)) - return $result; - while ($row = $result->fetchRow()) - $languages[] = "tr_strings_" . $row[0]; - //print_r($languages); - $result = $db->executeMultiple(($db->prepare("DELETE FROM ! WHERE page_id = '$PageID' and string_id = '$StringID'")), $languages); - if (DB::isError($result)) - return $result; - return $result; -} +<?php +/** +* Additional functions for Translation class +* +* Functions allowing user to create the new language, translation for specific +* strings and full management of the languages database. +* +* @author Wojciech Zieliñski <voyteck@caffe.com.pl> +* @author Lorenzo Alberton <l dot alberton at quipo dot it> +* @version 1.3 +* @access public +* @package Translation +*/ + +require_once 'DB.php'; + + +/** + * helper method + */ +function setDefaultTableDefinitions($LangID, $LangName, $CustomTables) +{ + //set defaults + $TableDefinitions = array( + 'langsavail' => array( + 'name' => 'tr_langsavail', + 'lang_id' => 'lang_id', + 'lang_name' => 'name', + 'metatags' => 'metatags', + 'errortext' => 'errortext' + ), + 'strings_'.$LangID => array( + 'name' => 'strings_'.$LangID, + 'page_id' => 'page_id', + 'string_id' => 'string_id', + 'string' => 'string' + ) + ); + if (is_array($CustomTables['langsavail'])) { + $TableDefinitions['langsavail']['name'] = isset($CustomTables['langsavail']['name']) ? $CustomTables['langsavail']['name'] : 'tr_langsavail'; + $TableDefinitions['langsavail']['lang_id'] = isset($CustomTables['langsavail']['lang_id']) ? $CustomTables['langsavail']['lang_id'] : 'lang_id'; + $TableDefinitions['langsavail']['lang_name'] = isset($CustomTables['langsavail']['lang_name']) ? $CustomTables['langsavail']['lang_name'] : 'lang_name'; + $TableDefinitions['langsavail']['metatags'] = isset($CustomTables['langsavail']['metatags']) ? $CustomTables['langsavail']['metatags'] : 'metatags'; + $TableDefinitions['langsavail']['errortext'] = isset($CustomTables['langsavail']['errortext']) ? $CustomTables['langsavail']['errortext'] : 'errortext'; + } elseif (!empty($CustomTables['langsavail'])) { + $TableDefinitions['langsavail']['name'] = $CustomTables['langsavail']; + } + + if (is_array($CustomTables['strings_'.$LangID])) { + $TableDefinitions['strings_'.$LangID]['name'] = isset($CustomTables['strings_'.$LangID]['name']) ? $CustomTables['strings_'.$LangID]['name'] : 'strings_'.$LangID; + $TableDefinitions['strings_'.$LangID]['page_id'] = isset($CustomTables['strings_'.$LangID]['page_id']) ? $CustomTables['strings_'.$LangID]['page_id'] : 'page_id'; + $TableDefinitions['strings_'.$LangID]['string_id'] = isset($CustomTables['strings_'.$LangID]['string_id']) ? $CustomTables['strings_'.$LangID]['string_id'] : 'string_id'; + $TableDefinitions['strings_'.$LangID]['string'] = isset($CustomTables['strings_'.$LangID]['string']) ? $CustomTables['strings_'.$LangID]['string'] : 'string'; + } elseif (!empty($CustomTables['strings_'.$LangID])) { + $TableDefinitions['strings_'.$LangID]['name'] = $CustomTables['strings_'.$LangID]; + } + return $TableDefinitions; +} + + +/** + * New language creation + * + * Creates new language in the system. + * Creates lang entry in the languages table and the table for language strings. + * If other langs have been created before and their tables were filled with + * strings, function addTranslation should be executed for each of the added + * strings just after calling this function and before using the Translation class + * for any purpose. + * + * @param string $LangID Language identifier + * @param string $LangName Language name - store the language name of the lang, + * possibly in the language described. This name can be later + * retrieved by calling getLangName and getOtherLangs methods + * and used for hyperlinks changing the site language. + * @param string $METATags Tags that may describe the language codepage etc. + * These tags can be retrieved by calling getMetaTags method. + * @param string $pear_DSN PEAR DSN string for database connection + * @param array $CustomTables Custom table definitions + * @return mixed Return 1 if everything went OK, a PEAR::DB_Error object if not. + */ +function createNewLang($LangID, $LangName, $METATags, $pear_DSN, $CustomTables=0) +{ + $TableDefinitions = setDefaultTableDefinitions($LangID, $LangName, $CustomTables); + + $db = DB::connect($pear_DSN); + if (DB::isError($db)) { + return $db; + } + $query = sprintf('INSERT INTO %s (%s, %s, %s) VALUES (%s, %s, %s)', + $TableDefinitions['langsavail']['name'], + $TableDefinitions['langsavail']['lang_id'], + $TableDefinitions['langsavail']['lang_name'], + $TableDefinitions['langsavail']['lang_metatags'], + addslashes($LangID), + addslashes($LangName), + addslashes($METATags) + ); + $result = $db->query($query); + if (DB::isError($result)) { + return $result; + } + + //check if the table already exists + $exists = false; + $res = $db->query('SHOW TABLES'); + if (DB::isError($res)) { + return $res; + } + while ($row = $res->fetchRow()) { + if ($row[0] == 'tr_strings_'.$LangID) { + $exists = true; + break; + } + } + if (!$exists) { + $query = 'CREATE TABLE '. $TableDefinitions['strings_'.$LangID]['name'] .'(' + . $TableDefinitions['strings_'.$LangID]['page_id'] .' VARCHAR(16) default NULL, ' + . $TableDefinitions['strings_'.$LangID]['string_id'].' VARCHAR(32) NOT NULL, ' + . $TableDefinitions['strings_'.$LangID]['string'] .' TEXT, ' + .'UNIQUE KEY page_id ('.$TableDefinitions['strings_'.$LangID]['page_id'].', ' + .$TableDefinitions['strings_'.$LangID]['string_id'].'))'; + $result = $db->query($query); + if (DB::isError($result)) { + $query = sprintf('DELETE FROM %s WHERE %s=%s', + $TableDefinitions['langsavail']['name'], + $TableDefinitions['langsavail']['lang_id'], + addslashes($LangID) + ); + $delresult = $db->query($query); + return $result; + } + } + return 1; +} + +/** + * Language removal + * + * Removes language from system. + * This function should be used carefully - it will permanently remove all the + * strings that has been added to the language table by dropping this table. + * If other langs are stored in the table, then only this lang column will be dropped. + * + * @param string $LangID Language identifier + * @param string $pear_DSN PEAR DSN string for database connection + * @param array $CustomTables Custom table definitions + * @param boolean $force If true, the table is dropped without checks + * @return mixed Return 1 if everything went OK, a PEAR::DB_Error object if not. + */ +function removeLang($LangID, $pear_DSN, $CustomTables=0, $force=false) +{ + $db = DB::connect($pear_DSN); + if (DB::isError($db)) { + return $db; + } + + $TableDefinitions = setDefaultTableDefinitions($LangID, $LangName, $CustomTables); + + if (!$force) { + //check if other langs are stored in this table + //'DESCRIBE' == 'SHOW COLUMNS FROM' + $res = $db->query('DESCRIBE '.$TableDefinitions['strings_'.$LangID]['name']); + if (DB::isError($res)) { + return $res; + } + if ($res->numRows() > 3) { + $query = 'ALTER TABLE '. $TableDefinitions['strings_'.$LangID]['name'] + .' DROP COLUMN '. $TableDefinitions['strings_'.$LangID]['string']; + $res = $db->query('DESCRIBE '.$TableDefinitions['strings_'.$LangID]['name']); + if (DB::isError($res)) { + return $res; + } + return 1; + } + } + + $result = $db->query('DROP TABLE '.$TableDefinitions['strings_'.$LangID]['name']); + if (DB::isError($result)) { + return $result; + } + $query = sprintf('DELETE FROM %s WHERE %s=%s', + $TableDefinitions['langsavail']['name'], + $TableDefinitions['langsavail']['lang_id'], + $LangID); + $result = $db->query($query); + if (DB::isError($result)) { + return $result; + } + return 1; +} + +/** + * Translation adding + * Adds string to one or more language tables. + * + * @param string $PageID page identifier. Might be "" if the string is to be + * available from any page, independendly from translation + * object creation parameters. + * @param string $StringID string identifier. Must be unique for the same PageID + * and strings that were created without PageID's. + * This rule must be respected to prevent ambiguities. + * @param array $String array of strings - the array keys should be languages id's, + * the values - the sttrings in these languages - e.g.: + * ("en"->"English text", "pl"->"Tekst polski", ...) + * @param string $pear_DSN PEAR DSN string for database connection + * @return mixed Return 1 if everything went OK, a PEAR::DB_Error object if not. + */ +function addTranslation($PageID, $StringID, $String, $pear_DSN) +{ + $db = DB::connect($pear_DSN); + if (DB::isError($db)) { + return $db; + } + foreach ($String as $LangID => $Text) { + $data[] = array("tr_strings_$LangID", $Text); + } + $result = $db->executeMultiple(($db->prepare("INSERT INTO ! (page_id, string_id, string) VALUES ('$PageID', '$StringID', ?)")), $data); + if (DB::isError($result)) { + return $result; + } + return 1; +} + +/** + * Translation removal + * + * Removes string from all of string tables + * @param string $PageID page identifier. + * @param string $StringID string identifier. + * @param string $pear_DSN PEAR DSN string for database connection + * @return integer $result 1 if everything went OK or PEAR DB_Error object if something goes wrong + */ +function removeTranslation($PageID, $StringID, $pear_DSN) { + $db = DB::connect($pear_DSN); + if (DB::isError($db)) + return $db; + $result = $db->query("SELECT lang_id FROM tr_langsavail"); + if (DB::isError($result)) + return $result; + while ($row = $result->fetchRow()) + $languages[] = "tr_strings_" . $row[0]; + //print_r($languages); + $result = $db->executeMultiple(($db->prepare("DELETE FROM ! WHERE page_id = '$PageID' and string_id = '$StringID'")), $languages); + if (DB::isError($result)) + return $result; + return $result; +} + ?> \ No newline at end of file
« previous php.pear.cvs (#15354) next »