<?php

namespace Kabano;

/**********************************************************
***********************************************************
**  
**  This class is to manage a poi object
**  
***********************************************************
**********************************************************/

require_once($config['third_folder']."Md/MarkdownExtra.inc.php");
require_once($config['includes_folder']."poi_types.struct.php");
require_once($config['includes_folder']."database.php");

// Objet représentant un point d'intérêt.
class Poi
{
	public $content_id = NULL;
	public $locale_id = NULL;
	public $source_id = NULL;
	public $version_id = NULL;
	public $permalink = NULL;
	public $version = 0;
	public $locale = 0;
	public $creation_date = NULL;
	public $update_date = NULL;
	public $author = NULL;
	public $is_public = NULL;
	public $is_archive = NULL; // Means destroyed for a POI
	public $is_commentable = NULL;
	public $type = "poi";
	public $poi_type = NULL;
	public $name = NULL;
	public $source = NULL;
	public $remote_source_id = NULL;
	public $parameters = NULL;

	public $lat;
	public $lon;
	public $ele;
	public $author_name;

	private function decodeJsonArray($value) {
		if ($value === null || $value === '') {
			return [];
		}

		// Les paramètres sont stockés en JSON.
		$decoded = json_decode($value, true);
		if (!is_array($decoded)) {
			return [];
		}

		return $decoded;
	}

	/*****
	** Checks if a page at this URL exists and return the ID
	*****/
	public function checkPermalink($permalink, $withArchive=0, $elementNb=0) {
	    global $config;

		// Récupère la dernière version du POI.
		$con = sql_connect();

	    $query = "SELECT
            content_versions.id AS version_id,
            content_versions.version,
            content_versions.update_date,
            content_versions.is_archive,
            content_versions.name,
            content_versions.content AS parameters,

            contents.id AS content_id,
            contents.permalink,
            contents.creation_date,
            contents.is_public,
            contents.is_commentable,
            contents.type,
            contents.poi_type,

            content_locales.id AS locale_id,
            content_locales.locale,
            content_locales.author,

            specs.source_id,
            specs.remote_source_id,
            ST_X(specs.geom) AS lon,
            ST_Y(specs.geom) AS lat,
            ST_Z(specs.geom) AS ele,

            sources.display_name AS source

	        FROM contents
	        INNER JOIN content_locales
	            ON contents.id = content_locales.content_id
	        INNER JOIN content_versions
	            ON content_locales.id = content_versions.locale_id
	        LEFT JOIN content_version_poi_specifications specs
	            ON specs.content_version_id = content_versions.id
	        LEFT JOIN sources
	            ON sources.id = specs.source_id

	        WHERE contents.permalink = $1
	          AND contents.type = 'poi'";

	    if ($withArchive == 0) {
	        $query .= " AND content_versions.is_archive = FALSE AND contents.is_public = TRUE";
	    }

	    $query .= " ORDER BY content_versions.update_date DESC LIMIT 1 OFFSET $2";

	    pg_prepare($con, "poi_check_permalink", $query)
	        or die ("Cannot prepare statement\n");

	    $result = pg_execute($con, "poi_check_permalink", [$permalink, $elementNb])
	        or die ("Cannot execute statement\n");

	    pg_close($con);

	    if (pg_num_rows($result) == 1) {
	        $row = pg_fetch_assoc($result);
	        $this->populate($row);
	        return 1;
	    }

	    return 0;
	}

