/_plugins_/tourinfrance/tourinfrance_fonctions.php

https://bitbucket.org/pombredanne/spip-zone-treemap · PHP · 216 lines · 132 code · 62 blank · 22 comment · 21 complexity · 3aa94d312d19a7b46643079ca55d34d8 MD5 · raw file

  1. <?php
  2. function traiter_donnees_tourinfrance($url_flux, $id_flux, $infos_flux, $update_flux=false) {
  3. if (file_exists($url_flux)) {
  4. $xml = simplexml_load_file($url_flux);
  5. $tab_infos_flux = unserialize($infos_flux);
  6. /***** PARCOURS DES OFFRES : <LISTING> *****/
  7. $offres = $xml->xpath('//child::LISTING');
  8. $exec = "<b>" . _T('tourinfrance:message_flux_propose') . "</b><br />";
  9. $exec .= "URL : <i>$url_flux</i><br /><ul class='liste_maj_base'>";
  10. $retour_exec = "";
  11. for($i=0; $i<count($offres); $i++){
  12. /***** VALEURS des ELEMENTS DES OFFRES *****/
  13. $commun = array();
  14. $extra = array();
  15. foreach( $offres[$i] as $element => $valeur ){
  16. $contenu = trim(strip_tags($valeur->asXML()));
  17. if(in_array($element, $tab_infos_flux)){
  18. $commun[array_search($element, $tab_infos_flux)] = $contenu;
  19. }
  20. else{
  21. $extra[$element] = $contenu;
  22. }
  23. }
  24. /***** DONNEES FORMATEES *****/
  25. $commun_srlz = serialize($commun);
  26. $extra_srlz = serialize($extra);
  27. /***** MODIFICATION DU FLUX DEPUIS ESPACE PRIVE *****/
  28. if($i==0 && $update_flux==true){
  29. //On change les DATEMAJ pour forcer la mise à jour.
  30. $bordereau = strtolower($commun["id_type"]);
  31. $nom_table_tourinfrance = "spip_tourinfrance_" . $bordereau;
  32. sql_updateq($nom_table_tourinfrance, array('datemaj'=>'00000000000001'), "id_flux=$id_flux");
  33. }
  34. /***** INSERER LES DONNEES *****/
  35. $retour_exec .= inserer_donnees_tourinfrance($id_flux, $commun_srlz, $extra_srlz);
  36. }
  37. if($retour_exec == ""){
  38. $retour_exec = _T('tourinfrance:message_flux_maj_aucune');
  39. }
  40. $exec .= $retour_exec . "</ul><br />";
  41. return $exec;
  42. }
  43. else{
  44. return "$url_flux " . _T('tourinfrance:message_fichier_introuvable');
  45. }
  46. }
  47. function inserer_donnees_tourinfrance($id_flux, $commun_srlz, $extra_srlz) {
  48. $commun = unserialize($commun_srlz);
  49. $extra = unserialize($extra_srlz);
  50. $id_offre = $commun["id_offre"];
  51. $nom_offre = $commun["nom_offre"];
  52. $nom_commune = $commun["commune"];
  53. $datemaj = formater_date($commun["datemaj"]);
  54. $bordereau = strtolower($commun["id_type"]); //Bordereau
  55. //RUBRIQUES : Recupere le TYPE D'OFFRE / BORDEREAU
  56. if ($req = sql_select("id_rubrique", "spip_rubriques", "titre=" . sql_quote($bordereau))) {
  57. while ($res = sql_fetch($req)) {
  58. $id_rubrique = $res['id_rubrique'];
  59. }
  60. }
  61. //GROUPES_MOTS : Recupere ID du GROUPE "COMMUNES"
  62. if ($req = sql_select("id_groupe", "spip_groupes_mots", "titre='communes'")) {
  63. while ($res = sql_fetch($req)) {
  64. $id_gp_mot_communes = $res['id_groupe'];
  65. }
  66. }
  67. //MOTS_CLES : Test existance de la COMMUNE en MOT-CLE
  68. if ($req = sql_select("id_mot", "spip_mots", "titre=" . sql_quote($nom_commune))) {
  69. if ($res = sql_fetch($req)) {
  70. $id_mot = $res['id_mot'];
  71. }
  72. else{ //INSERTION SPIP_MOTS : Si la COMMUNE n'est pas en MOT CLE, on l'ajoute.
  73. $id_mot = sql_insertq("spip_mots", array(
  74. 'titre'=>$nom_commune,
  75. 'id_groupe'=>$id_gp_mot_communes,
  76. 'type'=>'communes'
  77. ));
  78. }
  79. }
  80. //INSERER un ARTICLE dans la table SPIP_ARTICLES (bon id_rubrique)
  81. $champ_article = array(
  82. "id_rubrique" => $id_rubrique,
  83. "id_secteur" => $id_rubrique,
  84. "titre" => $commun["nom_offre"],
  85. "descriptif" => $commun["description_offre"],
  86. "texte" => $commun["description_offre"],
  87. "statut" => "publie"
  88. );
  89. //INSERER dans la bonne table SPIP_TOURINFRANCE
  90. $nom_table_tourinfrance = "spip_tourinfrance_" . $bordereau;
  91. $champ_tourinfrance_type = $commun;
  92. $champ_tourinfrance_type["id_flux"] = $id_flux;
  93. $champ_tourinfrance_type["extra"] = $extra_srlz;
  94. //Test l'EXISTANCE de l'offre, et si MISE A JOUR.
  95. $update = false;
  96. $exist = false;
  97. if ($req = sql_select("*", $nom_table_tourinfrance, "id_offre=" . sql_quote($id_offre))) {
  98. while ($res = sql_fetch($req)) {
  99. $exist = true;
  100. $id_article = $res['id_article'];
  101. $update = comparer_date($datemaj, $res['datemaj']); //true si MAJ
  102. }
  103. }
  104. /*********** IF !UPDATE id_offre n'existe pas deja ***************/
  105. if(!$exist){
  106. //MODIFICATION : Mise a jour statut de la rubrique si c'est le premier ajout.
  107. if (!sql_countsel('spip_articles', "id_rubrique=$id_rubrique")) {
  108. sql_updateq('spip_rubriques', array('statut'=>'publie'), "id_rubrique=$id_rubrique");
  109. }
  110. //INSERTION ARTICLE
  111. $id_article = sql_insertq("spip_articles", $champ_article);
  112. //INSERTION AUTEURS_ARTICLES (liaison)
  113. $liaison_auteur_article = sql_insertq("spip_auteurs_articles", array(
  114. 'id_auteur'=>'1',
  115. 'id_article'=>$id_article
  116. ));
  117. //INSERTION MOTS_ARTICLES (liaison)
  118. $liaison_mot_article = sql_insertq("spip_mots_articles", array(
  119. 'id_mot'=>$id_mot,
  120. 'id_article'=>$id_article
  121. ));
  122. $champ_tourinfrance_type["id_article"] = $id_article;
  123. //INSERTION TOURINFRANCE
  124. $id_tourinfrance = sql_insertq($nom_table_tourinfrance, $champ_tourinfrance_type);
  125. $retour = "<li>" . _T('tourinfrance:message_ajoute') . " : <b>" . _T('article') . " n°" . $id_article . "</b> : " . $id_offre . " - " . $nom_offre . "</li>";
  126. }
  127. /*********** IF UPDATE id_offre existe deja ***************/
  128. else if($update){
  129. //MODIFICATION ARTICLE
  130. sql_updateq("spip_articles", $champ_article, "id_article=" . intval($id_article));
  131. //MODIFICATION MOTS_ARTICLES (liaison)
  132. sql_updateq("spip_mots_articles", array('id_mot'=>$id_mot), "id_article='" . $id_article . "'");
  133. $champ_tourinfrance_type["id_article"] = $id_article;
  134. //MODIFICATION TOURINFRANCE
  135. sql_updateq($nom_table_tourinfrance, $champ_tourinfrance_type, "id_offre='" . $id_offre . "'");
  136. $retour = "<li>" . _T('tourinfrance:message_modifie') . " : <b>" . _T('article') . " n°" . $id_article . "</b> : " . $id_offre . " - " . $nom_offre . "</li>";
  137. }
  138. return $retour;
  139. }
  140. function formater_date($date) {
  141. $date = str_replace('T', ' ', $date);
  142. return (substr($date,0,19));
  143. }
  144. function comparer_date($newdate, $exdate) {
  145. $search = array('-', ' ', ':');
  146. $newdate = str_replace($search, '', $newdate);
  147. $exdate = str_replace($search, '', $exdate);
  148. $up = false;
  149. if($exdate!="00000000000000" && $newdate>$exdate){
  150. $up = true;
  151. }
  152. return $up;
  153. }
  154. ?>