<?php

class post
{
	public $id;
	public $error = FALSE;
	
	public function __construct($code, $mini = FALSE)
	{
		$this->db1 = & $GLOBALS['db1'];
		$this->db2 = & $GLOBALS['db2'];
		
		$code = $this->db2->e($code);
		
		if (strlen($code)!=11) {
			$this->error = TRUE;
			return;				
		}
		
		if ($mini == FALSE) {
		
			$r	= $this->db2->query('SELECT * FROM posts WHERE code="'.$code.'" LIMIT 1', FALSE);
			
			if( ! $o = $this->db2->fetch_object($r) ) {
				$this->error = TRUE;
				return;
			}
			
			$this->id = $o->idpost;
			$this->code = $o->code;
			$this->iduser = $o->iduser;
			$this->post = stripslashes($o->post);
			$this->whendate = $o->whendate;
			$this->numcomments = $o->numcomments;
			$this->numlikes = $o->numlikes;
			$this->typepost = $o->typepost;
			$this->valueattach = $o->valueattach;
			$this->whosee = $o->who_see;
			$this->id_wall = $o->id_wall;
			$this->posted_in = $o->posted_in;
			
		} else {
			
			$r	= $this->db2->query('SELECT idpost, iduser, post FROM posts WHERE code="'.$code.'" LIMIT 1', FALSE);
			if( ! $o = $this->db2->fetch_object($r) ) {
				$this->error = TRUE;
				return;
			}
			
			$this->id = $o->idpost;
			$this->iduser = $o->iduser;
			$this->post = stripslashes($o->post);		
		}
		return TRUE;
	}
	
	private function _deleteComments()
	{
		// Here we remove the comments on this post
		$allcomments = $this->db2->fetch_all('SELECT idcomment, iduser FROM comments WHERE idpost='.$this->id);
		
		foreach ($allcomments as $onecomments) {
			$this->db2->query('UPDATE users SET num_comments=num_comments-1 WHERE iduser='.$onecomments->iduser.' LIMIT 1');
			$this->_deleteReports(2,$onecomments->idcomment);
			$this->_deleteHiddens(2,$onecomments->idcomment);
		}
		$this->db2->query('DELETE FROM comments WHERE idpost='.$this->id);
		/************************************************/		
	}
	
	private function _deleteLikes()
	{
		// Here we remove the favorites this post
		$alllikes = $this->db2->fetch_all('SELECT iduser FROM likes WHERE idpost='.$this->id);
		foreach($alllikes as $onelike) {
			$this->db2->query('UPDATE users SET num_likes=num_likes-1 WHERE iduser='.$onelike->iduser.' LIMIT 1');
		}
		$this->db2->query('DELETE FROM likes WHERE idpost='.$this->id);
		/************************************************/	
	}
	
	private function _deleteActivities()
	{
		// Here we remove the "activities" made in this post. The posts diferentes to 10 (no shared)
		$this->db2->query('DELETE FROM activities WHERE iditem='.$this->id.' AND typeitem=1 AND action<>10');
		if ($this->typepost == 'share') $this->db2->query('DELETE FROM activities WHERE idresult='.$this->id.' AND typeitem=1 AND iduser='.$this->iduser);
		/************************************************/
	}
	
	private function _deleteNotifications()
	{
		// Here we remove the "notifications" made in this post
		$this->db2->query('DELETE FROM notifications WHERE notif_object_id='.$this->id.' AND notif_object_type=1');
		if ($this->typepost == 'share') $this->db2->query('DELETE FROM notifications WHERE idresult='.$this->id.' AND notif_object_type=1');
		/************************************************/
	}
	
	private function _deleteTrending()
	{
		// Here we remove the "activities" made in this post
		$this->db2->query('DELETE FROM trends WHERE idpost='.$this->id);
		/************************************************/
	}
	
	private function _deleteReports($typeItem, $iditem)
	{
		// Here we remove the reports
		if ($typeItem == 1) $this->db2->query('DELETE FROM reports WHERE typeitem=1 AND iditem='.$iditem);
		if ($typeItem == 2) $this->db2->query('DELETE FROM reports WHERE typeitem=2 AND iditem='.$iditem);
		/************************************************/	
	}

	private function _deleteHiddens($typeItem, $iditem)
	{
		// Here we remove the reports
		if ($typeItem == 1) $this->db2->query('DELETE FROM hiddens WHERE typeitem=1 AND iditem='.$iditem);
		if ($typeItem == 2) $this->db2->query('DELETE FROM hiddens WHERE typeitem=2 AND iditem='.$iditem);
		/************************************************/	
	}
	
	private function _deletePhotos()
	{
		global $C;
		// Now remove the photos
		if ($this->typepost=='photo') {
			$photosPost = explode(',', $this->valueattach);
			$numphotos = count($photosPost);
			if ($numphotos>0) {
				$raiz = '../';
				$folderphoto = $C->FOLDER_PHOTOS;
				$folderphotomini = $C->FOLDER_PHOTOS.'/min1/';
				foreach($photosPost as $onephoto) {
					if (file_exists($raiz.$folderphoto.$onephoto)) unlink($raiz.$folderphoto.$onephoto);
					if (file_exists($raiz.$folderphotomini.$onephoto)) unlink($raiz.$folderphotomini.$onephoto);
				}			
			}			
		}
	}

