<?php if(!defined('master_script')) die();

/**
 * Copyright (C) Nico Wenig
 * All rights reserved
 *
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @filesource
 */



/**
 * Get data for category
 *
 * @category   Extension (edit category)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-05-04
 */
	
	$database_folder = $system_data['folder']['database'] . 'standard/';
	$database_file_categories = $database_folder . 'catalogue.xml';
	$database_categories = is_file($database_file_categories) ? simplexml_load_file($database_file_categories) : simplexml_load_string('<?xml version="1.0" encoding="utf-8"?><cfiles></cfiles>');

	$category_entry = simplexml_load_string('<cfile></cfile>');
	
	foreach($database_categories->cfile as $category_entry)
		if($category_entry->id == $_GET['category']) $category_info = $category_entry;

/**
 * Get available page templates
 *
 * @category   Extension (edit category)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-03-24
 */

	$page_templates = filelist($system_data['folder']['frontend template'], 'tpl', array(), array(), true);

/**
 * Delete category
 *
 * @category   Extension (edit category)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-05-14
 */
	
	if(isset($_POST['delete_category']) && $_SESSION['user'] == sha1($administrator_name)) {
		for($i = 0; $i < count($database_categories->cfile); $i++) {
			$category_id = strval($database_categories->cfile[$i]->id);
			
			if($category_id == $_GET['category'] && $category_id != $system_data['backend']['info pages id']) {
				unset($database_categories->cfile[$i]);
				$database_categories->saveXML($database_file_categories);
				
				$database_file_category = $database_folder . $category_id . '.cat';
				
				if(is_file($database_file_category)) {
					$folder_trashcan = $system_data['folder']['settings'] . 'extensions/trashcan/pages/';
					if(!is_dir($folder_trashcan)) rmkdir($folder_trashcan);

					// Move pages in category to trashcan
					$pages_list = file($database_file_category);
					
					foreach($pages_list as $entry) {
						$entry = trim($entry);
						rec_rmdir($folder_trashcan . $entry);
						if(is_dir($system_data['folder']['pages'] . $entry)) rename($system_data['folder']['pages'] . $entry, $folder_trashcan . $entry);
					}
					
					// Delete category file
					unlink($database_file_category);
				}
			}
		}
		
		connector('edit-category delete');
		
		header('location: index.php?backend=edit-category&category&status=category-deleted');
	}

/**
 * Save category
 *
 * @category   Extension (edit category)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-12-07
 */

	if(isset($_POST['save_category'])) {
		// Clean data
		$_POST['language'] = str_replace(' ', '', clean_string($_POST['language'], true));
		$_POST['category_name'] = clean_string($_POST['category_name'], true, false);
		$_POST['published'] = isset($_POST['published']) ? 1 : 0;
		$_POST['position'] = intval($_POST['position']);
		$_POST['description'] = clean_string($_POST['description'], true);
		$_POST['tags'] = clean_string($_POST['tags'], true);
		$_POST['pagetopic'] = clean_string($_POST['pagetopic'], true);
		
		if($_POST['category_name'] == '') $_POST['category_name'] = $category_info->title;
		
		$cleaned_tags = explode(',', $_POST['tags']);
		$_POST['tags'] = '';
		foreach($cleaned_tags as $tag) if(trim($tag) != '') $_POST['tags'] .= ($_POST['tags'] == '' ? '' : ', ') . trim($tag);
		
		// Get position
   		for($i = 0; $i < count($database_categories->cfile); $i++) if($database_categories->cfile[$i]->id == $category_info->id) break;
		
		// Change title, language and template
		$database_categories->cfile[$i]->title = xml_string($_POST['category_name']);
		$database_categories->cfile[$i]->language = xml_string($_POST['language']);
		$database_categories->cfile[$i]->template = xml_string($_POST['template']);
		$database_categories->cfile[$i]->description = xml_string($_POST['description']);
		$database_categories->cfile[$i]->tags = xml_string($_POST['tags']);
		$database_categories->cfile[$i]->pagetopic = xml_string($_POST['pagetopic']);
		
		// Change published status
		$database_categories->cfile[$i]->published = $_POST['published'];

		// Change position
		$category_nodes = '';

		for($j = 0; $j < count($database_categories->cfile) + 1; $j++) {
			if($j == $_POST['position']) $category_nodes .= $database_categories->cfile[$i]->asXML();
			
			if(isset($database_categories->cfile[$j]->id) && $database_categories->cfile[$j]->id != $category_info->id)
				$category_nodes .= $database_categories->cfile[$j]->asXML();
		}

		$database_categories = simplexml_load_string('<?xml version="1.0" encoding="utf-8"?><cfiles>' . $category_nodes . '</cfiles>');

		connector('edit category save');
		
		// Save changed database
		$database_categories->saveXML($database_file_categories);
		
		// Reload
		header('location: index.php?backend=edit-category&category=' . $_GET['category']);
	}

?>