<?php
/**
 * $Horde: horde/services/portal/edit.php,v 1.44.8.1 2005/01/03 12:25:46 jan Exp $
 *
 * Copyright 1999-2005 Chuck Hagenbuch <chuck@horde.org>
 * Copyright 2003-2005 Mike Cochrane <mike@graftonhall.co.nz>
 * Copyright 2003-2005 Jan Schneider <jan@horde.org>
 *
 * See the enclosed file COPYING for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 */

@define('HORDE_BASE', dirname(__FILE__) . '/../..');
require_once HORDE_BASE . '/lib/base.php';
require_once 'Horde/Block.php';
require_once 'Horde/Block/Collection.php';
require_once 'Horde/Block/Layout.php';
require_once 'Horde/Identity.php';
require_once 'Horde/Help.php';

if (!Auth::isAuthenticated()) {
    Horde::authenticationFailureRedirect();
}

// Get form values.
$col = (int)Util::getFormData('col');
$row = (int)Util::getFormData('row');
$action = Util::getFormData('action');
$edit_row = null;
$edit_col = null;

// Get full name for title.
$identity = &Identity::singleton();
$fullname = $identity->getValue('fullname');
if (empty($fullname)) {
    $fullname = Auth::getAuth();
}

// Instantiate the blocks objects.
$blocks = &Horde_Block_Collection::singleton('portal');
$layout = &Horde_Block_Layout::singleton();

// Handle requested actions.
switch ($action) {
case 'moveUp':
case 'moveDown':
case 'moveLeft':
case 'moveRight':
case 'expandUp':
case 'expandDown':
case 'expandLeft':
case 'expandRight':
case 'shrinkLeft':
case 'shrinkRight':
case 'shrinkUp':
case 'shrinkDown':
case 'removeBlock':
    $result = call_user_func(array(&$layout, $action), $row, $col);
    if (is_a($result, 'PEAR_Error')) {
        $notification->push($result);
    } else {
        $layout->save();
    }
    break;

// Save the changes made to a block.
case 'save':
// Save the changes made to a block and continue editing.
case 'save-resume':
    // Get requested block type.
    list($newapp, $newtype) = explode(':', Util::getFormData('app'));

    // Is this a new block?
    $new = false;
    if ($layout->isEmpty($row, $col) ||
        !$layout->rowExists($row) ||
        !$layout->colExists($col)) {
        $new = true;
        // Make sure there is somewhere to put it.
        $layout->addBlock($row, $col);
    }

    // Or an existing one?
    $exists = false;
    $changed = false;
    if (!$new) {
        // Get target block info.
        $info = $layout->getBlockInfo($row, $col);
        $exists = $layout->isBlock($row, $col);
        // Has a different block been selected?
        if ($exists &&
            ($info['app'] != $newapp ||
             $info['block'] != $newtype)) {
            $changed = true;
        }
    }

    if ($new || $changed) {
        // Change app or type.
        $info = array();
        $info['app'] = $newapp;
        $info['block'] = $newtype;
        $params = $blocks->getParams($newapp, $newtype);
        foreach ($params as $newparam) {
            $info['params'][$newparam] = $blocks->getDefaultValue($newapp, $newtype, $newparam);
        }
        $layout->setBlockInfo($row, $col, $info);
    } elseif ($exists) {
        // Change values.
        $layout->setBlockInfo($row, $col, array('params' => Util::getFormData('params', array())));
    }
    $layout->save();
    if ($action == 'save') {
        break;
    }

// Make a block the current block for editing.
case 'edit':
    $edit_row = $row;
    $edit_col = $col;
    break;
}

$title = _("My Portal Layout");
require HORDE_TEMPLATES . '/common-header.inc';
require HORDE_TEMPLATES . '/portal/menu.inc';
$notification->notify(array('listeners' => 'status'));
require HORDE_TEMPLATES . '/portal/edit.inc';
require HORDE_TEMPLATES . '/common-footer.inc';
