Location: PHPKode > projects > Blandware AtLeap Lite - CMS on PHP > atleaplite/include/actions/global_property.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.
 */

/**
 * Actions for global properties.
 *
 * @package     AtleapLite
 * @author      Roman Puchkovskiy
 * @license     http://www.apache.org/licenses/LICENSE-2.0  Apache License, Version 2.0
 */

/**
 * Updates global properties.
 *
 * @param object $form  form object
 */
function doUpdateGlobalProperties(&$form)
{
    globalPropertyFromForm($form, 'maxImageWidth');
    globalPropertyFromForm($form, 'maxImageHeight');
    globalPropertyFromForm($form, 'thumbnailWidth');
    globalPropertyFromForm($form, 'thumbnailHeight');
    globalPropertyFromForm($form, 'pageCacheMaxAge');
    globalPropertyFromForm($form, 'resourceCacheMaxAge');

    saveGlobalProperties();
}

/**
 * Stores a global property to form object.
 *
 * @param object $form              form object
 * @param string $name              global property name
 * @param string $elemName optional name of element of form to which to save;
 * if omitted, it's assumed to be same as property name
 */
function globalPropertyToForm(&$form, $name, $elemName = null) {
    if ($elemName === null) {
        $elemName = $name;
    }
    $elem =& $form->getElement($elemName);
    $elem->setValue(getGlobalProperty($name));
}

/**
 * Retrieves a global property from form object and saves it.
 *
 * @param object $form              form object
 * @param string $name              global property name
 * @param string $elemName optional name of element of form from which to get;
 * if omitted, it's assumed to be same as property name
 */
function globalPropertyFromForm(&$form, $name, $elemName = null) {
    if ($elemName === null) {
        $elemName = $name;
    }
    setGlobalProperty($name, $form->getElementValue($elemName));
}

/**
 * Creates a global properties form.
 *
 * @param string $method    HTTP method
 * @param bool $init        whether form needs to be initialized from dao
 * @return object created form
 */
function &createGlobalPropertiesForm($method, $init) {
    $form = new FormBase('updateGlobalProperties', $method, buildUrl('updateGlobalProperties'));

    $form->addTextElement('maxImageWidth', getMessage('globalProperty.form.maxImageWidth'));
    $form->addTextElement('maxImageHeight', getMessage('globalProperty.form.maxImageHeight'));
    $form->addTextElement('thumbnailWidth', getMessage('globalProperty.form.thumbnailWidth'));
    $form->addTextElement('thumbnailHeight', getMessage('globalProperty.form.thumbnailHeight'));
    $form->addTextElement('pageCacheMaxAge', getMessage('globalProperty.form.pageCacheMaxAge'));
    $form->addTextElement('resourceCacheMaxAge', getMessage('globalProperty.form.resourceCacheMaxAge'));

    $form->addRequiredRule('maxImageWidth', getMessage('globalProperty.error.maxImageWidth.required'));
    $form->addDigitsRule('maxImageWidth', getMessage('globalProperty.error.maxImageWidth.digits'));
    $form->addRequiredRule('maxImageHeight', getMessage('globalProperty.error.maxImageHeight.required'));
    $form->addDigitsRule('maxImageHeight', getMessage('globalProperty.error.maxImageHeight.digits'));
    $form->addRequiredRule('thumbnailWidth', getMessage('globalProperty.error.thumbnailWidth.required'));
    $form->addDigitsRule('thumbnailWidth', getMessage('globalProperty.error.thumbnailWidth.digits'));
    $form->addRequiredRule('thumbnailHeight', getMessage('globalProperty.error.thumbnailHeight.required'));
    $form->addDigitsRule('thumbnailHeight', getMessage('globalProperty.error.thumbnailHeight.digits'));
    $form->addRequiredRule('pageCacheMaxAge', getMessage('globalProperty.error.pageCacheMaxAge.required'));
    $form->addDigitsRule('pageCacheMaxAge', getMessage('globalProperty.error.pageCacheMaxAge.digits'));
    $form->addRequiredRule('resourceCacheMaxAge', getMessage('globalProperty.error.resourceCacheMaxAge.required'));
    $form->addDigitsRule('resourceCacheMaxAge', getMessage('globalProperty.error.resourceCacheMaxAge.digits'));

    if ($init) {
        globalPropertyToForm($form, 'maxImageWidth');
        globalPropertyToForm($form, 'maxImageHeight');
        globalPropertyToForm($form, 'thumbnailWidth');
        globalPropertyToForm($form, 'thumbnailHeight');
        globalPropertyToForm($form, 'pageCacheMaxAge');
        globalPropertyToForm($form, 'resourceCacheMaxAge');
    }

    $form->addProceedElement(getMessage('common.button.update'));
    $form->addCancelElement(getMessage('common.button.cancel'));
    
    return $form;
}

/**
 * Shows a page with form to update global properties.
 */
function callUpdateGlobalProperties()
{
    global $smarty, $bottomLinks;

    setSystemMenuItemBold('updateGlobalProperties');

    loadGlobalProperties(true);

    $form =& createGlobalPropertiesForm('POST', true);

    showForm($smarty, $form, 'global_property/update.tpl', getMessage('globalProperty.update.title'));
}

/**
 * Updates global properties.
 */
function updateGlobalProperties()
{
    global $smarty, $bottomLinks;

    if (isCancelled()) {
        redirect(ADMIN_PAGE);
    }

    setSystemMenuItemBold('updateGlobalProperties');

    $form =& createGlobalPropertiesForm('POST', false);

    if ($form->validate()) {
        doUpdateGlobalProperties($form);
        redirect(ADMIN_PAGE);
    } else {
        showForm($smarty, $form, 'global_property/update.tpl', getMessage('globalProperty.update.title'));
    }
}

/**
 * Flushes all server cache.
 */
function flushServerCache() {
    $cacheManager = new CacheManager();
    $cacheManager->flushAllCache();
    redirect(ADMIN_PAGE);
}

/**
 * Returns URL to page with global properties form.
 *
 * @return URL
 */
function getGlobalPropertiesFormUrl() {
    return buildUrl('callUpdateGlobalProperties');
}


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