webservice/scripts/jobs/getActesAsso.php
2015-09-18 09:53:21 +00:00

129 lines
3.5 KiB
PHP

<?php
// --- Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(__DIR__ . '/../../application'));
// --- Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// --- Composer autoload
require_once realpath(__DIR__ . '/../../vendor/autoload.php');
// --- Create application, bootstrap, and run
$application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
try {
$opts = new Zend_Console_Getopt(
//Options
array(
'help|?' => "Aide.",
)
);
$opts->parse();
} catch (Zend_Console_Getopt_Exception $e) {
echo $e->getUsageMessage();
exit;
}
//Usage
if(isset($opts->help))
{
echo $opts->getUsageMessage();
exit;
}
function getFilePdf($url, $file)
{
$result = true;
$fp = fopen($file, 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
$data = curl_exec($ch);
// Check if any error occured
if(curl_errno($ch))
{
$result = false;
}
curl_close($ch);
fclose($fp);
return $result;
}
$reportSubject = '';
$reportMsg = '';
$c = new Zend_Config($application->getOptions());
//Lire la base de données - Récupérer les éléments sans fichier
//Connect to the database
try {
$db = Zend_Db::factory($c->profil->db->metier);
$db->getConnection();
} catch (Zend_Db_Adapter_Exception $e) {
} catch (Zend_Exception $e) {
}
$baseUrl = 'http://extranetrec.scores-decisions.com/fichier/pdfassociation/actes/';
$path = $c->profil->path->secure . '/associations/actes/';
//Lecture des données
$acteM = new Application_Model_AssoActes($db);
$sql = $acteM->select()->where('pdfLink=?', '');
$acteList = $acteM->fetchAll($sql);
if ($acteList->count()>0) {
foreach ($acteList->toArray() as $item) {
$date = substr($item['date_acte'],0,4).substr($item['date_acte'],5,2).substr($item['date_acte'],8,2);
if (intval($item['siren'])==0) {
$file = 'ST-'.$item['waldec'].'-'.$date;
} else {
$file = 'ST-'.$item['siren'].'-'.$date;
}
//Télécharger le fichier
$isDownload = getFilePdf($baseUrl.$file, $path.$file);
if ($isDownload && file_exists($path.$file)) {
//Récupérer les informations du PDF
$pdfInfos = array();
$pdfInfos['pdfLink'] = $file;
$pdfInfos['pdfSize'] = filesize($path.$file);
//Get PDF version
$handle = fopen($path.$file, 'r');
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
if (preg_match("/^\%PDF\-(.*)\s/U", $buffer, $matches)) {
$version = $matches[1];
}
break;
}
fclose($handle);
}
$pdfInfos['pdfVer'] = $version;
//Get PDF number of pages
$pdf = new Zend_Pdf($path.$file, null, true);
$pdfInfos['pdfPage'] = count($pdf->pages);
//Modifier la ligne dans la base de données
$acteM->update($pdfInfos, 'id='.$item['id']);
}
}
}
$reportMsg = "\n-= CLI getActesAsso.";
//Envoyer un mail de rapport
$mail = new Scores_Mail_Method($c->profil->mail);
$mail->setFrom('production@scores-decisions.com', 'Production');
$mail->addTo('support@scores-decisions.com', 'Support');
$mail->setSubjectC($reportSubject);
$mail->setBodyTextC($reportMsg);