<?php 
# ************** LICENCE ****************
/*
	Copyright (c) PORTANEO.
*/
# ***************************************
# Display search page and results
#
# !! be careful, this file must be saved under uft8 format, and display an e accentuated here : é
# ***************************************
$not_access=1;
$granted="I";

require_once('includes_mobile.php');
require_once('../includes/date.inc.php');
require_once('../../../../db_layer/'.__DBTYPE.'/enterprise.php');
require_once('../includes/template.inc.php');

$searchSelection = 0;
$search = $_GET['search'];
$keyword = str_replace(",","','",$search);
$cb = array(0,0,0);
$selectedTab = '';
$tabs = array();
$htmlTabs = '';

if (isset($_GET['tab']) && !empty($_GET['tab'])) {
	$selectedTab = $_GET['tab'];
}

//$htmlTabs = buildTabs($selectedTab,$tabs);

// when user came from search box (not dedicated search page) 
if (isset($_GET['cb1'])|| isset($_GET['cb2']) || isset($_GET['cb3'])) { 
	if (isset($_GET['cb1'])) $searchSelection+=1;
	if (isset($_GET['cb2'])) $searchSelection+=2;
	if (isset($_GET['cb3'])) $searchSelection+=4;
	setCookie("mobilesearch",$searchSelection);
}

if ($searchSelection==0) {
	$searchSelection = intval(isset($_COOKIE["mobilesearch"])       ?        $_COOKIE["mobilesearch"]       :        '');
}

$options = defineHeader();
          
echo page_begining();

echo '<div id="header">';
    echo generate_header($options);
echo '</div>';

echo '<div id="wrapper">';
echo '<div id="page">';

echo'<p><br />
     <form method="get" action="search.php">
       		<input type="text" width="200" name="search" value="'.$search.'" style="width: 75%" /> <input type="submit" value="'.lg('ok').'" /><br /><br />
            <input type="checkbox" width="200" name="cb1" '.($searchSelection & 1?'checked="checked"':'').' value="1" size="12" /> '.lg("inmynote").'<br>
            <input type="checkbox" width="200" name="cb2" '.($searchSelection & 2?'checked="checked"':'').' value="2" size="12" /> '.lg("innote").'<br>
            <input type="checkbox" width="200" name="cb3" '.($searchSelection & 4?'checked="checked"':'').' value="3" size="12" /> '.lg("body").'<br>
 	 </form>
	 </p><br />';
  
if (($searchSelection & 1) && isset($search)) {
    $allkeywords = explode(" ", $search);
    $arraylikewords = array();
    $arraywords = array();
    $list_keywords = "";
    $cpt = 0;
    foreach ($allkeywords as $word) {
        $word = trim($word);
        array_push($arraywords, $DB->quote($word) );
        if ($cpt == 0)
            array_push($arraylikewords, "'%$word%'");
        else
            array_push($arraylikewords, " OR na.title like '%$word%'");
        $cpt++;
    }
	$list_keywords .= implode( "," , $arraywords);
    $likeliste = implode(" ", $arraylikewords );

	$DB->getResults($xmlnotebooksearch_mynotebooksearch,
						$list_keywords,
						$likeliste,
                        $DB->escape($_SESSION['user_id']),
                        0
                   );
                   
	echo '<h2>'.lg("searchnote").'</h2>';
	$nb_search = $DB->nbResults();
	if ($nb_search>0) {
		while ($row = $DB->fetch(0)) {
			$id = $row['id'];
			$title = $row['title'];
			$pubdate = getDBDate($row['pubdate']);
			$name = $row['long_name'];
			$ownerid = $row['user_id'];
			
			echo'<div class="item">
                    <a href="notebook_article.php?id='.$ownerid.'&artid='.$id.'&page=0">'.$title.'</a> 
                    <div class="subtitle">'.formatDate($pubdate).'</div>
				</div>';
		}
	}
	else {
		echo lg("noresults");
	}
	$DB->freeResults();
}

if (($searchSelection & 2) && isset($search)) {
	//get the modules corresponding to the request based on the indexed keywords
    $allkeywords = explode(" ", $search);
    $arraylikewords = array();
    $arraywords = array();
    $list_keywords = "";
    $cpt = 0;
    foreach ($allkeywords as $word) {
        $word = trim($word);
        array_push($arraywords, $DB->quote($word) );
        if ($cpt == 0)
            array_push($arraylikewords, "'%$word%'");
        else
            array_push($arraylikewords, " e.title like '%$word%'");
        $cpt++;
    }
    $list_keywords .= implode( ",",$arraywords);
    $likeliste = implode(" OR ",$arraylikewords );

    $DB->getResults($xmlnotebooksearch_otherNotebookSearch,
                        $list_keywords,
                        $likeliste,
                        $DB->escape($_SESSION['user_id']),
                        0
                   );   

	echo '<h2>'.lg("searchnotes").'</h2>';
	$nb_search = $DB->nbResults();
	if ($nb_search>0) {
		while ($row = $DB->fetch(0)) {
			$id = $row['id'];
			$title = $row['title'];
			$pubdate = getDBDate($row['pubdate']);
			$name = $row['long_name'];
			$ownerid = $row['user_id'];
			
			echo'<div class="item">
                    <a href="notebook_article.php?id='.$ownerid.'&artid='.$id.'&page=0">'.$title.'</a><br /> 
                    <span class="subtitle">'.lg("by").' <a href="network_profil.php?id='.$ownerid.'">'.$name.'</a>, '.formatDate($pubdate).'</span>
				</div>';
		}
	}
	else {
		echo lg("noresults");
	}
	$DB->freeResults();
}


if (($searchSelection & 4)&&isset($search)) {	
    $allkeywords = explode(" ", $search);
    $arraywords = array();
    $arraylikewords = array();
    foreach ($allkeywords as $word) {
        array_push($arraywords, $DB->quote($word) );
        array_push($arraylikewords, " sk.label_simplified like '%$word%'");
    }
    $likeliste = ' OR ( ';
    $likeliste .= implode(" OR ", $arraylikewords );
    $likeliste .= ' ) ';    
	$keyword = implode( "," , $arraywords);

	$DB->getResults($xmlnetworksearch_getUserByKeywords,$keyword,$likeliste,$DB->escape($_SESSION['user_id']),0);
	echo '<h2>'.lg("searchperson").'</h2>'; 
	//if no result with tag, try the name
	if ($DB->nbResults()==0) {
		$DB->freeResults();
		$DB->getResults($xmlnetworksearch_getUserByNamePart,$DB->quote("%".$search."%"),0);
	}
	//if no results with name, try email
	if ($DB->nbResults()==0) {
		$DB->freeResults();
		$DB->getResults($xmlnetworksearch_getUserByName,$DB->quote($search));
	}
	//if no results with email, display warning
	if ($DB->nbResults()==0) {
		echo lg("noresults");
	}

	while($row=$DB->fetch(0)) {
		$id = $row['id'];
		$name = $row['long_name'];
		$pic = $row['picture'];
		echo '<div class="item" style="clear: both;height: 60px;">
			<a href="network_profil.php?id='.$id.'"><img src="../../../'.displayPicture($pic).'" class="avatar" width="50" height="50" style="float: left;margin-right: 5px;"></a>
			<a href="network_profil.php?id='.$id.'">'.$name.'</a><br />
			<a href="notebook_articles.php?id='.$id.'&page=0">[ '.lg("notebook").' ]</a>
			</div>';
	}
	$DB->freeResults();
}

echo '</div>';
echo '</div>';

echo generate_footer();
?>
