Kabano is a PHP web application listing alpine huts. https://kabano.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
6.2 KiB

  1. <?
  2. namespace Kabano;
  3. /**********************************************************
  4. ***********************************************************
  5. **
  6. ** This class is to manage a poi object
  7. **
  8. ***********************************************************
  9. **********************************************************/
  10. require_once($config['third_folder']."Md/MarkdownExtra.inc.php");
  11. // This array is related to the defined SQL enum, do not touch.
  12. $poi_types = array(
  13. "basic_hut" => array("Abri sommaire", "Abri", "#ef2929", "basic_hut"),
  14. "wilderness_hut" => array("Cabane non gardée", "Cabane", "#ef2929", "wilderness_hut"),
  15. "alpine_hut" => array("Refuge gardé", "Refuge", "#ef2929", "alpine_hut"),
  16. "halt" => array("Gîte d'étape", "Gîte", "#4e9a06", "halt"),
  17. "bivouac" => array("Zone de bivouac", "Bivouac", "#ef2929", "bivouac"),
  18. "campsite" => array("Camping", "Camping", "#4e9a06", "campsite")
  19. );
  20. class Poi
  21. {
  22. public $poi_id = NULL;
  23. public $locale_id = NULL;
  24. public $source_id = NULL;
  25. public $version_id = NULL;
  26. public $is_public = NULL;
  27. public $permalink = NULL;
  28. public $creation_date = NULL;
  29. public $name = NULL;
  30. public $position = NULL;
  31. public $type = NULL;
  32. public $locale = NULL;
  33. public $source = NULL;
  34. public $remote_source_id = NULL;
  35. public $author = NULL;
  36. public $version = NULL;
  37. public $update_date = NULL;
  38. public $is_archive = NULL;
  39. public $alt_type = NULL;
  40. public $is_destroyed = NULL;
  41. public $alt_name = NULL;
  42. public $alt_position = NULL;
  43. public $parameters = NULL;
  44. /*****
  45. ** Checks if a page at this URL exists and return the ID
  46. *****/
  47. public function checkPermalink($permalink, $withArchive=0, $elementNb=0) {
  48. global $config;
  49. $con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
  50. or die ("Could not connect to server\n");
  51. $query = "SELECT poi_versions.id AS version_id, * FROM pois INNER JOIN poi_locales ON pois.id = poi_locales.poi_id INNER JOIN poi_sources ON poi_locales.id = poi_sources.locale_id INNER JOIN poi_versions ON poi_sources.id = poi_versions.source_id WHERE permalink=$1";
  52. if($withArchive==0) {
  53. $query .= " AND is_archive=FALSE AND is_public=TRUE";
  54. }
  55. $query .= " ORDER BY update_date DESC LIMIT 1 OFFSET $2";
  56. pg_prepare($con, "prepare1", $query)
  57. or die ("Cannot prepare statement\n");
  58. $result = pg_execute($con, "prepare1", array($permalink, $elementNb))
  59. or die ("Cannot execute statement\n");
  60. pg_close($con);
  61. if(pg_num_rows($result) == 1) {
  62. $row = pg_fetch_assoc($result);
  63. $this->populate($row);
  64. return 1;
  65. }
  66. else {
  67. return 0;
  68. }
  69. }
  70. /*****
  71. ** Populate the object using its ID
  72. *****/
  73. public function populate($row) {
  74. $this->$poi_id = $row['poi_id'];
  75. $this->$locale_id = $row['locale_id'];
  76. $this->$source_id = $row['source_id'];
  77. $this->$version_id = $row['version_id'];
  78. $this->$is_public = $row['is_public'];
  79. $this->$permalink = $row['permalink'];
  80. $this->$creation_date = $row['creation_date'];
  81. $this->$name = $row['name'];
  82. $this->$position = $row['position'];
  83. $this->$type = $row['type'];
  84. $this->$locale = $row['locale'];
  85. $this->$source = $row['source'];
  86. $this->$remote_source_id = $row['remote_source_id'];
  87. $this->$author = $row['author'];
  88. $this->$version = $row['version'];
  89. $this->$update_date = $row['update_date'];
  90. $this->$is_archive = $row['is_archive'];
  91. $this->$alt_type = $row['alt_type'];
  92. $this->$is_destroyed = $row['is_destroyed'];
  93. $this->$alt_name = $row['alt_name'];
  94. $this->$alt_position = $row['alt_position'];
  95. $this->$parameters = $row['parameters'];
  96. }
  97. /*****
  98. ** Create a new poi, all field required except alt_*, *_id
  99. *****/
  100. public function insert() {
  101. global $config;
  102. global $user;
  103. $con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
  104. or die ("Could not connect to server\n");
  105. // Because it is the first insert.
  106. $this->alt_type = $this->type;
  107. $this->alt_name = $this->name;
  108. $this->alt_position = $this->position;
  109. $query = "INSERT INTO pois (is_public, permalink, creation_date, name, position, type) VALUES
  110. (TRUE, $1, $2, $3, $4, $5) RETURNING id";
  111. pg_prepare($con, "prepare1", $query)
  112. or die ("Cannot prepare statement\n");
  113. $result = pg_execute($con, "prepare1", array($this->permalink, date('r'), $this->name, $this->position, $this->type))
  114. or die ("Cannot execute statement\n");
  115. $this->poi_id = pg_fetch_assoc($result)['id'];
  116. $query = "INSERT INTO poi_locales (locale, poi_id) VALUES
  117. ($1, $2) RETURNING id";
  118. pg_prepare($con, "prepare2", $query)
  119. or die ("Cannot prepare statement\n");
  120. $result = pg_execute($con, "prepare2", array($this->locale, $this->poi_id))
  121. or die ("Cannot execute statement\n");
  122. $this->locale_id = pg_fetch_assoc($result)['id'];
  123. $query = "INSERT INTO poi_sources (source, remote_source_id, author, locale_id) VALUES
  124. ($1, $2, $3, $4) RETURNING id";
  125. pg_prepare($con, "prepare3", $query)
  126. or die ("Cannot prepare statement\n");
  127. $result = pg_execute($con, "prepare3", array($this->source, $this->remote_source_id, $this->author, $this->locale_id))
  128. or die ("Cannot execute statement\n");
  129. $this->source_id = pg_fetch_assoc($result)['id'];
  130. $query = "INSERT INTO poi_versions (version, update_date, is_archive, alt_type, is_destroyed, alt_name, alt_position, parameters, source_id) VALUES
  131. ('0', $1, FALSE, $2, $3, $4, $5, $6, $7) RETURNING id";
  132. pg_prepare($con, "prepare4", $query)
  133. or die ("Cannot prepare statement\n");
  134. $result = pg_execute($con, "prepare4", array(date('r'), $this->alt_type, $this->is_destroyed, $this->alt_name, $this->alt_position, $this->parameters, $this->source_id))
  135. or die ("Cannot execute statement\n");
  136. $this->version_id = pg_fetch_assoc($result)['id'];
  137. $query = "INSERT INTO poi_contributors (poi, contributor) VALUES
  138. ($1, $2)";
  139. pg_prepare($con, "prepare5", $query)
  140. or die ("Cannot prepare statement\n");
  141. $result = pg_execute($con, "prepare5", array($this->source_id, $user->id))
  142. or die ("Cannot execute statement\n");
  143. pg_close($con);
  144. error_log(
  145. date('r')." \t".$user->name." (".$user->id.") \tINSERT \tCreate new poi '".$this->permalink."'\r\n",
  146. 3,
  147. $config['logs_folder'].'wiki.log');
  148. }
  149. }
  150. ?>