<!-- MODULE: comments -->

<?php quick_help(txt('comments', 'quick-help: comments.')); ?>

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

/**
 * Default settings
 *
 * @category   Module (comments)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2010-02-05
 */

	if(!isset($module_settings["comments"]["embedded"])) $module_settings["comments"]["embedded"] = 0;
	if(!isset($module_settings["comments"]["email notification"])) $module_settings["comments"]["email notification"] = 0;
	if(!isset($module_settings["comments"]["closed"])) $module_settings["comments"]["closed"] = 0;

/**
 * Get list of comment files in page storage folder
 *
 * @category   Module (comments)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-05-16
 */

	$page_storage_path = $system_data['folder']['pages'] . $_GET['page'] . '/storage/';
	
	if(file_exists($page_storage_path)) {
		$page_storage_path_open = opendir($page_storage_path);
	   	
	   	while($entry = readdir($page_storage_path_open)) {
	   		if(is_file($page_storage_path . $entry)) {
	   			$path_parts = pathinfo($entry);
	   			if($path_parts['extension'] == 'xml') {
		   			if(!isset($path_parts['filename'])) $path_parts['filename'] = substr($entry, 0, strrpos($entry, '.'));
	   				$comment_files[] = $path_parts['filename'];
	   			}
	   		}
	   	}
	   	
		closedir($page_storage_path_open);
	}

/**
 * Save changes to selected comment file
 *
 * @category   Module (comments)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-06-06
 */

	if(isset($_POST['save_comment'])) {
		$comment_file = $page_storage_path . $_POST['comment_file'] . '.xml';
		
		if(file_exists($comment_file)) {
			$xml_content = simplexml_load_file($comment_file);
			$number_of_comments = isset($xml_content->comments) ? count($xml_content->comments[0]->children()) : 0;
			$comments = array();

			for($i = 0; $i < $number_of_comments; $i++) {
				$comments[$i]['name'] = $xml_content->comments[0]->comment[$i]->name;
				$comments[$i]['email'] = $xml_content->comments[0]->comment[$i]->email;
				$comments[$i]['website'] = $xml_content->comments[0]->comment[$i]->website;
				$comments[$i]['timestamp'] = date($user_data["date format print"], intval($xml_content->comments[0]->comment[$i]->timestamp));
				$comments[$i]['text'] = $xml_content->comments[0]->comment[$i]->text;
			}
			
			if(isset($_POST['selected-comment'])) {
				$_POST['commentName'] = stripslashes(strip_tags(trim($_POST['commentName'])));
				$_POST['commentEmail'] = stripslashes(strip_tags(trim($_POST['commentEmail'])));
				$_POST['commentWebsite'] = stripslashes(strip_tags(trim($_POST['commentWebsite'])));
				$_POST['commentText'] = stripslashes(trim($_POST['commentText']));
				
				$comment_number = intval($_POST['selected-comment']);
				
				if($_POST['commentName'] != '') {
					// Change comment
					$xml_content->comments[0]->comment[$comment_number]->name = xml_string($_POST['commentName']);
					$xml_content->comments[0]->comment[$comment_number]->email = xml_string($_POST['commentEmail']);
					$xml_content->comments[0]->comment[$comment_number]->website = xml_string($_POST['commentWebsite']);
					$xml_content->comments[0]->comment[$comment_number]->text = xml_string($_POST['commentText']);
					
					// Delete comment
					if(isset($_POST['delete_comment'])) {
						unset($xml_content->comments[0]->comment[$comment_number]);
						$_POST['selected-comment'] = 0;
					}
					
					connector('comments backend save comment');
					
		    		$xml_content->saveXML($comment_file);
	    		}
			}
		}
	}

/**
 * Backend template
 *
 * @category   Module (comments)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-11-15
 */

?>

