Translation2
[ class tree: Translation2 ] [ index: Translation2 ] [ all elements ]

Source for file dataobjectsimple.php

Documentation is available at dataobjectsimple.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Contains the Translation2_Container_dataobjectsimple class
  6.  *
  7.  * PHP versions 4 and 5
  8.  *
  9.  * LICENSE: Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. The name of the author may not be used to endorse or promote products
  17.  *    derived from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
  20.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22.  * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
  23.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  *
  30.  * @category  Internationalization
  31.  * @package   Translation2
  32.  * @author    Alan Knowles <alan@akbkhome.com>
  33.  * @copyright 2004-2008 Alan Knowles
  34.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  35.  * @version   CVS: $Id: dataobjectsimple.php,v 1.17 2008/02/02 18:05:06 quipo Exp $
  36.  * @link      http://pear.php.net/package/Translation2
  37.  */
  38.  
  39. /**
  40.  * require Translation2_Container class and DB_DataObjects
  41.  */
  42. require_once 'Translation2/Container.php';
  43. require_once 'DB/DataObject.php';
  44.  
  45. /**
  46.  * Simple storage driver for fetching data from a db with DB_DataObject
  47.  *
  48.  * This storage driver can use all databases which are supported
  49.  * by the PEAR::DB abstraction layer to fetch data.
  50.  *
  51.  * Database Structure:
  52.  * <pre>
  53.  *  // meta data etc. not supported yet...
  54.  *
  55.  *  create table translations (
  56.  *     id int(11) auto_increment not null primary key,
  57.  *     string_id int(11),
  58.  *     page varchar(128),
  59.  *     lang varchar(10),
  60.  *     translation text
  61.  *     );
  62.  * alter table translations add index page (page);
  63.  * alter table translations add index lang (lang);
  64.  * alter table translations add index string_id (string_id);
  65.  * </pre>
  66.  *
  67.  * - then just run the dataobjects createtables script.
  68.  *
  69.  * @category  Internationalization
  70.  * @package   Translation2
  71.  * @author    Alan Knowles <alan@akbkhome.com>
  72.  * @copyright 2004-2008 Alan Knowles
  73.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  74.  * @version   CVS: $Id: dataobjectsimple.php,v 1.17 2008/02/02 18:05:06 quipo Exp $
  75.  * @link      http://pear.php.net/package/Translation2
  76.  */
  77. {
  78.     // {{{ init
  79.  
  80.     /**
  81.      * Initialize the container
  82.      *
  83.      * @param string $table table name
  84.      *
  85.      * @return boolean true
  86.      */
  87.     function init($table = null)
  88.     {
  89.         $this->_setDefaultOptions();
  90.         if (!empty($table)) {
  91.             $this->options['table'$table;
  92.         }
  93.         return true;
  94.     }
  95.  
  96.     // }}}
  97.     // {{{ _setDefaultOptions()
  98.  
  99.     /**
  100.      * Set some default options
  101.      *
  102.      * @return void 
  103.      * @access private
  104.      */
  105.     function _setDefaultOptions()
  106.     {
  107.         $this->options['table''translations';
  108.     }
  109.  
  110.     // }}}
  111.     // {{{ fetchLangs()
  112.  
  113.     /**
  114.      * Fetch the available langs if they're not cached yet.
  115.      *
  116.      * @return void 
  117.      */
  118.     function fetchLangs()
  119.     {
  120.         $do = DB_DataObject::factory($this->options['table']);
  121.         $do->selectAdd();
  122.         $do->selectAdd('distinct lang');
  123.         $do->find();
  124.  
  125.         $ret = array();
  126.         while ($do->fetch()) {
  127.             $l $do->lang;
  128.             $ret[$l= array(
  129.                 'id'         => $l,
  130.                 'name'       => $l,
  131.                 'meta'       => '',
  132.                 'error_text' => '',
  133.             );
  134.         }
  135.         $this->langs =  $ret;
  136.     }
  137.  
  138.     // }}}
  139.     // {{{ getPage()
  140.  
  141.     /**
  142.      * Returns an array of the strings in the selected page
  143.      *
  144.      * @param string $pageID page/group ID
  145.      * @param string $langID language ID
  146.      *
  147.      * @return array 
  148.      */
  149.     function getPage($pageID = null$langID = null)
  150.     {
  151.         $langID $this->_getLangID($langID);
  152.         if (PEAR::isError($langID)) {
  153.             return $langID;
  154.         }
  155.         
  156.         // First get the array of string IDs
  157.         $do = DB_DataObject::factory($this->options['table']);
  158.         $do->lang = '-';
  159.         $do->page = $pageID;
  160.         $do->find();
  161.         
  162.         $stringIDs = array();
  163.         while ($do->fetch()) {
  164.             $stringIDs[$do->string_id$do->translation;
  165.         }
  166.  
  167.         // Now get the array of strings
  168.         $do = DB_DataObject::factory($this->options['table']);
  169.         $do->page = $pageID;
  170.         $do->lang = $langID;
  171.  
  172.         $do->find();
  173.         $translations = array();
  174.         while ($do->fetch()) {
  175.             $translations[$do->string_id$do->translation;
  176.         }
  177.         
  178.         // Construct an associative array of stringIDs and translations
  179.         $strings = array();
  180.         foreach ($translations as $key => $value{
  181.             $strings[$stringIDs[$key]] $value;
  182.         }
  183.  
  184.         return $strings;
  185.     }
  186.  
  187.     // }}}
  188.     // {{{ getOne()
  189.  
  190.     /**
  191.      * Get a single item from the container, without caching the whole page
  192.      *
  193.      * @param string $stringID string ID
  194.      * @param string $pageID   page/group ID
  195.      * @param string $langID   language ID
  196.      *
  197.      * @return string 
  198.      */
  199.     function getOne($stringID$pageID = null$langID = null)
  200.     {
  201.         $langID $langID $langID (isset($this->currentLang['id']$this->currentLang['id''-');
  202.         // get the string id
  203.         $do = DB_DataObject::factory($this->options['table']);
  204.         $do->lang        = '-';
  205.         $do->page        = $pageID;
  206.         $do->translation = $string;
  207.         // we dont have the base language translation..
  208.         if (!$do->find(true)) {
  209.             return '';
  210.         }
  211.         $stringID $do->string_id;
  212.  
  213.         $do = DB_DataObject::factory($this->options['table']);
  214.         $do->lang      = $langID;
  215.         $do->page      = $pageID;
  216.         $do->string_id = $stringID;
  217.         //print_r($do);
  218.         $do->selectAdd();
  219.         $do->selectAdd('translation');
  220.         if (!$do->find(true)) {
  221.             return '';
  222.         }
  223.         return $do->translation;
  224.  
  225.     }
  226.  
  227.     // }}}
  228.     // {{{ getStringID()
  229.  
  230.     /**
  231.      * Get the stringID for the given string
  232.      *
  233.      * @param string $string string
  234.      * @param string $pageID page/group ID
  235.      *
  236.      * @return string 
  237.      */
  238.     function getStringID($string$pageID = null)
  239.     {
  240.         // get the english version...
  241.  
  242.         $do = DB_DataObject::factory($this->options['table']);
  243.         $do->lang        = $this->currentLang['id'];
  244.         $do->page        = $pageID;
  245.         $do->translation = $string;
  246.         if ($do->find(true)) {
  247.             return '';
  248.         }
  249.         return $do->string_id;
  250.     }
  251.  
  252.     // }}}
  253. }
  254. ?>

Documentation generated on Tue, 06 May 2008 06:00:17 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.