home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Translation2 / Container / dataobjectsimple.php next >
Encoding:
PHP Script  |  2008-07-02  |  7.7 KB  |  255 lines

  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. class Translation2_Container_dataobjectsimple extends Translation2_Container
  78. {
  79.     // {{{ init
  80.  
  81.     /**
  82.      * Initialize the container
  83.      *
  84.      * @param string $table table name
  85.      *
  86.      * @return boolean true
  87.      */
  88.     function init($table = null)
  89.     {
  90.         $this->_setDefaultOptions();
  91.         if (!empty($table)) {
  92.             $this->options['table'] = $table;
  93.         }
  94.         return true;
  95.     }
  96.  
  97.     // }}}
  98.     // {{{ _setDefaultOptions()
  99.  
  100.     /**
  101.      * Set some default options
  102.      *
  103.      * @return void
  104.      * @access private
  105.      */
  106.     function _setDefaultOptions()
  107.     {
  108.         $this->options['table'] = 'translations';
  109.     }
  110.  
  111.     // }}}
  112.     // {{{ fetchLangs()
  113.  
  114.     /**
  115.      * Fetch the available langs if they're not cached yet.
  116.      *
  117.      * @return void
  118.      */
  119.     function fetchLangs()
  120.     {
  121.         $do = DB_DataObject::factory($this->options['table']);
  122.         $do->selectAdd();
  123.         $do->selectAdd('distinct lang');
  124.         $do->find();
  125.  
  126.         $ret = array();
  127.         while ($do->fetch()) {
  128.             $l = $do->lang;
  129.             $ret[$l] = array(
  130.                 'id'         => $l,
  131.                 'name'       => $l,
  132.                 'meta'       => '',
  133.                 'error_text' => '',
  134.             );
  135.         }
  136.         $this->langs =  $ret;
  137.     }
  138.  
  139.     // }}}
  140.     // {{{ getPage()
  141.  
  142.     /**
  143.      * Returns an array of the strings in the selected page
  144.      *
  145.      * @param string $pageID page/group ID
  146.      * @param string $langID language ID
  147.      *
  148.      * @return array
  149.      */
  150.     function getPage($pageID = null, $langID = null)
  151.     {
  152.         $langID = $this->_getLangID($langID);
  153.         if (PEAR::isError($langID)) {
  154.             return $langID;
  155.         }
  156.         
  157.         // First get the array of string IDs
  158.         $do = DB_DataObject::factory($this->options['table']);
  159.         $do->lang = '-';
  160.         $do->page = $pageID;
  161.         $do->find();
  162.         
  163.         $stringIDs = array();
  164.         while ($do->fetch()) {
  165.             $stringIDs[$do->string_id] = $do->translation;
  166.         }
  167.  
  168.         // Now get the array of strings
  169.         $do = DB_DataObject::factory($this->options['table']);
  170.         $do->page = $pageID;
  171.         $do->lang = $langID;
  172.  
  173.         $do->find();
  174.         $translations = array();
  175.         while ($do->fetch()) {
  176.             $translations[$do->string_id] = $do->translation;
  177.         }
  178.         
  179.         // Construct an associative array of stringIDs and translations
  180.         $strings = array();
  181.         foreach ($translations as $key => $value) {
  182.             $strings[$stringIDs[$key]] = $value;
  183.         }
  184.  
  185.         return $strings;
  186.     }
  187.  
  188.     // }}}
  189.     // {{{ getOne()
  190.  
  191.     /**
  192.      * Get a single item from the container, without caching the whole page
  193.      *
  194.      * @param string $stringID string ID
  195.      * @param string $pageID   page/group ID
  196.      * @param string $langID   language ID
  197.      *
  198.      * @return string
  199.      */
  200.     function getOne($stringID, $pageID = null, $langID = null)
  201.     {
  202.         $langID = $langID ? $langID : (isset($this->currentLang['id']) ? $this->currentLang['id'] : '-');
  203.         // get the string id
  204.         $do = DB_DataObject::factory($this->options['table']);
  205.         $do->lang        = '-';
  206.         $do->page        = $pageID;
  207.         $do->translation = $string;
  208.         // we dont have the base language translation..
  209.         if (!$do->find(true)) {
  210.             return '';
  211.         }
  212.         $stringID = $do->string_id;
  213.  
  214.         $do = DB_DataObject::factory($this->options['table']);
  215.         $do->lang      = $langID;
  216.         $do->page      = $pageID;
  217.         $do->string_id = $stringID;
  218.         //print_r($do);
  219.         $do->selectAdd();
  220.         $do->selectAdd('translation');
  221.         if (!$do->find(true)) {
  222.             return '';
  223.         }
  224.         return $do->translation;
  225.  
  226.     }
  227.  
  228.     // }}}
  229.     // {{{ getStringID()
  230.  
  231.     /**
  232.      * Get the stringID for the given string
  233.      *
  234.      * @param string $string string
  235.      * @param string $pageID page/group ID
  236.      *
  237.      * @return string
  238.      */
  239.     function getStringID($string, $pageID = null)
  240.     {
  241.         // get the english version...
  242.  
  243.         $do = DB_DataObject::factory($this->options['table']);
  244.         $do->lang        = $this->currentLang['id'];
  245.         $do->page        = $pageID;
  246.         $do->translation = $string;
  247.         if ($do->find(true)) {
  248.             return '';
  249.         }
  250.         return $do->string_id;
  251.     }
  252.  
  253.     // }}}
  254. }
  255. ?>