<?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/SectionBlog.class.php");
  require_once(dirname(__FILE__)."/../../cms/Auth.class.php");
  require_once(dirname(__FILE__)."/../../cms/Errors.inc.php");
  	  
  $cms = new SectionBlog($blogID);	
  $bsectionID = "";
  $bsectionName = "";
  $bsectionPosition = "";
  $bsectionVisible = false;

  /**
   * Modify action
   */
  if (isset($_GET['modify'])){
  	$bsectionID 	= $_GET['modify'];
  	$bsectionName 	= $cms->getBlogSectionFullName($bsectionID);
  	$bsectionPosition	= $cms->getBlogSectionPosition($bsectionID);
  	$bsectionVisible	= $cms->isBlogSectionVisible($bsectionID);
  	$modify = true;
  } else{
  	$modify = false;
  }


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


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

	  	$bsectionID = stripslashes($_POST['bsectionID']);
	   if (isset($_POST['bsectionName']))
		  	$bsectionName = stripslashes($_POST['bsectionName']);
		else
			$bsectionName = "";
 	 	if (isset($_POST['bsectionPosition']))
	  		$bsectionPosition 	= $_POST['bsectionPosition'];
	  	else
	  		$bsectionPosition = "";
		if (isset($_POST['bsectionVisible'])){
		  	if (strcasecmp($_POST['bsectionVisible'],"on")==0)
		  		$bsectionVisible = true;
		}else
			$bsectionVisible = false;
  	  	
		/** 
		 * Check values
		 */
		 $validator = new Validator();
		 $validator->validateTextOnlyNoSpaces($bsectionID,"Blog Section ID is empty or contains blanks or invalid chars");
		 $validator->validateTextOnly($bsectionName,"Blog Section Name is empty or contains invalid chars");
		 $validator->validateNumber($bsectionPosition,"Blog Section Position is not numeric");
		 if ($modify){
		 	if ($cms->exists($bsectionID))
			 	$validator->addError("Blog Section ID must be unique, ".$bsectionID." 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->createBlogSection($bsectionID,$bsectionName,$bsectionPosition,$bsectionVisible))){
			  		print(ERROR_FILESYSTEM_PERMISSION);
			  		die();
			  }else{
			  	$bsectionID = "";
			  	$bsectionName = "";
			  	$bsectionPosition = "";
			  	$bsectionVisible = false;
			  }			  	
		 }

		 
  	}
  }
?>	
<FORM action="<?=$_SERVER['PHP_SELF']?>" method="POST">
	<input type="hidden" name="blogID" value="<?=$blogID?>">
	<input type="hidden" name="contentType" value="<?=$contentType?>">
	<table cellspacing="0" cellpaddgin="0" border="0">
		<tr>
			<td><b>Blog Section ID</b></td>
			<td><input type="TEXT" name="bsectionID" size="10" value="<?=$bsectionID?>"></td>
		</tr>
		<tr>
			<td><b>Blog Section Name</b>&nbsp;</td>
			<td><input type="TEXT" name="bsectionName" size="20" value="<?=$bsectionName?>"></td>
		</tr>
		<tr>
			<td><b>Blog Section Position</b></td>
			<td>
				<SELECT name="bsectionPosition">
					<? for ($i=1;$i<50;$i++){
							if ($bsectionPosition==$i)
								$selected = 'selected';
							else
								$selected = '';
							print('<OPTION value='.$i.' '.$selected.'>'.$i.'</OPTION>');
						} // end for
					?>
				</SELECT>
			</td>
		</tr>
		<tr>
			<td><b>Blog Section Visible</b></td>
			<td><input type="CHECKBOX" name="bsectionVisible" <?if($bsectionVisible) 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="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

	$secs = $cms->getBlogSectionsID();

	for($i=0;$i<$cms->getBlogSectionsCount();$i++){
		$id = $secs[$i];
		$name = $cms->getBlogSectionFullName($id);
		$position = $cms->getBlogSectionPosition($id);
		$isVisible = $cms->isBlogSectionVisible($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.'&contentType='.$contentType?>"><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.'&contentType='.$contentType?>"><img src="./images/edit.jpg" width="16px" height="16px" border="0" alt="edit"></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="6"><hr size="-1"></TD>
	</TR>
</TABLE>
