Location: PHPKode > projects > Blandware AtLeap Lite - CMS on PHP > atleaplite/include/util/messages.php
<?php
/*
 *  Copyright 2008 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * Utils to work with localization messages.
 *
 * @package     AtleapLite
 * @author      Roman Puchkovskiy
 * @license     http://www.apache.org/licenses/LICENSE-2.0  Apache License, Version 2.0
 */
    
/**
 * Messages mapping (from keys to values)
 */
$applicationResources = array();

/**
 * Parses a message bundle from text to array.
 *
 * @param string $text  message bundle text representation
 * @return array parsed message bundle
 */
function parseMessageBundle($text) {
    $result = array();
    $lines = split("\n", $text);
    foreach ($lines as $line) {
        if (trim($line) === '' || $line[0] == '#') {
            continue;
        }
        list($key, $value) = explode('=', $line, 2);
        $key = trim($key);
        if ($key !== '') {
            $value = trim($value);
            $result[$key] = $value;
        }
    }
    return $result;
}

/**
 * Returns application resources.
 *
 * @global array application resources
 * @return array application resources
 */
function &getApplicationResources() {
    global $applicationResources;

    return $applicationResources;
}

/**
 * Returns message by key.
 *
 * @param string $key       message key
 * @return string message
 */
function getMessage($key) {
    $applicationResources = &getApplicationResources();

    $language = getCurrentLanguage();

    $e = $applicationResources[$language][$key] === '' || $applicationResources[$language][$key] === null;
    if ($e) {
        return $key;
    }

    $format = $applicationResources[$language][$key];
    $num = func_num_args();
    $args = array($format);

    for ($i = 1; $i < $num; $i++) {
        $args[] = func_get_arg($i);
    }

    $result = @call_user_func_array('sprintf', $args);
    if ($result === null || $result === '' || $result === false) {
        $result = $key;
    }

    $result = htmlspecialchars($result);

    return $result;
}

/**
 * Returns message bundle for a given language.
 *
 * @param string $lang      language code
 * @return array message bundle
 */
function getMessageBundle($lang) {
    $applicationResources = &getApplicationResources();

    return $applicationResources[$lang];
}

/**
 * Loads message bundles from DB.
 *
 * @global string database URL
 * @param bool $forceLoad   of true, messages will be loaded from the DB even
 * if they are in cache
 */
function loadMessageBundles($forceLoad = false) {
    global $dsn;

    $applicationResources = &getApplicationResources();

    $cacheManager = new CacheManager();
    $applicationResources = $cacheManager->getApplicationResources();
    if ($forceLoad || !$applicationResources) {
        configurateDataObject($dsn);
        $applicationResources = array();

        $ar =& getDao('message_bundle');
        $bundles = $ar->fetchAll();
        foreach ($bundles as $bundle) {
            $applicationResources[$bundle->language] = parseMessageBundle($bundle->messages);
        }

        $cacheManager->putApplicationResources($applicationResources);
    }
}

/**
 * Loads message bundles from DB as text. Cache is ignored.
 *
 * @global string database URL
 * @return array message bundles
 */
function loadMessageBundlesAsText() {
    global $dsn;

    configurateDataObject($dsn);
    $result = array();

    $ar =& getDao('message_bundle');
    $bundles = $ar->fetchAll();
    foreach ($bundles as $bundle) {
        $result[$bundle->language] = $bundle->messages;
    }
    return $result;
}

/**
 * Saves message bundle to DB.
 *
 * @global string database URL
 * @param array $messages       messages
 * @param string $code          language code
 */
function saveMessageBundle($messages, $code) {
    global $applicationResources, $dsn;

    configurateDataObject($dsn);

    $ar =& getDao('message_bundle');
    $ar->language = $code;
    $number = $ar->find(true);
    $ar->messages = $messages;
    if ($number > 0) {
        $ar->update();
    } else {
        $ar->insert();
    }
    $applicationResources[$code] = parseMessageBundle($messages);

    $cacheManager = new CacheManager();
    $cacheManager->putApplicationResources($applicationResources);
}


loadMessageBundles();


?>
Return current item: Blandware AtLeap Lite - CMS on PHP