Location: PHPKode > scripts > Internationalization and Localization > internationalization-and-localization/DatabaseDataSource.class.php
<?php

/**
 * Project:     CWF: Cricava Web Framework
 * File:        DatabaseDataSource.class.php
 *
 * @link http://www.cricava.com/
 * @copyright 2005-2009 Cricava Technologies, Inc.
 * @author Mariano Iglesias <hide@address.com>
 * @package com.cricava.cwf
 * @version 1.0
 */

/**#@+
 * Includes
 */
require_once(dirname(__FILE__) . '/DataSource.class.php');
/**#@-*/

/**
 * This class implements a database based data source.
 *
 * @author  Mariano Iglesias
 * @package com.cricava.cwf
 * @subpackage i18n
 * @since   1.0
 */

class DatabaseDataSource extends DataSource
{
	/**
	 * The database connection (implementation of DatabaseConnectionManager)
	 *
	 * @access private
	 * @since 1.0
	 * @var DatabaseConnectionManager
	 */
	var $databaseConnection;

	/**
	 * Creates a new instance of DatabaseDataSource.
	 *
	 * @param DatabaseConnectionManager	The database connection (implementation of DatabaseConnectionManager)
	 *
	 * @access public
	 * @since 1.0
	 */
	function & DatabaseDataSource(&$databaseConnection)
	{
		$this->databaseConnection =& $databaseConnection;
	}

	/**
	 * Function that handles the fetching of elements.
	 * <ul>
	 * <li>If no element specified, it will load all elements for a section.</li>
	 * <li>If no section specified, it will load all sections for the current container.</li>
	 * </ul>
	 *
	 * @param string	The language to use
	 * @param string	The locale to use (can be null)
	 * @param string	The container to use
	 * @param string	The section from where to get elements
	 * @param string	The element to obtain
	 *
	 * @return array	An indexed array by container, section, and elements
	 *
	 * @access public
	 * @since 1.0
	 */
	function & fetch($language, $locale, $container, $section = null, $element = null)
	{
		if (!isset($this->databaseConnection))
		{
			return $elements;
		}

		$query = '
			SELECT
				e.id AS id, l.iso639_1 AS language, lo.iso3166_1 AS locale, c.name AS container, s.name AS section, e.name AS element, ec.content AS content
			FROM
				' . $this->databaseConnection->getTableName('i18n_language') . ' AS l,
				' . $this->databaseConnection->getTableName('i18n_container') . ' AS c,
				' . $this->databaseConnection->getTableName('i18n_section') . ' AS s,
				' . $this->databaseConnection->getTableName('i18n_element') . ' AS e,
				' . $this->databaseConnection->getTableName('i18n_element_content') . ' AS ec
					LEFT JOIN ' . $this->databaseConnection->getTableName('i18n_locale') . ' AS lo ON lo.id = ec.locale
			WHERE
				l.iso639_1 = \'' . $this->databaseConnection->escape($language) . '\'
				AND
				c.name = \'' . $this->databaseConnection->escape($container) . '\'';

		if (isset($section))
		{
			$query .= '
				AND
				s.name = \'' . $this->databaseConnection->escape($section) . '\'';
		}

		if (isset($element))
		{
			$query .= '
				AND
				e.name = \'' . $this->databaseConnection->escape($element) . '\'';
		}

		$query .= '
				AND
				s.container = c.id
				AND
				e.section = s.id
				AND
				ec.language = l.id
				AND
				ec.element = e.id
			ORDER BY
				c.name, s.name, e.name';

		$resultSet =& $this->databaseConnection->execute($query);

		if ($resultSet !== false)
		{
			while($row =& $this->databaseConnection->fetch_array($resultSet))
			{
				if (!isset($elements[$row['container']]))
				{
					$elements[$row['container']] = array();
				}

				if (!isset($elements[$row['container']][$row['section']]))
				{
					$elements[$row['container']][$row['section']] = array();
				}

				$replaceElement = false;

				if (!isset($elements[$row['container']][$row['section']][$row['element']]))
				{
					$replaceElement = true;
				}
				else if (isset($locale) && strcasecmp($row['locale'], $locale) == 0)
				{
					$replaceElement = true;
				}

				if ($replaceElement == true)
				{
					$elements[$row['container']][$row['section']][$row['element']] = $row['content'];
				}

			}
		}

		return $elements;
	}

