<?php
/**
 * $Horde: kronolith/attendees.php,v 1.7.8.4 2005/07/18 19:24:27 selsky Exp $
 *
 * Copyright 2004-2005 Code Fusion  <http://www.codefusion.co.za/>
 *                Stuart Binge <s.binge@codefusion.co.za>
 *
 * See the enclosed file COPYING for license information (GPL).  If you
 * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
 */

@define('KRONOLITH_BASE', dirname(__FILE__));
require_once KRONOLITH_BASE . '/lib/base.php';
require_once KRONOLITH_BASE . '/lib/FBView.php';
require_once 'Horde/Identity.php';
require_once 'Horde/UI/Tabs.php';
require_once 'Horde/Variables.php';

// Get the current attendees array from the session cache.
$attendees = (isset($_SESSION['attendees']) && is_array($_SESSION['attendees'])) ? $_SESSION['attendees'] : array();

// Get our Action ID & Value. This specifies what action the user initiated.
$actionID = Util::getFormData('actionID', false);
$actionValue = Util::getFormData('actionValue', false);
if (!$actionID) {
    $actionID = Util::getFormData('clearAll') ? 'clear' : false;
    if (!$actionID) {
        $actionID = Util::getFormData('addNew') ? 'add' : false;
        $actionValue = Util::getFormData('newAttendees', '');
    }
}

// Get the current Free/Busy view; default to the 'day' view if none specified.
$view = Util::getFormData('view', 'day');

// Pre-format our delete image/link.
$delimg = Horde::img('delete.png', _("Remove Attendee"), null, $registry->getImageDir('horde'));

// Perform the specified action, if there is one.
switch ($actionID) {
case 'add':
    // Add new attendees. Multiple attendees can be seperated on a
    // single line by whitespace and/or commas.
    $new = preg_split('/[\s,]+/', $actionValue, -1, PREG_SPLIT_NO_EMPTY);
    if (is_array($new)) {
        foreach ($new as $newattendee) {
            // Avoid overwriting existing attendees with the default values
            if (!array_key_exists($newattendee, $attendees))
                $attendees[$newattendee] = array('attendance' => KRONOLITH_PART_REQUIRED, 'response' => KRONOLITH_RESPONSE_NONE);
        }
    }

    $_SESSION['attendees'] = $attendees;
    break;

case 'remove':
    // Remove the specified attendee.
    if (isset($attendees[$actionValue])) {
        unset($attendees[$actionValue]);
        $_SESSION['attendees'] = $attendees;
    }
    break;

case 'changeatt':
    // Change the attendance status of an attendee
    list($partval, $partname) = explode(' ', $actionValue, 2);
    if (array_key_exists($partname, $attendees)) {
        $attendees[$partname]['attendance'] = $partval;
        $_SESSION['attendees'] = $attendees;
    }
    break;

case 'dismiss':
    // Close the attendee window.
    if ($browser->hasFeature('javascript')) {
        Util::closeWindowJS();
    } else {
        $url = Util::getFormData('url');

        if (!empty($url)) {
            $location = Horde::applicationUrl($url, true);
        } else {
            $url = Util::addParameter($prefs->getValue('defaultview') . '.php', 'month', Util::getFormData('month'));
            $url = Util::addParameter($url, 'year', Util::getFormData('year'));
            $location = Horde::applicationUrl($url, true);
        }

        // Make sure URL is unique.
        $location = Util::addParameter($location, 'unique', md5(microtime()));

        header('Location: ' . $location);
    }
    break;

case 'clear':
    // Remove all the attendees.
    $attendees = array();
    $_SESSION['attendees'] = $attendees;
    break;
}

$ident = &Identity::singleton();
$identities = $ident->getAll('id');
$vars = &Variables::getDefaultVariables();
$tabs = &new Horde_UI_Tabs(null, $vars);
$tabs->addTab(_("Day"), 'javascript:switchView(\'day\')', 'day');
$tabs->addTab(_("Work Week"), 'javascript:switchView(\'workweek\')', 'workweek');
$tabs->addTab(_("Week"), 'javascript:switchView(\'week\')', 'week');

$attendee_view = &Kronolith_FreeBusy_View::singleton($view);

// Add the creator as a required attendee in the Free/Busy display (if
// the user has an email address configured).
$myemail = $ident->getValue('from_addr');
if (!empty($myemail)) {
    $vfb = Kronolith::getFreeBusy($myemail);
    if (!is_a($vfb, 'PEAR_Error')) {
        $attendee_view->addRequiredMember($vfb);
    } else {
        $notification->push(sprintf(_("Error retrieving free/busy information for %s: %s"), $myemail, $vfb->getMessage()));
    }
}

// Add the Free/Busy information for each attendee.
foreach ($attendees as $email => $status) {
    if ($status['attendance'] == KRONOLITH_PART_REQUIRED) {
        $vfb = Kronolith::getFreeBusy($email);
        if (!is_a($vfb, 'PEAR_Error')) {
            $attendee_view->addRequiredMember($vfb);
        } else {
            $notification->push(sprintf(_("Error retrieving free/busy information for %s: %s"), $email, $vfb->getMessage()));
        }
    } elseif ($status['attendance'] == KRONOLITH_PART_OPTIONAL) {
        $vfb = Kronolith::getFreeBusy($email);
        if (!is_a($vfb, 'PEAR_Error')) {
            $attendee_view->addOptionalMember($vfb);
        } else {
            $notification->push(sprintf(_("Error retrieving free/busy information for %s: %s"), $email, $vfb->getMessage()));
        }
    }
}

$timestamp = (int)Util::getFormData('timestamp', time());
list($vfb_html, $legend_html) = $attendee_view->render($timestamp);

$title = _("Edit attendees");
$form_handler = Horde::applicationUrl('attendees.php');
Horde::addScriptFile('tooltip.js', 'horde', true);
require KRONOLITH_TEMPLATES . '/common-header.inc';
require KRONOLITH_TEMPLATES . '/attendees/attendees.inc';
require $registry->get('templates', 'horde') . '/common-footer.inc';
