Skip to content
Snippets Groups Projects
PhasePrepareIntegrations.php 5.68 KiB
<?php
/**
 * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved.
 *
 * BOKEH is free software; you can redistribute it and/or modify
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
 * the Free Software Foundation.
 *
 * There are special exceptions to the terms and conditions of the AGPL as it
 * is applied to this software (see README file).
 *
 * BOKEH is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
 * along with BOKEH; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 */

class Class_Cosmogramme_Integration_PhasePrepareIntegrationsException extends Exception {}

class Class_Cosmogramme_Integration_PhasePrepareIntegrations extends Class_Cosmogramme_Integration_PhaseAbstract {
	const MY_ID = 0;

	protected
		$_ftp_path,
		$_integration_path,
		$_label = 'Déplacement des intégrations en file d\'attente';



	public function _init($phase) {
		if (!$this->_ftp_path = Class_CosmoVar::get('ftp_path'))
			throw new Class_Cosmogramme_Integration_PhasePrepareIntegrationsException('La variable ftp_path n\'est pas définie');

		if (!$this->_integration_path = Class_CosmoVar::get('integration_path'))
			throw new Class_Cosmogramme_Integration_PhasePrepareIntegrationsException('La variable integration_path n\'est pas définie');
	}


	protected function _previousPhaseIds() {
		return [0];
	}


	public function _execute() {
		$this->_log->ecrire('<table class="blank" cellspacing="0" cellpadding="5px">');

		foreach (Class_IntMajAuto::findAllBy(['order' => 'rang']) as $majauto)
			$this->_runOne($majauto);

		$this->_log->ecrire('</table>');
	}


	public function _runOne($majauto) {
		$id_bib = $majauto->getIdBib();
		if(!$bib = Class_IntBib::find($id_bib))
			return;

		$this->_log->ecrire( '<tr><td class="blank"><span class="bib">' . $bib->getNomCourt($id_bib) .'</span></td><td class="blank">'.$majauto->getNomFichier().'</td><td class="blank">');

		$ftpfile = $this->_getFtpFile($majauto->getNomFichier());
		$id_upload = Class_CosmoVar::get('ID_upload') + 1;
		$newfile = "integre$id_upload.pan";

		if (false !== strpos($majauto->getNomFichier(), 'http://')) {
			$uri = $majauto->getNomFichier();
			if (Class_Cosmogramme_Integration::findFirstBy(['traite' => 'non',
																											'fichier' => $uri]))
				return $this;

			$this->_log->ecrire('<span class="vert">Création de l\'intégration pour '. $uri .'</span></td>');
			$this->_newIntegration($uri, $majauto);
			return $this;
		}


		if (!$ftpfile || !$this->getFileSystem()->is_file($ftpfile)) {
			$this->_log->ecrire("pas de transfert pour: $ftpfile</td>");
			return $this;
		}

		if (!$this->_checkFileSize($ftpfile, $majauto))
			return $this;

		if (true !== $this->getFilesystem()->rename($ftpfile, $this->_integration_path.$newfile)) {
				$this->_log->ecrire('<span class="rouge">erreur au transfert du fichier</span></td>');
				$this->_incrementData('traitement_erreurs');
				return $this;
		}

		$this->_log->ecrire('<span class="vert">transfert vers '.$newfile .'</span></td>');
		$this->_newIntegration($newfile, $majauto);
		Class_CosmoVar::setValueOf('ID_upload', $id_upload);
	}


	public function _getFtpFile($filename) {
		$ftp_path = $this->_ftp_path;
		if(strpos($filename, '[DATE]') === false)
			return $ftp_path.$filename;

		// parcourir le dossier pour chercher le fichier
		$filename = str_replace('[DATE]', '' ,$filename);
		while(true) {
			if (false === $pos = strpos($filename,'/'))
				$pos = strpos($filename,'-');

			if($pos === false)
				break;

			$pos++;

			$ftp_path .= substr($filename, 0, $pos);
			$filename =substr($filename, $pos);
		}

		if (!$dir = $this->getFileSystem()->opendir($ftp_path)) {
			$this->_log->ecrire('<span class="rouge">répertoire inaccessible: '.$ftp_path.'</span>');
			$this->_incrementData('traitement_erreurs');
			return null;
		}

		while (($file = $this->getFileSystem()->readdir($dir)) !== false) {
			if(strpos($file, $filename) === false)
				continue;

			$ret = $ftp_path.$file;
			break;
		}
		$this->getFileSystem()->closedir($dir);
		return $ret;
	}


	public function _checkFileSize($file, $majauto) {
		if (!$profil = Class_IntProfilDonnees::find($majauto->getProfil()))
			return false;

		$file_type = $profil->getTypeFichier();
		$minsize = $majauto->getTailleMinImportTotal();
		$type_operation = $majauto->getTypeOperation();


		if($minsize > 0 && $file_type == 0 && $type_operation == 2 ) {
			$file_size = filesize($file);
			if($file_size > 0)
				$file_size = (int) (($file_size / 1024) / 1024);

			if($file_size < $minsize) {
				$this->_log->ecrire('<span class="rouge">Le fichier est trop petit : '.$file_size.' mo -> taille minimun attendue : '.$minsize.' mo</span></td>');
					return false;
			}
		}
		return true;
	}


	public function _newIntegration($filename, $majauto) {
		$id_bib = $majauto->getIdBib();
		$id_profil = $majauto->getProfil();
		$type_operation = $majauto->getTypeOperation();
		$type_doc = $majauto->getTypeDoc();

		$integration = Class_Cosmogramme_Integration::newInstance(['id_bib' => $id_bib,
																															 'type_operation' => $type_operation,
																															 'profil' => $id_profil,
																															 'type_doc' => $type_doc,
																															 'date_transfert' => $this->getCurrentDate(),
																															 'fichier' => $filename,
																															 'traite' => 'non']);
		$integration->save();
	}
}