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

/**
 * Default settings
 *
 * @category   Module (list-pages)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-05-13
 */
 	
 	global $system_data, $user_data, $pagelist, $page_info;
		
	if(!isset($module_settings['list-pages']['sort by'])) $module_settings['list-pages']['sort by'] = 'none';
	if(!isset($module_settings["list-pages"]["show info pages"])) $module_settings["list-pages"]["show info pages"] = 1;
	if(!isset($module_settings['list-pages']['sort ascending'])) $module_settings['list-pages']['sort ascending'] = 1;
	if(!isset($module_settings['list-pages']['show sort headlines'])) $module_settings['list-pages']['show sort headlines'] = 1;
	if(!isset($module_settings['list-pages']['categories'])) $module_settings['list-pages']['categories'] = array();
	if(!isset($module_settings['list-pages']['number of pages'])) $module_settings['list-pages']['number of pages'] = 0;

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

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

/**
 * Create pages array
 *
 * @category   Module (list-pages)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2010-06-30
 */
	
	// Get catalogue
	$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>');
	
	// Parse catalogue
	$pagelist = array();
	
	foreach($database_categories as $category_entry) {
		$id = strval($category_entry->id);
		
		// Exclude some categories
		$go = 1;
		
		if($id == $system_data['backend']['info pages id']) $go = $module_settings["list-pages"]["show info pages"];
		if($category_entry->published == 0) $go = 0;
		if(count($module_settings['list-pages']['categories']) > 0 && !in_array($id, $module_settings['list-pages']['categories'])) $go = 0;
		if(in_array($id, $module_settings['list-pages']['categories'])) $go = 1;
		
		$category_info_languages = explode(',', $category_entry->language);
		if($category_entry->language != '' && !in_array($_SESSION['language'], $category_info_languages)) $go = 0;
		
		// Create the pagelist
		if($go == 1) {
			$pagelist[$id]['id'] = $id;
			$pagelist[$id]['title'] = strval($category_entry->title);
			$pagelist[$id]['description'] = strval($category_entry->description);
			$pagelist[$id]['tags'] = strval($category_entry->tags);
			$pagelist[$id]['pages'] = array();
			$pagelist[$id]['first page'] = '';
			
			$catalogue_file = $database_folder . $id . '.cat';
			$catalogue_file_entries = is_file($catalogue_file) ? file($catalogue_file) : array();
			
			$i = 0;
			
			foreach($catalogue_file_entries as $catalogue_file_entry) {
				if($module_settings['list-pages']['number of pages'] == 0 || $module_settings['list-pages']['number of pages'] > $i) {
					$page_info_file = $system_data['folder']['pages'] . trim($catalogue_file_entry) . '/' . $system_data['file']['page info file'];
					
					if(is_file($page_info_file)) {
						$page_info_file_xml = simplexml_load_file($page_info_file);
						$page_info_languages = explode(',', $page_info_file_xml->language);
						
						if($page_info_file_xml->language == '' || $page_info_file_xml->language == $_SESSION['language'] || in_array($_SESSION['language'], $page_info_languages)) {
							if($pagelist[$id]['first page'] == '') $pagelist[$id]['first page'] = $system_data['frontend']['href attribute'] . $page_info_file_xml->alias;
							
							if($page_info_file_xml->published == 1) {
								$page_info_file_xml->href = ($page_info_file_xml->url == '') ? $system_data['frontend']['href attribute'] . $page_info_file_xml->alias : $page_info_file_xml->url;
								if($page_info_file_xml->alias == $system_data['file']['homepage']) $page_info_file_xml->href = $user_data['base url'];
								$pagelist[$id]['pages'][] = $page_info_file_xml;
								$i++;
							}
						}
					}
				}
			}
		}
	}
	
	// Sort pages
	if($module_settings['list-pages']['sort by'] != 'none') {
		switch($module_settings['list-pages']['sort by']) {
			case 'category' : case 'initial' : default : $sort_by = 'title'; break;
			case 'date' : $sort_by = 'timestamp'; break;
		}
		
		$pagelist_sorted = sort_pagelist($sort_by, $module_settings['list-pages']['sort ascending']);
		$pagelist = array();
		
		switch($module_settings['list-pages']['sort by']) {
			case 'category' : default :
				foreach($pagelist_sorted as $page_entry) {
					$key = strval($page_entry->category);
					$pagelist[$key]['title'] = $key;
					$pagelist[$key]['pages'][] = $page_entry;
				}
				
				break;

			case 'date' :
				foreach($pagelist_sorted as $page_entry) {
					$key = date($user_data["date format sort"], strval($page_entry->timestamp));
					$pagelist[$key]['title'] = $key;
					$pagelist[$key]['pages'][] = $page_entry;
				}
				
				break;

			case 'initial' :
				foreach($pagelist_sorted as $page_entry) {
					$key = strtoupper(substr(strval($page_entry->title), 0, 1));
					$pagelist[$key]['title'] = $key;
					$pagelist[$key]['pages'][] = $page_entry;
				}
				
				break;
		}
		
		($module_settings['list-pages']['sort ascending'] == 1) ? asort($pagelist) : arsort($pagelist);
	}
	
	connector('list-pages frontend output');
	
/**
 * Include module frontend template
 *
 * @category   Module (list-pages)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-05-13
 */

	include_template('list-pages');

?>