Location: PHPKode > projects > Blandware AtLeap Lite - CMS on PHP > atleaplite/include/util/global_properties.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 global properties.
 *
 * @package     AtleapLite
 * @author      Roman Puchkovskiy
 * @license     http://www.apache.org/licenses/LICENSE-2.0  Apache License, Version 2.0
 */

/**
 */
require 'server_options.php';
require_once 'include/util/cache_manager.php';

/**
 * Global properties
 */
$globalProperties = array();

/**
 * Returns global properties.
 *
 * @global array global properties
 * @return array global properties
 */
function &getGlobalProperties() {
    global $globalProperties;

    return $globalProperties;
}

/**
 * Returns a global property value.
 *
 * @param string $name              property name
 * @param string $default optional  default value which is returned if no such
 * property is defined
 * @return string global property value
 */
function getGlobalProperty($name, $default = null) {
    $globalProperties = &getGlobalProperties();

    if (isset($globalProperties[$name])) {
        return $globalProperties[$name];
    } else {
        return $default;
    }
}

/**
 * Sets global property (only in memory, not in DB yet).
 *
 * @param string $name  property name
 * @param string $value property value
 */
function setGlobalProperty($name, $value) {
    $globalProperties = &getGlobalProperties();

    $globalProperties[$name] = $value;
}

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

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

        $gp =& getDao('global_property');
        $props = $gp->fetchAll();
        foreach ($props as $prop) {
            $globalProperties[$prop->name] = $prop->value;
        }

        $cacheManager->putGlobalProperties($globalProperties);
    }
}

/**
 * Saves global properties to DB.
 *
 * @global array global properties
 * @global string database URL
 */
function saveGlobalProperties() {
    global $globalProperties, $dsn;

    configurateDataObject($dsn);

    foreach ($globalProperties as $name => $value) {
        $gp =& getDao('global_property');
        $gp->name = $name;
        $number = $gp->find(true);
        $gp->value = $value;
        if ($number > 0) {
            $gp->update();
        } else {
            $gp->insert();
        }
    }

    $cacheManager = new CacheManager();
    $cacheManager->putGlobalProperties($globalProperties);
}


loadGlobalProperties();


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