<?php
/**********************************************************************/
# Title: MW- ver. 4.1
# Copyright By  Moy-weB  (http://moyweb.moy.su)
# Изготовлениш шаблонов (скинов), разработка дизайна, написание скриптов
# Версия для Lineage-II
# Corsar, I-Andi (2009)
/**********************************************************************/
require_once("header.php");
if (isset($_GET['id'])) 
{
$id = $_GET['id'];
if (!file_exists("../data/content/" . $id . ".php")) die ("Искомой страницы не существует. <a href='index.php'>На главную</a>");
include("../data/content/" . $id . ".php"); 
$file_menu = file("../data/menu.csv");
foreach( $file_menu as $line )
{
	$elem = explode (";", $line);
	if ( $elem[0] == $id ) 
	{
		$alias = $elem[1];
		$parent = trim($elem[3]);	
	}
}
?>
<br>
<h3>Редактирование страницы</h3> 

	<table border="0" width="850">
	<tr><td valign=top align="left">




<!----->

	<!-- This <div> holds alert messages to be display in the sample page. -->
	<div id="alerts">
		<noscript>
			<p>
				<strong>CKEditor requires JavaScript to run</strong>. In a browser with no JavaScript
				support, like yours, you should still see the contents (HTML data) and you should
				be able to edit it normally, without a rich editor interface.
			</p>
		</noscript>
	</div>
	<!-- This <fieldset> holds the HTML that you will usually find in your
	     pages. -->
	<form action="admin_edit.php" method="post">
<input type="hidden" name="action" value="do">
<input type="hidden" name="id" value="<? echo $id; ?>">
			<p>
				<label for="editor1">
					 (<span id="count"> </span>
					languages!):</label><br/>
				<select disabled="disabled" id="languages" onchange="createEditor( this.value );">
					<script type="text/javascript">
					//<![CDATA[

						// Get the language list from the _languages.js file.
						for ( var i = 0 ; i < window.CKEDITOR_LANGS.length ; i++ )
						{
							document.write(
								'<option value="' + window.CKEDITOR_LANGS[i].code + '">' +
									window.CKEDITOR_LANGS[i].name +
								'</option>' );
						}

					//]]>
					</script>
				</select>
				<br/>

			</p>
			<p>
				<textarea cols="80" id="message" name="editor1"  rows="10"><? echo $content; ?></textarea>
				<script type="text/javascript">
				//<![CDATA[

					// Set the number of languages.
					document.getElementById( 'count' ).innerHTML = window.CKEDITOR_LANGS.length;

					var editor;

					function createEditor( languageCode )
					{
						if ( editor )
							editor.destroy();

						// Replace the <textarea id="editor"> with an CKEditor
						// instance, using default configurations.
						editor = CKEDITOR.replace( 'editor1',
							{
								language : languageCode,

								on :
								{
									instanceReady : function()
									{
										// Wait for the editor to be ready to set
										// the language combo.
										var languages = document.getElementById( 'languages' );
										languages.value = this.langCode;
										languages.disabled = false;
									}
								}
							} );
					}

					// At page startup, load the default language:
					createEditor( '' );

				//]]>
				</script>
			</p>
	 

<!----->

<br />
</td></tr></table>
<table><tr><td valign=top>
<fieldset >
<legend>&nbsp;&nbsp; Данные страницы&nbsp;&nbsp;</legend>	

<table width="300" height="370">
<tr><td>Название страницы (title):</td><td><input name="title" type="text" value="<? echo $page_title; ?>" size="54"></td></tr>
<tr><td>Заголовок страницы (h3):</td><td><input name="h3" type="text" value="<? echo $page_h3; ?>" size="54"></td></tr>
<tr><td>Ключевые слова (keywords):</td><td><input name="keys" type="text" value="<? echo $page_keyws; ?>" size="54"></td></tr>
<tr><td>Описание страницы (description):</td><td><input name="desc" type="text" value="<? echo $page_descr; ?>" size="54"></td></tr>
<tr><td>Ссылка в меню:</td><td><input name="link" type="text" value="<? echo $page_4menu; ?>" size="54"></td></tr>
<tr><td>Псевдоним (alias):</td><td><input name="alias" type="text" size="54" value="<?php echo $alias;?>"/></td>
</tr>
    <td>Родительская страница</td>
  <td align="left">
  <?php
		$k = 0;
		
		$id = 0;
		if ( isset($_GET["id"]) && !empty($_GET["id"]))
		{
			$id = trim($_GET["id"]);
		}
		else
		{
			$alias = $_GET ['alias'];
			foreach( $file_menu as $line )
			{
				$el = explode(";",$line);
				if ( trim($el[1]) == trim($alias) ) 
				{
					$id = trim($el[0]);
				}
			}	
		}
		
		foreach( $file_menu as $line )
		{
			$elem = explode (";", $line);
		    if ( trim($elem[0]) == $id ) $parent = trim($elem[3]);		
		}
			
		if ( $parent == 0 ) $l .='<option value="0" selected>Корень меню</option>';
		else $l .= '<option value="0">Корень меню</option>'; 
		
		foreach( $file_menu as $line )
		{
			$elem = explode (";", $line);
			
			if ( trim($elem[3]) == 0 )
			{
				if ( trim($elem[0]) == $parent ) $l .= '<option value="'.$elem[0].'" selected>'.trim($elem[2]).'</option>';
				else $l .= '<option value="'.$elem[0].'">'.trim($elem[2]).'</option>';	
				++$k;
			}
		    
		}
	?>
  	<select name="parent" size="<?php echo($k + 1); ?>">
		<?=$l; ?>
	</select>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="Сохранить">

</form>
</td>
</tr></table>

</td><td valign="top">
		
<fieldset>
<legend>&nbsp;&nbsp; файлы и картинки &nbsp;&nbsp;</legend>		
		<table border=0>
		<tr>
		<td>
<iframe src="admin_img_form.php" frameborder="0" width="150" height="366" marginwidth="0" marginheight="0"></iframe>
		</td>
		
		<td>
<iframe src="admin_file_form.php" frameborder="0" width="150" height="366" marginwidth="0" marginheight="0"></iframe>
		</td>
		</tr>
		</table>
</fieldset>
</td></tr></table>
<br>

 




<?php
}
if (isset($_POST['action']))
{
	if (!isset($_POST['editor1']) || !isset($_POST['title']) || !isset ($_POST['link']) || !isset ($_POST['parent']) || !isset ($_POST['id']))
	{
		die ("Не указаны необходимые данные. <a href='index.php'>На главную</a>");
	}

	$text = stripslashes($_POST['editor1']);
	$content = "<?php\r\n\$page_title = '" . $_POST['title'] . "';\r\n" .
		"\$page_h3 = '" . $_POST['h3'] . "';\r\n" .
		"\$page_descr = '" . $_POST['desc'] . "';\r\n" .
		"\$page_keyws = '" . $_POST['keys'] . "';\r\n" .
		"\$page_4menu = '" . $_POST['link'] . "';\r\n" .
		"\$alias = '" . $_POST['alias'] . "';\r\n\r\n" .
		"\$content = <<< EOT\r\n" . $text . "\r\n" .
		"EOT;\r\n?>";
	if (!function_exists('file_put_contents'))
	{
		function file_put_contents ($filename, $content)
		{
			if ($fp = @fopen($filename, 'w'))
			{
				$result = fwrite($fp, $content);
				fclose ($fp);
				return $result;
			}
			else
			{
				return false;
			}
		} 
	}
	if (!file_put_contents("../data/content/" . $_POST['id'] . ".php", $content)) die ("Ошибка записи");
	else 
	{
		$csvcont='';
		$menu_file = file("../data/menu.csv");
		$zap = false;
		foreach($menu_file as $line)
		{
			$elem = explode(";",$line);
			if ( $elem[0] == $_POST['id'])
			{
				$csvcont = $csvcont.$_POST['id'].";".$_POST['alias'].";".$_POST['link'].";".$_POST['parent']."\n";
				$zap = true;
			}
			else $csvcont = $csvcont.$line;
		}
		if ( !$zap ) $csvcont .= $_POST['id'].";".$_POST['alias'].";".$_POST['link'].";".$_POST['parent']."\n";
		if(!file_put_contents('../data/menu.csv', $csvcont)) die('Ошибка записи меню');
		echo ("Успешно изменено. <a href='admin_selectpage.php'>К списку страниц</a>");
	}
}
require_once("footer.php");
?>