<form accept-charset="utf-8" method="post" action="index.php?backend=edit-content&amp;page=<?php echo $_GET['page']; ?>&amp;module-to-edit=<?php echo $_GET['module-to-edit']; ?>&amp;module-position=<?php echo $_GET['module-position']; ?>" onsubmit="return this.confirm">
	<?php include($system_data['folder']['modules'] . 'module-properties/backend-template.tpl'); ?>
	
	<!-- Settings -->
	<h4><?php echo txt('comments', 'title: comments'); ?></h4>
	
	<div class="column">
		<p>
			<input type="checkbox" name="embedded" id="embedded" <?php if($module_settings["comments"]["embedded"] == 1) echo 'checked="checked"'; ?>>
			<label class="checkbox" for="embedded"><?php echo txt('comments', 'Mit anderem Modul verknüpfen'); ?></label> 
			<small><a class="look-in-help" href="index.php?backend=help#embedded_modules" title="<?php echo txt('comments', 'Informationen zum Thema „Module verknüpfen” in der Hilfe nachschlagen'); ?>">?</a></small>
		</p>
		
		<p>
			<input type="checkbox" name="email_notification" id="email_notification" <?php if($module_settings["comments"]["email notification"] == 1) echo 'checked="checked"'; ?>>
			<label class="checkbox" for="email_notification"><?php echo txt('comments', 'Neue Kommentare melden'); ?></label>
		</p>

		<p>
			<input type="checkbox" name="close_comments" id="close_comments" <?php if($module_settings["comments"]["closed"] == 1) echo 'checked="checked"'; ?>>
			<label class="checkbox" for="close_comments"><?php echo txt('comments', 'Kommentare schließen'); ?></label>
		</p>
		
		<?php connector('comments backend left'); ?>
	</div>

	<div class="column last-column">
		<?php if(isset($comment_files)) { ?>
			<?php $active_comment_file = isset($_POST['comment_file']) ? $_POST['comment_file'] : ''; ?>
			
			<p>
				<label for="comment_file"><?php echo txt('comments', 'Seite oder Datei'); ?>:</label>
				<select size="1" name="comment_file" id="comment_file" onchange="loadCommentFile(this)">
					<?php if(in_array($_GET['page'], $comment_files)) { ?>
						<option value="<?php echo clean_output($_GET['page']); ?>" <?php if($active_comment_file == $_GET['page']) echo 'selected="selected"'; ?>><?php echo $page_info->title; ?></option>
					<?php } foreach($comment_files as $comment_file) if($comment_file != $_GET['page']) { ?>
						<option value="<?php echo clean_output($comment_file); ?>" <?php if($active_comment_file == $comment_file) echo 'selected="selected"'; ?>><?php echo $comment_file; ?></option>
					<?php } ?>
				</select>
			</p>
		<?php } ?>

		<?php connector('comments backend right'); ?>
	</div>
		
	<div style="clear: both"></div>
	
	<!-- Edit comment -->
	<h4><?php echo txt('comments', 'Kommentare bearbeiten'); ?></h4>
	
	<div style="width: 75%">
		<div id="comments-form-container"><?php if(!isset($comment_files)) echo txt('comments', 'Keine Kommentare gefunden.'); ?></div>
		<div id="comment-container"></div>
	</div>
		
	<?php if(isset($comment_files)) { ?>
	    <script type="text/javascript">
	    	// Defaults
	    	var dateFormat = '<?php echo $user_data["date format print"]; ?>';
	    	var pageStoragePath = '<?php echo $page_storage_path; ?>';
			var commentsFormContainer = document.getElementById('comments-form-container');
	    	var commentElement = document.getElementById('comment-container');
	    	var selectedComment = <?php echo isset($_POST['selected-comment']) ? $_POST['selected-comment'] : 0; ?>;
			var comments = [];
			
	    	// Load comment file
			var loadCommentFile = function(selectElement) {
				commentsFormContainer.innerHTML = '<div class="loading"></div>';
				
				var XMLfile = pageStoragePath + selectElement.options[selectElement.selectedIndex].value + '.xml';
	
				var XMLcontent = MoreCSS.createXMLHttpRequest();
			   	XMLcontent.open('get', noCache(XMLfile), true);
			   	
				XMLcontent.onreadystatechange = function() {
					if(XMLcontent.readyState == 4 && XMLcontent.status == 200) {
						var fileNode = XMLcontent.responseXML.getElementsByTagName('file')[0];
						if(fileNode) var commentsNode = fileNode.getElementsByTagName('comments')[0];
						if(commentsNode) showComments(commentsNode); else showMessageNoComments();
					}
				};
	
				XMLcontent.send(null);
			};
			
			// Show comments
			var showComments = function(commentsNode) {
				comments = [];
	
				// Get comment data
				var commentNodes = commentsNode.getElementsByTagName('comment');
				
				for(var i = 0; i < commentNodes.length; i++) {
					var name, website = '', email, timestamp, text;
					
					var nameNode = commentNodes[i].getElementsByTagName('name')[0];
					var emailNode = commentNodes[i].getElementsByTagName('email')[0];
					var websiteNode = commentNodes[i].getElementsByTagName('website')[0];
					var timestampNode = commentNodes[i].getElementsByTagName('timestamp')[0];
					var textNode = commentNodes[i].getElementsByTagName('text')[0];
					
					var name = (typeof nameNode.childNodes[0] != 'undefined') ? nameNode.childNodes[0].nodeValue : '';
					var email = (typeof emailNode.childNodes[0] != 'undefined') ? emailNode.childNodes[0].nodeValue : '';
					var website = (typeof websiteNode.childNodes[0] != 'undefined') ? websiteNode.childNodes[0].nodeValue : '';
					var timestamp = (typeof timestampNode.childNodes[0] != 'undefined') ? timestampNode.childNodes[0].nodeValue : '';
					var text = (typeof textNode.childNodes[0] != 'undefined') ? textNode.childNodes[0].nodeValue : '';
					
					comments[i] = [name, email, website, timestamp, text];
				}
				
				// Show select form with comments
				commentsFormContainer.innerHTML = '';
				var commentsForm = '';
	
				if(comments.length > 0) {
					for(var i = 0; i < comments.length; i++) {
						commentsForm += '<option value="' + i + '"' + (selectedComment == i ? 'selected="selected"' : '') + '>' + comments[i][0] + ', ' + date('d.m.Y, H:i', parseInt(comments[i][3])) + '</option>';
					}
		
					commentsFormContainer.innerHTML = '<label for="selectComment"><?php echo txt('comments', 'Kommentar'); ?>:</label><select style="width: 95%" size="1" name="selected-comment" id="selected-comment" onchange="editComment(this)">' + commentsForm + '</select>';
				}
				
				editComment(document.getElementById('selected-comment'));
			};
			
			// Show message no comments
			var showMessageNoComments = function() {
				commentsFormContainer.innerHTML = '<?php echo txt('comments', 'Keine Kommentare gefunden.'); ?>';
			};
			
			// Show comment to edit
			var editComment = function(selectElement) {
				if(selectElement) {
					var comment = comments[selectElement.options[selectElement.selectedIndex].value];
					
					commentForm = '<br><p><label for="commentName"><?php echo txt('comments', 'Name'); ?>:</label><input type="text" class="text" name="commentName" id="commentName" value="' + htmlspecialchars(comment[0]) + '"></p>';
					commentForm += '<p><label for="commentEmail"><?php echo txt('comments', 'E-Mail-Adresse'); ?>:</label><input type="text" class="text" name="commentEmail" id="commentEmail" value="' + htmlspecialchars(comment[1]) + '"></p>';
					commentForm += '<p><label for="commentWebsite"><?php echo txt('comments', 'Website'); ?>:</label><input type="text" class="text" name="commentWebsite" id="commentWebsite" value="' + htmlspecialchars(comment[2]) + '"></p>';
					commentForm += '<p><label for="commentText"><?php echo txt('comments', 'Text'); ?>:</label><textarea cols="55" rows="10" name="commentText" id="commentText">' + htmlspecialchars(comment[4]) + '</textarea></p>';
					commentForm += '<p><input type="submit" name="save_comment" id="save_comment" value="<?php echo txt('comments', 'Speichern'); ?>"> <input type="button" value="<?php echo txt('comments', 'Löschen'); ?>" onclick="if(window.confirm(\'<?php echo txt('comments', 'Wollen Sie diesen Kommentar wirklich löschen?'); ?>\') == true) { document.getElementById(\'delete_comment\').click(); document.getElementById(\'save_comment\').click(); }"> <input style="visibility: hidden" type="checkbox" name="delete_comment" id="delete_comment"></p>';
					
					commentElement.innerHTML = commentForm;
				} else showMessageNoComments();
			};
			
			// Start
			loadCommentFile(document.getElementById('comment_file'));
		</script>
	<?php } ?>
	
	<!-- Save -->
	<input name="save_comments" type="hidden">
	<input id="save" name="save_content" type="submit" value="<?php echo txt('comments', 'Speichern'); ?>">
	<input id="delete" name="delete_content" type="submit" value="<?php echo txt('comments', 'Löschen'); ?>" onclick="this.form.confirm = window.confirm('<?php echo txt('comments', 'Soll das Inhaltselement wirklich gelöscht werden?'); ?>')">
</form>