<?php

if(!isset($_SESSION)) session_start();
include_once("config.php");
include("loginCheckPoint.php");
include_once("authorizations.php");
require_once("demosettings.php");
//include_once("C:/xampp/htdocs/iventitymatcherhtm/bsw/utils.php");
include("slotToCaption.php");
require_once 'parseUtils.php';

//$pageNumber = $_REQUEST["pageNumber"];
$idEnd = $_REQUEST["idEnd"];
$numberOfLines = 100;
$saveToFile = false;

$userdetails = getUserDetails();
$username = $userdetails['Username'];
$commonFilter = "WHERE timeStamp IS NOT NULL";

	$time = "5000";
	try{
	$mysqli = sqlCreateConnection("listFilesEng");
	if (!($mysqli->connect_error)) {    
		$sql = "SELECT isRunning FROM user_status WHERE username=\"$username\""; 												
		$res = $mysqli->query($sql);		
		if(is_object($res)){
			$row = $res->fetch_object();
			if ($row->isRunning == 0)
				$time = "1000000000";
			else{							
				$time = "5000";
				$sql = "SELECT filename,date FROM files $commonFilter ORDER BY id DESC LIMIT 1,1";
				$res = $mysqli->query($sql);
				if(is_object($res)){
					
					$row = $res->fetch_object();
					$fileName = $row->filename;
					$db_time = strtotime($row->date);					
					$t = time();					
					if (($db_time - $t) > 300)
					{
						$sql = "UPDATE user_status SET isRunning=0 WHERE username=\"$username\""; 												
						$res = $mysqli->query($sql);
						write_to_log("ERROR", "UPDATE user_status SET isRunning=0 t=$t time=".$db_time);	
						$time = "1000000000";
					}				
				}else{
					$fileName = "";
				}
			}
		}
	}
	}catch (Exception $e){	
		$time = "5000";
	}
	sqlClose($mysqli, 'listFilesEng');
	


