Parser.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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->removeFoldersFromThursdayToMonday($rutaPerfil);
  32. $this->newFolder($rutaPerfil);
  33. if ($this->newFolder($rutaPerfil . "/" . $fechaCarpeta)) {
  34. $this->rutaDiariaFecha = $rutaPerfil . "/" . $fechaCarpeta . "/";
  35. $this->downloadZip($row->url_descarga);
  36. }
  37. $files = get_filenames($this->rutaDiariaFecha . "/format");
  38. emailInicioProcesoDescarga($fechaImportacionLog, $files);
  39. $this->parserfile->index($this->rutaDiariaFecha, $fechaCarpeta, $row, $idUltimaImportacion);
  40. }
  41. */
  42. $idUltimaImportacion = 81;
  43. $this->tablasCalculadas($idUltimaImportacion);
  44. $this->load->library('ExportXls');
  45. $this->exportxls->exportDatosCompletos();
  46. $this->Parser_model->stopParser();
  47. }
  48. private function downloadZip($urlXML)
  49. {
  50. $fechaDescarga = $this->getFechaForDownloadZip();
  51. $fileName = basename($urlXML);
  52. $downladedZip = $this->downloadFile($fileName, $urlXML . $fechaDescarga, $this->rutaDiariaFecha);
  53. if ($downladedZip) {
  54. $zip = new ZipArchive;
  55. if ($zip->open($this->rutaDiariaFecha . $fileName)) {
  56. $zip->extractTo($this->rutaDiariaFecha);
  57. $zip->close();
  58. unlink($this->rutaDiariaFecha . $fileName);
  59. $archivosTotales = get_filenames($this->rutaDiariaFecha);
  60. foreach ($archivosTotales as $file) {
  61. $this->getFile($this->rutaDiariaFecha . $file, $file);
  62. }
  63. }
  64. }
  65. }
  66. private function getFechaForDownloadZip(): String
  67. {
  68. $result = "_";
  69. $anyo = date("Y");
  70. $mes = date("m");
  71. $mesPrevious = date("m", strtotime("-1 months"));
  72. $yearPrevious = date("Y", strtotime("-1 year"));
  73. $dia = date("d");
  74. if (date("m") == "01") {
  75. $result .= $yearPrevious . $mesPrevious . ".zip";
  76. } else {
  77. if ($dia == "01") {
  78. $result .= $anyo . $mesPrevious . ".zip";
  79. } else {
  80. $result .= $anyo . $mes . ".zip";
  81. }
  82. }
  83. return $result;
  84. }
  85. private function getFile($url, $fileName)
  86. {
  87. $feed = implode(file($url));
  88. $xml = simplexml_load_string($feed);
  89. $json = json_encode($xml);
  90. $array = json_decode($json, true);
  91. $this->formatXml($feed, $fileName);
  92. return $array;
  93. }
  94. private function newFolder($rutaFinal)
  95. {
  96. $result = false;
  97. if (!is_dir($rutaFinal)) {
  98. $result = mkdir($rutaFinal);
  99. mkdir($rutaFinal . "/format");
  100. }
  101. return $result;
  102. }
  103. private function formatXml($xml, $fileName)
  104. {
  105. $openingFormat = $this->regExIniciosEsquema($xml);
  106. $closureFormat = $this->regExFinalesEsquema($openingFormat);
  107. unlink($this->rutaDiariaFecha . "/" . $fileName);
  108. file_put_contents($this->rutaDiariaFecha . "/format/" . $fileName, $closureFormat);
  109. }
  110. private function downloadFile($fileName, $url, $ruta)
  111. {
  112. if (file_put_contents($ruta . $fileName, file_get_contents($url))) {
  113. return true;
  114. } else {
  115. return false;
  116. }
  117. }
  118. private function regExIniciosEsquema($xml)
  119. {
  120. $pattern = "/(<cac:)|(<cbc:)|(<cac-place-ext:)|(<cbc-place-ext:)|(<at:)/";
  121. $result = preg_replace($pattern, "<", $xml);
  122. return $result;
  123. }
  124. private function regExFinalesEsquema($xml)
  125. {
  126. $pattern = "/(<\/cac:)|(<\/cbc:)|(<\/cac-place-ext:)|(<\/cbc-place-ext:)|(<\/at:)/";
  127. $result = preg_replace($pattern, "</", $xml);
  128. return $result;
  129. }
  130. private function tablasCalculadas($idUltimaImportacion)
  131. {
  132. $this->Parser_model->truncateTables("vista_datos_licitacion");
  133. $licitaciones = $this->Xlsexport_model->getLicitaciones($idUltimaImportacion);
  134. insertBucleDuplicateOnKey($licitaciones['datos'], "vista_datos_licitacion", "insertVistaDatosLicitacionesPreExcel");
  135. }
  136. private function removeFoldersFromThursdayToMonday($ruta)
  137. {
  138. $dayWeek = date('l');
  139. if ($dayWeek == "Friday") {
  140. $folders = glob($ruta . "/*");
  141. $thursday = date('Y-m-d', strtotime(date("Y-m-d") . ' -1 days'));
  142. $minusSixDays = date('Y-m-d', strtotime($thursday . ' -6 days'));
  143. foreach ($folders as $folder) {
  144. $folderDate = substr($folder, -19, 10);
  145. if ($folderDate <= $thursday && $folderDate >= $minusSixDays) {
  146. eliminarArchivosProcesoCompleto($folder);
  147. }
  148. }
  149. }
  150. }
  151. }