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_Admin_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-2007 Alan Knowles
  34.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  35.  * @version   CVS: $Id: dataobjectsimple.php,v 1.11 2007/11/10 00:02:50 quipo Exp $
  36.  * @link      http://pear.php.net/package/Translation2
  37.  */
  38.  
  39. /**
  40.  * require Translation2_Container_dataobjectsimple class
  41.  */
  42. require_once 'Translation2/Container/dataobjectsimple.php';
  43.  
  44. /**
  45.  * Storage driver for storing/fetching data to/from a database
  46.  *
  47.  * This storage driver can use all databases which are supported
  48.  * by PEAR::DB_DataObject to fetch data.
  49.  *
  50.  * Database Structure:
  51.  *
  52.  * <pre>
  53.  * // meta data etc. not supported
  54.  *
  55.  * table: translations
  56.  *  id          // not null primary key autoincrement..
  57.  *  string_id   // translation id
  58.  *  page        // indexed varchar eg. (mytemplate.html)
  59.  *  lang        // index varchar (eg. en|fr|.....)
  60.  *  translation // the translated value in language lang.
  61.  * </pre>
  62.  *
  63.  * @category  Internationalization
  64.  * @package   Translation2
  65.  * @author    Alan Knowles <alan@akbkhome.com>
  66.  * @copyright 2004-2007 Alan Knowles
  67.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  68.  * @link      http://pear.php.net/package/Translation2
  69.  */
  70. {
  71.     // {{{ addLang()
  72.  
  73.     /**
  74.      * Creates a new table to store the strings in this language.
  75.      * If the table is shared with other langs, it is ALTERed to
  76.      * hold strings in this lang too.
  77.      *
  78.      * @param array $langData array('lang_id'    => 'en',
  79.      *                               'table_name' => 'i18n',
  80.      *                               'name'       => 'english',
  81.      *                               'meta'       => 'some meta info',
  82.      *                               'error_text' => 'not available');
  83.      * @param array $options  DB_DataObject options
  84.      *
  85.      * @return true|PEAR_Error
  86.      */
  87.     function addLang($langData$options = array())
  88.     {
  89.         $do = DB_DataObject::factory($this->options['table']);
  90.         $do->lang = $langData['lang_id'];
  91.         if (!$do->find()) {
  92.             $do->insert();
  93.         }
  94.     }
  95.     
  96.     // }}}
  97.     // {{{ addLangToList()
  98.  
  99.     /**
  100.      * Creates a new entry in the langsAvail table.
  101.      * If the table doesn't exist yet, it is created.
  102.      *
  103.      * @param array $langData array('lang_id'    => 'en',
  104.      *                               'table_name' => 'i18n',
  105.      *                               'name'       => 'english',
  106.      *                               'meta'       => 'some meta info',
  107.      *                               'error_text' => 'not available');
  108.      *
  109.      * @return true|PEAR_Error
  110.      */
  111.     function addLangToList($langData)
  112.     {
  113.         return true;
  114.     }
  115.  
  116.     // }}}
  117.     // {{{ add()
  118.  
  119.     /**
  120.      * Add a new entry in the strings table.
  121.      *
  122.      * @param string $string      string
  123.      * @param string $pageID      page/group ID
  124.      * @param array  $stringArray Associative array with string translations.
  125.      *                Sample format:  array('en' => 'sample', 'it' => 'esempio')
  126.      *
  127.      * @return true|PEAR_Error
  128.      */
  129.     function add($string$pageID$stringArray)
  130.     {
  131.         //look up the string id first..
  132.         $do = DB_DataObject::factory($this->options['table']);
  133.         $do->lang = '-';
  134.         $do->translation = $string;
  135.         $do->page = $page;
  136.         if ($do->find(true)) {
  137.             $stringID $do->string_id;
  138.         else {
  139.             // insert it and use the 'id' as the string id
  140.             $stringID $do->insert();
  141.             $do->string_id = $stringID;
  142.             $do->update();
  143.         }
  144.  
  145.         foreach ($stringArray as $lang=>$value{
  146.             $do = DB_DataObject::factory($this->options['table']);
  147.             $do->string_id = $stringID;
  148.             $do->page      = $pageID;
  149.             $do->lang      = $lang;
  150.             if ($do->find(true)) {
  151.                 $do->translation = $value;
  152.                 $do->update();
  153.                 continue;
  154.             }
  155.             $do->translation = $value;
  156.             $do->insert();
  157.         }
  158.  
  159.         return true;
  160.     }
  161.  
  162.     // }}}
  163.     // {{{ update()
  164.  
  165.     /**
  166.      * Update an existing entry in the strings table.
  167.      *
  168.      * @param string $stringID    string ID
  169.      * @param string $pageID      page/group ID
  170.      * @param array  $stringArray Associative array with string translations.
  171.      *                Sample format: array('en' => 'sample', 'it' => 'esempio')
  172.      *
  173.      * @return true|PEAR_Error
  174.      */
  175.     function update($stringID$pageID$stringArray)
  176.     {
  177.         $this->add($stringID$pageID$stringArray);
  178.         return true;
  179.     }
  180.  
  181.     // }}}
  182.     // {{{ remove()
  183.  
  184.     /**
  185.      * Remove an entry from the strings table.
  186.      *
  187.      * @param string $stringID string ID
  188.      * @param string $pageID   page/group ID
  189.      *
  190.      * @return true|PEAR_Error
  191.      */
  192.     function remove($stringID$pageID)
  193.     {
  194.         // get the string id
  195.         $do = DB_DataObject::factory($this->options['table']);
  196.         $do->page = $pageID;
  197.         $do->translation = $stringID;
  198.         // we don't have the base language translation..
  199.         if (!$do->find()) {
  200.             return '';
  201.         }
  202.  
  203.         while ($do->fetch()) {
  204.             $do2 = DB_DataObject::factory($this->options['table']);
  205.             $do2->get($do->id);
  206.             $do2->delete();
  207.         }
  208.         return true;
  209.     }
  210.  
  211.     // }}}
  212.     // {{{ removePage()
  213.  
  214.     /**
  215.      * Remove all the strings in the given page/group
  216.      *
  217.      * @param string $pageID page/group ID
  218.      *
  219.      * @return mixed true on success, PEAR_Error on failure
  220.      */
  221.     function removePage($pageID = null)
  222.     {
  223.         $do = DB_DataObject::factory($this->options['table']);
  224.         $do->page = $pageID;
  225.         // we don't have the base language translation..
  226.         if (!$do->find()) {
  227.             return '';
  228.         }
  229.  
  230.         while ($do->fetch()) {
  231.             $do2 = DB_DataObject::factory($this->options['table']);
  232.             $do2->get($do->id);
  233.             $do2->delete();
  234.         }
  235.         return true;
  236.     }
  237.  
  238.     // }}}
  239. }
  240. ?>

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