<?php
# -- BEGIN LICENSE BLOCK ----------------------------------
#
# This file is part of Dotclear 2.
#
# Copyright (c) 2003-2008 Olivier Meunier and contributors
# Licensed under the GPL version 2.0 license.
# See LICENSE file or
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#
# -- END LICENSE BLOCK ------------------------------------

require dirname(__FILE__).'/../inc/admin/prepend.php';

dcPage::check('categories');

$cat_id = '';
$cat_title = '';
$cat_url = '';
$cat_desc = '';
$cat_position = '';

# Getting existing category
if (!empty($_REQUEST['id']))
{
	try {
		$rs = $core->blog->getCategory($_REQUEST['id']);
	} catch (Exception $e) {
		$core->error->add($e->getMessage());
	}
	
	if (!$core->error->flag() && !$rs->isEmpty())
	{
		$cat_id = $rs->cat_id;
		$cat_title = $rs->cat_title;
		$cat_url = $rs->cat_url;
		$cat_desc = $rs->cat_desc;
		$cat_position = $rs->cat_position;
	}
}

# Remove category
if (!empty($_POST['id']) && $cat_id && !empty($_POST['delete']))
{
	try {
		$core->blog->delCategory($_POST['id']);
	} catch (Exception $e) {
		$core->error->add($e->getMessage());
	}
	
	if (!$core->error->flag()) {
		http::redirect('categories.php');
	}
}

# Create or update a category
if (!empty($_POST))
{
	$cur = $core->con->openCursor($core->prefix.'category');
	
	$cur->cat_title = $cat_title = $_POST['cat_title'];
	
	if (isset($_POST['cat_desc'])) {
		$cur->cat_desc = $cat_desc = $_POST['cat_desc'];
	}
	
	if (isset($_POST['cat_url'])) {
		$cur->cat_url = $cat_url = $_POST['cat_url'];
	} else {
		$cur->cat_url = $cat_url;
	}
	
	# Update category
	if ($cat_id)
	{
		try {
			# --BEHAVIOR-- adminBeforeCategoryUpdate
			$core->callBehavior('adminBeforeCategoryUpdate',$cur,$cat_id);

			$core->blog->updCategory($_POST['id'],$cur);

			# --BEHAVIOR-- adminAfterCategoryUpdate
			$core->callBehavior('adminAfterCategoryUpdate',$cur,$cat_id);
			
			http::redirect('category.php?id='.$_POST['id'].'&upd=1');
		} catch (Exception $e) {
			$core->error->add($e->getMessage());
		}
	}
	# Create category
	else
	{
		try {
			# --BEHAVIOR-- adminBeforeCategoryCreate
			$core->callBehavior('adminBeforeCategoryCreate',$cur);

			$id = $core->blog->addCategory($cur);

			# --BEHAVIOR-- adminAfterCategoryCreate
			$core->callBehavior('adminAfterCategoryCreate',$cur,$id);

			http::redirect('categories.php?add=1');
		} catch (Exception $e) {
			$core->error->add($e->getMessage());
		}
	}
}



$title = $cat_id ? __('Edit category') : __('New category');

dcPage::open($title,
	dcPage::jsConfirmClose('category-form').	
	dcPage::jsToolBar().
	dcPage::jsLoad('js/_category.js')
);

if (!empty($_GET['upd'])) {
		echo '<p class="message">'.__('Category has been successfully updated.').'</p>';
}

echo
'<h2>'.html::escapeHTML($core->blog->name).' &gt; <a href="categories.php">'.
__('Categories').'</a> &gt; '.$title.'</h2>'.

'<form action="category.php" method="post" id="category-form">'.
'<fieldset class="constrained">'.
'<p><label class="required" title="'.__('Required field').'">'.__('Title:').' '.
form::field('cat_title',20,255,html::escapeHTML($cat_title),'',2).
'</label></p>'.

'<div class="lockable">'.
'<p><label>'.__('URL:').' '.form::field('cat_url',20,255,html::escapeHTML($cat_url),'',3).
'</label></p>'.
'<p class="form-note warn" id="note-cat-url">'.
__('Warning: If you set the URL manually, it may conflict with another category.').'</p>'.
'</div>'.

'<p class="area"><label for="cat_desc">'.__('Description:').'</label> '.
form::textarea('cat_desc',50,8,html::escapeHTML($cat_desc),'',4).
'</p>'.

'<p><input type="submit" accesskey="s" value="'.__('Save').'" tabindex="5" />'.
($cat_id ? form::hidden('id',$cat_id) : '').
$core->formNonce().
'</p>'.
'</fieldset>'.
'</form>';

dcPage::helpBlock('core_categories');
dcPage::close();
?>