Location: PHPKode > projects > Jarida > jarida_1.0/admin/article.php
<?php

include("header.php");

if(!$_GET['do'])
{
  //If $do is empty we load the default page listing all articles

  //We select all article_id from the database and initialize article objects...
  $query = "SELECT article_id FROM tblArticle ORDER BY article_id DESC";

  $result = $db->doQuery($query);

  while($row = $result->getArray())
    {
      $article = new Article($db, $row['article_id']);
      $articles[] = array('id' => $article->getId(),
			  'numComments' => $article->getNumComments(),
			  'title' =>$article->getTitle());
    }
      
  $template->assign('articles', $articles);
    
}



if($_GET['do'] == "add")
{
  if($_POST['submit'])
    {
     
      $title = $_POST['title'];     
      $body = $_POST['body'];
      $date = time();
      $views = 0;

      $query = "INSERT INTO tblArticle (article_title, article_date,
                    article_body, article_views)
                    VALUES ('$title', '$date', '$body', '$views')";
      
      if(!$result = $db->doQuery($query))
	{
	  $confirm_message = $lang['db_problem'] . "<br><br>" . $db->getError();
	}
      else
	{
	  $confirm_message = "<p>" . $lang['add_success'] . "</p><p><a href=\"$PHP_SELF\">" . $lang['manage_articles'] . "</a></p>";
	}

      if($_FILES['file']['name'])
	{
	  $article_id = $result->getId();
	  $file = add_img("../img/upload/");
	  $query = "INSERT INTO tblArticlePhoto (photo_article_id, photo_filename) VALUES('$article_id', '$file')";
	  $result = $db->doQuery($query);
	}


      $template->assign('confirm_message', $confirm_message);
    }
  else
    {
      //Lets display the available smilies
      $query = "SELECT * FROM tblSmilie";

      $result = $db->doQuery($query);

      while($row = $result->getarray())
	{
	  $smilie = new Smilie($db, $row['smilie_id']);
	  $smilies[] = array('file' => $smilie->getFile(),
			     'code' => $smilie->getCode());
	}
      
      $template->assign('smilies', $smilies);
      
    }
}


if($_GET['do'] == "edit")
{
  if($_POST['submit'])
    {
      $article = new Article($db, $_POST['id']);
      $article->setTitle($_POST['title']);     
      $article->setBody($_POST['body']);

      //First let's check if we should delete an existing image or add a new image
      if($_POST['delphoto'])
	{
	  $query = "SELECT photo_filename FROM tblArticlePhoto WHERE photo_article_id = " . $article->getId();
	  if($result = $db->doQuery($query))
	    {
	      $row = $result->getArray();
	      unlink("../img/upload/".$row['photo_filename']);
	      $query2 = "DELETE FROM tblArticlePhoto WHERE photo_article_id = " . $article->getId();
	      $result2 = $db->doQuery($query2);
	    }
	  
	}
      elseif($_FILES['file']['name'])
	{
	  $article_id = $article->getId();
	  $file = add_img("../img/upload/");
	  $query = "INSERT INTO tblArticlePhoto (photo_article_id, photo_filename) VALUES('$article_id', '$file')";
	  $result = $db->doQuery($query);
	  
	}

      if(!$article->edit())
		{
	  		$confirm_message = $lang['db_problem'] . "<br><br>" . $db->getError();
		}
      else
		{
	  		$confirm_message = "<p>" . $lang['edit_success'] . "</p><p><a href=\"$PHP_SELF\">" . $lang['manage_articles'] . "</a></p>";
		}

      $template->assign('confirm_message', $confirm_message);
    }
  else
    {

      //Lets read the article first
      $article = new Article($db, $_GET['id']);

      //Now lets create a proper array before sending this to the template...
      $article_arr = array('title' => $article->getTitle(),		
			   'body' => $article->getBody());

      $query = "SELECT photo_id, photo_filename FROM tblArticlePhoto WHERE photo_article_id = " . $article->getId();
      $result = $db->doQuery($query);

      if($result->getNumRows() > 0)
	{
	  $row = $result->getArray();
	  $dimensions = getimagesize("../img/upload/" . $row['photo_filename']);
	  $dimensions2 = imageResize($dimensions[0], $dimensions[1], 100);
	  $photo_row = "<tr><td><img src=\"../img/upload/" . $row['photo_filename'] . "\" width=\"". $dimensions2['width'] . "\" height=\"". $dimensions2['height'] ."\" /></td><td><input type=\"checkbox\" name=\"delphoto\" value=\"1\" />" . $lang['delete'] ."</td></tr>";
	}
      else
	{
	  $photo_row = "<tr><td class=\"admin_form_label\">" . $lang['photo_file'] .":</td>
<td class=\"admin_form_input\"><input type=\"file\" name=\"file\" size=\"50\" /></td></tr>";
	}
      //Now we assign it to the template
      $template->assign('article', $article_arr);
      $template->assign('photo_row', $photo_row);

      //Lets display the available smilies
      $query = "SELECT * FROM tblSmilie";

      $result = $db->doQuery($query);

      while($row = $result->getarray())
	{
	  $smilie = new Smilie($db, $row['smilie_id']);
	  $smilies[] = array('file' => $smilie->getFile(),
			     'code' => $smilie->getCode());
	}
      
      
      $template->assign('smilies', $smilies);
      
    }
}

if($_GET['do'] == "delete")
{
  //This is the delete routine
  //Delete the article from the table and display confirmation
  $article = new Article($db, $_GET['id']);
  if(!$article->delete())
    {
      $confirm_message = $lang['db_problem'] . "<br><br>" . $db->getError();
    }
  else
    {
      $confirm_message = "<p>" . $lang['delete_success'] . "</p><p><a href=\"$PHP_SELF\">" . $lang['manage_articles'] . "</a></p>";
    }

  $template->assign('confirm_message', $confirm_message);
} 



//Lets display the template
$template->display($config['theme'].'/admin/article.tpl');


include("footer.php");

?>
Return current item: Jarida