<?php
/**
 * phpaga
 *
 * Settings 
 *
 * This is the sitewide settings management console.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id$
 *
 * 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.
 */

define('PHPAGA_COMPRESSOUT_DISABLED', true);
include_once('./config.php');
phpaga_user_pageperm(PHPAGA_PERM_MANAGE_SYSSETTINGS);

include_once(PHPAGA_LIBPATH.'pdf.php');

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

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

foreach(range(1, 12) as $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)));

foreach(range(1, $numdays) as $i)
    $options_days[$i] = $i;

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

if (isset($REQUEST_DATA['action']) && ($REQUEST_DATA['action'] == ACTION_SAVECONFIG)
    && (isset($REQUEST_DATA['cfgitem']) && (count($REQUEST_DATA['cfgitem'])))) {

    foreach($REQUEST_DATA['cfgitem'] as $key => $value) {

        $result = phpaga_config_updateitem($key, $value);

        if (PhPagaError::isError($result)) {
            $result->printMesssage();
            $update_result = $result;
        }
    }

    /* 
     * Check for uploaded files 
     *
     * Files related to system settings go directly into PHPAGA_FILEPATH/00/
     *
     */

    if (isset($_FILES['logo'])) {

        $dest_dir = PHPAGA_FILEPATH.'00/';
        $status = null;

        if (!is_dir(PHPAGA_FILEPATH))
            $status = sprintf(_("%s: The directory does not exist"), PHPAGA_FILEPATH);

        elseif (!is_dir($dest_dir) && !mkdir($dest_dir))
            $status = sprintf(_("%s: The directory does not exist and can't be created"), $dest_dir);

        elseif (!is_writable($dest_dir))
            $status = sprintf(_("%s: The directory is not writable"), $dest_dir);

        if (strlen($status)) {
            phpaga_error($status);
        } else {

            $ferror = $_FILES['logo']['error'];

            if ((!$ferror == UPLOAD_ERR_OK) && (!$ferror == UPLOAD_ERR_NO_FILE)) {
                $err = true;
                phpaga_error(sprintf(_("An error happened while trying to upload the file %s: %s"),
                    $_FILES['logo']['name'],
                    phpaga_files_getuploadstatusmessage($ferror)));
            } else if($ferror == UPLOAD_ERR_OK) {

                $dest_name = $_FILES['logo']['name'];

                if (!move_uploaded_file($_FILES['logo']['tmp_name'], $dest_dir.$dest_name)) {
                    $err = true;
                    phpaga_error(sprintf(_("An error happened while trying to upload the file %s: %s"),
                        $_FILES['logo']['name'],
                        $dest_name->getMessage()));
                } else {

                    $result = phpaga_config_updateitem('LETTERHEAD_LOGO_FILE', $dest_name);

                    if (PhPagaError::isError($result)) {
                        $result->printMesssage();
                    }
                }
            }
        }
    }

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

    $pdfclasses = array('invoice' => array('InvoicePDF' => 'InvoicePDF'), 'quotation' => array('QuotationPDF' => 'QuotationPDF'));

    foreach (get_declared_classes() as $class) {
        if (is_subclass_of($class, 'InvoicePDF'))
            $pdfclasses['invoice'][$class] = $class;
        elseif (is_subclass_of($class, 'QuotationPDF'))
            $pdfclasses['quotation'][$class] = $class;
    }

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

    if (isset($REQUEST_DATA['action']) && ($REQUEST_DATA['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');

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

    $options_countries = phpaga_countries_getCountryList();
    $options_currencies = phpaga_currencies_getCurrencyList();
    $options_languages = phpaga_getLanguages();
    $options_bmt = phpaga_bills_getInvoiceTypeList();
    $options_prjstat = phpaga_projects_getPrjstatus(true);

    $config = phpaga_config_get();

    $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_prjstat', $options_prjstat);
    $tpl->assign('options_pdfpageformat', $options_pdfpageformat);
    $tpl->assign('pdfclasses', $pdfclasses);

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

?>
