<?php 
/*
 *
 * sflog!
 * Copyright (C) 2005 Alberto Sfolcini <a.sfolcini@gmail.com>
 *
 * This file is a part of sflog!.
 *
 * sflog! is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * sflog! is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */
 
  require_once(dirname(__FILE__)."/../../cms/Validator.class.php");
  require_once(dirname(__FILE__)."/../../cms/Section.class.php");
  require_once(dirname(__FILE__)."/../../cms/Auth.class.php");
  require_once(dirname(__FILE__)."/../../cms/Errors.inc.php");
  
  $cms = new Section($blogID);	
  $sectionID = "";
  $sectionName = "";
  $sectionPosition = "";
  $sectionVisible = false;

  /**
   * Modify action
   */
  if (isset($_GET['modify'])){
  	$sectionID 	= $_GET['modify'];
  	$sectionName 	= $cms->getSectionFullName($sectionID);
  	$sectionPosition	= $cms->getSectionPosition($sectionID);
  	$sectionVisible	= $cms->isSectionVisible($sectionID);
  	$modify = true;
  } else{
  	$modify = false;
  }


  /**
   * Delete action
   */
  if (isset($_GET['delete'])){
  	$id = $_GET['delete'];
  	$cms->deleteSection($id);	
  }


  if (isset($_POST['sectionID'])){
  	if (!($_POST['sectionID']=='')){

	  	$sectionID = $_POST['sectionID'];
	   if (isset($_POST['sectionName']))
		  	$sectionName = stripslashes($_POST['sectionName']);
		else
			$sectionName = "";
 	 	if (isset($_POST['sectionPosition']))
	  		$sectionPosition 	= stripslashes($_POST['sectionPosition']);
	  	else
	  		$sectionPosition = "";
		if (isset($_POST['sectionVisible'])){
		  	if (strcasecmp($_POST['sectionVisible'],"on")==0)
		  		$sectionVisible = true;
		}else
			$sectionVisible = false;
  	  	
		/** 
		 * Check values
		 */
		 $validator = new Validator();
		 $validator->validateTextOnlyNoSpaces($sectionID,"sectionID is empty or contains blanks or invalid chars");
		 $validator->validateTextOnly($sectionName,"sectionName is empty or contains invalid chars");
		 $validator->validateNumber($sectionPosition,"sectionPosition is not numeric");
		 if ($modify){
		 	if ($cms->exists($sectionID))
			 	$validator->addError("SectionID must be unique, ".$sectionID." already exists.");
		 }
		 // prints errors
		 if ($validator->foundErrors()){
		 	print("Errors found:<BR>");
		 	print("<font color=\"red\">");	 	
		 	print($validator->listDotErrors());
		 	print("</font>");
		 }else { 	
			 /**
			  * Init CMS system
		  	  */
			  if (!($cms->createSection($sectionID,$sectionName,$sectionPosition,$sectionVisible))){
			  		print(ERROR_FILESYSTEM_PERMISSION);
			  		die();
			  }else{
			  	$sectionID = "";
			  	$sectionName = "";
			  	$sectionPosition = "";
			  	$sectionVisible = false;
			  }			  	
		 }

		 
  	}
  }
?>	
<FORM action="<?=$_SERVER['PHP_SELF']?>" method="POST">
	<input type="hidden" name="blogID" value="<?=$blogID?>">
	<table cellspacing="0" cellpaddgin="0" border="0">
		<tr>
			<td><b>Section ID</b></td>
			<td><input type="TEXT" name="sectionID" size="10" value="<?=$sectionID?>"></td>
		</tr>
		<tr>
			<td><b>Name</b>&nbsp;</td>
			<td><input type="TEXT" name="sectionName" size="20" value="<?=$sectionName?>"></td>
		</tr>
		<tr>
			<td><b>Position</b></td>
			<td>
				<SELECT name="sectionPosition">
					<? for ($i=1;$i<50;$i++){
							if ($sectionPosition==$i)
								$selected = 'selected';
							else
								$selected = '';
							print('<OPTION value='.$i.' '.$selected.'>'.$i.'</OPTION>');
						} // end for
					?>
				</SELECT>
			</td>
		</tr>
		<tr>
			<td><b>Visible</b></td>
			<td><input type="CHECKBOX" name="sectionVisible" <?if($sectionVisible) echo('checked')?>></td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td>
				<input type="SUBMIT" value="update" size="1">
			</td>
		</tr>
	</table>
</FORM>
<?php
  
  
?>
<TABLE width="99%" cellpadding="4" cellspacing="1" border="0" background="grey">
	<TR bgcolor="#C7D3E0">
		<TD width="1%">&nbsp;</TD>
		<TD width="1%">&nbsp;</TD>
		<TD width="1%">&nbsp;</TD>
		<TD width="10%"><b>id<b></TD>
		<TD width="40%"><b>name<b></TD>
		<TD width="2%"><b>position<b></TD>
		<TD width="2%"><b>visible</b></TD>
	</TR>
<?php

	$sections = $cms->getSectionsID();

	for($i=0;$i<$cms->getSectionsCount();$i++){
		$id = $sections[$i];
		$name = $cms->getSectionFullName($id);
		$position = $cms->getSectionPosition($id);
		$isVisible = $cms->isSectionVisible($id);
		if ($isVisible)
			$visible='<img src="images/public.jpg" width="16" height="16" border="0" alt="Visible">';
		else
			$visible='<img src="images/lock.jpg" width="16" height="16" border="0" alt="Not visible">';

?>
	<TR bgcolor="white">
		<TD><a href="<?=$_SERVER['PHP_SELF']."?delete=".$id.'&blogID='.$blogID?>"><img src="./images/delete.png" width="16px" height="16px" border="0" alt="delete"></a></TD>
		<TD><a href="<?=$_SERVER['PHP_SELF']."?modify=".$id.'&blogID='.$blogID?>"><img src="./images/edit.jpg" width="16px" height="16px" border="0" alt="edit"></a></TD>
		<TD><a href="<?='sectionContent.php?sectionID='.$id.'&blogID='.$blogID?>"><img src="./images/add.png" width="16px" height="16px" border="0" alt="Add content"></a></TD>
		<TD><?=$id?></TD>
		<TD><?=$name?></TD>
		<TD align="right"><?=$position?></TD>
		<TD align="center"><?=$visible?></TD>
	</TR>
<?php
	} // end for
?>
	<TR bgcolor="white">
		<TD colspan="7"><hr size="-1"></TD>
	</TR>
</TABLE>