<?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/PhotoBlog.class.php");
  require_once(dirname(__FILE__)."/../../cms/Auth.class.php");
  require_once(dirname(__FILE__)."/../../cms/Errors.inc.php");
 
  $cms = new PhotoBlog($blogID);	
  $photoBlogID = "";
  $photoBlogText = "";
  $photoBlogPosition = "";
  $photoBlogColNumba = "1";
  $photoBlogThumbSize = "100";

  /**
   * Modify action
   */
  if (isset($_GET['modify'])){
  	$photoBlogID 		= $_GET['modify'];
  	$photoBlogText 		= $cms->getPhotoBlogText($photoBlogID);
  	$photoBlogComment 	= $cms->getPhotoBlogComment($photoBlogID);
  	$photoBlogPosition	= $cms->getPhotoBlogPosition($photoBlogID);
  	$photoBlogColNumba	= $cms->getPhotoBlogColNumba($photoBlogID);
  	$photoBlogThumbSize	= $cms->getPhotoBlogThumbSize($photoBlogID);
  	$modify = true;
  } else{
  	$modify = false;
  }


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


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

	  	$photoBlogID = stripslashes($_POST['photoBlogID']);
 	    if (isset($_POST['photoBlogText']))
		  	$photoBlogText = stripslashes($_POST['photoBlogText']);
		else
			$photoBlogText = "";
 	 	if (isset($_POST['photoBlogComment']))
		  	$photoBlogComment = stripslashes($_POST['photoBlogComment']);
		else
			$photoBlogComment = "";
 	 	if (isset($_POST['photoBlogPosition']))
	  		$photoBlogPosition 	= stripslashes($_POST['photoBlogPosition']);
	  	else
	  		$photoBlogPosition = "";
 	 	if (isset($_POST['photoBlogColNumba']))
	  		$photoBlogColNumba 	= stripslashes($_POST['photoBlogColNumba']);
	  	else
	  		$photoBlogColNumba = "1";
 	 	if (isset($_POST['photoBlogThumbSize']))
	  		$photoBlogThumbSize 	= stripslashes($_POST['photoBlogThumbSize']);
	  	else
	  		$photoBlogThumbSize = "100";
  	  	
		/** 
		 * Check values
		 */
		 $validator = new Validator();
		 $validator->validateTextOnlyNoSpaces($photoBlogID,"ID is empty or contains blanks or invalid chars");
		 $validator->validateTextOnly($photoBlogText,"Text is empty or contains invalid chars");
		 $validator->validateNumber($photoBlogPosition,"Position is not numeric");
		 if ($modify){
		 	if ($cms->exists($photoBlogID))
			 	$validator->addError("photoBlog ID must be unique, ".$photoBlogID." 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->createPhotoBlog($photoBlogID,$photoBlogText,$photoBlogComment,$photoBlogPosition,$photoBlogColNumba,$photoBlogThumbSize))){
			  		print(ERROR_FILESYSTEM_PERMISSION);
			  		die();
			  }else{
			  	$photoBlogID = "";
			  	$photoBlogText 	= "";
			  	$photoBlogComment  = "";
			  	$photoBlogPosition = "";
			  	$photoBlogColNumba = "1";
			  	$photoBlogThumbSize = "100";
			  }		  	
		 }

		 
  	}
  }
?>	
<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>ID</b></td>
			<td><input type="TEXT" name="photoBlogID" size="10" value="<?=$photoBlogID?>"></td>
		</tr>
		<tr>
			<td><b>Text</b>&nbsp;</td>
			<td><input type="TEXT" name="photoBlogText" size="40" value="<?=$photoBlogText?>"></td>
		</tr>
		<tr>
			<td><b>Album comment</b>&nbsp;</td>
			<td><textarea name="photoBlogComment" cols="50" rows="3"><?=$photoBlogComment?></textarea></td>
		</tr>		
		<tr>
			<td><b>Position</b></td>
			<td>
				<SELECT name="photoBlogPosition">
					<? for ($i=1;$i<100;$i++){
							if ($photoBlogPosition==$i)
								$selected = 'selected';
							else
								$selected = '';
							print('<OPTION value='.$i.' '.$selected.'>'.$i.'</OPTION>');
						} // end for
					?>
				</SELECT>
			</td>
		</tr>
		<tr>
			<td><b>Album Columns</b></td>
			<td>
				<SELECT name="photoBlogColNumba">
					<? for ($i=1;$i<20;$i++){
							if ($photoBlogColNumba==$i)
								$selected = 'selected';
							else
								$selected = '';
							print('<OPTION value='.$i.' '.$selected.'>'.$i.'</OPTION>');
						} // end for
					?>
				</SELECT>
			</td>
		</tr>
		<tr>
			<td><b>Thumbnail size</b></td>
			<td>
				<SELECT name="photoBlogThumbSize">
					<? for ($i=60;$i<260;$i=$i+10){
							if ($photoBlogThumbSize==$i)
								$selected = 'selected';
							else
								$selected = '';
							print('<OPTION value='.$i.' '.$selected.'>'.$i.'</OPTION>');
						} // end for
					?>
				</SELECT>
			</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="25%"><b>text<b></TD>
		<TD width="2%"><b>position<b></TD>
		<TD width="2%"><b>cols<b></TD>
		<TD width="2%"><b>thumbsize<b></TD>
	</TR>
<?php

	$pbs = $cms->getphotoBlogID();

	for($i=0;$i<$cms->getPhotoBlogCount();$i++){
		$id = $pbs[$i];
		$text = $cms->getPhotoBlogText($id);
		$position = $cms->getPhotoBlogPosition($id);
		$cols = $cms->getPhotoBlogColNumba($id);
		$thumbSize = $cms->getPhotoBlogThumbSize($id);
?>
	<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><?=$text?></TD>
		<TD align="right"><?=$position?></TD>
		<TD align="right"><?=$cols?></TD>
		<TD align="right"><?=$thumbSize?></TD>
	</TR>
<?php
	} // end for
?>
	<TR bgcolor="white">
		<TD colspan="8"><hr size="-1"></TD>
	</TR>
</TABLE>
