Parser.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. defined('BASEPATH') or exit('No direct script access allowed');
  3. class Parser extends CI_Controller
  4. {
  5. //TODO: PARAMETRIZAR BBDD -> SI EXISTE UN PROCESO EN MARCHA, NO INICIAR SIGUIENTE DESCARGA, AVISAR
  6. protected $rutaDiaria = FCPATH . "DOWNLOADS/DAILY/";
  7. protected $rutaDiariaFecha = "";
  8. protected $rutaMensual = FCPATH . "DOWNLOADS/MONTH/";
  9. protected $enlaceAtom = "";
  10. protected $fechaCreacionLog = "";
  11. public function __construct()
  12. {
  13. parent::__construct();
  14. $this->load->helper('file');
  15. $this->load->helper('xml');
  16. $this->load->library('Parserfile');
  17. $this->load->model("Parser_model");
  18. }
  19. public function index()
  20. {
  21. $fechaCarpeta = "";
  22. $this->Parser_model->inicioParser();
  23. $resultSetPerfiles = $this->Parser_model->getUrlArchivos();
  24. $fechaCarpeta = date("Y-m-d_H-i-s");
  25. $fechaImportacionLog = date("Y-m-d H:i:s");
  26. foreach ($resultSetPerfiles['datos'] as $row) {
  27. $idUltimaImportacion = $this->Parser_model->setFechaNuevaImportacion($fechaImportacionLog);
  28. $this->Xlsexport_model->getLastDateLog($idUltimaImportacion);
  29. $this->fechaCreacionLog = $fechaCarpeta;
  30. $rutaPerfil = $this->rutaDiaria . "/" . $row->prefijos_tablas;
  31. $this->newFolder($rutaPerfil);
  32. if ($this->newFolder($rutaPerfil . "/" . $fechaCarpeta)) {
  33. $this->rutaDiariaFecha = $rutaPerfil . "/" . $fechaCarpeta . "/";
  34. $this->downloadZip($row->url_descarga);
  35. }
  36. $files = get_filenames($this->rutaDiariaFecha . "/format");
  37. emailInicioProcesoDescarga($fechaImportacionLog, $files);
  38. $this->Parserfile->index($this->rutaDiariaFecha, $fechaCarpeta, $row, $idUltimaImportacion);
  39. }
  40. $this->tablasCalculadas($idUltimaImportacion);
  41. $this->load->library('exportXls');
  42. $this->exportxls->exportDatosCompletos();
  43. $this->Parser_model->stopParser();
  44. }
  45. private function downloadZip($urlXML)
  46. {
  47. $fechaDescarga = $this->getFechaForDownloadZip();
  48. $fileName = basename($urlXML);
  49. $downladedZip = $this->downloadFile($fileName, $urlXML . $fechaDescarga, $this->rutaDiariaFecha);
  50. if ($downladedZip) {
  51. $zip = new ZipArchive;
  52. if ($zip->open($this->rutaDiariaFecha . $fileName)) {
  53. $zip->extractTo($this->rutaDiariaFecha);
  54. $zip->close();
  55. unlink($this->rutaDiariaFecha . $fileName);
  56. $archivosTotales = get_filenames($this->rutaDiariaFecha);
  57. foreach ($archivosTotales as $file) {
  58. $this->getFile($this->rutaDiariaFecha . $file, $file);
  59. }
  60. }
  61. }
  62. }
  63. private function getFechaForDownloadZip(): String
  64. {
  65. $result = "_";
  66. $anyo = date("Y");
  67. $mes = date("m");
  68. $mesPrevious = date("m", strtotime("-1 months"));
  69. $yearPrevious = date("Y", strtotime("-1 year"));
  70. $dia = date("d");
  71. if (date("m") == "01") {
  72. $result .= $yearPrevious . $mesPrevious . ".zip";
  73. } else {
  74. if ($dia == "01") {
  75. $result .= $anyo . $mesPrevious . ".zip";
  76. } else {
  77. $result .= $anyo . $mes . ".zip";
  78. }
  79. }
  80. return $result;
  81. }
  82. private function getFile($url, $fileName)
  83. {
  84. $feed = implode(file($url));
  85. $xml = simplexml_load_string($feed);
  86. $json = json_encode($xml);
  87. $array = json_decode($json, true);
  88. $this->formatXml($feed, $fileName);
  89. return $array;
  90. }
  91. private function newFolder($rutaFinal)
  92. {
  93. $result = false;
  94. if (!is_dir($rutaFinal)) {
  95. $result = mkdir($rutaFinal);
  96. mkdir($rutaFinal . "/format");
  97. }
  98. return $result;
  99. }
  100. private function formatXml($xml, $fileName)
  101. {
  102. $openingFormat = $this->regExIniciosEsquema($xml);
  103. $closureFormat = $this->regExFinalesEsquema($openingFormat);
  104. file_put_contents($this->rutaDiariaFecha . "/format/" . $fileName, $closureFormat);
  105. }
  106. private function downloadFile($fileName, $url, $ruta)
  107. {
  108. if (file_put_contents($ruta . $fileName, file_get_contents($url))) {
  109. return true;
  110. } else {
  111. return false;
  112. }
  113. }
  114. private function regExIniciosEsquema($xml)
  115. {
  116. $pattern = "/(<cac:)|(<cbc:)|(<cac-place-ext:)|(<cbc-place-ext:)|(<at:)/";
  117. $result = preg_replace($pattern, "<", $xml);
  118. return $result;
  119. }
  120. private function regExFinalesEsquema($xml)
  121. {
  122. $pattern = "/(<\/cac:)|(<\/cbc:)|(<\/cac-place-ext:)|(<\/cbc-place-ext:)|(<\/at:)/";
  123. $result = preg_replace($pattern, "</", $xml);
  124. return $result;
  125. }
  126. private function tablasCalculadas($idUltimaImportacion)
  127. {
  128. $this->Parser_model->truncateTables("vista_datos_licitacion");
  129. $licitaciones = $this->Xlsexport_model->getLicitaciones($idUltimaImportacion);
  130. insertBucleDuplicateOnKey($licitaciones['datos'], "vista_datos_licitacion", "");
  131. }
  132. }