	/*****
	** Populate the object using its ID
	*****/
	public function populate($row) {
		if (!is_array($row)) {
			return;
		}

		// Décode les paramètres JSON et mappe les champs.
		$decodedParameters = null;
		if (array_key_exists('parameters', $row)) {
			$decodedParameters = $this->decodeJsonArray($row['parameters']);
		}

		if (array_key_exists('content_id', $row)) {
			$this->content_id = $row['content_id'];
		}
		if (array_key_exists('locale_id', $row)) {
			$this->locale_id = $row['locale_id'];
		}
		if (array_key_exists('source_id', $row)) {
			$this->source_id = $row['source_id'];
		}
		if (array_key_exists('version_id', $row)) {
			$this->version_id = $row['version_id'];
		}
		if (array_key_exists('permalink', $row)) {
			$this->permalink = $row['permalink'];
		}
		if (array_key_exists('version', $row)) {
			$this->version = $row['version'];
		}
		if (array_key_exists('locale', $row)) {
			$this->locale = $row['locale'];
		}
		if (array_key_exists('creation_date', $row)) {
			$this->creation_date = $row['creation_date'];
		}
		if (array_key_exists('update_date', $row)) {
			$this->update_date = $row['update_date'];
		}
		if (array_key_exists('author', $row)) {
			$this->author = $row['author'];
		}
		if (array_key_exists('is_public', $row)) {
			$this->is_public = $row['is_public'];
		}
		if (array_key_exists('is_archive', $row)) {
			$this->is_archive = $row['is_archive'];
		}
		if (array_key_exists('is_commentable', $row)) {
			$this->is_commentable = $row['is_commentable'];
		}
		if (array_key_exists('type', $row)) {
			$this->type = $row['type'];
		}
		if (array_key_exists('poi_type', $row)) {
			$this->poi_type = $row['poi_type'];
		}
		if (array_key_exists('name', $row)) {
			$this->name = $row['name'];
		}
		if ($decodedParameters !== null) {
			$this->parameters = $decodedParameters;
		}
		if (array_key_exists('lon', $row)) {
			$this->lon = $row['lon'];
		}
		if (array_key_exists('lat', $row)) {
			$this->lat = $row['lat'];
		}
		if (array_key_exists('ele', $row)) {
			$this->ele = $row['ele'];
		}
		if (array_key_exists('source', $row)) {
			$this->source = $row['source'];
		}
		if (array_key_exists('remote_source_id', $row)) {
			$this->remote_source_id = $row['remote_source_id'];
		}
	}

	/*****
	** Create a new poi
	*****/
	public function insert() {
		global $config;
		global $user;
		
		// Création d'un nouveau POI.
		$con = sql_connect();

		pg_query($con, "BEGIN");

		$query = "INSERT INTO contents (is_commentable, is_public, permalink, creation_date, type, poi_type) VALUES
			($1, TRUE, $2, $3, $4, $5) RETURNING id";

		pg_prepare($con, "prepare1", $query) 
			or die ("Cannot prepare statement\n");
		$result = pg_execute($con, "prepare1", array($this->is_commentable, $this->permalink, date('r'), $this->type, $this->poi_type))
			or die ("Cannot execute statement\n");

		$this->content_id = pg_fetch_assoc($result)['id'];

		$query = "INSERT INTO content_locales (content_id, locale, author) VALUES
			($1, $2, $3) RETURNING id";

		pg_prepare($con, "prepare2", $query) 
			or die ("Cannot prepare statement\n");
		$result = pg_execute($con, "prepare2", array($this->content_id, $this->locale, $user->id))
			or die ("Cannot execute statement\n");

		$this->locale_id = pg_fetch_assoc($result)['id'];

		$query = "INSERT INTO content_versions (version, update_date, is_archive, name, content, locale_id) VALUES
			('0', $1, FALSE, $2, $3, $4) RETURNING id";

		pg_prepare($con, "prepare3", $query) 
			or die ("Cannot prepare statement\n");

		$result = pg_execute($con, "prepare3", array(date('r'), $this->name, $this->parameters, $this->locale_id))
			or die ("Cannot execute statement\n");

		$this->version_id = pg_fetch_assoc($result)['id'];

		$query = "INSERT INTO content_version_poi_specifications (content_version_id, geom, source_id, remote_source_id) VALUES
			($1, ST_SetSRID(ST_MakePoint($2, $3, $4), 4326), $5, $6)";

		pg_prepare($con, "prepare4", $query) 
			or die ("Cannot prepare statement\n");
		$result = pg_execute($con, "prepare4", array($this->version_id, $this->lon, $this->lat, $this->ele ,$this->source_id, $this->remote_source_id))
			or die ("Cannot execute statement\n");

		$query = "INSERT INTO content_contributors (content, contributor) VALUES
			($1, $2)";

		pg_prepare($con, "prepare5", $query) 
			or die ("Cannot prepare statement\n");
		$result = pg_execute($con, "prepare5", array($this->locale_id, $user->id))
			or die ("Cannot execute statement\n");

		pg_query($con, "COMMIT");

		pg_close($con);

		error_log(
			date('r')." \t".$user->name." (".$user->id.") \tINSERT \tCreate new poi '".$this->permalink."'\r\n",
			3,
			$config['logs_folder'].'poi.log');
	}