	/**
	 * Obtain the list of languages and their locales.
	 *
	 * @param string	The language to use when obtaining language & locale names
	 * @param boolean	true if you want to obtain only active languages & locales, false otherwise
	 *
	 * @return array	The list of languages
	 *
	 * @access public
	 * @since 1.0
	 */
	function & fetchLanguages($language, $onlyActive = true)
	{
		if (!isset($this->databaseConnection))
		{
			return $languages;
		}

		$query = '
			SELECT
				l.id AS id, l.iso639_1 AS language, l.active AS active, ln.name AS name, lo.id AS locale_id, lo.iso3166_1 AS locale, lo.active AS locale_active, lon.name AS locale_name
			FROM
				' . $this->databaseConnection->getTableName('i18n_language') . ' AS l2,
				' . $this->databaseConnection->getTableName('i18n_language_name') . ' AS ln,
				' . $this->databaseConnection->getTableName('i18n_language') . ' AS l
					LEFT JOIN
						' . $this->databaseConnection->getTableName('i18n_locale') . ' AS lo ON (lo.language = l.id';

		if ($onlyActive == true)
		{
			$query .= ' AND (lo.active = \'Y\' OR lo.active = \'y\')';
		}

		$query .= ')
					LEFT JOIN
						' . $this->databaseConnection->getTableName('i18n_locale_name') . ' AS lon ON (lon.locale = lo.id AND lon.language = l2.id)
			WHERE
				l2.iso639_1 = \'' . $this->databaseConnection->escape($language) . '\'
				AND
				ln.language = l2.id';

		if ($onlyActive == true)
		{
			$query .= '
				AND
				(l.active = \'Y\' OR l.active = \'y\')';
		}

		$query .= '
				AND
				ln.language_parent = l.id
			ORDER BY
				ln.name, lon.name';

		$resultSet =& $this->databaseConnection->execute($query);

		if ($resultSet !== false)
		{
			while($row =& $this->databaseConnection->fetch_array($resultSet))
			{
				if (!isset($languages[$row['language']]))
				{
					$languages[$row['language']] = array (
						'code' => $row['language'],
						'name' => $row['name'],
						'active' => ($row['active'] == 'Y' || $row['active'] == 'y' ? true : false)
					);

					if ($onlyActive == true)
					{
						unset($languages[$row['language']]['active']);
					}
				}

				if (isset($row['locale']))
				{
					if (!isset($languages[$row['language']]['locales'][$row['locale']]))
					{
						$languages[$row['language']]['locales'][$row['locale']] = array (
							'code' => $row['locale'],
							'name' => $row['locale_name'],
							'active' => ($row['locale_active'] == 'Y' || $row['locale_active'] == 'y' ? true : false)
						);

						if ($onlyActive == true)
						{
							unset($languages[$row['language']]['locales'][$row['locale']]['active']);
						}
					}
				}
			}
		}

		return $languages;
	}

	/**
	 * Returns the database connection.
	 *
	 * @return DatabaseConnectionManager	The database connection.
	 *
	 * @access public
	 * @since 1.0
	 */
	function & getDatabaseConnection()
	{
		return $this->databaseConnection;
	}

	/**
	 * Sets the database connection.
	 *
	 * @param DatabaseConnectionManager	The database connection.
	 *
	 * @access public
	 * @since 1.0
	 */
	function & setDatabaseConnection(&$databaseConnection)
	{
		$this->databaseConnection =& $databaseConnection;
	}
}

?>
Return current item: Internationalization and Localization