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 / Admin / Container / mdb2.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  25.9 KB  |  754 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Contains the Translation2_Admin_Container_mdb2 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-2007 Lorenzo Alberton
  34.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  35.  * @version   CVS: $Id: mdb2.php,v 1.41 2008/05/03 09:17:59 quipo Exp $
  36.  * @link      http://pear.php.net/package/Translation2
  37.  */
  38.  
  39. /**
  40.  * require Translation2_Container_mdb2 class
  41.  */
  42. require_once 'Translation2/Container/mdb2.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 the PEAR::MDB2 abstraction layer to store and fetch data.
  49.  *
  50.  * @category  Internationalization
  51.  * @package   Translation2
  52.  * @author    Lorenzo Alberton <l.alberton@quipo.it>
  53.  * @copyright 2004-2007 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. class Translation2_Admin_Container_mdb2 extends Translation2_Container_mdb2
  58. {
  59.     // {{{
  60.  
  61.     /**
  62.      * Fetch the table names from the db
  63.      *
  64.      * @access private
  65.      * @return array|PEAR_Error
  66.      */
  67.     function _fetchTableNames()
  68.     {
  69.         $this->db->loadModule('Manager');
  70.         return $this->db->manager->listTables();
  71.     }
  72.  
  73.     // }}}
  74.     // {{{ addLang()
  75.  
  76.     /**
  77.      * Creates a new table to store the strings in this language.
  78.      * If the table is shared with other langs, it is ALTERed to
  79.      * hold strings in this lang too.
  80.      *
  81.      * @param array $langData array('lang_id'    => 'en',
  82.      *                              'table_name' => 'i18n',
  83.      *                              'name'       => 'english',
  84.      *                              'meta'       => 'some meta info',
  85.      *                              'error_text' => 'not available');
  86.      * @param array $options  array('charset'   => 'utf8',
  87.      *                              'collation' => 'utf8_general_ci');
  88.      *
  89.      * @return true|PEAR_Error
  90.      */
  91.     function addLang($langData, $options = array())
  92.     {
  93.         $tables = $this->_fetchTableNames();
  94.         if (PEAR::isError($tables)) {
  95.             return $tables;
  96.         }
  97.  
  98.         $lang_col  = $this->_getLangCol($langData['lang_id']);
  99.         $charset   = empty($options['charset'])   ? null : $options['charset'];
  100.         $collation = empty($options['collation']) ? null : $options['collation'];
  101.         $this->db->loadModule('Manager');
  102.  
  103.         if (in_array($langData['table_name'], $tables)) {
  104.             //table exists
  105.             $table_changes = array(
  106.                 'add' => array(
  107.                     $lang_col => array(
  108.                         'type' => 'text',
  109.                         'charset'   => $charset,
  110.                         'collation' => $collation,
  111.                     )
  112.                 )
  113.             );
  114.             ++$this->_queries;
  115.             return $this->db->manager->alterTable($langData['table_name'], $table_changes, false);
  116.         }
  117.  
  118.         //table does not exist
  119.         $table_definition = array(
  120.             $this->options['string_page_id_col'] => array(
  121.                 'type'      => 'text',
  122.                 'length'    => $this->options['string_page_id_col_length'],
  123.                 'default'   => null,
  124.                 'charset'   => $charset,
  125.                 'collation' => $collation,
  126.             ),
  127.             $this->options['string_id_col'] => array(
  128.                 'type'      => 'text',
  129.                 'notnull'   => 1,
  130.                 'charset'   => $charset,
  131.                 'collation' => $collation,
  132.             ),
  133.             $lang_col => array(
  134.                 'type'      => 'text',
  135.                 'charset'   => $charset,
  136.                 'collation' => $collation,
  137.             ),
  138.         );
  139.         ++$this->_queries;
  140.         $res = $this->db->manager->createTable($langData['table_name'], $table_definition);
  141.         if (PEAR::isError($res)) {
  142.             return $res;
  143.         }
  144.         $mysqlClause = ($this->db->phptype == 'mysql') ? '(255)' : '';
  145.         
  146.         $constraint_name = $langData['table_name']
  147.             .'_'. $this->options['string_page_id_col']
  148.             .'_'. $this->options['string_id_col'];
  149.         $constraint_definition = array(
  150.             'fields' => array(
  151.                 $this->options['string_page_id_col'] => array(),
  152.                 $this->options['string_id_col'].$mysqlClause => array(),
  153.             ),
  154.             'unique' => true,
  155.         );
  156.         ++$this->_queries;
  157.         $res = $this->db->manager->createConstraint($langData['table_name'], $constraint_name, $constraint_definition);
  158.         if (PEAR::isError($res)) {
  159.             return $res;
  160.         }
  161.  
  162.         $index_name = $langData['table_name'] .'_'. $this->options['string_page_id_col'];
  163.         $index_definition = array(
  164.             'fields' => array($this->options['string_page_id_col'] => array())
  165.         );
  166.         ++$this->_queries;
  167.         $res = $this->db->manager->createIndex($langData['table_name'], $index_name, $index_definition);
  168.         if (PEAR::isError($res)) {
  169.             return $res;
  170.         }
  171.         
  172.         $index_name = $langData['table_name'] .'_'. $this->options['string_id_col'];
  173.         $index_definition = array(
  174.             'fields' => array($this->options['string_id_col'] => array('length' => 255))
  175.         );
  176.         ++$this->_queries;
  177.         $res = $this->db->manager->createIndex($langData['table_name'], $index_name, $index_definition);
  178.         if (PEAR::isError($res)) {
  179.             return $res;
  180.         }
  181.         
  182.         return true;
  183.     }
  184.  
  185.     // }}}
  186.     // {{{ addLangToList()
  187.  
  188.     /**
  189.      * Creates a new entry in the langsAvail table.
  190.      * If the table doesn't exist yet, it is created.
  191.      *
  192.      * @param array $langData array('lang_id'    => 'en',
  193.      *                              'table_name' => 'i18n',
  194.      *                              'name'       => 'english',
  195.      *                              'meta'       => 'some meta info',
  196.      *                              'error_text' => 'not available',
  197.      *                              'encoding'   => 'iso-8859-1');
  198.      *
  199.      * @return true|PEAR_Error
  200.      */
  201.     function addLangToList($langData)
  202.     {
  203.         $tables = $this->_fetchTableNames();
  204.         if (PEAR::isError($tables)) {
  205.             return $tables;
  206.         }
  207.  
  208.         if (!in_array($this->options['langs_avail_table'], $tables)) {
  209.             $queries   = array();
  210.             $queries[] = sprintf('CREATE TABLE %s ('
  211.                                 .'%s VARCHAR(16), '
  212.                                 .'%s VARCHAR(200), '
  213.                                 .'%s TEXT, '
  214.                                 .'%s VARCHAR(250), '
  215.                                 .'%s VARCHAR(16) )',
  216.                 $this->db->quoteIdentifier($this->options['langs_avail_table'], true),
  217.                 $this->db->quoteIdentifier($this->options['lang_id_col'], true),
  218.                 $this->db->quoteIdentifier($this->options['lang_name_col'], true),
  219.                 $this->db->quoteIdentifier($this->options['lang_meta_col'], true),
  220.                 $this->db->quoteIdentifier($this->options['lang_errmsg_col'], true),
  221.                 $this->db->quoteIdentifier($this->options['lang_encoding_col'], true)
  222.             );
  223.             $queries[] = sprintf('CREATE UNIQUE INDEX %s_%s_index ON %s (%s)',
  224.                 $this->db->quoteIdentifier($this->options['langs_avail_table'], true),
  225.                 $this->db->quoteIdentifier($this->options['lang_id_col'], true),
  226.                 $this->db->quoteIdentifier($this->options['langs_avail_table'], true),
  227.                 $this->db->quoteIdentifier($this->options['lang_id_col'], true)
  228.             );
  229.  
  230.             foreach ($queries as $query) {
  231.                 ++$this->_queries;
  232.                 $res = $this->db->exec($query);
  233.                 if (PEAR::isError($res)) {
  234.                     return $res;
  235.                 }
  236.             }
  237.         }
  238.  
  239.         $query = sprintf('INSERT INTO %s (%s, %s, %s, %s, %s) VALUES (%s, %s, %s, %s, %s)',
  240.             $this->db->quoteIdentifier($this->options['langs_avail_table'], true),
  241.             $this->db->quoteIdentifier($this->options['lang_id_col'], true),
  242.             $this->db->quoteIdentifier($this->options['lang_name_col'], true),
  243.             $this->db->quoteIdentifier($this->options['lang_meta_col'], true),
  244.             $this->db->quoteIdentifier($this->options['lang_errmsg_col'], true),
  245.             $this->db->quoteIdentifier($this->options['lang_encoding_col'], true),
  246.             $this->db->quote($langData['lang_id']),
  247.             $this->db->quote($langData['name']),
  248.             $this->db->quote($langData['meta']),
  249.             $this->db->quote($langData['error_text']),
  250.             $this->db->quote($langData['encoding'])
  251.         );
  252.  
  253.         ++$this->_queries;
  254.         $res = $this->db->exec($query);
  255.         $this->options['strings_tables'][$langData['lang_id']] = $langData['table_name'];
  256.         if (PEAR::isError($res)) {
  257.             return $res;
  258.         }
  259.         return true;
  260.     }
  261.  
  262.     // }}}
  263.     // {{{ removeLang()
  264.  
  265.     /**
  266.      * Remove the lang from the langsAvail table and drop the strings table.
  267.      * If the strings table holds other langs and $force==false, then
  268.      * only the lang column is dropped. If $force==true the whole
  269.      * table is dropped without any check
  270.      *
  271.      * @param string  $langID language ID
  272.      * @param boolean $force  if true, the whole table is dropped without checks
  273.      *
  274.      * @return true|PEAR_Error
  275.      */
  276.     function removeLang($langID, $force)
  277.     {
  278.         //remove from langsAvail
  279.         $query = sprintf('DELETE FROM %s WHERE %s = %s',
  280.             $this->db->quoteIdentifier($this->options['langs_avail_table'], true),
  281.             $this->db->quoteIdentifier($this->options['lang_id_col'], true),
  282.             $this->db->quote($langID, 'text')
  283.         );
  284.         ++$this->_queries;
  285.         $res = $this->db->exec($query);
  286.         if (PEAR::isError($res)) {
  287.             return $res;
  288.         }
  289.  
  290.         $this->db->loadModule('Manager');
  291.         $lang_table = $this->_getLangTable($langID);
  292.         if ($force) {
  293.             //remove the whole table
  294.             ++$this->_queries;
  295.             return $this->db->manager->dropTable($lang_table);
  296.         }
  297.  
  298.         //drop only the column for this lang
  299.         $table_changes = array(
  300.             'remove' => array($this->_getLangCol($langID) => array())
  301.         );
  302.         ++$this->_queries;
  303.         return $this->db->manager->alterTable($lang_table, $table_changes, false);
  304.     }
  305.  
  306.     // }}}
  307.     // {{{ updateLang()
  308.  
  309.     /**
  310.      * Update the lang info in the langsAvail table
  311.      *
  312.      * @param array $langData language data
  313.      *
  314.      * @return true|PEAR_Error
  315.      */
  316.     function updateLang($langData)
  317.     {
  318.         $allFields = array(
  319.             //'lang_id'    => 'lang_id_col',
  320.             'name'       => 'lang_name_col',
  321.             'meta'       => 'lang_meta_col',
  322.             'error_text' => 'lang_errmsg_col',
  323.             'encoding'   => 'lang_encoding_col',
  324.         );
  325.         $updateFields = array_keys($langData);
  326.         $langSet  = array();
  327.         foreach ($allFields as $field => $col) {
  328.             if (in_array($field, $updateFields)) {
  329.                 $langSet[] = $this->db->quoteIdentifier($this->options[$col], true) . ' = ' .
  330.                              $this->db->quote($langData[$field]);
  331.             }
  332.         }
  333.         $query = sprintf('UPDATE %s SET %s WHERE %s=%s',
  334.             $this->db->quoteIdentifier($this->options['langs_avail_table'], true),
  335.             implode(', ', $langSet),
  336.             $this->db->quoteIdentifier($this->options['lang_id_col'], true),
  337.             $this->db->quote($langData['lang_id'])
  338.         );
  339.  
  340.         ++$this->_queries;
  341.         $res = $this->db->exec($query);
  342.         $this->fetchLangs();  //update memory cache
  343.         if (PEAR::isError($res)) {
  344.             return $res;
  345.         }
  346.         return true;
  347.     }
  348.  
  349.     // }}}
  350.     // {{{ add()
  351.  
  352.     /**
  353.      * Add a new entry in the strings table.
  354.      *
  355.      * @param string $stringID    string ID
  356.      * @param string $pageID      page/group ID
  357.      * @param array  $stringArray Associative array with string translations.
  358.      *               Sample format: array('en' => 'sample', 'it' => 'esempio')
  359.      *
  360.      * @return true|PEAR_Error
  361.      */
  362.     function add($stringID, $pageID, $stringArray)
  363.     {
  364.         $langs = array_intersect(
  365.             array_keys($stringArray),
  366.             $this->getLangs('ids')
  367.         );
  368.  
  369.         if (!count($langs)) {
  370.             //return error: no valid lang provided
  371.             return true;
  372.         }
  373.  
  374.         // Langs may be in different tables - we need to split up queries along
  375.         // table lines, so we can keep DB traffic to a minimum.
  376.  
  377.         $unquoted_stringID = $stringID;
  378.         $unquoted_pageID   = $pageID;
  379.         $stringID          = $this->db->quote($stringID, 'text');
  380.         $pageID            = is_null($pageID) ? 'NULL' : $this->db->quote($pageID, 'text');
  381.         // Loop over the tables we need to insert into.
  382.         foreach ($this->_tableLangs($langs) as $table => $tableLangs) {
  383.             $exists = $this->_recordExists($unquoted_stringID, $unquoted_pageID, $table);
  384.             if (PEAR::isError($exists)) {
  385.                 return $exists;
  386.             }
  387.             $func  = $exists ? '_getUpdateQuery' : '_getInsertQuery';
  388.             $query = $this->$func($table, $tableLangs, $stringID, $pageID, $stringArray);
  389.             
  390.             ++$this->_queries;
  391.             $res = $this->db->exec($query);
  392.             if (PEAR::isError($res)) {
  393.                 return $res;
  394.             }
  395.         }
  396.  
  397.         return true;
  398.     }
  399.  
  400.     // }}}
  401.     // {{{ update()
  402.  
  403.     /**
  404.      * Update an existing entry in the strings table.
  405.      *
  406.      * @param string $stringID    string ID
  407.      * @param string $pageID      page/group ID
  408.      * @param array  $stringArray Associative array with string translations.
  409.      *               Sample format: array('en' => 'sample', 'it' => 'esempio')
  410.      *
  411.      * @return true|PEAR_Error
  412.      */
  413.     function update($stringID, $pageID, $stringArray)
  414.     {
  415.         return $this->add($stringID, $pageID, $stringArray);
  416.     }
  417.  
  418.     // }}}
  419.     // {{{ _getInsertQuery()
  420.  
  421.     /**
  422.      * Build a SQL query to INSERT a record
  423.      *
  424.      * @param string $table        table name
  425.      * @param array  &$tableLangs  tables containing the languages
  426.      * @param string $stringID     string ID
  427.      * @param string $pageID       page/group ID
  428.      * @param array  &$stringArray array of strings
  429.      *
  430.      * @return string INSERT query
  431.      * @access private
  432.      */
  433.     function _getInsertQuery($table, &$tableLangs, $stringID, $pageID, &$stringArray)
  434.     {
  435.         $tableCols = $this->_getLangCols($tableLangs);
  436.         $langData = array();
  437.         foreach ($tableLangs as $lang) {
  438.             $langData[$lang] = $this->db->quote($stringArray[$lang], 'text');
  439.         }
  440.         foreach (array_keys($tableCols) as $k) {
  441.             $tableCols[$k] = $this->db->quoteIdentifier($tableCols[$k], true);
  442.         }
  443.  
  444.         return sprintf('INSERT INTO %s (%s, %s, %s) VALUES (%s, %s, %s)',
  445.             $this->db->quoteIdentifier($table, true),
  446.             $this->db->quoteIdentifier($this->options['string_id_col'], true),
  447.             $this->db->quoteIdentifier($this->options['string_page_id_col'], true),
  448.             implode(', ', $tableCols),
  449.             $stringID,
  450.             $pageID,
  451.             implode(', ', $langData)
  452.         );
  453.     }
  454.  
  455.     // }}}
  456.     // {{{ _getUpdateQuery()
  457.  
  458.     /**
  459.      * Build a SQL query to UPDATE a record
  460.      *
  461.      * @param string $table        table name
  462.      * @param array  &$tableLangs  tables containing the languages
  463.      * @param string $stringID     string ID
  464.      * @param string $pageID       page/group ID
  465.      * @param array  &$stringArray array of strings
  466.      *
  467.      * @return string UPDATE query
  468.      * @access private
  469.      */
  470.     function _getUpdateQuery($table, &$tableLangs, $stringID, $pageID, &$stringArray)
  471.     {
  472.         $tableCols = $this->_getLangCols($tableLangs);
  473.         $langSet = array();
  474.         foreach ($tableLangs as $lang) {
  475.             $langSet[] = $this->db->quoteIdentifier($tableCols[$lang], true) . ' = ' .
  476.                          $this->db->quote($stringArray[$lang], 'text');
  477.         }
  478.  
  479.         return sprintf('UPDATE %s SET %s WHERE %s = %s AND %s = %s',
  480.             $this->db->quoteIdentifier($table, true),
  481.             implode(', ', $langSet),
  482.             $this->db->quoteIdentifier($this->options['string_id_col'], true),
  483.             $stringID,
  484.             $this->db->quoteIdentifier($this->options['string_page_id_col'], true),
  485.             $pageID
  486.         );
  487.     }
  488.  
  489.     // }}}
  490.     // {{{ remove()
  491.  
  492.     /**
  493.      * Remove an entry from the strings table.
  494.      *
  495.      * @param string $stringID string ID
  496.      * @param string $pageID   page/group ID
  497.      *
  498.      * @return mixed true on success, PEAR_Error on failure
  499.      */
  500.     function remove($stringID, $pageID)
  501.     {
  502.         $tables = array_unique($this->_getLangTables());
  503.  
  504.         $stringID = $this->db->quote($stringID, 'text');
  505.         // get the tables and skip the non existent ones
  506.         $dbTables = $this->_fetchTableNames();
  507.         foreach ($tables as $table) {
  508.             if (!in_array($table, $dbTables)) {
  509.                 continue;
  510.             }
  511.             $query = sprintf('DELETE FROM %s WHERE %s = %s AND %s',
  512.                  $this->db->quoteIdentifier($table, true),
  513.                  $this->db->quoteIdentifier($this->options['string_id_col'], true),
  514.                  $stringID,
  515.                  $this->db->quoteIdentifier($this->options['string_page_id_col'], true)
  516.             );
  517.             if (is_null($pageID)) {
  518.                 $query .= ' IS NULL';
  519.             } else {
  520.                 $query .= ' = ' . $this->db->quote($pageID, 'text');
  521.             }
  522.  
  523.             ++$this->_queries;
  524.             $res = $this->db->exec($query);
  525.             if (PEAR::isError($res)) {
  526.                 return $res;
  527.             }
  528.         }
  529.  
  530.         return true;
  531.     }
  532.  
  533.     // }}}
  534.     // {{{ removePage
  535.  
  536.     /**
  537.      * Remove all the strings in the given page/group
  538.      *
  539.      * @param string $pageID page/group ID
  540.      *
  541.      * @return mixed true on success, PEAR_Error on failure
  542.      */
  543.     function removePage($pageID = null)
  544.     {
  545.         $tables = array_unique($this->_getLangTables());
  546.  
  547.         // get the tables and skip the non existent ones
  548.         $dbTables = $this->_fetchTableNames();
  549.         foreach ($tables as $table) {
  550.             if (!in_array($table, $dbTables)) {
  551.                 continue;
  552.             }
  553.             $query = sprintf('DELETE FROM %s WHERE %s',
  554.                  $this->db->quoteIdentifier($table, true),
  555.                  $this->db->quoteIdentifier($this->options['string_page_id_col'], true)
  556.             );
  557.             if (is_null($pageID)) {
  558.                 $query .= ' IS NULL';
  559.             } else {
  560.                 $query .= ' = ' . $this->db->quote($pageID, 'text');
  561.             }
  562.  
  563.             ++$this->_queries;
  564.             $res = $this->db->exec($query);
  565.             if (PEAR::isError($res)) {
  566.                 return $res;
  567.             }
  568.         }
  569.  
  570.         return true;
  571.     }
  572.  
  573.     // }}}
  574.     // {{{ getPageNames()
  575.     
  576.     /**
  577.      * Get a list of all the pageIDs in any table.
  578.      *
  579.      * @return array
  580.      */
  581.     function getPageNames()
  582.     {
  583.         $pages = array();
  584.         foreach ($this->_getLangTables() as $table) {
  585.             $query = sprintf('SELECT DISTINCT %s FROM %s',
  586.                  $this->db->quoteIdentifier($this->options['string_page_id_col'], true),
  587.                  $this->db->quoteIdentifier($table, true)
  588.             );
  589.             ++$this->_queries;
  590.             $res = $this->db->queryCol($query);
  591.             if (PEAR::isError($res)) {
  592.                 return $res;
  593.             }
  594.             $pages = array_merge($pages, $res);
  595.         }
  596.         return array_unique($pages);
  597.     }
  598.     
  599.     // }}}
  600.     // {{{ _tableLangs()
  601.  
  602.     /**
  603.      * Get table -> language mapping
  604.      *
  605.      * The key of the array is the table that a language is stored in;
  606.      * the value is an /array/ of languages stored in that table.
  607.      *
  608.      * @param array $langs Languages to get mapping for
  609.      *
  610.      * @return array Table -> language mapping
  611.      * @access private
  612.      * @see    Translation2_Container_MDB2::_getLangTable()
  613.      */
  614.     function &_tableLangs($langs)
  615.     {
  616.         $tables = array();
  617.         foreach ($langs as $lang) {
  618.             $table = $this->_getLangTable($lang);
  619.             $tables[$table][] = $lang;
  620.         }
  621.         return $tables;
  622.     }
  623.  
  624.     // }}}
  625.     // {{{ _getLangTables()
  626.  
  627.     /**
  628.      * Get tables for languages
  629.      *
  630.      * This is like _getLangTable(), but it returns an array of the tables for
  631.      * multiple languages.
  632.      *
  633.      * @param array $langs Languages to get tables for
  634.      *
  635.      * @return array
  636.      * @access private
  637.      */
  638.     function &_getLangTables($langs = null)
  639.     {
  640.         $tables = array();
  641.         $langs  = !is_array($langs) ? $this->getLangs('ids') : $langs;
  642.         foreach ($langs as $lang) {
  643.             $tables[] = $this->_getLangTable($lang);
  644.         }
  645.         $tables = array_unique($tables);
  646.         return $tables;
  647.     }
  648.  
  649.     // }}}
  650.     // {{{ _getLangCols()
  651.  
  652.     /**
  653.      * Get table columns strings are stored in
  654.      *
  655.      * This is like _getLangCol(), except it returns an array which contains
  656.      * the mapping for multiple languages.
  657.      *
  658.      * @param array $langs Languages to get mapping for
  659.      *
  660.      * @return array  Language -> column mapping
  661.      * @access private
  662.      * @see    Translation2_Container_DB::_getLangCol()
  663.      */
  664.     function &_getLangCols($langs)
  665.     {
  666.         $cols = array();
  667.         foreach ($langs as $lang) {
  668.             $cols[$lang] = $this->_getLangCol($lang);
  669.         }
  670.         return $cols;
  671.     }
  672.  
  673.     // }}}
  674.     // {{{ _recordExists()
  675.  
  676.     /**
  677.      * Check if there's already a record in the table with the
  678.      * given (pageID, stringID) pair.
  679.      *
  680.      * @param string $stringID string ID
  681.      * @param string $pageID   page/group ID
  682.      * @param string $table    table name
  683.      *
  684.      * @return boolean|PEAR_Error
  685.      * @access private
  686.      */
  687.     function _recordExists($stringID, $pageID, $table)
  688.     {
  689.         $stringID = $this->db->quote($stringID, 'text');
  690.         $pageID   = is_null($pageID) ? ' IS NULL' : ' = ' . $this->db->quote($pageID, 'text');
  691.         $query    = sprintf('SELECT COUNT(*) FROM %s WHERE %s=%s AND %s%s',
  692.             $this->db->quoteIdentifier($table, true),
  693.             $this->db->quoteIdentifier($this->options['string_id_col'], true),
  694.             $stringID,
  695.             $this->db->quoteIdentifier($this->options['string_page_id_col'], true),
  696.             $pageID
  697.         );
  698.         ++$this->_queries;
  699.         $res = $this->db->queryOne($query);
  700.         if (PEAR::isError($res)) {
  701.             return $res;
  702.         }
  703.         return ($res > 0);
  704.     }
  705.  
  706.     // }}}
  707.     // {{{ _filterStringsByTable()
  708.  
  709.     /**
  710.      * Get only the strings for the langs in the given table
  711.      *
  712.      * @param array  $stringArray Associative array with string translations.
  713.      *               Sample format: array('en' => 'sample', 'it' => 'esempio')
  714.      * @param string $table       table name
  715.      *
  716.      * @return array strings
  717.      * @access private
  718.      */
  719.     function &_filterStringsByTable($stringArray, $table)
  720.     {
  721.         $strings = array();
  722.         foreach ($stringArray as $lang => $string) {
  723.             if ($table == $this->_getLangTable($lang)) {
  724.                 $strings[$lang] = $string;
  725.             }
  726.         }
  727.         return $strings;
  728.     }
  729.  
  730.     // }}}
  731.     // {{{ _getLangsInTable()
  732.     
  733.     /**
  734.      * Get the languages sharing the given table
  735.      *
  736.      * @param string $table table name
  737.      *
  738.      * @return array
  739.      */
  740.     function &_getLangsInTable($table)
  741.     {
  742.         $this->fetchLangs(); // force cache refresh
  743.         $langsInTable = array();
  744.         foreach (array_keys($this->langs) as $lang) {
  745.             if ($table == $this->_getLangTable($lang)) {
  746.                 $langsInTable[] = $lang;
  747.             }
  748.         }
  749.         return $langsInTable;
  750.     }
  751.     
  752.     // }}}
  753. }
  754. ?>