	/*****
	** Edit a POI by archiving the current version and inserting a new one
	*****/
	public function update() {
		global $config;
		global $user;

		// Vérifie que les identifiants sont chargés.
		if ($this->content_id == 0 || $this->locale_id == 0 || $this->version_id == 0)
			die("Cannot update entry without giving ID");

		$this->version++;

		$con = sql_connect();

		pg_query($con, "BEGIN");

		// 1) Archive old versions
		$query = "UPDATE content_versions SET is_archive = TRUE WHERE locale_id = $1";
		pg_prepare($con, "poi_update_archive", $query);
		pg_execute($con, "poi_update_archive", array($this->locale_id));

		// 2) Insert new version
		$query = "INSERT INTO content_versions (version, update_date, is_archive, name, content, locale_id) VALUES
			($1, $2, FALSE, $3, $4, $5) RETURNING id";

		pg_prepare($con, "poi_update_newversion", $query);

		$result = pg_execute($con, "poi_update_newversion", array($this->version, date('r'), $this->name, $this->parameters, $this->locale_id));

		$this->version_id = pg_fetch_assoc($result)['id'];

		// 3) Insert new geometry + source info for this new version
		$query = "INSERT INTO content_version_poi_specifications (content_version_id, geom, source_id, remote_source_id) VALUES
			($1, ST_SetSRID(ST_MakePoint($2, $3, $4), 4326), $5, $6)";

		pg_prepare($con, "poi_insert_specs_update", $query);
		pg_execute($con, "poi_insert_specs_update", array( $this->version_id, $this->lon, $this->lat, $this->ele, $this->source, $this->remote_source_id	));

		// 4) Update is_commentable
		$query = "UPDATE contents SET is_commentable = $1, poi_type = $2 WHERE id = $3";
		pg_prepare($con, "poi_update_commentable", $query);
		pg_execute($con, "poi_update_commentable", array( $this->is_commentable ? 't' : 'f', $this->poi_type, $this->content_id));

		// 5) Add contributor
		$query = "INSERT INTO content_contributors (content, contributor)
				  VALUES ($1, $2) ON CONFLICT (content, contributor) DO NOTHING";

		pg_prepare($con, "poi_update_contrib", $query);
		pg_execute($con, "poi_update_contrib", array($this->content_id, $user->id));

		pg_query($con, "COMMIT");
		pg_close($con);

		error_log(
			date('r')." \t".$user->name." (".$user->id.") \tUPDATE \tEdit POI '".$this->permalink."'\r\n",
			3,
			$config['logs_folder'].'poi.log'
		);
	}

	/*****
	** Archive a POI
	*****/
	public function delete() {
		global $config;
		global $user;

		// Archivage logique du POI.
		$con = sql_connect();

		$query = "UPDATE contents SET is_public = FALSE WHERE id = $1";

		pg_prepare($con, "poi_delete", $query);
		pg_execute($con, "poi_delete", array($this->content_id));

		pg_close($con);

		error_log(
			date('r')." \t".$user->name." (".$user->id.") \tDELETE \tArchive POI '".$this->permalink."'\r\n",
			3,
			$config['logs_folder'].'poi.log'
		);
	}