//default behavior
			if ($time != "1000000000"){
				$sort = "DESC";
			}else {
				$sort ="";
			}
			if (empty($idEnd)){
				if (getUserPermission(CAN_SHOW_ALL_USER)) 
					$sql = "SELECT * FROM files $commonFilter ORDER BY id $sort limit $numberOfLines";
				else{
					$username = $userdetails['Username'];				
					$sql = "SELECT * FROM files $commonFilter AND username='$username' ORDER BY userId $sort limit $numberOfLines";
				}				
			}
			else{
				//TODO handle users page display
				$begin = $idEnd;
				$username = $userdetails['Username'];	
				if (getUserPermission(CAN_SHOW_ALL_USER)) 			 
					$sql = "SELECT * FROM files $commonFilter AND id>$begin ORDER BY id $sort limit $numberOfLines";
				else 
					$sql = "SELECT * FROM files $commonFilter AND username='$username' AND userId>$begin ORDER BY userId $sort limit $numberOfLines";				
			}					
			$mysqli = sqlCreateConnection("listFilesEng");
			if ($mysqli->connect_error) {    
				sleep(10);
			}		
		
			$res = $mysqli->query($sql);
			write_to_log("TRACE", "list files sql: $sql");
			$line = 0;	
			if ($shouldListFiles)
			{
			$file_list = file_put_contents("$TEMP_FOLDER\\list_data.txt", "File Name;Languages;Topic;Theatre;Positive Sentiment;Negative Sentiment" . PHP_EOL);				
			$file_listwith_entity = file_put_contents("$TEMP_FOLDER\\list_data_with_entity.txt", "File Name;Languages;Topic;Theatre;Positive Sentiment;Negative Sentiment;Person;Organization;places" . PHP_EOL);
			}
			if (!empty($res)){			
				$startId = -1;
				while ($row = $res->fetch_object()) {
					$data_list = "";
					if (!empty($row->filename)){
						$tooltip = array();
						if (!empty($row->file_size)){
							$size = intval($row->file_size/1000);
							$tooltip[] = "File size = " . $size . "KB";
						}
						if (!empty($row->file_creation_date))
							$tooltip[] = "
	File creation date = " . $row->file_creation_date;
						if (!empty($row->file_format))
							$tooltip[] = "
	File format = " . $row->file_format;
						if (!empty($row->file_last_accessed_date))
							$tooltip[] = "
	File last accessed date = " . $row->file_last_accessed_date;
						if (!empty($row->file_last_modified_date))
							$tooltip[] = "
	File last modified date = " . $row->file_last_modified_date;
						if (!empty($row->line_count))
							$tooltip[] = "
	Number of line = " . $row->line_count;
						if (!empty($row->token_count))
							$tooltip[] = "
	Number of token = " . $row->token_count;
						if (!empty($row->idiological_stream))
							$tooltip[] = "
	Idiological stream = " . $row->idiological_stream;
						if (!empty($row->islamic_sect))
							$tooltip[] = "
	Islamic sect = " . $row->islamic_sect ."
	";					if (!empty($tooltip)){
							//echo "<tr>";
							if (getUserPermission(CAN_SHOW_ALL_USER)) 
								$id = $row->id;															
							else
								$id = $row->userId;						
							//echo "<td class=\"filestdhd\">$id</td>";						
							$rowNum = $id;
	
							if(!empty($row->languages)){
								$langArr = array();
								$lang = explode(";", $row->languages);
								
								foreach ($lang as $l){
									$langArr[$l] = $l;
								}
								$first = true;
								foreach ($langArr as $l){
									if ($first){
										$languages = $l;
									}else
										$languages .= ", " . $l;  
								}	
							}else{
								$languages = "NA";
							}				
							$counted_theatre = str_ireplace(";", ", ", $row->counted_theatre);				
							$ideological_stream = str_ireplace(";", ", ", $row->ideological_stream);
							//clean duplicate anti
							$e = explode(";", $row->anti);
							$aggArr = array();
							foreach ($e as $a){
								$aggArr[$a] = $a;
							}
							$first = true;
							$aggregated_anti = "";
							foreach ($aggArr as $a){
								if ($first){
									$first = false;
									$aggregated_anti .= $a;
								}
								else							
									$aggregated_anti .= "<br>" . $a;
								
							}
							
							//clean duplicate pro
							$e = explode(";", $row->pro);
							$aggArr = array();
							foreach ($e as $a){
								$aggArr[$a] = $a;
							}
							$first = true;
							$aggregated_pro = "";
							foreach ($aggArr as $a){
								if ($first){
									$first = false;
									$aggregated_pro .= $a;
								}
								else							
									$aggregated_pro .= "<br>" . $a;
								
							}
																	
							$antipro = "";
							if (strlen($aggregated_anti) > 1){
								$antipro = "<b>Anti</b> - " . $aggregated_anti;
								if (strlen($aggregated_pro) > 1){
									$antipro .= " <br> " . "<b>Pro</b> - " . $aggregated_pro;
								}
							} elseif 	(strlen($aggregated_pro) > 1){
								$antipro .= "<b>Pro</b> - " . $aggregated_pro;
							} else{
								$antipro = "";
							}
							$e = explode("-domain-parameter",$row->domain);
							$domain = $e[0];
							
						$response["rows"][] = array("rowNum" => $rowNum, "docId" => $row->id, "timeStamp" => $row->timeStamp, "tooltip" => $tooltip, "filename" => $row->filename, "languages" => $languages,
							"document_topic" => "".$row->document_topic, "counted_theatre" => $counted_theatre,
							"from_field" => "" . $row->from_field, "to_field" => $row->to_field, "cc_field" => $row->cc_field, "bcc_field" => $row->bcc_field, "subject_field" => "".$row->subject_field,
							"aggregated_pro" => $aggregated_pro, "aggregated_anti" => $aggregated_anti,
						"pro" => $row->pro, "anti" => $row->anti,
						"results" => $row->results, "shortFilename" => $row->shortFilename,
						"origFolder" => $row->origFolder, "session" => $row->session, "username" => $row->username,
						"type" => $row->type, "data" => $row->data, "userId" => $row->userId, "domain" => $domain,
						"file_size" => $row->file_size, "batch_serial_id" => $row->batch_serial_id, "batch_id" => $row->batch_id,						
						"file_last_modified_date" => $row->file_last_modified_date, "scan_time" => $row->scan_time, "line_count" => $row->line_count,
						"token_count" => $row->token_count, "batch_serial_id" => $row->batch_serial_id, "islamic_sect" => $row->islamic_sect,
						"threat_rating" => $row->threat_rating, "document_topic" => $row->document_topic, "user_notes" => $row->user_notes,						
						"emailid" => $row->emailid, "date_field" => $row->date_field, "date" => $row->date, "ideological_stream" => $row->ideological_stream,
						"file_creation_date" => $row->file_creation_date, "file_format" => $row->file_format, "file_last_accessed_date" => $row->file_last_accessed_date);
												
						$arr_l = array();
						$arr_l["rowNum"] = $rowNum;
						$arr_l["docId"] = $row->id;
						//change order and aggregete domain and topic
						/*foreach ($listPageColumn as $k_l => $v_l){
							if ($v_l === "Domain" || $v_l === "Topic")
								if (array_key_exists("Topic",$arr_l))
									$arr_l["Topic"] .= "," . $row->$k_l;
								else 
									$arr_l["Topic"] = $row->$k_l;
							else
								$arr_l[$v_l] = $row->$k_l;							
						}*/
						foreach ($listPageColumn as $k_l => $v_l){
							if ($v_l === "Topic")	
							{
								//uniquely join document_topic and domain
								$arr_l[$v_l] = join(",", array_unique(array_filter(explode(",", $row->document_topic . "," . $row->domain))));
							}								
							else
								$arr_l[$v_l] = $row->$k_l;							
						}
						$response["rows1"][] = str_replace(";",",",$arr_l);						
						
							if ($saveToFile){
								//add person org and location
								$sql = "SELECT DISTINCT name FROM person_object WHERE docId=$id";								
								$res_list = $mysqli->query($sql);
								$person = "";
								$first = true;
								while ($row_list = $res_list->fetch_object()){										
									if (!empty($row_list->name)){
										if ($first){
											$person .= $row_list->name;
											$first = false;
										}else 
											$person .= "," . $row_list->name;
									}
								}	
								//add person  and location
								$sql = "SELECT DISTINCT name FROM organizational_identity WHERE docId=$id";								
								$res_list = $mysqli->query($sql);
								$org = "";
								$first = true;
								while ($row_list = $res_list->fetch_object()){										
									if (!empty($row_list->name)){
										if ($first){
											$org .= $row_list->name;
											$first = false;
										}else 
											$org .= "," . $row_list->name;
									}
								}	
								$sql = "SELECT DISTINCT name FROM place_object WHERE docId=$id";								
								$res_list = $mysqli->query($sql);
								$place = "";
								$first = true;
								while ($row_list = $res_list->fetch_object()){										
									if (!empty($row_list->name)){
										if ($first){
											$place .= $row_list->name;
											$first = false;
										}else 
											$place .= "," . $row_list->name;
									}
								}	
								if ($shouldListFiles)				
								$file_listwith_entity = file_put_contents("$TEMP_FOLDER\\list_data_with_entity.txt", $data_list.";".$person.";".$org.";".$place . PHP_EOL ,FILE_APPEND);
							}
							if (getUserPermission(CAN_SHOW_ALL_USER)) 
								$endId = $row->id;
							else 
								$endId = $row->userId;

							//if not initialized - set first row to current row
							if ($startId == -1)
								$startId = $endId;
							
							if ($shouldListFiles)
							$file_list = file_put_contents("$TEMP_FOLDER\\list_data.txt", $data_list . PHP_EOL ,FILE_APPEND);
						}
					}		
					$line++;
														
					
				}
			}else{
				$endId = 0;
				$startId = 0;
			}
			if (getUserPermission(CAN_SHOW_ALL_USER)) 
					$sql = "SELECT max(id) as m FROM files";
				else 
					$sql = "SELECT max(userId) as m FROM files WHERE username='$username'";				
      		$res = $mysqli->query($sql);	
      		if(is_object($res)){
      			$row = $res->fetch_object();	
      			$max = $row->m;
      		}
            sqlClose($mysqli, 'listFilesEng');
            $response["max"] = $max;
			$response["line"] = $line;
			$response["numberOfLines"] = $numberOfLines;
			$response["idEnd"] = $idEnd;
			$response["startId"] = $startId;
			$response["endId"] = $endId;
			
			$response["time"] = $time;
			$response["fileName"] = $fileName;
				
 			ob_clean();
			echo json_encode($response);
      ?>