<?php

/**
 * Save standard page
 *
 * @category   Extension (edit page)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 */
	
	// Clean data
	$_POST['position'] = !isset($_POST['position']) ? 0 : $_POST['position'];
	$_POST['category'] = isset($_POST['category']) ? clean_string($_POST['category'], true, false) : '';
	$_POST['description'] = clean_string($_POST['description'], true);
	$_POST['metatitle'] = clean_string($_POST['metatitle'], true);
	$_POST['pagetopic'] = clean_string($_POST['pagetopic'], true);
	$_POST['tags'] = clean_string($_POST['tags'], true);
	$_POST['url'] = isset($_POST['url']) ? $_POST['url'] : '';
	$_POST['language'] = str_replace(' ', '', clean_string($_POST['language'], true));
	
	// Settings
	$page_template_file = $system_data['folder']['settings'] . 'defaults/standard-page.php';
	if(!is_file($page_template_file)) $page_template_file = $system_data['folder']['defaults'] . 'pages/standard-page.php';
	
    // Create database folder
    if(!is_dir($database_folder)) rmkdir($database_folder);
    
    // Create or get category entry		
    $category_exists = 0;
    
    foreach($database_categories->cfile as $category_entry) {
    	if($category_entry->title == trim($_POST['category'])) {
    		$database_category_id = $category_entry->id;
    		$category_exists = 1;
    	}
    	
    	if($category_entry->id == $system_data['backend']['info pages id']) {
    		$info_category_title = $category_entry->title;
    	}
    }

    if(in_array($_GET['page'], $system_data['backend']['not editable pages'])) {
    	$database_category_id = $system_data['backend']['info pages id'];
    	$_POST['category'] = isset($info_category_title) ? $info_category_title : 'Info pages';
    	$category_exists = isset($info_category_title) ? 1 : 0;
    }
    
    if($category_exists == 0) {
    	if(!isset($database_category_id)) $database_category_id = time();
    	$last = count($database_categories->cfile);
    	
    	$database_categories->cfile[$last]->id = $database_category_id;
    	$database_categories->cfile[$last]->title = xml_string($_POST['category']);
    	$database_categories->cfile[$last]->published = ($_POST['published'] == 1) ? 1 : 0;
    	
    	$database_categories->saveXML($database_file_categories);
    }
    
    // Delete old database page entry
    $database_file_category = $database_folder . $page_info->catalogue . '.cat';
    $database_category = is_file($database_file_category) ? file($database_file_category) : array();
    
    for($i = 0; $i < count($database_category); $i++) {
    	$database_category[$i] = trim($database_category[$i]);
    	$database_category[$i] = ($database_category[$i] == $_GET['page'] || $database_category[$i] == '') ? '' : $database_category[$i] . "\n";
    }
    
    if($page_info->catalogue != '' && is_file($database_file_category))
    	file_put_contents($database_file_category, implode($database_category));
    
    // Reset page position if page move to new category
    if(strval($page_info->catalogue) != $database_category_id) $_POST['position'] = 0;
    
    // Create new database page entry
    $database_file_category = $database_folder . $database_category_id . '.cat';
    $database_category = is_file($database_file_category) ? file($database_file_category) : array();
    $database_category_new = array();
    
    for($i = 0; $i <= count($database_category); $i++) {
    	if($i == $_POST['position']) $database_category_new[] = $_POST['alias'] . "\n";
    	if(isset($database_category[$i])) {
    		$database_category[$i] = trim($database_category[$i]);
    		if($database_category[$i] != '') $database_category_new[] = $database_category[$i] . "\n";
    	}
    }
    
    file_put_contents($database_file_category, implode($database_category_new));
	
	// Set page info vars
	$page_info_xml->description = xml_string($_POST['description']);
	$page_info_xml->metatitle = xml_string($_POST['metatitle']);
	$page_info_xml->pagetopic = xml_string($_POST['pagetopic']);
	$page_info_xml->tags = xml_string($_POST['tags']);
	$page_info_xml->category = xml_string($_POST['category']);
	$page_info_xml->catalogue = xml_string($database_category_id);
	$page_info_xml->language = xml_string($_POST['language']);
	$page_info_xml->url = xml_string($_POST['url']);
    		
?>