	/*****
	** Restore a POI
	*****/
	public function restore() {
		global $config;
		global $user;

		// Restauration d'un POI archivé.
		$con = sql_connect();

		$query = "UPDATE contents SET is_public = TRUE WHERE id = $1";

		pg_prepare($con, "poi_restore", $query);
		pg_execute($con, "poi_restore", array($this->content_id));

		pg_close($con);

		error_log(
			date('r')." \t".$user->name." (".$user->id.") \tRESTORE \tPublish POI '".$this->permalink."'\r\n",
			3,
			$config['logs_folder'].'poi.log'
		);
	}
}

// Liste de points d'intérêt (POI).
class Pois
{
	public $objs = [];
	public $number = 0;

	// Liste des POIs publiés (ou archivés si demandé).
	public function listPois($archive=0) {
		global $config;

		// Chargement des POIs avec leurs versions actives.
		$con = sql_connect();

		$query = "SELECT
            content_versions.id AS version_id,
            content_versions.version,
            content_versions.update_date,
            content_versions.is_archive,
            content_versions.name,
            content_versions.content AS parameters,

            contents.id AS content_id,
            contents.permalink,
            contents.creation_date,
            contents.is_public,
            contents.is_commentable,
            contents.type,
            contents.poi_type,

            content_locales.id AS locale_id,
            content_locales.locale,
            content_locales.author,

            specs.source_id,
            specs.remote_source_id,
            ST_X(specs.geom) AS lon,
            ST_Y(specs.geom) AS lat,
            ST_Z(specs.geom) AS ele,

            sources.display_name AS source

	        FROM contents
	        INNER JOIN content_locales
	            ON contents.id = content_locales.content_id
	        INNER JOIN content_versions
	            ON content_locales.id = content_versions.locale_id
	        LEFT JOIN content_version_poi_specifications specs
	            ON specs.content_version_id = content_versions.id
	        LEFT JOIN sources
	            ON sources.id = specs.source_id

	        WHERE contents.type = 'poi'
	          AND content_versions.is_archive = FALSE";

		if ($archive != 1)
			$query .= " AND contents.is_public=TRUE ";

		$query .= " ORDER BY content_versions.update_date DESC";

		pg_prepare($con, "pois_list", $query);
		$result = pg_execute($con, "pois_list", []);

		pg_close($con);

		for ($i = 0; $i < pg_num_rows($result); $i++) {
			$row = pg_fetch_assoc($result, $i);
			$this->objs[$i] = new Poi;
			$this->objs[$i]->populate($row);
		}
	}

	public function getHistory($permalink) {
		global $config;

		// Historique des versions d'un POI.
		$con = sql_connect();

		$query = "SELECT
            content_versions.id AS version_id,
            content_versions.version,
            content_versions.update_date,
            content_versions.is_archive,
            content_versions.name,
            content_versions.content AS parameters,

            contents.id AS content_id,
            contents.permalink,
            contents.creation_date,
            contents.is_public,
            contents.is_commentable,
            contents.type,
            contents.poi_type,

            content_locales.id AS locale_id,
            content_locales.locale,
            content_locales.author,

            specs.source_id,
            specs.remote_source_id,
            ST_X(specs.geom) AS lon,
            ST_Y(specs.geom) AS lat,
            ST_Z(specs.geom) AS ele,

            sources.display_name AS source

	        FROM contents
	        INNER JOIN content_locales
	            ON contents.id = content_locales.content_id
	        INNER JOIN content_versions
	            ON content_locales.id = content_versions.locale_id
	        LEFT JOIN content_version_poi_specifications specs
	            ON specs.content_version_id = content_versions.id
	        LEFT JOIN sources
	            ON sources.id = specs.source_id

	        WHERE contents.permalink = $1
	          AND contents.type = 'poi'

			ORDER BY content_versions.update_date DESC
		";

		pg_prepare($con, "poi_history", $query);
		$result = pg_execute($con, "poi_history", array($permalink));

		pg_close($con);

		$this->number = pg_num_rows($result);

		for ($i = 0; $i < $this->number; $i++) {
			$row = pg_fetch_assoc($result, $i);
			$this->objs[$i] = new Poi;
			$this->objs[$i]->populate($row);
		}
	}
}
