<?php

// ini_set("memory_limit", "-1");
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
//error_reporting(E_ALL);

require_once "/home/mysamm/Lib/MysqlDB/connection.php"; 
require_once "/home/mysamm/Lib/MongoDB/mongo_connect.php"; 
require_once "/home/mysamm/public_html/vendor/autoload.php"; 
ini_set('max_execution_time', 500);

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class ApiSdata extends Connection{
	function __construct() {
		$this->mdb = new MongoConn();
		$this->db = new connection();
	}
	
	function get_assigned_category($REQUEST){
		
		$collections = $REQUEST['tbl'];
		$report_id = $REQUEST['request'];
		$filter = array('report_id'=>$report_id);
		$data = $this->mdb->Query($collections,$filter);
		foreach($data as $row){
			$rowm = (array) $row;
			unset($rowm['_id']);
			$return[] = $rowm;
		}
		$data_json  = $this->encodeJson($return);
		echo $data_json; exit;
	
	}
	
	
	function discards_changes($REQUEST){
		
		$collections = $REQUEST['tbl'];
		$report_id = $REQUEST['request'];
		$filter = array('report_id'=>$report_id);
	    $this->mdb->delete_document($collections,$filter);
		$return[] = '1';
		$data_json  = $this->encodeJson($return);
		echo $data_json; exit;
	}
	
	function run_category_assignment($REQUEST){
		
		$report_id = $REQUEST['request'];
		$website_id = $REQUEST['website_id'];
		$user_id = $REQUEST['user_id'];
		
		$cmd="nohup php /home/mysamm/openai/ContentGenerator/website_category_assign_dev.php $user_id $website_id $report_id >/dev/null 2>&1 & ";
		#print "$cmd"; die;
		shell_exec($cmd);
		
		$return[] = '1';
		$data_json  = $this->encodeJson($return);
		echo $data_json; exit;
	}
	
	
	function compareDates($date1, $date2){
      if (strtotime($date1) < strtotime($date2))
         return 1;
      else if (strtotime($date1) > strtotime($date2))
         return -1;
      else
         return 0;
	}
	
	
	function match_string($string_1, $string_2){
		
		
		$string_1 = strtolower($string_1);
		$string_1 = trim($string_1);
		$string_1 = preg_replace("/\W+/is", "", $string_1);
		
		
		$string_2 = strtolower($string_2);
		$string_2 = trim($string_2);
		$string_2 = preg_replace("/\W+/is", "", $string_2);
		
		if($string_1 == $string_2){
			return 1;
		}else{
			return 2;
		}
	}
	
	public function encodeJson($responseData) {
		$jsonResponse = json_encode($responseData, JSON_INVALID_UTF8_IGNORE);
		return $jsonResponse;		
	}
  

}

?>