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

/**
 * Default settings
 *
 * @category   Module (list-news)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-06-26
 */

 	global $system_data, $page_info, $news_database;
		
	if(!isset($module_settings['list-news']['mode'])) $module_settings['list-news']['mode'] = 'latest';
	if(!isset($module_settings["list-news"]["archive page"])) $module_settings["list-news"]["archive page"] = $page_info->alias;
	if(!isset($module_settings["list-news"]["latest"])) $module_settings["list-news"]["latest"] = 10;
	if(!isset($module_settings["list-news"]["pages"])) $module_settings["list-news"]["pages"] = 10;
	if(!isset($module_settings["list-news"]["show author"])) $module_settings["list-news"]["show author"] = 1;
	if(!isset($module_settings["list-news"]["show date"])) $module_settings["list-news"]["show date"] = 1;
	if(!isset($module_settings["list-news"]["show teaser"])) $module_settings["list-news"]["show teaser"] = 0;

/**
 * Languages
 *
 * @category   Module (list-news)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2010-07-28
 */

	$modules['list-news']['key'] = 'list-news';
	include_txt($modules['list-news']['key'], $system_data['folder']['modules'] . $modules['list-news']['key'] . '/languages/');
	include_txt($modules['list-news']['key'], $system_data['folder']['settings'] . 'modules/' . $modules['list-news']['key'] . '/languages/');

/**
 * Create news pages array
 *
 * @category   Module (list-news)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2010-02-05
 */
	
    // Defaults
    $news_database_folder = $system_data['folder']['database'] . 'news/';
    $module_settings["list-news"]["archive page"] = $system_data['frontend']['href attribute'] . $module_settings["list-news"]["archive page"];
    
    if($module_settings['list-news']['mode'] == 'topics') {
    	// Defaults
    	$module_settings["list-news"]["topics"] = is_file($news_database_folder . 'topics.xml') ? simplexml_load_file($news_database_folder . 'topics.xml') : simplexml_load_string('<?xml version="1.0" encoding="utf-8"?><cfiles></cfiles>');
		
		// Get number of pages in category
		foreach($module_settings['list-news']['topics'] as $topics_entry) {
			if(is_file($news_database_folder . $topics_entry->id . '.cat')) {
				$topic_entry_pages = file($news_database_folder . $topics_entry->id . '.cat');
				
				foreach($topic_entry_pages as $page_entry) {
					$page_entry_info = get_page_info(trim($page_entry));
					if($page_entry_info->published == 1) $topics_entry->pages = intval($topics_entry->pages) + 1;
				}
			}
		}
		
		// Is topic database empty?
		if(count($module_settings["list-news"]["topics"]->cfile) == 0) $module_settings['list-news']['mode'] = 'none';
    } else {
	    // Defaults
	    $module_settings["list-news"]['news archive'] = array();
	    $module_settings["list-news"]['latest pages'] = array();

    	if($module_settings['list-news']['mode'] == 'pages' && get_url_value('topic') != false && get_url_value('topic') != '') {
    		$database_topics_file = $news_database_folder . get_url_value('topic'). '.cat';
    		$database_topics_array = is_file($database_topics_file) ? file($database_topics_file) : array();
    		$database_topics_array = array_reverse($database_topics_array);
    		
    		foreach($database_topics_array as $page_entry) {
    			$page_entry_info = get_page_info(trim($page_entry));
    			
   				if($page_entry_info->published == 1)
   					$module_settings["list-news"]['latest pages'][] = $page_entry_info;
    		}    		
    	} else {	
		    if($module_settings['list-news']['mode'] == 'pages')
		    	$module_settings["list-news"]["latest"] = $module_settings["list-news"]["pages"];
		           
			// Get archive and latest
		    $news_database_files = filelist($news_database_folder, 'xml', 0, array('topics.xml'), true);
		
		    // Get pages for selected month
		    if($module_settings['list-news']['mode'] == 'pages' && get_url_value('year') != false && get_url_value('month') != false) {
		    	$news_catalogue_file = $news_database_folder . get_url_value('year') . '-' . get_url_value('month') . '.xml';
		    	if(is_file($news_catalogue_file)) $news_database_files = array($news_catalogue_file);
		    }
		    
		    rsort($news_database_files);
		    
		    foreach($news_database_files as $news_database_file) {
		    	$year = substr(basename($news_database_file), 0, 4);
		    	$month = substr(basename($news_database_file), 5, 2);
		    	
		    	$cat_xml = simplexml_load_file($news_database_file);
		    	$cat_xml->year = $year;
		    	$cat_xml->month = $month;
		    	$cat_xml->catalogue = str_replace('.xml', '', basename($news_database_file));
		    	
		    	$module_settings["list-news"]['news archive'][$year][] = $cat_xml;
		    	
		    	// Get latest pages
		    	if(get_url_value('year') != false && get_url_value('month') != false)
		    		$module_settings["list-news"]['latest'] = count($cat_xml->page);
		    	
		    	if(count($module_settings["list-news"]['latest pages']) < $module_settings["list-news"]['latest']) {
		    		$temp = array();
		    		
		    		foreach($cat_xml->page as $page_entry) {
		    			$page_entry_xml = get_page_info($page_entry->alias);
		    			if($page_entry_xml->published == 1) $temp[] = $page_entry_xml;
		    		}
		    		
		    		$module_settings["list-news"]['latest pages'] = array_merge($module_settings["list-news"]['latest pages'], array_reverse($temp));
		    	}
		    }
		}
	    
	    if(count($module_settings["list-news"]['latest pages']) > $module_settings["list-news"]['latest'])
		    $module_settings["list-news"]['latest pages'] = array_slice($module_settings["list-news"]['latest pages'], 0, $module_settings["list-news"]['latest']);
	    
	    // Disable mode if no news pages found
	    if(count($module_settings["list-news"]['news archive']) == 0)
	    	if($module_settings["list-news"]['mode'] != 'pages') $module_settings["list-news"]['mode'] = '';
	    
	    // Get content for latest pages
	    if($module_settings['list-news']['mode'] == 'pages') {
	    	foreach($module_settings['list-news']['latest pages'] as $page_entry) {
				$simulate_feed_script = 'true';
				$description = generate_content($system_data['folder']['pages'] . $page_entry->alias . '/' . $system_data['file']['index page'], $page_entry->alias);
				$description = strip_custom_tags($description) == '' ? strval($page_entry->teaser) : strip_custom_tags($description, true, true, true, false, true);
				if($description == '') $description = strval($page_entry->description);
				$page_entry->content = $description;
				$simulate_feed_script = 'false';
			}
	    }
    }
    
	connector('list-news frontend output');
	
/**
 * Include module frontend template
 *
 * @category   Module (list-news)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-06-26
 */
	
	include_template('list-news');

?>