<?php
@define('PENGUINCODER_BASE', dirname(__FILE__));
require PENGUINCODER_BASE . '/lib/base.php';

global $pdata;

// page variables
$actionId = Util::getFormData('actionId');
$id = Util::getFormData('id');
$category = Util::getFormData('category');
$admin = Auth::getAuth('penguincoder:admin');
$adminDelete = Auth::getAuth('penguincoder:admin', PERMS_DELETE);
$goodEntry = true;

// sorting variables, only for list showing and determining which
// direction to fetch the next journal from (previous, next)
// direction is sort direction, either resolves to true (ascending) or
// false (descending)
$direction = Util::getFormData('direction');
if ($direction == '1') {
    $direction = true;
} elseif ($direction == '0') {
    $direction = false;
} else {
    $direction = null;
}

// order is the field to order the entries by, true is date otherwise it
// is defaulted to the entry title
$order = Util::getFormData('order');
if ($order !== 'title') {
    $order = 'id';
}

switch ($actionId) {
case 'post':
case 'edit':
    require_once PENGUINCODER_BASE . '/lib/DataForm.php';
    if (!$admin) {
        // only admins can edit
        header('Location: ' . Horde::selfUrl());
        break;
    }
    $vars = &Variables::getDefaultVariables();
    $vars->set('id', $id);
    $vars->set('actionId', $actionId);
    $vars->set('category', $category);
    $form = &Horde_Form::singleton('PDataForm', &$vars);
    if ($form->validate($vars)) {
        $vals = array(
                'title' => $vars->get('title'),
                'info' => $vars->get('info'),
                'category' => $vars->get('category'),
                'attributes' => serialize(array()),
        );
        if ($actionId == 'edit') {
            $vals['time_modified'] = time();
            $result = $pdata->update($id, $vals);
        } else {
            $vals['time_created'] = time();
            $result = $pdata->add($vals);
        }
        if (is_a($result, 'PEAR_Error')) {
            $notification->push(_("There was a problem saving the entry: ") .
                    $result->getMessage(), 'horde.error');
        } else {
            $notification->push(_("Success!"), 'horde.success');
            $goodEntry = false;
        }
    } elseif ($actionId == 'edit') {
        $entry = $pdata->getById($id);
        if (is_a($entry, 'PEAR_Error')) {
            $goodEntry = false;
            $notification->push(_("There was a problem fetching the data: ") .
                    $entry->getMessage(), 'horde.error');
        } else {
            $vars->set('time_created', $entry['time_created']);
            $vars->set('category', $entry['category']);
            $vars->set('title', $entry['title']);
            $vars->set('info', $entry['info']);
        }
    }
    break;
case 'show':
    // base url for generation later
    $baseUrl = Util::addParameter(Horde::selfUrl(), 'actionId', 'show');
    $appUrl = Horde::selfUrl();

    // get the maximum journal id
    $maxJournalId = $pdata->getMaxJournalId();
    $minJournalId = $pdata->getMinJournalId();

    // if no direction has been specified, then you want this id, fetch it
    if (is_null($direction)) {
        $entry = $pdata->getById($id);
    } elseif ($direction) {
        $entry = $pdata->getJournalById($id, '>', 'ASC');
    } else {
        $entry = $pdata->getJournalById($id, '<', 'DESC');
    }

    // check the fetched entry
    if (is_a($entry, 'PEAR_Error')) {
        $goodEntry = false;
        $notification->push("There was a problem getting the journal entry: " .
            $entry->getMessage(), 'horde.error');
    }
    break;
default:
    if ($direction) {
        $direction = 'ASC';
    } else {
        $direction = 'DESC';
    }
    $entries = $pdata->getByCategory('Journal', $order, $direction);
    if (is_a($entries, 'PEAR_Error')) {
        $goodEntry = false;
        $notification->push("There was a problem getting the information: " .
            $entries->getMessage(), 'horde.error');
    }
    break;
}

require PENGUINCODER_TEMPLATES . '/common-header.inc';
require PENGUINCODER_TEMPLATES . '/menu/menu.inc';

switch ($actionId) {
case 'post':
case 'edit':
    if (!is_null($form)) {
        $renderer = &new Horde_Form_Renderer();
        if ($goodEntry) {
            $form->renderActive($renderer, $vars, Horde::selfUrl(), 'post');
        } else {
            $form->renderInactive($renderer, $vars, Horde::selfUrl(), 'post');
        }
    }
    break;
case 'show':
    $commentcount = $registry->call('forums/numMessages', array(
        $entry['id'], 'penguincoder'));
    if (is_a($commentcount, 'PEAR_Error')) {
        $commentcount = 0;
    }
    if (!is_a($entry, 'PEAR_Error')) {
        require PENGUINCODER_TEMPLATES . '/data/header.inc';
        require PENGUINCODER_TEMPLATES . '/actions/header.inc';
        require PENGUINCODER_TEMPLATES . '/actions/main.inc';
        require PENGUINCODER_TEMPLATES . '/actions/nav.inc';
        require PENGUINCODER_TEMPLATES . '/actions/admin.inc';
        require PENGUINCODER_TEMPLATES . '/actions/footer.inc';
        require PENGUINCODER_TEMPLATES . '/data/data.inc';
        require PENGUINCODER_TEMPLATES . '/data/footer.inc';
    }
    $comments = $registry->call('forums/postMessage', array(
        'penguincoder', $entry['id'], 'commentCallback'
    ));
    $threads = $registry->call('forums/renderThreads', array(
        $entry['id'], true, 'penguincoder'));
    if (is_a($threads, 'PEAR_Error')) {
        $threads = $threads->getMessage();
    }
    if (is_a($comments, 'PEAR_Error')) {
        $comments = $comments->getMessage();
    }
    if (!empty($threads)) {
        echo '<br />' . $threads;
    }
    if (!empty($comments)) {
        echo '<br />' . $comments;
    }
    break;
default:
    if (!is_a($entries, 'PEAR_Error') && count($entries)) {
        if ($direction == 'DESC') {
            $direction = 0;
        } else {
            $direction = 1;
        }
        require PENGUINCODER_TEMPLATES . '/journal/listheader.inc';
        $style = 'item0';
        $counter = 0;
        foreach ($entries as $entry) {
            if ($style === 'item1') {$style = 'item0';}
            else {$style = 'item1';}
            include PENGUINCODER_TEMPLATES . '/journal/listentry.inc';
            $counter++;
        }
        include PENGUINCODER_TEMPLATES . '/journal/listfooter.inc';
    }
    break;
}
echo '<p>&nbsp;</p>';
require $registry->get('templates', 'horde') . '/common-footer.inc';