<?php
/**
 * $Horde: kronolith/prefs.php,v 1.29 2003/05/22 21:48:29 chuck Exp $
 *
 * Copyright 2001-2003 Jon Parise <jon@horde.org>
 *
 * See the enclosed file COPYING for license information (GPL).  If you
 * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
 */

function handle_remote_cal_management($updated)
{
    global $prefs;

    $calName = Horde::getFormData('remote_name');
    $calUrl  = Horde::getFormData('remote_url');
    $calActionID = Horde::getFormData('remote_action', 'add');

    if ($calActionID == 'add') {
        if (!empty($calName) && !empty($calUrl)) {
            $cals = unserialize($prefs->getValue('remote_cals'));
            $cals[] = array('name' => $calName,
                            'url'  => $calUrl);
            $prefs->setValue('remote_cals', serialize($cals));                
            $updated = true;
            return false;
        }
    } else if ($calActionID == 'delete') {
        $cals = unserialize($prefs->getValue('remote_cals'));
        foreach ($cals as $key => $cal) {
            if ($cal['url'] == $calUrl) {
                unset($cals[$key]);
                break;
            }
        }
        $prefs->setValue('remote_cals', serialize($cals));                
        $updated = true;
        return false;
    }
    return true;
}

function handle_category_management($updated)
{
    global $prefs;

    $actionID = Horde::getFormData('managementActionID');
    $categoryID = Horde::getFormData('category_id');
    $categoryName = Horde::getFormData('category_name');
    $newcolors = Horde::getFormData('newColors');

    /* Always save colors. */
    $event_colors = '';
    foreach ($newcolors as $key => $color) {
        $event_colors .= (empty($event_colors)) ? "$key:$color" : "|$key:$color";
    }
    $prefs->setValue('event_colors', $event_colors);

    switch ($actionID) {
        case KRONOLITH_DELETE_CATEGORY:
            Kronolith::deleteCategory($categoryID);
            break;

        case KRONOLITH_RENAME_CATEGORY:
            Kronolith::renameCategory($categoryID, $categoryName);
            break;

        case KRONOLITH_ADD_CATEGORY:
            Kronolith::addCategory($categoryName);
            break;

        default:
            /* Must have been the Save button. */
            $update = true;
            return true;
    }

    /* Return false so it stays on this page. */
    return false;
}

function handle_shareselect($updated)
{
    global $prefs;

    $default_share = Horde::getFormData('default_share');
    if (!is_null($default_share)) {
        $sharelist = Kronolith::listCalendars();
        if (is_array($sharelist) > 0 && isset($sharelist[$default_share])) {
            $prefs->setValue('default_share', $default_share);
            $updated = true;
        }
    }
    return $updated;
}

function prefs_callback()
{
    global $prefs, $js_onLoad;

    if ($prefs->isDirty('language')) {
        $js_onLoad = 'if (window.parent.frames.horde_menu) window.parent.frames.horde_menu.location.reload();';
    }
}

define('KRONOLITH_BASE', dirname(__FILE__));
require_once KRONOLITH_BASE . '/lib/base.php';
require_once HORDE_BASE . '/lib/PrefsUI.php';
require KRONOLITH_BASE . '/config/prefs.php';

/* See if we have a preferences group set. */
$group = Horde::getFormData('group');
$js_onLoad = null;

if (PrefsUI::handleForm($group)) {
    $group = null;
    include KRONOLITH_BASE . '/config/prefs.php';
}

$title = _("User Options");
require KRONOLITH_TEMPLATES . '/common-header.inc';
Kronolith::menu();

/* Assign variables for select lists. */
if (!$prefs->isLocked('timezone')) {
    $timezone_options = &$tz;
}
if (!$prefs->isLocked('day_hour_start') || !$prefs->isLocked('day_hour_end')) {
    $day_hour_start_options = array();
    for ($i = 0; $i <= 48; $i++) {
        $day_hour_start_options[$i] = date(($prefs->getValue('twentyFour')) ? 'G:i' : 'g:ia', mktime(0, $i * 30, 0));
    }
    $day_hour_end_options = $day_hour_start_options;
}

/* Show the UI. */
PrefsUI::generateUI($group);

require KRONOLITH_TEMPLATES . '/common-footer.inc';
