Location: PHPKode > scripts > I18N class > i18n-class/langTable.class.php
<?php
    /*
       langTable.class.php - MySQL access class for i18n.class.php
       Copyright (C) 2005  Alan H. Lake

         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., 675 Mass Ave, Cambridge, MA 02139, USA.

       Current version: 1.0.1 (September 10, 2005)
    */

class LangTable_Class {
  var $host;
  var $user;
  var $password;
  var $db;
  var $table;

  function LangTable_Class($host,$user,$password,$db,$table) {
    $this->host = $host;
    $this->user = $user;
    $this->password = $password;
    $this->db = $db;
    $this->table = $table;
  }

  function CreateTable(){
    $sql = 'CREATE TABLE '.$this->table.' (
              Code       varchar(15)  NOT NULL default "",
              Identifier varchar(50)  NOT NULL default "",
              Value      text         NOT NULL default "",
             PRIMARY KEY (Code, Identifier)) TYPE=MyISAM;';
    if(!@mysql_query($sql)){
      $msg = mysql_error();
      die('Could not create table "'.$this->table.'".<br>'.$sql.'<br>'.$msg.'<br>');
    }
  }
  
  function GetLocales(){
    $oRfc1766 = $_SESSION['oRfc1766'];
    if(!isset($oRfc1766))
      die('$oRfc1766 is not set.');
    $sql = 'SELECT DISTINCT Code FROM '.$this->table;
    $result = @mysql_query($sql);
    if(!$result){
      $msg = mysql_error();
      die('"'.$sql.'" Failed.<br />.'.$msg.'<br />');
    }
    while($row = @mysql_fetch_object($result)){
      $code = $row->Code;
      if($oRfc1766->is_valid_tag($code)){
        $name = $oRfc1766->get_name($code);
        $locale["$code"] = $name;
      }else 
        die('LangTable: '.$code.' is not RFC1766 valid.<br>');
    }
    return($locale);
  }

    function GetValue($code,$identifier){
      $sql = 'SELECT * FROM '.$this->table.' WHERE Code = "'.$code.'" AND Identifier = "'.$identifier.'"';
      if(!$result = @mysql_query($sql)){
        $msg = mysql_error();
        die('"'.$sql.'" Failed.<br />.'.$msg.'<br />');
      }
      $rowsSelected = @mysql_num_rows($result);
      if($rowsSelected == 0)
        return "";
      $row = @mysql_fetch_object($result);
      $value = $row->Value;
      return($value);
    } // function GetValue($code,$identifier)


  function OpenDb(){
    $cnx = mysql_connect($this->host,$this->user,$this->password) or
      die("Could not connect: ".mysql_error());
    mysql_select_db($this->db) or
      die("Could not access ".$this->db.'.');
  } // function openDb()

  function ReadLocaleData($selectedLocale){
    $this->OpenDb();
    $sql = 'SELECT * FROM '.$this->table.' WHERE Code = "'.$selectedLocale.'"';
    $result = @mysql_query($sql);
    if(!$result){
      $msg = mysql_error();
      die('"'.$sql.'" Failed.<br />.'.$msg.'<br />');
    }
    while($row = @mysql_fetch_object($result)){
      $identifier = $row->Identifier;
      $value = addcslashes($row->Value,'"$
');
      $string[$identifier] = "$value);";
    }
    return($string);
  }

  function TableExists(){
    $result = mysql_list_tables($this->db);
    if(!$result)
      die('mysql_list_tables('.$this->db.') failed.<br>'.mysql_error().'<br>');
    while($row = mysql_fetch_row($result))  
      if($row[0] == $this->table)
        return(true);
    return(false);
  }

  function WriteLocaleData($selectedLocale,$identifier,$value){
    $this->OpenDb();
    $sql = 'SELECT * FROM '.$this->table.' WHERE ';
    $sql .= 'Code = "'.$selectedLocale.'" ';
    $sql .= 'AND Identifier = "'.$identifier.'"';
    $result = @mysql_query($sql);
    if(!$result){
      $msg = mysql_error();
      die('"'.$sql.'" Failed.<br />.'.$msg.'<br />');
    }
    $count = @mysql_num_rows($result);
    if($count > 0){
      $sql = 'UPDATE '.$this->table.' SET ';
      $sql .= 'Value = "'.$value.'" ';
      $sql .= 'WHERE Code = "'.$selectedLocale.'" ';
      $sql .= 'AND Identifier = "'.$identifier.'"';
    }else{
      $sql = 'INSERT INTO '.$this->table.' SET ';
      $sql .= 'Code = "'.$selectedLocale.'", ';
      $sql .= 'Identifier = "'.$identifier.'", ';
      $sql .= 'Value = "'.$value.'"';
    }
    if(!@mysql_query($sql)){
      $msg = mysql_error();
      die('"'.$sql.'" Failed.<br />.'.$msg.'<br />');
    }
  }
}
?>
Return current item: I18N class