<?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/Link.class.php");
  require_once(dirname(__FILE__)."/../../cms/Auth.class.php");
  require_once(dirname(__FILE__)."/../../cms/Errors.inc.php");
  	  
  $cms = new Link($blogID);	
  $linkID = "";
  $linkText = "";
  $linkURL 	= "";
  $linkPosition = "";

  /**
   * Modify action
   */
  if (isset($_GET['modify'])){
  	$linkID 	= $_GET['modify'];
  	$linkText 	= $cms->getLinkText($linkID);
  	$linkURL 	= $cms->getLinkURL($linkID);
  	$linkPosition	= $cms->getLinkPosition($linkID);
  	$modify = true;
  } else{
  	$modify = false;
  }


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


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

	  	$linkID = stripslashes($_POST['linkID']);
	   if (isset($_POST['linkText']))
		  	$linkText = stripslashes($_POST['linkText']);
		else
			$LinkText = "";
	   if (isset($_POST['linkURL']))
		  	$linkURL = stripslashes($_POST['linkURL']);
		else
			$linkURL = "";
 	 	if (isset($_POST['linkPosition']))
	  		$linkPosition 	= stripslashes($_POST['linkPosition']);
	  	else
	  		$linkPosition = "";
  	  	
		/** 
		 * Check values
		 */
		 $validator = new Validator();
		 $validator->validateTextOnlyNoSpaces($linkID,"ID is empty or contains blanks or invalid chars");
		 $validator->validateGeneral($linkText,"Text is empty or contains invalid chars");
		 $validator->validateGeneral($linkURL,"URL is empty or contains invalid chars");
		 $validator->validateNumber($linkPosition,"Position is not numeric");
		 if ($modify){
		 	if ($cms->exists($linkID))
			 	$validator->addError("Link ID must be unique, ".$linkID." 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->createLink($linkID,$linkText,$linkURL,$linkPosition))){
			  		print(ERROR_FILESYSTEM_PERMISSION);
			  		die();
			  }else{
			  	$linkID = "";
			  	$linkText = "";
			  	$linkURL = "";
			  	$linkPosition = "";
			  }			  	
		 }

		 
  	}
  }
?>	
<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="linkID" size="10" value="<?=$linkID?>"></td>
		</tr>
		<tr>
			<td><b>Text</b>&nbsp;</td>
			<td><input type="TEXT" name="linkText" size="40" value="<?=$linkText?>"></td>
		</tr>
		<tr>
			<td><b>Url</b>&nbsp;</td>
			<td><input type="TEXT" name="linkURL" size="80" value="<?=$linkURL?>"></td>
		</tr>
		<tr>
			<td><b>Position</b></td>
			<td>
				<SELECT name="linkPosition">
					<? for ($i=1;$i<100;$i++){
							if ($linkPosition==$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="20%"><b>text<b></TD>
		<TD width="40%"><b>url<b></TD>
		<TD width="2%"><b>position<b></TD>
	</TR>
<?php

	$links = $cms->getLinkID();

	for($i=0;$i<$cms->getLinkCount();$i++){
		$id = $links[$i];
		$text = $cms->getLinkText($id);
		$url = $cms->getLinkURL($id);
		$position = $cms->getLinkPosition($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><?=$url?></TD>
		<TD align="right"><?=$position?></TD>
	</TR>
<?php
	} // end for
?>
	<TR bgcolor="white">
		<TD colspan="6"><hr size="-1"></TD>
	</TR>
</TABLE>
