ParserCompleto.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. defined('BASEPATH') or exit('No direct script access allowed');
  3. /**
  4. *
  5. * Controller ParserCompleto
  6. *
  7. * This controller for ...
  8. *
  9. * @package CodeIgniter
  10. * @category Controller CI
  11. * @author Setiawan Jodi <jodisetiawan@fisip-untirta.ac.id>
  12. * @author Raul Guerrero <r.g.c@me.com>
  13. * @link https://github.com/setdjod/myci-extension/
  14. * @param ...
  15. * @return ...
  16. *
  17. */
  18. class Parsercompleto extends CI_Controller
  19. {
  20. protected $rutaDiaria = FCPATH . "DOWNLOADS/DAILY/";
  21. protected $rutaDiariaFecha = "";
  22. protected $rutaMensual = FCPATH . "DOWNLOADS/MONTH/";
  23. protected $enlaceAtom = "";
  24. protected $fechaCreacionLog = "";
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. $this->load->helper('file');
  29. $this->load->helper('xml');
  30. $this->load->library('Parserfile');
  31. $this->load->model("Parser_model");
  32. }
  33. public function index()
  34. {
  35. $fechaCarpeta = "";
  36. $this->Parser_model->inicioParser();
  37. $resultSetPerfiles = $this->Parser_model->getUrlArchivos();
  38. $fechaCarpeta = date("Y-m-d_H-i-s");
  39. $fechaImportacionLog = date("Y-m-d H:i:s");
  40. foreach ($resultSetPerfiles['datos'] as $row) {
  41. $idUltimaImportacion = $this->Parser_model->setFechaNuevaImportacion($fechaImportacionLog);
  42. $this->fechaCreacionLog = $fechaCarpeta;
  43. $rutaPerfil = $this->rutaDiaria . "/" . $row->prefijos_tablas;
  44. $this->newFolder($rutaPerfil);
  45. if ($this->newFolder($rutaPerfil . "/" . $fechaCarpeta)) {
  46. $this->rutaDiariaFecha = $rutaPerfil . "/" . $fechaCarpeta . "/";
  47. $this->downloadProcess($row->url_descarga . ".atom");
  48. }
  49. $files = get_filenames($this->rutaDiariaFecha . "/format");
  50. emailInicioProcesoDescarga($fechaImportacionLog, $files);
  51. $this->parserfile->index($this->rutaDiariaFecha, $fechaCarpeta, $row, $idUltimaImportacion);
  52. eliminarArchivosProcesoCompleto($this->rutaDiariaFecha);
  53. }
  54. $this->tablasCalculadas($idUltimaImportacion);
  55. $this->load->library('exportXls');
  56. $this->exportxls->exportDatosCompletos();
  57. }
  58. private function downloadProcess($urlXML)
  59. {
  60. $fileName = basename($urlXML);
  61. $this->downloadFile($fileName, $urlXML, $this->rutaDiariaFecha);
  62. $archivoAtom = $this->getFile($this->rutaDiariaFecha . $fileName, $fileName);
  63. $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
  64. while ($nuevoAtomAttrNext !== "") {
  65. $fileName = basename($nuevoAtomAttrNext);
  66. $this->downloadFile($fileName, $nuevoAtomAttrNext, $this->rutaDiariaFecha);
  67. $archivoAtom = $this->getFile($this->rutaDiariaFecha . $fileName, $fileName);
  68. $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
  69. }
  70. }
  71. private function readNextAttribute($archivo)
  72. {
  73. $enlace = "";
  74. foreach ($archivo['link'] as $row) {
  75. $attrNext = $row['@attributes']['rel'];
  76. $attrHref = $row['@attributes']['href'];
  77. if ($attrNext === "next") {
  78. $enlace = $attrHref;
  79. } else {
  80. $enlace = "";
  81. }
  82. }
  83. return $enlace;
  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:)/";
  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:)/";
  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", "");
  135. }
  136. }
  137. /* End of file ParserCompleto.php */
  138. /* Location: ./application/controllers/ParserCompleto.php */