PageRenderTime 23ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/braldahim/library/Bral/Lieux/Notaire.php

https://github.com/Canop/braldahim
PHP | 436 lines | 342 code | 77 blank | 17 comment | 82 complexity | 4c76bfbd0006e3267b5fafbc9a1badf6 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 Bral_Lieux_Notaire extends Bral_Lieux_Lieu {
  8. function prepareCommun() {
  9. Zend_Loader :: loadClass('Lieu');
  10. Zend_Loader :: loadClass("Region");
  11. Zend_Loader :: loadClass("Palissade");
  12. Zend_Loader :: loadClass("Echoppe");
  13. Zend_Loader :: loadClass("Champ");
  14. $regionTable = new Region();
  15. $regions = $regionTable->fetchAll(null, 'nom_region');
  16. $regions = $regions->toArray();
  17. $regionCourante = null;
  18. foreach ($regions as $r) {
  19. if ($r["x_min_region"] <= $this->view->user->x_braldun && $r["x_max_region"] >= $this->view->user->x_braldun && $r["y_min_region"] <= $this->view->user->y_braldun && $r["y_max_region"] >= $this->view->user->y_braldun) {
  20. $this->view->regionCourante = $r;
  21. break;
  22. }
  23. }
  24. if ($this->view->regionCourante == null) {
  25. throw new Zend_Exception(get_class($this) . " Region inconnue x:" . $this->view->user->x_braldun . " y:" . $this->view->user->y_braldun);
  26. }
  27. $this->view->tabRegionCourante = $this->view->regionCourante;
  28. $this->idSelection = 0;
  29. if ($this->request->get("valeur_1") != null) {
  30. list ($this->view->idTypeCourant, $this->idSelection) = preg_split("/_/", $this->request->get("valeur_1"));
  31. }
  32. $selectedChamp = "";
  33. $selectedEchoppe = "";
  34. if ($this->view->idTypeCourant == "acheterchamp") {
  35. $selectedChamp = "selected";
  36. } else if ($this->view->idTypeCourant == "acheterechoppe") {
  37. $selectedEchoppe = "selected";
  38. }
  39. if ($this->view->user->niveau_braldun >= 5) {
  40. $tabTypeAchat[] = array("id_type_action" => "acheterchamp_0", "texte" => "Acheter un champ", "selected" => $selectedChamp);
  41. }
  42. $tabTypeAchat[] = array("id_type_action" => "acheterechoppe_0", "texte" => "Acheter une échoppe", "selected" => $selectedEchoppe);
  43. $this->prepareEchoppes($tabTypeAchat);
  44. $this->view->typeAction = $tabTypeAchat;
  45. if ($this->view->idTypeCourant == "acheterchamp") {
  46. $this->prepareAcheterChamp();
  47. } elseif ($this->view->idTypeCourant == "acheterechoppe") {
  48. $this->prepareAcheterEchoppe();
  49. } elseif ($this->view->idTypeCourant == "deplacerechoppe") {
  50. $this->prepareDeplacerEchoppe();
  51. } elseif ($this->view->idTypeCourant == "supprimerechoppe") {
  52. $this->prepareSupprimerEchoppe();
  53. }
  54. $this->view->coutCastars = $this->calculCoutCastars();
  55. $this->view->achatPossible = (($this->view->user->castars_braldun - $this->view->coutCastars) >= 0);
  56. }
  57. private function prepareEchoppes(&$tabTypeAchat) {
  58. $echoppesTable = new Echoppe();
  59. $echoppesRowset = $echoppesTable->findByIdBraldun($this->view->user->id_braldun);
  60. $idRecu = $this->view->idTypeCourant."_".$this->idSelection;
  61. $tabEchoppes = null;
  62. foreach ($echoppesRowset as $e) {
  63. $echoppe = array (
  64. "id_echoppe" => $e["id_echoppe"],
  65. "x_echoppe" => $e["x_echoppe"],
  66. "y_echoppe" => $e["y_echoppe"],
  67. "nom_metier" => $e["nom_masculin_metier"],
  68. "id_metier" => $e["id_metier"],
  69. "id_region" => $e["id_region"],
  70. "nom_region" => $e["nom_region"]
  71. );
  72. if ($this->view->tabRegionCourante["id_region"] == $e["id_region"]) {
  73. $idDeplacement = "deplacerechoppe_".$e["id_echoppe"];
  74. $idSuppression = "supprimerechoppe_".$e["id_echoppe"];
  75. $selectedDeplacement = "";
  76. $selectedSuppression = "";
  77. if ($idRecu == $idDeplacement) {
  78. $selectedDeplacement = "selected";
  79. $this->echoppeCourante = $echoppe;
  80. } elseif ($idRecu == $idSuppression) {
  81. $selectedSuppression = "selected";
  82. $this->echoppeCourante = $echoppe;
  83. }
  84. $nom = $e["nom_masculin_metier"]. " x:".$e["x_echoppe"]." y:".$e["y_echoppe"];
  85. $tabTypeAchat[] = array("id_type_action" => $idDeplacement, "texte" => "Déplacer l'échoppe ".$nom, "selected" => $selectedDeplacement);
  86. $tabTypeAchat[] = array("id_type_action" => $idSuppression, "texte" => "Supprimer l'échoppe ".$nom, "selected" => $selectedSuppression);
  87. }
  88. $tabEchoppes[] = $echoppe;
  89. }
  90. $this->view->nEchoppes = count($tabEchoppes);
  91. $this->echoppes = $tabEchoppes;
  92. }
  93. private function prepareAcheterChamp() {
  94. Zend_Loader :: loadClass("Champ");
  95. $champsTable = new Champ();
  96. $champsRowset = $champsTable->findByIdBraldun($this->view->user->id_braldun);
  97. $tabChamps = null;
  98. foreach ($champsRowset as $e) {
  99. $tabChamps[] = array (
  100. "id_champ" => $e["id_champ"],
  101. "x_champ" => $e["x_champ"],
  102. "y_champ" => $e["y_champ"],
  103. "id_region" => $e["id_region"],
  104. "nom_region" => $e["nom_region"],
  105. );
  106. }
  107. $this->view->nChamps = count($tabChamps);
  108. $this->view->nChampsPossibleMax = floor($this->view->user->niveau_braldun / 10) + 1;
  109. $this->view->nChampsPossible = $this->view->nChampsPossibleMax - $this->view->nChamps;
  110. }
  111. private function prepareAcheterEchoppe() {
  112. Zend_Loader :: loadClass("BraldunsMetiers");
  113. $braldunsMetiersTable = new BraldunsMetiers();
  114. $braldunsMetierRowset = $braldunsMetiersTable->findMetiersEchoppeByBraldunId($this->view->user->id_braldun);
  115. $this->view->aucuneEchoppe = true;
  116. $this->view->construireMetierPossible = false;
  117. foreach ($braldunsMetierRowset as $m) {
  118. if ($m["est_actif_hmetier"] != "oui") {
  119. continue;
  120. } else {
  121. $this->view->construireMetierPossible = true;
  122. $this->id_metier_courant = $m["id_metier"];
  123. }
  124. if ($this->view->user->sexe_braldun == 'feminin') {
  125. $this->view->nom_metier_courant = $m["nom_feminin_metier"];
  126. } else {
  127. $this->view->nom_metier_courant = $m["nom_masculin_metier"];
  128. }
  129. if (count($this->echoppes) > 0) {
  130. foreach ($this->echoppes as $e) {
  131. if ($e["id_metier"] == $m["id_metier"] && $this->view->tabRegionCourante["id_region"] == $e["id_region"]) {
  132. $this->view->aucuneEchoppe = false;
  133. break;
  134. }
  135. }
  136. }
  137. if ($m["est_actif_hmetier"] == "oui") {
  138. break;
  139. }
  140. }
  141. }
  142. private function prepareDeplacerEchoppe() {
  143. $this->verificationEchoppes();
  144. }
  145. private function prepareSupprimerEchoppe() {
  146. $this->verificationEchoppes();
  147. }
  148. private function verificationEchoppes() {
  149. $trouvee = false;
  150. foreach($this->echoppes as $e) {
  151. if ($e["id_echoppe"] == $this->idSelection) {
  152. $trouvee = true;
  153. }
  154. }
  155. if ($trouvee == false) {
  156. throw new Zend_Exception("Erreur Echoppe ".$this->idSelection);
  157. }
  158. }
  159. function prepareFormulaire() {
  160. }
  161. function prepareResultat() {
  162. if ($this->view->idTypeCourant == "acheterchamp") {
  163. $this->prepareResultatAchatChamp();
  164. } elseif ($this->view->idTypeCourant == "acheterechoppe") {
  165. $this->prepareResultatAchatEchoppe();
  166. } elseif ($this->view->idTypeCourant == "deplacerechoppe") {
  167. $this->prepareResultatDeplacerEchoppe();
  168. } elseif ($this->view->idTypeCourant == "supprimerechoppe") {
  169. $this->prepareResultatSupprimerEchoppe();
  170. } else {
  171. throw new Zend_Exception(get_class($this) . "Erreur val1:" . $this->request->get("valeur_1"). " typeCourant:".$this->view->idTypeCourant);
  172. }
  173. }
  174. private function prepareResultatAchatChamp() {
  175. if ($this->view->nChampsPossible < 0 || $this->view->achatPossible !== true && $this->utilisationPaPossible !== true) {
  176. throw new Zend_Exception(get_class($this) . " Achat interdit");
  177. }
  178. if ($this->verificationPositions() == false) {
  179. return;
  180. }
  181. $champTable = new Champ();
  182. $data = array (
  183. 'id_fk_braldun_champ' => $this->view->user->id_braldun,
  184. 'x_champ' => $this->view->x_construction,
  185. 'y_champ' => $this->view->y_construction,
  186. 'z_champ' => 0,
  187. 'date_creation_champ' => date("Y-m-d H:i:s"),
  188. 'date_utilisation_champ' => date("Y-m-d 00:00:00"),
  189. "id_fk_type_graine_champ" => 1, // pour clé étrangère.
  190. );
  191. $champTable->insert($data);
  192. $this->view->constructionChampOk = true;
  193. $this->view->user->castars_braldun = $this->view->user->castars_braldun - $this->view->coutCastars;
  194. $this->majBraldun();
  195. }
  196. private function prepareResultatAchatEchoppe() {
  197. if ($this->view->aucuneEchoppe !== true || $this->view->construireMetierPossible !== true || $this->view->achatPossible !== true && $this->utilisationPaPossible !== true) {
  198. throw new Zend_Exception(get_class($this) . " Construction interdite");
  199. }
  200. if ($this->verificationPositions() == false) {
  201. return;
  202. }
  203. $echoppesTable = new Echoppe();
  204. $data = array (
  205. 'id_fk_braldun_echoppe' => $this->view->user->id_braldun,
  206. 'x_echoppe' => $this->view->x_construction,
  207. 'y_echoppe' => $this->view->y_construction,
  208. 'z_echoppe' => 0,
  209. 'id_fk_metier_echoppe' => $this->id_metier_courant,
  210. 'date_creation_echoppe' => date("Y-m-d H:i:s"),
  211. );
  212. $idEchoppe = $echoppesTable->insert($data);
  213. $this->view->constructionEchoppeOk = true;
  214. $this->idSelection = $idEchoppe;
  215. $this->constructionRoute();
  216. $this->view->user->castars_braldun = $this->view->user->castars_braldun - $this->view->coutCastars;
  217. $this->majBraldun();
  218. }
  219. private function constructionRoute() {
  220. $routeTable = new Route();
  221. // Suppression d'une route s'il y en a une
  222. $where = "x_route = ".$this->view->x_construction. " AND y_route=".$this->view->y_construction. " and z_route=0";
  223. $routeTable->delete($where);
  224. // et construction d'une route d'échoppe
  225. $data = array(
  226. "x_route" => $this->view->x_construction,
  227. "y_route" => $this->view->y_construction,
  228. "z_route" => 0,
  229. "id_fk_braldun_route" => null,
  230. "id_fk_echoppe_route" => $this->idSelection,
  231. "date_creation_route" => date("Y-m-d H:i:s"),
  232. "id_fk_type_qualite_route" => null,
  233. "type_route" => "echoppe",
  234. "est_visible_route" => 'oui',
  235. );
  236. $idRoute = $routeTable->insert($data);
  237. }
  238. private function prepareResultatDeplacerEchoppe() {
  239. $this->verificationEchoppes();
  240. if ($this->verificationPositions() == false) {
  241. return;
  242. }
  243. $echoppesTable = new Echoppe();
  244. $data = array (
  245. 'x_echoppe' => $this->view->x_construction,
  246. 'y_echoppe' => $this->view->y_construction,
  247. 'z_echoppe' => 0,
  248. );
  249. $where = "id_echoppe = ".intval($this->idSelection);
  250. $echoppesTable->update($data, $where);
  251. $this->view->deplacerEchoppeOk = true;
  252. $routeTable = new Route();
  253. // Suppression d'une route s'il y en a une
  254. $where = "x_route = ".$this->echoppeCourante["x_echoppe"]. " AND y_route=".$this->echoppeCourante["y_echoppe"]. " and z_route = 0";
  255. $routeTable->delete($where);
  256. $this->constructionRoute();
  257. $this->view->user->castars_braldun = $this->view->user->castars_braldun - $this->view->coutCastars;
  258. $this->majBraldun();
  259. }
  260. private function prepareResultatSupprimerEchoppe() {
  261. $this->verificationEchoppes();
  262. $echoppesTable = new Echoppe();
  263. $where = "id_echoppe = ".intval($this->idSelection);
  264. $echoppesTable->delete($where);
  265. //Suppression route automatique (cascade)
  266. $this->view->supprimerEchoppeOk = true;
  267. $this->view->user->castars_braldun = $this->view->user->castars_braldun - $this->view->coutCastars;
  268. $this->majBraldun();
  269. }
  270. private function verificationPositions() {
  271. $x = $this->request->get("valeur_2");
  272. $y = $this->request->get("valeur_3");
  273. if ($x == "") {
  274. throw new Zend_Exception(get_class($this) . " X interdit");
  275. }
  276. if ($y == "") {
  277. throw new Zend_Exception(get_class($this) . " y interdit");
  278. }
  279. $x = intval($x);
  280. $y = intval($y);
  281. $z = 0;
  282. $this->view->x_construction = $x;
  283. $this->view->y_construction = $y;
  284. // on verifie que l'on est pas sur un lieu
  285. $lieuxTable = new Lieu();
  286. $lieux = $lieuxTable->findByCase($x, $y, $z);
  287. $this->view->construireLieuOk = true;
  288. if (count($lieux) > 0) {
  289. $this->view->construireLieuOk = false;
  290. return false;
  291. }
  292. // on verifie que l'on est pas sur une echoppe
  293. $echoppesTable = new Echoppe();
  294. $echoppes = $echoppesTable->findByCase($x, $y, $z);
  295. $this->view->construireLieuEchoppeOk = true;
  296. if (count($echoppes) > 0) {
  297. $this->view->construireLieuEchoppeOk = false;
  298. return false;
  299. }
  300. // on verifie que l'on est pas sur une palissade
  301. $palissadesTable = new Palissade();
  302. $palissades = $palissadesTable->findByCase($x, $y, $z);
  303. $this->view->construireLieuPalissadeOk = true;
  304. if (count($palissades) > 0) {
  305. $this->view->construireLieuPalissadeOk = false;
  306. return false;
  307. }
  308. // on verifie que l'on est pas sur une palissade
  309. $champsTable = new Champ();
  310. $champs = $champsTable->findByCase($x, $y, $z);
  311. $this->view->construireLieuChampOk = true;
  312. if (count($champs) > 0) {
  313. $this->view->construireLieuChampOk = false;
  314. return false;
  315. }
  316. // on verifie que l'on est pas sur une route
  317. Zend_Loader::loadClass("Route");
  318. $routeTable = new Route();
  319. $routes = $routeTable->findByCaseHorsBalise($x, $y, $z);
  320. $this->view->construireLieuRouteOk = true;
  321. if (count($routes) > 0) {
  322. $this->view->construireLieuRouteOk = false;
  323. return false;
  324. }
  325. // on verifie que l'on est pas sur une eau
  326. Zend_Loader::loadClass("Eau");
  327. $eauTable = new Eau();
  328. $nbEaux = $eauTable->countByCase($x, $y, $z);
  329. $this->view->construireLieuEauOk = true;
  330. if ($nbEaux > 0) {
  331. $this->view->construireLieuEauOk = false;
  332. return false;
  333. }
  334. // on verifie que la position est dans la comté de la tentative
  335. $this->view->construireRegionOk = true;
  336. if ($this->view->regionCourante["x_min_region"] > $x || $this->view->regionCourante["x_max_region"] < $x || $this->view->regionCourante["y_min_region"] > $y || $this->view->regionCourante["y_max_region"] < $y) {
  337. $this->view->construireRegionOk = false;
  338. return false;
  339. }
  340. return true;
  341. }
  342. function getListBoxRefresh() {
  343. return $this->constructListBoxRefresh(array("box_laban", "box_vue", "box_echoppes", "box_champs"));
  344. }
  345. /* la premiere echoppe est gratuite */
  346. private function calculCoutCastars() {
  347. if ($this->view->idTypeCourant == "acheterechoppe") {
  348. if ($this->view->nEchoppes < 1) {
  349. return 0;
  350. } else {
  351. return 500;
  352. }
  353. } elseif ($this->view->idTypeCourant == "deplacerechoppe" || $this->view->idTypeCourant == "supprimerechoppe") {
  354. return 500;
  355. } elseif ($this->view->idTypeCourant == "acheterchamp") {
  356. return $this->view->nChamps * 10 + 10;
  357. }
  358. }
  359. }