<?php

	/*************************************************************************/
	// needed before proceeding
	require_once('_all-required-language.php');
	
	/*************************************************************************/

	$this->load_langfile('global/global.php');
	$this->load_langfile('outside/profile.php');

	/*************************************************************************/
	
	// needed before proceeding
	require_once('_all-required-search.php');
	
	/*************************************************************************/

	$D->errorsearch = 0;

	$D->query = '';
	
	if ($this->param('t')) {
		$D->query = mb_convert_encoding($this->param('t'), 'UTF-8');
	 	//$D->query = htmlspecialchars($this->param('t'));
	}


	if (strlen($D->query)>0) {

		$D->ITEMS_PER_PAGE = $C->NUM_SEARCH_PAGE;
		
		$sqlPostsHiddens = '';
		if ($D->is_logged == 1) {
			$sqlPostsHiddens = ' SELECT iditem FROM hiddens WHERE typeitem=1 AND iduser='.$this->user->id;
			$sqlPostsHiddens = ' idpost NOT IN ('.$sqlPostsHiddens.') AND ';
		}	
		
		$D->allresults = $this->db2->fetch_all("
		SELECT DISTINCT idpost 
		FROM trends 
		WHERE ".$sqlPostsHiddens."
		trend='".$this->db2->e($D->query)."'");
		
		$D->totalactivities = count($D->allresults);

		if ($D->totalactivities > 0) {
			
			$idsposts = array();
			foreach($D->allresults as $oneresult) $idsposts[] = $oneresult->idpost;
			
			$theposts = $this->db2->fetch_all('
			SELECT idpost, posts.code as pcode, valueattach, numlikes, posted_in, id_wall, numcomments, post, who_see, whendate, typepost, users.code as ucode, users.iduser as uiduser, username, firstname, lastname, avatar, verified 
			FROM posts, users 
			WHERE 
			users.iduser=posts.iduser
			AND who_see<>2  
			AND idpost in ('.implode($idsposts,',').') 
			ORDER BY whendate DESC');
		
			$D->numactivities = count($theposts);
			
			$D->htmlResult = '';
			foreach($theposts as $onepost) {
		
				$D->a_date = $onepost->whendate;
				$D->codeUser = $onepost->ucode;
				$D->valueattach = $onepost->valueattach;
				
				$D->typepost = $onepost->typepost;
			
				$D->isMyPost = FALSE;
				if ($D->is_logged == 1) {
					$D->isMyPost = $onepost->uiduser == $this->user->id ? TRUE : FALSE;
				}	
		
				$D->isaPage = $D->isOnlyOne = $D->isaGroup = $D->isWithOther = 0;
				
				if ($onepost->posted_in == 0) {
					if ($onepost->uiduser == $onepost->id_wall) {
						$D->isOnlyOne = 1;
						$D->codeUser = $onepost->ucode;
						$D->userName = $onepost->username;
						$D->nameUser = (empty($onepost->firstname) || empty($onepost->lastname))?$onepost->username:($onepost->firstname.' '.$onepost->lastname);
						$D->userAvatar = $onepost->avatar;
					}
					
					if ($onepost->uiduser != $onepost->id_wall) {
						$D->isWithOther = 1;
						$D->codeUser = $onepost->ucode;
						$D->userName = $onepost->username;
						$D->nameUser = (empty($onepost->firstname) || empty($onepost->lastname))?$onepost->username:($onepost->firstname.' '.$onepost->lastname);
						$D->userAvatar = $onepost->avatar;
		
						$wallsec = $this->network->infoBasicWall($onepost->posted_in, $onepost->id_wall);
						$D->other_code = $wallsec->code;
						$D->other_userName = $wallsec->username;
						$D->other_nameUser = (empty($wallsec->firstname) || empty($wallsec->lastname))?$wallsec->username:($wallsec->firstname.' '.$wallsec->lastname);
		
						$D->postInMyWall = FALSE;
						if ($D->is_logged == 1) {
							$D->postInMyWall = $onepost->id_wall == $this->user->id ? TRUE : FALSE;
						}
						
					}
				}
				
				// is a post of a page
				if ($onepost->posted_in == 1) {
					$D->isaPage = 1;
					$D->idPage = $onepost->id_wall;
					$thePage = $this->db2->fetch('SELECT code, url, avatar_page, title FROM pages WHERE idpage='.$D->idPage.' LIMIT 1');
					$D->pUserName = $thePage->url;
					$D->pCode = $thePage->code;
					$D->pAvatar = $thePage->avatar_page;
					$D->pTitle = stripslashes($thePage->title);
				}
				
				// is a post of a group
				if ($onepost->posted_in == 2) {
					$D->isaGroup = 1; 
		
					$D->codeUser = $onepost->ucode;
					$D->userName = $onepost->username;
					$D->nameUser = (empty($onepost->firstname) || empty($onepost->lastname))?$onepost->username:($onepost->firstname.' '.$onepost->lastname);
					$D->userAvatar = $onepost->avatar;
		
					$wallsec = $this->network->infoBasicWall($onepost->posted_in, $onepost->id_wall);
					//is a group
					$D->other_code = $wallsec->code;
					$D->other_url = $wallsec->url;
					$D->other_name = stripslashes($wallsec->name_group);			
				}
		
								
				$D->isShare=0;
					
				if ($D->typepost == 'share') {
					$infop = explode(':',$D->valueattach);
					$D->idpostShared = $infop[0];
					$D->idpost = $onepost->idpost;
					$D->isShare=1; 
				} else {
					$D->idpost = $onepost->idpost;
					$D->idpostShared = $onepost->idpost;
				}
		
				$D->codepost = $onepost->pcode;
				$D->idUser = $onepost->uiduser;
				$D->numlikes = $onepost->numlikes;
				$D->numcommentstotal = $onepost->numcomments;
				$D->post = stripslashes($onepost->post);
				$D->whosee = $onepost->who_see;
		
				// see if the favorite is for the observer
				$iduhi = 0; //see hiddens comments for user
				$D->liketoUser = 0;
				if ($D->is_logged == 1) {
					if ($this->network->PostLiketoUser($this->user->id, $D->idpost) > 0) $D->liketoUser = 1;
					$iduhi = $this->user->id;
				}
					
				$D->htmlcommentspost = '';
				$D->totalcomments = $this->network->getNumCommentsPost($D->idpost, $iduhi);
				$allcommentspost = $this->network->getCommentsPost(0,$C->NUM_COMMENTS_PER_POST, $D->idpost, $iduhi);
				$D->numcomments = count($allcommentspost);
					
				$allcommentspost = array_reverse($allcommentspost);	
				
				foreach($allcommentspost as $onecomment){
					$D->o_comment = stripslashes($onecomment->comment);
					$D->o_username = stripslashes($onecomment->username);
					$D->o_firstname = stripslashes($onecomment->firstname);
					$D->o_lastname = stripslashes($onecomment->lastname);
					$D->o_ucode = $onecomment->ucode;
					$D->o_nameUser = (empty($D->o_firstname) || empty($D->o_lastname))?stripslashes($D->o_username):(stripslashes($D->o_firstname).' '.stripslashes($D->o_lastname));
					$D->o_whendate = $onecomment->whendate;
		
					$D->o_avatar =  empty($onecomment->avatar)?$C->AVATAR_DEFAULT:$onecomment->avatar;
					$D->o_idcomment = $onecomment->idcomment;
					$D->o_idUser = $onecomment->iduser;
					$D->o_idpost = $D->idpost;
					$D->o_idUserOwner = $D->idUser;
					$D->o_codepost = $D->codepost;
					$D->htmlcommentspost .= $this->load_template('__onecomment-post.php', FALSE);
				}
				unset($onecomment);		
				
				$D->htmlpostshare = '';
				if ($D->isShare == 1) {
		
					$D->codepostSh = $this->network->getCodePost($D->idpostShared);
					$onePostSh = new post($D->codepostSh);
					if (!$onePostSh->error) {
						$D->a_dateSh = $onePostSh->whendate;
						$D->idUserSh = $onePostSh->iduser;
						$D->typepostSh = $onePostSh->typepost;
						$D->postSh = stripslashes($onePostSh->post);
						$D->valueattachSh = $onePostSh->valueattach;
						$D->whoseeSh = $onePostSh->whosee;
		
						$usSh = $this->network->get_user_by_id($D->idUserSh);
						
						$D->isaPageSh = $D->isOnlyOneSh = $D->isaGroupSh = $D->isWithOtherSh = 0;
						
						if ($onePostSh->posted_in == 0) {
							$D->codeUserSh = $usSh->code;
							$D->userNameSh = $usSh->username;
							$D->nameUserSh = (empty($usSh->firstname) || empty($usSh->lastname))?$usSh->username:($usSh->firstname.' '.$usSh->lastname);
							$D->userAvatarSh = $usSh->avatar;
			
							if ($onePostSh->iduser == $onePostSh->id_wall) {
								$D->isOnlyOneSh = 1;
							} else {
								$D->isWithOtherSh = 1;
								$wallsec = $this->network->infoBasicWall($onePostSh->posted_in, $onePostSh->id_wall);
								$D->other_codeSh = $wallsec->code;
								$D->other_userNameSh = $wallsec->username;
								$D->other_nameUserSh = (empty($wallsec->firstname) || empty($wallsec->lastname))?$wallsec->username:($wallsec->firstname.' '.$wallsec->lastname);
							}
						}
						
						// is a post of a page
						if ($onePostSh->posted_in == 1) {
							$D->isaPageSh = 1;
							$D->idPageSh = $onePostSh->id_wall;
							$thePage = $this->db2->fetch('SELECT code, url, avatar_page, title FROM pages WHERE idpage='.$D->idPageSh.' LIMIT 1');
							$D->pUserNameSh = $thePage->url;
							$D->pCodeSh = $thePage->code;
							$D->pAvatarSh = $thePage->avatar_page;
							$D->pTitleSh = stripslashes($thePage->title);
						}
						
						if ($onePostSh->posted_in == 2) {
							$D->isaGroupSh = 1;
		
							$D->idUserShSh = $onePostSh->iduser;
							$theUser = $this->db2->fetch('SELECT code, avatar, username, firstname, lastname FROM users WHERE iduser='.$D->idUserShSh.' LIMIT 1');
							$D->UcodeUserSh = $theUser->code;
							$D->UuserNameSh = $theUser->username;
							$D->UnameUserSh = (empty($theUser->firstname) || empty($theUser->lastname))?$theUser->username:($theUser->firstname.' '.$theUser->lastname);
							$D->UuserAvatarSh = $theUser->avatar;
				
							$wallsecShSh = $this->network->infoBasicWall($onePostSh->posted_in, $onePostSh->id_wall);
							//is a group
							$D->other_codeShSh = $wallsecShSh->code;
							$D->other_urlShSh = $wallsecShSh->url;
							$D->other_nameShSh = stripslashes($wallsecShSh->name_group);			
						}
		
						$D->cadextraSh = '';
						if ($D->typepostSh == 'album') {
							$infoalbumSh = $this->db1->fetch('SELECT * FROM albums WHERE idpost='.$D->idpostShared.' LIMIT 1');
							$D->codealbumSh = $infoalbumSh->code;
							$D->idalbumSh = $infoalbumSh->idalbum;
							
							$allPhotosSh = $this->db1->fetch_all('SELECT photo FROM albums_photos WHERE idalbum='.$D->idalbumSh.' ORDER BY whendate DESC');
							$totalPhotosSh = count($allPhotosSh);
							$D->valueattachAlbumSh = '';
							foreach ($allPhotosSh as $onePhotoSh) {
								$D->valueattachAlbumSh .= $onePhotoSh->photo.',';
							}
							$D->valueattachAlbumSh = trim($D->valueattachAlbumSh,',');
							$D->cadextraSh = $this->lang('global_txt_addphotos').' '.$totalPhotosSh.' '.($totalPhotosSh==1?$this->lang('global_txt_addphotos_photo'):$this->lang('global_txt_addphotos_photos'));
							
						}
		
		
						$D->htmlpostshare .= $this->load_template('__one-share-post.php', FALSE);//'Share';
					} else {
						$D->idpostShared = $D->idpost;
						$D->htmlpostshare .= $this->load_template('__one-share-post-nofound.php', FALSE);
					}
					unset($onePostSh);
				}
				
				$D->cadextra = '';
				if ($D->typepost == 'album') {
					$infoalbum = $this->db1->fetch('SELECT * FROM albums WHERE idpost='.$D->idpost.' LIMIT 1');
					$D->codealbum = $infoalbum->code;
					$D->idalbum = $infoalbum->idalbum;
					
					$allPhotos = $this->db1->fetch_all('SELECT photo FROM albums_photos WHERE idalbum='.$D->idalbum.' ORDER BY whendate DESC');
					$totalPhotos = count($allPhotos);
					$D->valueattach = '';
					foreach ($allPhotos as $onePhoto) {
						$D->valueattach .= $onePhoto->photo.',';
					}
					$D->valueattach = trim($D->valueattach,',');
					$D->cadextra = $this->lang('global_txt_addphotos').' '.$totalPhotos.' '.($totalPhotos==1?$this->lang('global_txt_addphotos_photo'):$this->lang('global_txt_addphotos_photos'));
					
				}
						
				if ($D->isShare == 1) $D->htmlResult .= $this->load_template('__one-post-shared.php', FALSE);
				else $D->htmlResult .= $this->load_template('__one-post.php', FALSE);		
			}
		
			unset($onePost);

		}
		
		
		
	} else {
		$D->errorsearch = 1;
		$D->msgerror = $this->lang('global_search_qshort');
	}

	/*************************************************************************/

	$D->page_title = $this->lang('global_search_title').' - #'.$D->query.' - '.$C->SITE_TITLE;
		
	$this->load_template('search.php');
?>