Location: PHPKode > projects > Open Hot or Not > openhotornot/entities/comments.php
<?php
if  (!preg_match("/\/index\.php$/",$_SERVER["SCRIPT_NAME"])) exit("Vete a tomar por culo." . $_SERVER["SCRIPT_NAME"]);
/**
* $Id: comments.php 4 2010-05-19 10:06:17Z ferenczy $
* Pello Xabier Altadill Izura
* http://openhotornot.sf.net
* Clase para las news o noticias.
*/

include_once "./entities/entity.php";
include_once './lib/utils.php';

class comments extends entity
{

	public $id;
	public $title;
	public $text;
	public $newdate;
	public $permalink;
	private $util;


	/**
	* savecomment_action
	* Saves comment in action
	*/
	public function savecomment_action ($what="idnew",$id,$form)
	{
		$html = "";
		$error = 0;
		
		$this->sec->sanitize("post");

		if ($form->validate() && preg_match("/^[0-9]+$/",$id))
		{
			$userid = ($_SESSION["userid"])?$_SESSION["userid"]:0;
			$this->db->db_query("new_comment",$what,$_POST["who"],$_POST["text"],$id,$userid);
			$html .= $form->generate($formmode);
			$error = 0;
		}
		else
		{
			$html .= $form->regenerate($formmode);
			$error = 1;
		}
		
		return array($html,$error);

	}
	
	
	/**
	* getcomments
	* Loads comments for given new
	*/
	public function getcomments ($id,$permalink,$what="idnew")
	{
		$html = "<div id='comments'><div class='contentsubheader'>"._("Comments")."</div>\n"; 
		$util = new utils($this->cfg);

		$totalcomments = $this->db->db_query("select_total_comments",$what,$id);
		$total = $totalcomments[0]["total"];
		$this->cfg["comments_pagination"] = 10;

		$p = $_GET["p"];
		$p = ($p=="" || !preg_match("/^[0-9]+$/",$p) || $p<0 || (($p-1)*$this->cfg["comments_pagination"])>=$total)?1:$p;		

		$page = ($p-1) * $this->cfg["comments_pagination"];

		$comments = $this->db->db_query("select_comments",$what,$id,$page,$this->cfg["comments_pagination"]);

		if ($this->cfg["mod_rewrite"])
		{
			$link = $this->cfg["url"]."/pose/".$permalink;
		}
		else
		{
			$link = $this->cfg["url"]."/pose/".$permalink;
			//$link = "?ac=show&pose=".$permalink;
		}
					
		$i = $page + 1;
		
		foreach ($comments as $n)
		{
			$author = ($n["fullname"]=="")?"<strong>".$n["who"]."</strong>":"<a href='".$this->cfg["url"]."user/".$n["login"]."' title='".sprintf(_("Visit %s profile"),$n["login"])."'><strong>".$n["fullname"]."</strong></a>";
			$authorname = ($n["fullname"]=="")?$n["who"]:$n["fullname"];
			$avatar = ($n["avatar"]=="")?"/anonymous":$n["avatar"];
			$html .= "<div class='comment'>\n<div class='commentborder'>";

			//$html .= "\t<div class='commentavatar'><img src='".$this->cfg["url"]."/uploads".$avatar."_0.jpg' alt='".sprintf(_("%s user avatar"),$author)."' title='".sprintf(_("%s user avatar"),$author)."' align='' /></div>\n";
			
			$html .= "<div class='commentnumber'>";
			$html .= "<legend><a name='".$n["id"]."'>#".$i."</a></legend>\n";
			$html .= ($n["idauthor"]==$_SESSION["userid"])?"<a href='/pose/comments/edit/".$n["id"]."' title='"._("edit comment")."'>"._("edit")."</a>":"";
			$html .= "<span class='commentdata'><img src='".$this->cfg["url"]."/uploads".$avatar."_0.jpg' alt='".sprintf(_("%s user avatar"),$authorname)."' title='".sprintf(_("%s user avatar"),$authorname)."' align='middle' />\n";
			$html .= "<span class='commentauthor'>".$author." "._("says:")."</span> ";
			$html .= "</span>\n";
			$html .= "</div>";
			$html .= "<div class='commenttext'>".$util->beautify($n["text"])."</div>\n";
			$html .= "</div>\n";
			$html .= "<div class='commentdata'>";
			$html .= $util->votingpanelcomments("",$n["id"],array($n["sum"],$n["count"]),"comment");
			$html .=_("in")." <span class='commentdate'>".date("F j, Y, g:i a",$n["commentdate"])."</span>";

			$html .= "</div></div>\n";
			
			$i++;
			
		}
		
	
		$html .= $util->getPaginationString($p,$total,$link."/",$this->cfg["comments_pagination"]);

		$html .= "</div>\n"; 
		
		return $html;

	}
	
	/**
	* editcomment_action
	* Saves comment in action
	*/
	public function editcomment_action ($id)
	{
		$html = "";
		$error = 0;
		
		$comdata = $this->db->db_query("select_comment_origin",$id);
		
		if (!count($comdata) || $_SESSION["userid"]!=$comdata[0]["idauthor"])
		{
			return _("Don't play with me");
			$this->sec->register("editcomment",$id);
		}		

		$formname = ($this->sec->sessionstarted)?"form_comments_logged":"form_comments";
		
		$form = new forms($formname,$this->db,$this->sec,array($id,$_SESSION["userid"]));
		$form->captcha = !$this->sec->sessionstarted;
		$form->action = $_SERVER["REQUEST_URI"];
	
		$html = $form->generate(0);
		$html .= "<input type='hidden' name='op' value='save' />\n";
		$html .= "<input type='hidden' name='id' value='".$id."' />\n";
				//$tmphtml .= "<input type='hidden' name='ac' value='show' />\n";
		$html .= "<input type='submit' name='submit_".$formname."' value='".sprintf(_("Send"))."' />\n";			
		$html .= "</fieldset>\n";
		$html .= "</form>\n";	
						
		return $html;
		
	}


	/**
	* updatecomment_action
	* Update comment in action
	*/
	public function updatecomment_action ($id)
	{
		$html = "";
		$error = 0;

		$comdata = $this->db->db_query("select_comment_origin",$id);
		
		if (!count($comdata) || $_SESSION["userid"]!=$comdata[0]["idauthor"])
		{
			return _("Don't play with me");
			$this->sec->register("editcomment",$id);
		}		
		
		$formname = ($this->sec->sessionstarted)?"form_comments_logged":"form_comments";
		
		$form = new forms($formname,$this->db,$this->sec,array($id,$_SESSION["userid"]));
		$form->captcha = !$this->sec->sessionstarted;
		$form->action = $_SERVER["REQUEST_URI"]."/save";

		if ($form->validate())
		{
			if ($this->db->db_query("update_comment",$_POST["text"],$id,$_SESSION["userid"]))
			{
				$link = ($comdata[0]["newslink"]!="")?$comdata[0]["newslink"]:$comdata[0]["poselink"];
				return "ok:" .$link."#".$comdata[0]["id"];
			}
		} 		
		else
		{
			$html = $form->regenerate(0);
			$html .= "<input type='hidden' name='op' value='save' />\n";
			$html .= "<input type='hidden' name='id' value='".$id."' />\n";
				//$tmphtml .= "<input type='hidden' name='ac' value='show' />\n";
			$html .= "<input type='submit' name='submit_".$formname."' value='".sprintf(_("Send"))."' />\n";			
			$html .= "</fieldset>\n";
			$html .= "</form>\n";	
		}
						
		return $html;
		
	}	
	
}
?>
Return current item: Open Hot or Not