* @author Raul Guerrero * @link https://github.com/setdjod/myci-extension/ * @param ... * @return ... * */ class Parsercompleto extends CI_Controller { protected $rutaDiaria = FCPATH . "DOWNLOADS/DAILY"; protected $rutaDiariaFecha = ""; protected $rutaMensual = FCPATH . "DOWNLOADS/MONTH"; protected $enlaceAtom = ""; protected $fechaCreacionLog = ""; public function __construct() { parent::__construct(); $this->load->helper('file'); $this->load->helper('xml'); $this->load->library('Parserfile'); $this->load->model("Parser_model"); } public function index() { $fechaCarpeta = ""; $this->Parser_model->inicioParser(); $resultSetPerfiles = $this->Parser_model->getUrlArchivos(); $fechaCarpeta = date("Y-m-d_H-i-s"); $fechaImportacionLog = date("Y-m-d H:i:s"); foreach ($resultSetPerfiles['datos'] as $row) { $idUltimaImportacion = $this->Parser_model->setFechaNuevaImportacion($fechaImportacionLog); $this->fechaCreacionLog = $fechaCarpeta; $rutaPerfil = $this->rutaDiaria . "/" . $row->prefijos_tablas; $this->newFolder($rutaPerfil); if ($this->newFolder($rutaPerfil . "/" . $fechaCarpeta)) { $this->rutaDiariaFecha = $rutaPerfil . "/" . $fechaCarpeta . "/"; $this->downloadProcess($row->url_descarga . ".atom"); } $files = get_filenames($this->rutaDiariaFecha . "/format"); emailInicioProcesoDescarga($fechaImportacionLog, $files); $this->parserfile->index($this->rutaDiariaFecha, $fechaCarpeta, $row, $idUltimaImportacion); eliminarArchivosProcesoCompleto($this->rutaDiariaFecha); } $this->tablasCalculadas($idUltimaImportacion); $this->load->library('exportXls'); $this->exportxls->exportDatosCompletos(); } private function downloadProcess($urlXML) { $fileName = basename($urlXML); $this->downloadFile($fileName, $urlXML, $this->rutaDiariaFecha); $archivoAtom = $this->getFile($this->rutaDiariaFecha . $fileName, $fileName); $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom); while ($nuevoAtomAttrNext !== "") { $fileName = basename($nuevoAtomAttrNext); $this->downloadFile($fileName, $nuevoAtomAttrNext, $this->rutaDiariaFecha); $archivoAtom = $this->getFile($this->rutaDiariaFecha . $fileName, $fileName); $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom); } } private function readNextAttribute($archivo) { $enlace = ""; foreach ($archivo['link'] as $row) { $attrNext = $row['@attributes']['rel']; $attrHref = $row['@attributes']['href']; if ($attrNext === "next") { $enlace = $attrHref; } else { $enlace = ""; } } return $enlace; } private function getFile($url, $fileName) { $feed = implode(file($url)); $xml = simplexml_load_string($feed); $json = json_encode($xml); $array = json_decode($json, true); $this->formatXml($feed, $fileName); return $array; } private function newFolder($rutaFinal) { $result = false; if (!is_dir($rutaFinal)) { $result = mkdir($rutaFinal); mkdir($rutaFinal . "/format"); } return $result; } private function formatXml($xml, $fileName) { $openingFormat = $this->regExIniciosEsquema($xml); $closureFormat = $this->regExFinalesEsquema($openingFormat); unlink($this->rutaDiariaFecha . "/" . $fileName); file_put_contents($this->rutaDiariaFecha . "/format/" . $fileName, $closureFormat); } private function downloadFile($fileName, $url, $ruta) { if (file_put_contents($ruta . $fileName, file_get_contents($url))) { return true; } else { return false; } } private function regExIniciosEsquema($xml) { $pattern = "/(Parser_model->truncateTables("vista_datos_licitacion"); $licitaciones = $this->Xlsexport_model->getLicitaciones($idUltimaImportacion); insertBucleDuplicateOnKey($licitaciones['datos'], "vista_datos_licitacion", ""); } } /* End of file ParserCompleto.php */ /* Location: ./application/controllers/ParserCompleto.php */