	public function deletePost()
	{
		$this->db2->query('DELETE FROM posts WHERE idpost='.$this->id.' AND iduser='.$this->iduser.' LIMIT 1');
			
		switch ($this->posted_in) {
			case 0:
				if ($this->id_wall == $this->iduser) $this->db2->query('UPDATE users SET num_posts=num_posts-1, num_posts_inwall=num_posts_inwall-1 WHERE iduser='.$this->iduser.' LIMIT 1');
				else { 
					$this->db2->query('UPDATE users SET num_posts_inwall=num_posts_inwall-1 WHERE iduser='.$this->id_wall.' LIMIT 1');
					$this->db2->query('UPDATE users SET num_posts=num_posts-1 WHERE iduser='.$this->iduser.' LIMIT 1');
				}
				break;	
			case 1:
				$this->db2->query('UPDATE pages SET num_posts=num_posts-1 WHERE idpage='.$this->id_wall.' LIMIT 1');
				break;	
			case 2:
				$this->db2->query('UPDATE groups SET num_posts=num_posts-1 WHERE idgroup='.$this->id_wall.' LIMIT 1');
				break;
		}
		
		if ($this->typepost == 'share') {
			$infop = explode(':',$this->valueattach);
			if ($this->iduser != intval($infop[1])) $this->db2->query('UPDATE posts SET numshares=numshares-1 WHERE idpost='.$infop[0].' LIMIT 1');
		}
		
		$this->_deleteComments();
		$this->_deleteLikes();
		$this->_deleteActivities();
		$this->_deleteNotifications();
		$this->_deleteTrending();
		$this->_deletePhotos();
		$this->_deleteReports(1, $this->id);
		$this->_deleteHiddens(1, $this->id);
	}
	
	public function deleteAccesoriesPost()
	{
		$this->_deleteComments();
		$this->_deleteLikes();
		$this->_deleteActivities();
		$this->_deleteNotifications();
		$this->_deleteTrending();
		$this->_deletePhotos();
	}
	
	public function likeOfUser($iduser)
	{
		$r	= $this->db2->fetch_field('SELECT idlike FROM likes WHERE iduser='.$iduser.' AND idpost='.$this->id.' LIMIT 1');
		return $r;
	}
	
	public function numComments($iduh=0)
	{
		if ($iduh == 0) return $this->numcomments;
		else {
			$sqlCommentsHiddens = ' SELECT iditem FROM hiddens WHERE typeitem=2 AND iduser='.$iduh;
			$sqlCommentsHiddens = ' idcomment NOT IN ('.$sqlCommentsHiddens.') AND ';
			
			$numcoms = $this->db2->fetch_field('
			SELECT count(idcomment) 
			FROM comments 
			WHERE '.$sqlCommentsHiddens.'
			idpost='.$this->id);

			return $numcoms;
		}

	}
	
	public function getAllComments()
	{
		$r = $this->db2->fetch_all('SELECT idcomment, comments.whendate, comment, comments.iduser, username, firstname, lastname, avatar FROM comments, users WHERE users.iduser=comments.iduser AND idpost='.$this->id.' ORDER BY comments.whendate ASC');
		return $r;
	}
	
	public function getComments($start, $quantity, $iduhidden=0)
	{
		$sqlCommentsHiddens = '';
		if ($iduhidden != 0) {
			$sqlCommentsHiddens = ' SELECT iditem FROM hiddens WHERE typeitem=2 AND iduser='.$iduhidden;
			$sqlCommentsHiddens = ' idcomment NOT IN ('.$sqlCommentsHiddens.') AND ';
		}
		
		$r = $this->db2->fetch_all('
		SELECT idcomment, comments.whendate, comment, comments.iduser, username, firstname, lastname, avatar, idpost, users.code as ucode 
		FROM comments, users 
		WHERE '.$sqlCommentsHiddens.'
		users.iduser=comments.iduser 
		AND idpost='.$this->id.' 
		ORDER BY comments.whendate DESC 
		LIMIT '.$start.','.$quantity);
		return $r;
	}
	
	public function reportPost($idinformer)
	{
		$numregs = $this->db2->fetch_field("SELECT count(idreport) FROM reports WHERE typeitem=1 AND iditem=".$this->id);
		if ($numregs == 0) $this->db2->query("INSERT INTO reports SET typeitem=1, iditem=".$this->id.", idinformer=".$idinformer.", iddenounced=".$this->iduser.", whendate='".time()."'");
		$this->hiddenPost($idinformer);
	}

	public function undoReportPost($idinformer)
	{
		$this->db2->query("DELETE FROM reports WHERE typeitem=1 AND iditem=".$this->id." AND idinformer=".$idinformer." LIMIT 1");
		$this->undoHiddenPost($idinformer);
	}

	public function hiddenPost($iduser)
	{
		$numregs = $this->db2->fetch_field("SELECT count(idhidden) FROM hiddens WHERE typeitem=1 AND iditem=".$this->id." AND iduser=".$iduser);
		if ($numregs == 0) $this->db2->query("INSERT INTO hiddens SET typeitem=1, iditem=".$this->id.", iduser=".$iduser.", whendate='".time()."'");
	}

	public function undoHiddenPost($iduser)
	{
		$this->db2->query("DELETE FROM hiddens WHERE typeitem=1 AND iditem=".$this->id." AND iduser=".$iduser." LIMIT 1");
	}
	
}
?>