<?php
/**
 * phpaga
 *
 * Main
 *
 * This is the sitewide settings management console.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: siteconfig.php 726 2006-08-05 22:34:30Z florian $
 *
 * Copyright (c) 2002, Florian Lanthaler <florian@phpaga.net>
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in
 *      the documentation and/or other materials provided with the
 *      distribution.
 *
 *    * Neither the name of Florian Lanthaler nor the names of his
 *      contributors may be used to endorse or promote products derived
 *      from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

include_once ("./config.php");
phpaga_user_pageperm(PHPAGA_PERM_MANAGE_SYSSETTINGS);

$options_months = array();
$options_days = array();

$oldlocale = setlocale(LC_TIME, 0);
setlocale(LC_TIME, PHPAGA_LANGNAME);

for ($i = 1; $i <= 12; $i++) {
    $options_months[$i] = strftime("%B", strtotime("2005-$i-01"));
}

setlocale(LC_TIME, $oldlocale);

$numdays = date("t", strtotime(sprintf("2005-%s-01", PHPAGA_FIN_FISCYEAR_MONTH)));

for ($i = 1; $i <= $numdays; $i++) {
    $options_days[$i] = $i;
}

$config = array();
$update_result = PHPAGA_ERR_NONE;

if (isset($_REQUEST["action"])
    && ($_REQUEST["action"] == ACTION_SAVECONFIG)
    && (isset($_REQUEST["cfgitem"])
        && (count($_REQUEST["cfgitem"]))))
{

    foreach($_REQUEST["cfgitem"] as $key => $value)
    {
        $result = phpaga_config_updateitem ($key, $value);

        if ($result != PHPAGA_ERR_NONE)
        {
            phpaga_error($result);
            $update_result = $result;
        }
    }

    if ($update_result == PHPAGA_ERR_NONE)
        header("Location: siteconfig.php?action=".ACTION_OKMSG);
}
else
{

    phpaga_header(array("menuitem" => "administration"));

    if (isset($_REQUEST["action"]) && ($_REQUEST["action"] == ACTION_OKMSG))
        phpaga_message(_("Settings saved"),
                       _("The configuration settings have been saved successfully."));

    $options_truefalse = array();
    $options_truefalse["true"] = _("Yes");
    $options_truefalse["false"] = _("No");

    $options_imagetypes = array("jpg" => "jpg", "gif" => "gif", "png" => "png");

    $options_pdfpageformat = array("a4" => "a4", "letter" => "letter");

    $options_graphstyle = array("2" => "2D", "3" => "3D");

    $styles = phpaga_getSkinNames();
    $templatesets = phpaga_getTemplateSetNames();

    phpaga_countries_getCountryList($options_countries);
    phpaga_currencies_getCurrencyList($options_currencies);
    phpaga_getLanguages($options_languages);
    phpaga_bills_getInvoiceTypeList($options_bmt);

    phpaga_config_get($config);

    $tpl = new psmarty;

    $tpl->assign("admin", true);
    $tpl->assign("config", $config);
    $tpl->assign("options_truefalse", $options_truefalse);
    $tpl->assign("options_styles", $styles);
    $tpl->assign("options_templatesets", $templatesets);
    $tpl->assign("options_imagetypes", $options_imagetypes);
    $tpl->assign("options_countries", $options_countries);
    $tpl->assign("options_currencies", $options_currencies);
    $tpl->assign("options_languages", $options_languages);
    $tpl->assign("options_months", $options_months);
    $tpl->assign("options_days", $options_days);
    $tpl->assign("options_bmt", $options_bmt);
    $tpl->assign("options_pdfpageformat", $options_pdfpageformat);
    $tpl->assign("options_graphstyle", $options_graphstyle);

    $tpl->display("siteconfig.tpl.html");
    phpaga_footer();

}

?>