<?php

/**
 * Save news page
 *
 * @category   Extension (edit page)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2010-07-19
 */
	
	// Clean data
	$_POST['topic'] = isset($_POST['topic']) ? clean_string($_POST['topic'], true, false) : '';
	$_POST['description'] = clean_string($_POST['description'], true);
	$_POST['tags'] = clean_string($_POST['tags'], true);
	$_POST['teaser'] = clean_string($_POST['teaser']);
	
	// Settings
	$page_template_file = $system_data['folder']['defaults'] . 'pages/news-page.php';
	
    // Get database
    if(!is_dir($database_folder)) rmkdir($database_folder);
    
    $old_database_file = $database_folder . (intval($page_info->timestamp) == '' ? 'error' :  date('Y-m', intval($page_info->timestamp))) . '.xml';
    $new_database_file = $database_folder . date('Y-m', intval($_POST['timestamp'])) . '.xml';
    
    // Delete old database entry
    $old_database = is_file($old_database_file) ? simplexml_load_file($old_database_file) : simplexml_load_string('<?xml version="1.0" encoding="utf-8"?><pages></pages>');
    
    foreach($old_database->page as $page_entry) {
    	if(strval($page_entry->alias) == $page_info->alias) {
    		$old_database = removeSimpleXMLnode($old_database, $page_entry);
    		break;
    	}
    }
    
    if(is_file($old_database_file)) {
    	count($old_database->page) == 0 ? unlink($old_database_file) : $old_database->saveXML($old_database_file);
    }
    
    // Create new database entry
    $new_database = is_file($new_database_file) ? simplexml_load_file($new_database_file) : simplexml_load_string('<?xml version="1.0" encoding="utf-8"?><pages></pages>');
    $last_entry = count($new_database->page);
    
    $new_database->page[$last_entry]->published = xml_string($_POST['published']);
    $new_database->page[$last_entry]->searchable = xml_string($_POST['searchable']);
    $new_database->page[$last_entry]->timestamp = xml_string($_POST['timestamp']);
    $new_database->page[$last_entry]->alias = xml_string($_POST['alias']);
    $new_database->page[$last_entry]->title = xml_string($_POST['title']);
    $new_database->page[$last_entry]->language = xml_string($_POST['language']);
    $new_database->page[$last_entry]->topic = xml_string($_POST['topic']);
    
    $new_database_array = array();
    $i = 1;
    
    foreach($new_database->page as $page_entry) {
    	$new_database_array[intval($page_entry->timestamp) + $i] = $page_entry->asXML();
    	$i++;
    }
    
    ksort($new_database_array);
    $new_database = simplexml_load_string('<?xml version="1.0" encoding="utf-8"?><pages>' . implode('', $new_database_array) . '</pages>');
    $new_database->saveXML($new_database_file);
    
    // Get file paths for old and new topic database
    foreach($database_topics->cfile as $topic_entry) {
    	if(strval($topic_entry->title) == $page_info->topic) $old_catalogue_file = $database_folder . $topic_entry->id . '.cat';
    	if(strval($topic_entry->title) == $_POST['topic']) $new_catalogue_file = $database_folder . $topic_entry->id . '.cat';
    }
    
    // Delete old entry in topic database
    if(isset($old_catalogue_file)) {
    	$catalogue_array = file($old_catalogue_file);
    	$key = array_search(strval($page_info->alias) . "\n", $catalogue_array);
    	if($key !== false) unset($catalogue_array[$key]);
    	
    	$catalogue_string = trim(implode('', $catalogue_array));
    	if(is_file($old_catalogue_file)) $catalogue_string == '' ? unlink($old_catalogue_file) : file_put_contents($old_catalogue_file, $catalogue_string . "\n");
    	
    	if(!is_file($old_catalogue_file))
    		foreach($database_topics->cfile as $topic_entry)
    			if(strval($topic_entry->title) == $page_info->topic)
    				$database_topics = removeSimpleXMLnode($database_topics, $topic_entry);
    }
    
    // Create new entry in topic database
    if($_POST['topic'] != '') {
        if(!isset($new_catalogue_file) || (isset($old_catalogue_file) && !is_file($old_catalogue_file))) {
        	$cfile_id = time();
        	$new_catalogue_file = $database_folder . $cfile_id . '.cat';
        	
        	$cfiles = count($database_topics->cfile);
        	$database_topics->cfile[$cfiles]->title = $_POST['topic'];
        	$database_topics->cfile[$cfiles]->id = $cfile_id;
        }
        
        $catalogue_array = is_file($new_catalogue_file) ? file($new_catalogue_file) : array();
        
        $i = 1;
        
        foreach($catalogue_array as $catalogue_entry) {
        	$temp_page_info_xml = get_page_info(trim($catalogue_entry));
        	$new_catalogue_array[intval($temp_page_info_xml->timestamp) + $i] = trim($catalogue_entry);
        	$i++;
        }

        $new_catalogue_array[intval($_POST['timestamp']) + $i] = $_POST['alias'];
        
        ksort($new_catalogue_array);
        file_put_contents($new_catalogue_file, implode("\n", $new_catalogue_array) . "\n");
    }
    
    $database_topics->saveXML($database_file_topics);
    
	// Set page info vars
	$page_info_xml->description = xml_string($_POST['description']);
	$page_info_xml->tags = xml_string($_POST['tags']);
	$page_info_xml->topic = xml_string($_POST['topic']);
	$page_info_xml->language = xml_string($_POST['language']);
	$page_info_xml->teaser = xml_string($_POST['teaser']);
    $page_info_xml->catalogue = xml_string(basename($new_database_file));
    		
?>