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

Source for file Admin.php

Documentation is available at Admin.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Contains the Translation2_Admin base 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    Lorenzo Alberton <l.alberton@quipo.it>
  33.  * @copyright 2004-2005 Lorenzo Alberton
  34.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  35.  * @version   CVS: $Id: Admin.php,v 1.25 2008/05/03 09:17:59 quipo Exp $
  36.  * @link      http://pear.php.net/package/Translation2
  37.  */
  38.  
  39. /**
  40.  * require Translation2 base class
  41.  */
  42. require_once 'Translation2.php';
  43.  
  44. /**
  45.  * Administration utilities for translation string management
  46.  *
  47.  * Set of methods to easily add/remove languages and strings,
  48.  * with a common API for all the containers.
  49.  *
  50.  * @category  Internationalization
  51.  * @package   Translation2
  52.  * @author    Lorenzo Alberton <l.alberton@quipo.it>
  53.  * @copyright 2004-2005 Lorenzo Alberton
  54.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  55.  * @link      http://pear.php.net/package/Translation2
  56.  */
  57. {
  58.     // {{{ class vars
  59.  
  60.     // }}}
  61.     // {{{ factory()
  62.  
  63.     /**
  64.      * Return a Translation2_Admin instance already initialized
  65.      *
  66.      * @param string $driver  Type of the storage driver
  67.      * @param mixed  $options Additional options for the storage driver
  68.      *                         (example: if you are using DB as the storage
  69.      *                         driver, you have to pass the DSN string here)
  70.      * @param array  $params  Array of parameters for the adapter class
  71.      *                         (i.e. you can set here the mappings between your
  72.      *                         table/field names and the ones used by this class)
  73.      *
  74.      * @return object Translation2 instance or PEAR_Error on failure
  75.      * @access public
  76.      * @static
  77.      */
  78.     function factory($driver$options ''$params = array())
  79.     {
  80.         $tr = new Translation2_Admin;
  81.         $tr->storage = Translation2_Admin::_storageFactory($driver$options);
  82.         if (PEAR::isError($tr->storage)) {
  83.             return $tr->storage;
  84.         }
  85.         $tr->_setDefaultOptions();
  86.         $tr->_parseOptions($params);
  87.         $tr->storage->_parseOptions($params);
  88.         return $tr;
  89.     }
  90.  
  91.     // }}}
  92.     // {{{ _storageFactory()
  93.  
  94.     /**
  95.      * Return a storage driver based on $driver and $options
  96.      *
  97.      * Override Translation2::_storageFactory()
  98.      *
  99.      * @param string $driver  Type of storage class to return
  100.      * @param string $options Optional parameters for the storage class
  101.      *
  102.      * @return object Object Storage object
  103.      * @access private
  104.      * @static
  105.      */
  106.     function _storageFactory($driver$options '')
  107.     {
  108.         if (is_object($driver)) {
  109.             return $driver;
  110.         }
  111.         $storage_path  'Translation2/Admin/Container/'.strtolower($driver).'.php';
  112.         $storage_class 'Translation2_Admin_Container_'.strtolower($driver);
  113.         include_once $storage_path;
  114.         $storage = new $storage_class;
  115.         $err $storage->init($options);
  116.         if (PEAR::isError($err)) {
  117.             return $err;
  118.         }
  119.         return $storage;
  120.     }
  121.  
  122.     // }}}
  123.     // {{{ _setDefaultOptions()
  124.  
  125.     /**
  126.      * Set some default options
  127.      *
  128.      * @return void 
  129.      * @access private
  130.      */
  131.     function _setDefaultOptions()
  132.     {
  133.         $this->options['autoCleanCache'= false;
  134.         $this->options['cacheOptions']   = array('defaultGroup' => 'Translation2');
  135.         parent::_setDefaultOptions();
  136.     }
  137.  
  138.     // }}}
  139.     // {{{ getAdminDecorator()
  140.  
  141.     /**
  142.      * Return an instance of an admin decorator
  143.      *
  144.      * @param string $decorator Name of the decorator
  145.      *
  146.      * @return object Decorator object reference
  147.      * @see    Translation2::getDecorator()
  148.      * @access public
  149.      */
  150.     function &getAdminDecorator($decorator)
  151.     {
  152.         $decorator_path  'Translation2/Admin/Decorator/'.$decorator.'.php';
  153.         $decorator_class 'Translation2_Admin_Decorator_'.$decorator;
  154.         include_once $decorator_path;
  155.         if (func_num_args(> 1{
  156.             $obj =func_get_arg(1);
  157.             $ret = new $decorator_class($obj);
  158.         else {
  159.             $ret = new $decorator_class($this);
  160.         }
  161.         return $ret;
  162.     }
  163.  
  164.     // }}}
  165.     // {{{ addLang
  166.  
  167.     /**
  168.      * Prepare the storage container for a new lang.
  169.      * If the langsAvail table doesn't exist yet, it is created.
  170.      *
  171.      * @param array $langData array('lang_id'    => 'en',
  172.      *                               'table_name' => 'i18n',
  173.      *                               'name'       => 'english',
  174.      *                               'meta'       => 'some meta info',
  175.      *                               'error_text' => 'not available');
  176.      * @param array $options  array('charset'   => 'utf8',
  177.      *                               'collation' => 'utf8_general_ci');
  178.      *
  179.      * @return mixed true on success, PEAR_Error on failure
  180.      */
  181.     function addLang($langData$options = array())
  182.     {
  183.         $res $this->storage->addLang($langData$options);
  184.         if (PEAR::isError($res)) {
  185.             return $res;
  186.         }
  187.         $res $this->storage->addLangToList($langData);
  188.         if (PEAR::isError($res)) {
  189.             return $res;
  190.         }
  191.         $this->storage->fetchLangs()//update local cache
  192.         if ($this->options['autoCleanCache']{
  193.             $this->cleanCache();
  194.         }
  195.         return true;
  196.     }
  197.  
  198.     // }}}
  199.     // {{{ removeLang
  200.  
  201.     /**
  202.      * Remove the lang from the langsAvail table and drop the strings table.
  203.      * If the strings table holds other langs and $force==false, then
  204.      * only the lang column is dropped. If $force==true the whole
  205.      * table is dropped without any check
  206.      *
  207.      * @param string  $langID language ID
  208.      * @param boolean $force  remove the language info without further checks
  209.      *
  210.      * @return mixed true on success, PEAR_Error on failure
  211.      */
  212.     function removeLang($langID = null$force = false)
  213.     {
  214.         if (is_null($langID)) {
  215.             //return error
  216.         }
  217.         $res $this->storage->removeLang($langID$force);
  218.         if (PEAR::isError($res)) {
  219.             return $res;
  220.         }
  221.         unset($this->storage->langs[$langID]);
  222.         if ($this->options['autoCleanCache']{
  223.             $this->cleanCache();
  224.         }
  225.         return true;
  226.     }
  227.  
  228.     // }}}
  229.     // {{{ updateLang
  230.  
  231.     /**
  232.      * Update the lang info in the langsAvail table
  233.      *
  234.      * @param array $langData array containing language info
  235.      *
  236.      * @return mixed true on success, PEAR_Error on failure
  237.      */
  238.     function updateLang($langData)
  239.     {
  240.         $result $this->storage->updateLang($langData);
  241.         if ($this->options['autoCleanCache']{
  242.             $this->cleanCache();
  243.         }
  244.         return $result;
  245.     }
  246.  
  247.     // }}}
  248.     // {{{ add
  249.  
  250.     /**
  251.      * Add a new translation
  252.      *
  253.      * @param string $stringID    string ID
  254.      * @param string $pageID      page/group ID
  255.      * @param array  $stringArray Associative array with string translations.
  256.      *                Sample format:  array('en' => 'sample', 'it' => 'esempio')
  257.      *
  258.      * @return mixed true on success, PEAR_Error on failure
  259.      */
  260.     function add($stringID$pageID$stringArray)
  261.     {
  262.         $result $this->storage->add($stringID$pageID$stringArray);
  263.         if ($this->options['autoCleanCache']{
  264.             $this->cleanCache();
  265.         }
  266.         return $result;
  267.     }
  268.  
  269.     // }}}
  270.     // {{{ update
  271.  
  272.     /**
  273.      * Update an existing translation
  274.      *
  275.      * @param string $stringID    string ID
  276.      * @param string $pageID      page/group ID
  277.      * @param array  $stringArray Associative array with string translations.
  278.      *                Sample format:  array('en' => 'sample', 'it' => 'esempio')
  279.      *
  280.      * @return mixed true on success, PEAR_Error on failure
  281.      */
  282.     function update($stringID$pageID$stringArray)
  283.     {
  284.         $result $this->storage->update($stringID$pageID$stringArray);
  285.         if ($this->options['autoCleanCache']{
  286.             $this->cleanCache();
  287.         }
  288.         return $result;
  289.     }
  290.  
  291.     // }}}
  292.     // {{{ remove
  293.  
  294.     /**
  295.      * Remove a translated string
  296.      *
  297.      * @param string $stringID string ID
  298.      * @param string $pageID   page/group ID
  299.      *
  300.      * @return mixed true on success, PEAR_Error on failure
  301.      * @todo add a third $langs option, to conditionally remove only the langs specified
  302.      */
  303.     function remove($stringID$pageID = null)
  304.     {
  305.         $result $this->storage->remove($stringID$pageID);
  306.         if ($this->options['autoCleanCache']{
  307.             $this->cleanCache();
  308.         }
  309.         return $result;
  310.     }
  311.  
  312.     // }}}
  313.     // {{{ removePage
  314.  
  315.     /**
  316.      * Remove all the strings in the given page/group
  317.      *
  318.      * @param string $pageID page/group ID
  319.      *
  320.      * @return mixed true on success, PEAR_Error on failure
  321.      */
  322.     function removePage($pageID = null)
  323.     {
  324.         $result $this->storage->removePage($pageID);
  325.         if ($this->options['autoCleanCache']{
  326.             $this->cleanCache();
  327.         }
  328.         return $result;
  329.     }
  330.  
  331.     // }}}
  332.     // {{{ getPageNames()
  333.  
  334.     /**
  335.      * Get a list of all the pageIDs in any table.
  336.      *
  337.      * @return array 
  338.      */
  339.     function getPageNames()
  340.     {
  341.         return $this->storage->getPageNames();
  342.     }
  343.  
  344.     // }}}
  345.     // {{{ cleanCache()
  346.  
  347.     /**
  348.      * If you use the CacheLiteFunction decorator, you may want to invalidate
  349.      * the cache after a change in the data base.
  350.      *
  351.      * @return void 
  352.      */
  353.     function cleanCache()
  354.     {
  355.         static $cacheLiteFunction = null;
  356.         if (is_null($cacheLiteFunction)) {
  357.             include_once 'Cache/Lite/Function.php';
  358.             $cacheLiteFunction = new Cache_Lite_Function($this->options['cacheOptions']);
  359.         }
  360.         $cacheLiteFunction->clean($this->options['cacheOptions']['defaultGroup']);
  361.     }
  362.  
  363.     // }}}
  364. }
  365. ?>

Documentation generated on Fri, 14 Nov 2008 11:30:09 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.