/braldahim/application/models/Lieu.php

https://github.com/Canop/braldahim · PHP · 337 lines · 275 code · 55 blank · 7 comment · 44 complexity · 7a816db6e3d23f168003d7aff16f007f MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of Braldahim, under Gnu Public Licence v3.
  4. * See licence.txt or http://www.gnu.org/licenses/gpl-3.0.html
  5. * Copyright: see http://www.braldahim.com/sources
  6. */
  7. class Lieu extends Zend_Db_Table {
  8. protected $_name = 'lieu';
  9. protected $_primary = 'id_lieu';
  10. public function findById($id) {
  11. $where = $this->getAdapter()->quoteInto('id_lieu = ?', (int)$id);
  12. return $this->fetchRow($where);
  13. }
  14. function countAll() {
  15. $db = $this->getAdapter();
  16. $select = $db->select();
  17. $select->from('lieu', 'count(*) as nombre');
  18. $sql = $select->__toString();
  19. $resultat = $db->fetchAll($sql);
  20. $nombre = $resultat[0]["nombre"];
  21. return $nombre;
  22. }
  23. public function findByType($type, $estSoule = null, $estReliee = null) {
  24. $db = $this->getAdapter();
  25. $select = $db->select();
  26. $select->from('lieu', '*')
  27. ->from('type_lieu', '*')
  28. ->where('lieu.id_fk_type_lieu = ?', $type)
  29. ->where('lieu.id_fk_type_lieu = type_lieu.id_type_lieu')
  30. ->joinLeft('ville', 'id_fk_ville_lieu = id_ville');
  31. if ($estSoule != null) {
  32. $select->where('lieu.est_soule_lieu = ?', $estSoule);
  33. }
  34. if ($estReliee != null) {
  35. $select->where("ville.est_reliee_ville like ? ", $estReliee);
  36. }
  37. $sql = $select->__toString();
  38. return $db->fetchAll($sql);
  39. }
  40. public function findByCritere($estDonjon = null, $estSoule = null, $estReliee = null, $estMythique, $estRuine) {
  41. Zend_Loader::loadClass("TypeLieu");
  42. $db = $this->getAdapter();
  43. $select = $db->select();
  44. $select->from('lieu', '*')
  45. ->from('type_lieu', '*')
  46. ->where('lieu.id_fk_type_lieu = type_lieu.id_type_lieu')
  47. ->joinLeft('ville', 'id_fk_ville_lieu = id_ville');
  48. if ($estDonjon != null) {
  49. $select->where('lieu.est_donjon_lieu = ?', $estDonjon);
  50. }
  51. if ($estSoule != null) {
  52. $select->where('lieu.est_soule_lieu = ?', $estSoule);
  53. }
  54. if ($estReliee != null) {
  55. $select->where("ville.est_reliee_ville like ? ", $estReliee);
  56. }
  57. if ($estMythique == "non") {
  58. $select->where("id_fk_type_lieu not like ? ", TypeLieu::ID_TYPE_LIEUMYTHIQUE);
  59. }
  60. if ($estRuine == "non") {
  61. $select->where("id_fk_type_lieu not like ? ", TypeLieu::ID_TYPE_RUINE);
  62. }
  63. $sql = $select->__toString();
  64. return $db->fetchAll($sql);
  65. }
  66. public function findByTypeAndRegion($type, $region, $estSoule = null, $estCapitale = null) {
  67. $db = $this->getAdapter();
  68. $select = $db->select();
  69. $select->from('lieu', '*')
  70. ->from('type_lieu', '*')
  71. ->from('region', '*')
  72. ->where('region.id_region = ?', $region)
  73. ->where('lieu.id_fk_type_lieu = ?', $type)
  74. ->where('lieu.id_fk_type_lieu = type_lieu.id_type_lieu')
  75. ->where('lieu.x_lieu >= region.x_min_region')
  76. ->where('lieu.x_lieu <= region.x_max_region')
  77. ->where('lieu.y_lieu >= region.y_min_region')
  78. ->where('lieu.y_lieu <= region.y_max_region')
  79. ->joinLeft('ville', 'id_fk_ville_lieu = id_ville');
  80. if ($estSoule != null) {
  81. $select->where('lieu.est_soule_lieu = ?', $estSoule);
  82. }
  83. if ($estCapitale != null) {
  84. $select->where('ville.est_capitale_ville = ?', $estCapitale);
  85. }
  86. $sql = $select->__toString();
  87. return $db->fetchAll($sql);
  88. }
  89. function selectVue($x_min, $y_min, $x_max, $y_max, $z, $id_type = null) {
  90. $db = $this->getAdapter();
  91. $select = $db->select();
  92. $select->from('lieu', '*')
  93. ->from('type_lieu', '*')
  94. ->where('x_lieu <= ?', $x_max)
  95. ->where('x_lieu >= ?', $x_min)
  96. ->where('y_lieu >= ?', $y_min)
  97. ->where('y_lieu <= ?', $y_max)
  98. ->where('z_lieu = ?', $z)
  99. ->where('lieu.id_fk_type_lieu = type_lieu.id_type_lieu');
  100. if ($id_type != null) {
  101. $select->where('id_fk_type_lieu = ?', $id_type);
  102. }
  103. $sql = $select->__toString();
  104. return $db->fetchAll($sql);
  105. }
  106. function countVue($x_min, $y_min, $x_max, $y_max, $z, $id_type) {
  107. $db = $this->getAdapter();
  108. $select = $db->select();
  109. $select->from('lieu', 'count(*) as nombre')
  110. ->where('x_lieu <= ?', $x_max)
  111. ->where('x_lieu >= ?', $x_min)
  112. ->where('y_lieu >= ?', $y_min)
  113. ->where('y_lieu <= ?', $y_max)
  114. ->where('z_lieu = ?', $z);
  115. if ($id_type != null) {
  116. $select->where('id_fk_type_lieu = ?', $id_type);
  117. }
  118. $sql = $select->__toString();
  119. $resultat = $db->fetchAll($sql);
  120. $nombre = $resultat[0]["nombre"];
  121. return $nombre;
  122. }
  123. function findNomById($id) {
  124. $db = $this->getAdapter();
  125. $select = $db->select();
  126. $select->from('lieu', '*')
  127. ->from('type_lieu', '*')
  128. ->where('id_lieu = ?', $id)
  129. ->where('lieu.id_fk_type_lieu = type_lieu.id_type_lieu');
  130. $sql = $select->__toString();
  131. $lieu = $db->fetchRow($sql);
  132. if ($lieu == null) {
  133. $retour = "lieu inconnu";
  134. } else {
  135. $retour = $lieu["nom_lieu"] . " (" . $lieu["id_lieu"] . ")";
  136. }
  137. return $retour;
  138. }
  139. function findByCase($x, $y, $z) {
  140. $db = $this->getAdapter();
  141. $select = $db->select();
  142. $select->from('lieu', '*')
  143. ->from('type_lieu', '*')
  144. ->where('x_lieu = ?', $x)
  145. ->where('y_lieu = ?', $y)
  146. ->where('z_lieu = ?', $z)
  147. ->where('lieu.id_fk_type_lieu = type_lieu.id_type_lieu')
  148. ->joinLeft('ville', 'id_fk_ville_lieu = id_ville');
  149. $sql = $select->__toString();
  150. return $db->fetchAll($sql);
  151. }
  152. function countByCase($x, $y, $z) {
  153. $db = $this->getAdapter();
  154. $select = $db->select();
  155. $select->from('lieu', 'count(*) as nombre')
  156. ->where('x_lieu = ?', $x)
  157. ->where('y_lieu = ?', $y)
  158. ->where('z_lieu = ?', $z);
  159. $sql = $select->__toString();
  160. $resultat = $db->fetchAll($sql);
  161. $nombre = $resultat[0]["nombre"];
  162. return $nombre;
  163. }
  164. function findByTypeAndCase($type, $x, $y, $z) {
  165. $db = $this->getAdapter();
  166. $select = $db->select();
  167. $select->from('lieu', '*')
  168. ->from('type_lieu', '*')
  169. ->where('x_lieu = ?', $x)
  170. ->where('y_lieu = ?', $y)
  171. ->where('z_lieu = ?', $z)
  172. ->where('lieu.id_fk_type_lieu = ?', $type)
  173. ->where('lieu.id_fk_type_lieu = type_lieu.id_type_lieu')
  174. ->joinLeft('ville', 'id_fk_ville_lieu = id_ville');
  175. $sql = $select->__toString();
  176. return $db->fetchAll($sql);
  177. }
  178. function findByIdCommunaute($idCommunaute, $x = null, $y = null, $z = null, $pourEntretien = false, $idTypeLieu = null, $niveauMin = null, $estDependance = false) {
  179. if ($idCommunaute == null) {
  180. return $idCommunaute;
  181. }
  182. $db = $this->getAdapter();
  183. $select = $db->select();
  184. $select->from('lieu', '*')
  185. ->from('type_lieu', '*')
  186. ->where('lieu.id_fk_communaute_lieu = ?', $idCommunaute)
  187. ->where('lieu.id_fk_type_lieu = type_lieu.id_type_lieu')
  188. ->joinLeft('ville', 'id_fk_ville_lieu = id_ville');
  189. if ($estDependance == false) {
  190. $select->from('type_lieu_communaute', '*')
  191. ->where('type_lieu.id_fk_type_lieu_communaute_type_lieu = type_lieu_communaute.id_type_lieu_communaute');
  192. }
  193. if ($x != null && $y != null && $z != null) {
  194. $select->where('x_lieu = ?', $x);
  195. $select->where('y_lieu = ?', $y);
  196. $select->where('z_lieu = ?', $z);
  197. }
  198. if ($idTypeLieu != null) {
  199. $select->where('type_lieu.id_type_lieu = ?', intval($idTypeLieu));
  200. }
  201. if ($niveauMin != null) {
  202. $select->where('niveau_lieu >= ?', intval($niveauMin));
  203. }
  204. if ($pourEntretien) {
  205. // 15 jours
  206. // Si le batiment est de niveau 0, construit il y a moins de 15j => pas d'entretien
  207. $select->where("date_entretien_lieu <= ?", Bral_Util_ConvertDate::get_date_add_day_to_date(date("Y-m-d H:i:s"), -15));
  208. }
  209. $sql = $select->__toString();
  210. return $db->fetchAll($sql);
  211. }
  212. function findDependanceByIdTypeAndIdCommunaute($idTypeLieu, $idCommunaute) {
  213. $db = $this->getAdapter();
  214. $select = $db->select();
  215. $select->from('lieu', '*')
  216. ->from('type_lieu', '*')
  217. ->from('type_dependance', '*')
  218. ->where('type_dependance.id_fk_type_lieu_enfant_type_dependance = id_type_lieu')
  219. ->where('lieu.id_fk_communaute_lieu = ?', $idCommunaute)
  220. ->where('lieu.id_fk_type_lieu = type_lieu.id_type_lieu')
  221. ->where('type_dependance.id_fk_type_lieu_type_dependance = ?', intval($idTypeLieu));
  222. $sql = $select->__toString();
  223. return $db->fetchAll($sql);
  224. }
  225. public function findByTypeAndPosition($type, $x, $y, $estSoule = null, $estReliee = null) {
  226. $db = $this->getAdapter();
  227. $select = $db->select();
  228. $select->from('lieu', '*, SQRT(((x_lieu - ' . $x . ') * (x_lieu - ' . $x . ')) + ((y_lieu - ' . $y . ') * ( y_lieu - ' . $y . '))) as distance')
  229. ->from('type_lieu', '*')
  230. ->where('lieu.id_fk_type_lieu = ?', $type)
  231. ->where('lieu.id_fk_type_lieu = type_lieu.id_type_lieu')
  232. ->joinLeft('ville', 'id_fk_ville_lieu = id_ville')
  233. ->order(array('distance ASC'));
  234. if ($estSoule != null) {
  235. $select->where('lieu.est_soule_lieu = ?', $estSoule);
  236. }
  237. if ($estReliee != null) {
  238. $select->where("ville.est_reliee_ville like ? ", $estReliee);
  239. }
  240. $sql = $select->__toString();
  241. return $db->fetchAll($sql);
  242. }
  243. public function findByPositionMax($x, $y, $max) {
  244. $db = $this->getAdapter();
  245. $select = $db->select();
  246. $select->from('lieu', '*, SQRT(((x_lieu - ' . $x . ') * (x_lieu - ' . $x . ')) + ((y_lieu - ' . $y . ') * ( y_lieu - ' . $y . '))) as distance')
  247. ->from('type_lieu', '*')
  248. ->where('lieu.id_fk_type_lieu = type_lieu.id_type_lieu')
  249. ->where('SQRT(((x_lieu - ' . $x . ') * (x_lieu - ' . $x . ')) + ((y_lieu - ' . $y . ') * ( y_lieu - ' . $y . '))) <= ?', $max)
  250. ->joinLeft('ville', 'id_fk_ville_lieu = id_ville')
  251. ->order(array('distance ASC'));
  252. $sql = $select->__toString();
  253. return $db->fetchAll($sql);
  254. }
  255. public function findAllLieuAvecVille() {
  256. $db = $this->getAdapter();
  257. $select = $db->select();
  258. $select->from('lieu', '*')
  259. ->from('type_lieu', '*')
  260. ->from('ville', '*')
  261. ->from('region', '*')
  262. ->where('lieu.id_fk_type_lieu = type_lieu.id_type_lieu')
  263. ->where('lieu.id_fk_ville_lieu = id_ville')
  264. ->where('ville.id_fk_region_ville = region.id_region');
  265. $sql = $select->__toString();
  266. return $db->fetchAll($sql);
  267. }
  268. public function findAllLieuQueteAvecRegion() {
  269. $db = $this->getAdapter();
  270. $select = $db->select();
  271. $select->from('lieu', '*')
  272. ->from('type_lieu', '*')
  273. ->from('ville', '*')
  274. ->from('region', '*')
  275. ->where('lieu.id_fk_type_lieu = type_lieu.id_type_lieu')
  276. ->where('lieu.id_fk_ville_lieu = id_ville')
  277. ->where('ville.id_fk_region_ville = region.id_region')
  278. ->where('nom_systeme_type_lieu = ?', 'quete');
  279. $sql = $select->__toString();
  280. return $db->fetchAll($sql);
  281. }
  282. }