<?php
/**
 * YANA library
 *
 * @package yana
 * @licence http://creativecommons.org/licenses/by-nc-sa/2.5/
 */

/**
 * «Singleton» Language
 *
 * @access      public
 * @package     yana
 * @subpackage  core
 */
class Language extends Singleton
{

    /**#@+
     * @ignore
     * @access  private
     */

    /** @var String    */ var $directory   = "";
    /** @var dirStream */ var $dirStream   = null;
    /** @var dirStream */ var $defaultDir  = null;
    /** @var array     */ var $files       = array();

    /**#@-*/

    /**
     * «Singleton» Constructor
     *
     * @param string $directory
     * @param dirStream $dirStream
     * @param string $defaultDir
     *
     * @ignore
     */
    function Language($directory, $dirStream, $defaultDir)
    {
        $this->checkSingletonConstraint();
        assert('is_string($directory) && is_dir($directory);');
        assert('is_string($defaultDir) && is_dir($defaultDir);');
        settype($directory, "String");
        settype($defaultDir, "String");

        $this->directory  = $directory;
        $this->dirStream  = $dirStream;
        $this->dirStream->read();
        $this->defaultDir = $defaultDir;
    }

    /**
     * read language strings from a file
     *
     * The file should be taken from 'languages/*.config',
     * without path and file extension.
     *
     * The file contents will automatically be stored in the global
     * registry and can be accessed using $YANA->getVar('LANGUAGE.SOME_VALUE').
     *
     * This function issues an E_USER_NOTICE if the file does not exist.
     * It returns bool(true) on success and bool(false) on error.
     *
     * @access  public
     * @param   string  $filename
     * @return  boolean
     */
    function read($filename)
    {
        assert('is_string($filename);');
        settype($filename,  "String");
        global $YANA;

        if (!preg_match("/^[\w_-\d]+$/i", $filename)) {
            trigger_error("The provided language-file id '".$filename."' contains illegal characters. Be aware that only alphanumeric (a-z,0-9,-,_) characters are allowed.", E_USER_NOTICE);
            return false;
        }
        if (isset($this->files[$filename])) {
            return true;
        }

        /* preload defaults */
        $defaultFile = $this->defaultDir.$filename.".config";
        if (file_exists($defaultFile)) {
            $LANGUAGE = getConfigFile($defaultFile);
        } else {
            $LANGUAGE = array();
        }

        /* override defaults where available */
        $selectedFile = $this->directory.$filename.".config";
        if ($selectedFile !== $defaultFile) {
            if (file_exists($selectedFile)){
                    $this->files[$filename] = new smlfile($selectedFile);
                    $this->files[$filename]->init();
                    $LANGUAGE = mergeArrays($LANGUAGE, $this->files[$filename]->get("*"));
            } else {
                /* No specific language file found, falling back to defaults. */
                trigger_error("No language-file '".$selectedFile."' found for id '".$filename."'. Falling back to default language.", E_USER_NOTICE);
            }
        }
        $YANA->merge("*", $LANGUAGE);
        return !empty($LANGUAGE);
    }

    /**
     * get
     *
     * @access public
     * @param string $reference (optional)
     * @param string $key (optional)
     * @return array
     *
     * @ignore
     */
    function get($reference = "*", $key = "*")
    {
        settype($reference, "String");
        settype($key,       "String");

        $REF = array();
        if (empty($reference)) {
            $reference = "*";
        }
        if (empty($key)) {
            $key = "*";
        }
        $reference = strtoupper($reference);
        $key       = strtoupper($key);

        if ($reference == "*") {
            foreach ($this->files as $a => $element)
            {
                $REF = mergeArrays($REF, $element->get($key));
            }
            return $REF;
        } else if (!isset($this->files[$reference])) {     
            return false;
        } else {
            return $this->files[$reference]->get($key);
        }

    }

    /**
     * getContent
     *
     * @access public
     * @return array (numeric array of strings)
     *
     * @ignore
     */
    function getContent()
    {
        $content = $this->dirStream->getContent();
        assert('is_array($content);');
        if (is_array($content)) {
            return $content;
        } else {
            return array();
        }
    }
    
    /**
     * getContent
     *
     * @access public
     * @return string
     *
     * @ignore
     */
    function getDirectory()
    {
        assert('is_string($this->directory);');
        return $this->directory;
    }
    
    /**
     * getInfo
     *
     * @access public
     * @param string $filename
     * @return array
     *
     * @ignore
     */
    function getInfo($filename)
    {
        assert('is_string($filename);');
        settype($filename, "String");

        $file = ($this->dirStream->getFilename()).$filename;
        if (is_file($file)) {
            $REF = getConfigFile($file);
            assert('is_array($REF["LANGUAGE_INFO"]);');
            if (is_array($REF['LANGUAGE_INFO'])) {
                $REF['LANGUAGE_INFO']['LAST_CHANGE'] = filemtime($file);
                clearstatcache();
                return $REF["LANGUAGE_INFO"];
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

}

?>