extranet/library/Scores/RapportComment.php

732 lines
20 KiB
PHP

<?php
class RapportComment
{
protected $siret;
protected $idEntreprise;
protected $siren;
protected $bilansInfos = array();
protected $ratiosEntrep = array();
protected $ratiosSecteur = array();
protected $ratiosInfos = array();
protected $tabProjection = array();
protected $commentaires = array();
protected $commentairesOutput = '';
protected $graphCouleurs = array();
protected $pathImage = '';
public function __construct($tabCommentaires = array())
{
require_once 'Vendors/ChartDirector/phpchartdir.php';
$this->commentaires = $tabCommentaires;
$this->parse();
$this->pathImage = APPLICATION_PATH.'/../cache/pages/imgcache/';
}
protected function parse()
{
$couleurs = array();
if(count($this->commentaires)>0) {
$totalLigne = count($this->commentaires);
$currentLigne = 0;
$inH = 0;
$inTable = false;
foreach($this->commentaires as $key => $commentaires)
{
$output = '';
$ligne = true;
$currentLigne++;
if(!is_array($commentaires) && !empty($commentaires))
{
//Traitement préalable des lignes
$commentaires = html_entity_decode($commentaires, ENT_QUOTES,'utf-8');
//Traitement des balises siren
$commentaires = $this->siren($commentaires);
//Traitement des balises lien
$commentaires = $this->lien($commentaires);
//Traitement des balises li+, li-, li:, li
$commentaires = $this->liste($commentaires);
//Traitement des images
$commentaires = $this->image($commentaires);
//Récupération des couleurs pour les graphiques
$couleurs = $this->graphique_couleur($commentaires);
if (count($couleurs)>0){
$graphCouleurs = $couleurs;
$commentaires = preg_replace('/COULEUR\([^\)]*\)/', '', $commentaires);
$couleurs = array();
}
//Traitement des graphiques
$commentaires = $this->graphique($commentaires);
//Traitement pour bloc texte
if (preg_match('/(.*?)\<h([0-9]{1})\>(.*?)\<\/h[0-9]{1}\>/', $commentaires, $matches)){
//Fermeture
if ($inH>0){
$commentaires = $matches[1].'</div><h'.$matches[2].'>'.$matches[3].'</h'.$matches[2].'>';
}
//Ouverture
$commentaires = $commentaires.'<div class="texth'.$matches[2].'">';
$inH++;
$ligne = false;
}
//Fermeture dernier div
if ($currentLigne == $totalLigne){
$commentaires = $commentaires.'</div>';
}
//Traitement pour fin bloc texte;
$output.= $commentaires."\n";
//Détection des début et fin de tableau
if ( preg_match('/<(table)/i', $commentaires) ){
$inTable = true;
}
if ( preg_match('/<\/(table)>/i', $commentaires) ){
$inTable = false;
}
//Nouvelle ligne
if($inTable){ $ligne = false;}
if($ligne){ $output.= '<br/>'; }
}
}
$this->commentairesOutput = $output;
}
}
protected function siren($content){
$pattern = '/(.*?)<siren>([0-9]{3})(?: )?([0-9]{3})(?: )?([0-9]{3})<\/siren>(.*?)/i';
$replace = '$1<a href=/?page=identite&siret=$2$3$4>$2 $3 $4</a>$5';
$output = preg_replace($pattern, $replace, $content);
return $output;
}
protected function liste($content){
$output = $content;
$pattern = '/(.*?)<li([:|\=|\+|-]?)>(.*)/i';
$pattern_all = '/<li([:|\=|\+|-]?)>(.*)(?:<br\/>)/isU';
preg_match_all($pattern_all, $content, $listes);
if ( isset($listes[0]) && count($listes[0])>0 ){
//FB::log($listes, 'listes');
foreach ( $listes[0] as $index => $elementR ){
$output = $this->liste_replace($elementR, $listes[2][$index],
$listes[1][$index], $output);
//FB::log($output, 'output');
}
} elseif ( preg_match($pattern, $content, $matches) ){
//FB::log($content, 'liste');
switch($matches[2]){
case '+':
$replace = '$1<span class="plus">$3</span>';
break;
case '-':
$replace = '$1<span class="moins">$3</span>';
break;
case '=':
$replace = '$1<image style="vertical-align:middle;" src="./img/li-egale.gif">$3';
break;
case ':':
$replace = '$1<span>$3</span>';
break;
default:
$replace = '$1<span class="point">$3</span>';
break;
}
$output = preg_replace($pattern, $replace, $content);
}
return $output;
}
protected function liste_replace($elementR, $element, $type, $content){
switch ( $type ){
case '+':
$replace = '<span class="plus">'.$element.'</span><br/>';
break;
case '-':
$replace = '<span class="moins">'.$element.'</span><br/>';
break;
case '=':
$replace = '<image style="vertical-align:middle;" src="./img/li-egale.gif">'.$matches[2].'<br/>';
break;
case ':':
$replace = '<span>'.$element.'</span><br/>';
break;
default:
$replace = '<span class="point">'.$element.'</span><br/>';
break;
}
$output = str_replace($elementR, $replace, $content);
return $output;
}
protected function image($content){
$pattern = '/IMAGE\((.*?)\)/i';
preg_match($pattern, $content, $matches);
$replace = '<img src="./img/commentaires/'.strtolower($matches[1]).'"/>';
$output = str_replace($matches[0], $replace, $content);
return $output;
}
protected function lien($content)
{
$path = array(
'pdf' => '/fichier/commentaires',
'html' => '',
);
$pattern = "/<lien='(.*?).(pdf|html|htm)'>(.*?)<\/lien>/i";
preg_match($pattern, $content, $matches);
if(count($matches)>0){
switch($matches[2]){
case 'pdf':
$content = str_replace(
$matches[0],
"<a href='".$path['pdf']."/".$matches[1].".".
$matches[2]."' target='_blank'>".
$matches[3]."</a>",
$content);
break;
case 'htm':
case 'html':
$content = str_replace(
$matches[0],
"<a href='".$path['html'].$matches[1].".".
$matches[2]."' target='_blank'>".
$matches[3]."</a>",
$content);
break;
}
}
return $content;
}
protected function graphique_couleur($content)
{
$this->graphCouleurs = array();
//Association des couleurs Nom => code Hexa
$assocNomCouleurs = array(
'BLA' => 0xFFFFFF,
'BLA1' => 0xFFFAFA,
'BLA2' => 0xF0FFFF,
'BLA3' => 0xFFFFF0,
'BLE' => 0x0000FF,
'BLE1' => 0x00BFFF,
'BLE2' => 0x00008B,
'BLE3' => 0x87F0FA,
'JAU' => 0xFFF000,
'JAU1' => 0xFFEFD5,
'JAU2' => 0xFFA500,
'JAU3' => 0xFFFFE0,
'LUM' => 0xADFF2F,
'LUM1' => 0x00FFFF,
'LUM2' => 0xFF00FF,
'LUM3' => 0xF0FFF0,
'MAR' => 0xD2691E,
'MAR1' => 0xF4A460,
'MAR2' => 0x8B4513,
'MAR3' => 0xF5DEB3,
'MET' => 0xFFD700,
'MET1' => 0xFFF8DC,
'MET2' => 0xC0C0C0,
'MET3' => 0xFDF5E6,
'NOI' => 0x000000,
'NOI1' => 0x808080,
'NOI2' => 0x2F4F4F,
'NOI3' => 0xDCDCDC,
'ROU' => 0xFF0000,
'ROU1' => 0xFF7F50,
'ROU2' => 0xB22222,
'ROU3' => 0xFFDAB9,
'VER' => 0x228B22,
'VER1' => 0x66CDAA,
'VER2' => 0x066400,
'VER3' => 0x98FB98,
'VIO' => 0xC71585,
'VIO1' => 0xEE82EE,
'VIO2' => 0x8B008B,
'VIO3' => 0xFFF0F5,
);
$pattern = '/COULEUR\((.*)\)/i';
///Detection couleur
if ( preg_match($pattern, $content, $matches) ) {
//FB::log($content, 'DETECTION Couleurs');
$listeCouleurs = $matches[1];
//Récupération des couleurs
$pattern = '/([a-z0-9]{3,4})/i';
if ( preg_match_all($pattern, $listeCouleurs, $matches) ) {
//FB::log($matches, 'matches');
$nbCouleurs = count($matches[1]);
for($i=0;$i<$nbCouleurs;$i++){
$this->graphCouleurs[] = $assocNomCouleurs[$matches[1][$i]];
}
}
}
//FB::log($graphCouleurs,' DETECTION : graphCouleurs');
return $this->graphCouleurs;
}
protected function graphique($content)
{
//@todo VARIABLE
global $siret, $siren, $idEntreprise; //FB::log($content, 'G');
$pattern = '/\[GRAPHIQUE id=(.*?) titre=\'(.*?)\',(.*)\]/';
if( preg_match($pattern, $content, $matches) ){
//FB::log($matches,'graphique');
$image_id = $matches[1];
$titre = $matches[2];
//Génération id/nom fichier graphique
if (($siret*1)==0 || ($siren*1)<100){
$id = 'gcomment-'.$idEntreprise.'-'.$image_id;
}else{
$id = 'gcomment-'.$siret.'-'.$image_id;
}
//Determine le type du graphique
$type = '';
$typePattern = array(
'projection' => '/(R\[.*\]#H,.*_Y#L)/',
'stacked' => '/((R\[.*\]#HS,?){1,};?){1,}/',
'histo' => '/(Rs?\[.*\]#[H|L],?){1,}/',
'radar' => '/(R\[.*\]#R,?){1,}/',
);
foreach($typePattern as $type => $pattern){
if (preg_match($pattern,$matches[3], $match_type)>0){
//FB::log($match_type, $type);
break;
}
}
//FB::log($type, 'Graphique');
switch($type){
case 'histo':
$image_file = $this->graph_histo($id, $titre, $matches[3]);
break;
case 'radar':
$image_file = $this->graph_radar($id, $titre, $matches[3]);
break;
case 'stacked':
$image_file = $this->graph_stacked($id, $titre, $matches[3]);
break;
case 'projection':
$image_file = $this->graph_histo($id, $titre, $matches[3]);
break;
default:
$image_file = 'Erreur';
break;
}
//Le fichier image existe
if( file_exists(PATH_SITE.'/cache/pages/imgcache/'.$image_file) ){
$content = str_replace(
$matches[0],
'<img src="./fichier/imgcache/'.$image_file.'" />',
$content);
}else{
$content = str_replace(
$matches[0],
'Erreur génération graphique',
$content);
}
}
return $content;
}
protected function graph_stacked($id, $titre, $strRatios)
{
//@todo VARIABLE
global $bilansInfos, $ratiosEntrep, $ratiosSecteur, $ratiosInfos, $graphCouleurs;
$file = $id.'.png';
if( cache_graph($this->pathImage.$file) ){ //@todo cache_graph
$output = $file;
} else {
$couleurs = array();
if(count($graphCouleurs)>0){
$couleurs = $graphCouleurs;
} else {
$couleurs = array(
0xaaaaff,
0x6666ff,
0x3cb371, /*medium sea green*/
0xff8080,
0x000000, /*noir*/
/*0x80ff80,*/
0xffe4c4, /*abricot*/
0xbc8f8f, /*rosy brown*/
0xc0c0c0, /*silver*/
0xfffff0, /*ivoire*/
/*0xf0ffff, /*azure*/
0xff7f50, /*coral*/
0xa0522d, /*marron*/
0xffd700, /*gold*/
0x8866ff,
0x0000cd, /*blue*/
0xee82e2, /*violet*/
);
}
//Tri des bilans
foreach ($bilansInfos as $key => $row) {
$bilansInfos[$key]['cle'] = $key;
$date[$key] = $row['dateCloture'];
$duree[$key] = $row['duree'];
$devise[$key] = $row['devise'];
$unite[$key] = $row['unite'];
$cle[$key] = $row['cle'];
}
array_multisort(
$date, SORT_DESC,
$duree, SORT_DESC,
$devise, SORT_DESC,
$unite, SORT_DESC,
$cle, SORT_DESC,
$bilansInfos);
$index = $bilansInfos[0]['cle'];
$dateCloture = substr($bilansInfos[0]['dateCloture'],0,4);
//Génération du graphique
$c = new XYChart(548, 300);
$c->addTitle($titre, 'times.ttf', 10);
$c->setPlotArea(70, 30, 200, 200);
$legendObj = $c->addLegend2(300, 50, -2, "times.ttf", 9);
$legendObj->setBackground(Transparent, Transparent);
$labels = array($dateCloture);
$c->xAxis->setLabels($labels);
$layer = $c->addBarLayer2(Stack);
$layer->setBorderColor(Transparent, softLighting(Left));
$cpt_couleur = 0;
$pattern = '/(Rs?)\[(.*)]#.{1}/';
$ratios = explode(';', $strRatios);
$div = 1;
foreach ( $ratios as $indexGroup => $groupRatios ){
$stackRatios = explode(',', $groupRatios);
$layer->addDataGroup('Group'.$indexGroup);
//Analyse pour unité correcte
foreach ( $stackRatios as $ratio ){
preg_match($pattern, $ratio, $matches);
$value = $ratiosEntrep[$index]['r'.$matches[2]];
if ( !isset($dUnite) ) {
$dUnite = $ratiosInfos['r'.$matches[2]]['unite'];
}
if ( $dUnite == 'EUR' ){
if ( $div<1000000 && abs($value)/1000000>0 ){
$dUnite = 'M EUR';
$div = 1000000;
} elseif ( $div<1000 && abs($value)/1000>0 ){
$dUnite = 'K EUR';
$div = 1000;
}
}
}
//Assignation valeur
foreach ( $stackRatios as $ratio ){
preg_match($pattern, $ratio, $matches);
$value = $ratiosEntrep[$index]['r'.$matches[2]]/$div;
//FB::log($couleurs[$cpt_couleur], 'Couleur r'.$matches[2]);
$titre = htmlspecialchars_decode(
$ratiosInfos['r'.$matches[2]]['libelle'],ENT_QUOTES);
$layer->addDataSet(array($value), $couleurs[$cpt_couleur], $titre);
$cpt_couleur++;
if($cpt_couleur>=count($couleurs)) { $cpt_couleur=0; }
}
}
if ( !isset($dUnite) ) { $dUnite = 'EUR'; }
$layer->setBarGap(0.2, 0);
$c->yAxis->setAutoScale(0.2);
$c->yAxis->setTitle($dUnite);
$c->yAxis->setWidth(2);
$c->xAxis->setTitle('Années');
$c->xAxis->setWidth(2);
$c->xAxis->setLabels($labelsX);
if ( $c->makeChart($this->pathImage.$file) === true ){
$output = $file;
} else {
$output = false;
}
}
return $output;
}
protected function graph_histo($id, $titre, $strRatios)
{
//@todo VARIABLE
global $graphCouleurs, $bilansInfos, $ratiosEntrep, $ratiosSecteur, $tabProjection, $ratiosInfos;
$file = $id.'.png';
if( cache_graph($this->pathImage.$file) ){
$output = $file;
} else {
//Gestion des couleurs
if(count($this->graphCouleurs)>0){
$couleurs = $this->graphCouleurs;
} else {
$couleurs = array(0xff8080, 0x80ff80, 0x8080ff);
}
$ratios = explode(',', $strRatios);
$data = array();
$dataInfos = array();
$nbRatio = 0;
//Détection des ratios
$patternR = '/(Rs?)\[(.*)]#(.{1})/';
$patternP = '/(.*)_Y#(.{1})/';
foreach ( $ratios as $ratio ) {
//Ratios secteur et entreprise
if( preg_match($patternR, $ratio, $matches) ){
$typeRatio = $matches[1];
$numRatio = $matches[2];
$titreRatio = $this->graph_lib($numRatio);
$unite = $ratiosInfos['r'.$numRatio]['unite'];
$div = 1;
switch ( $typeRatio ){
case 'R' :
$titreRatio.= ' - Entreprise';
$tabRatio = $ratiosEntrep;
break;
case 'Rs' :
$titreRatio.= ' - Secteur';
$tabRatio = $ratiosSecteur;
break;
}
//Informations données
$dataInfos[$nbRatio] = array(
'titre' => $titreRatio,
'style' => $matches[3],
);
//Assignation données
$xPrec = 0;
foreach ( $tabRatio as $key => $valRatio ){
$index = (int)substr($bilansInfos[$key]['dateCloture'],0,4);
($xPrec != 0) ? $diff = $xPrec-$index : $diff = 1;
if ($diff != 0){
$data[$index][$nbRatio] = $valRatio['r'.$numRatio];
if ( $unite == 'EUR' ){
if ( $div<1000000 && abs($valRatio['r'.$numRatio])/1000000>0 ){
$unite = 'M EUR';
$div = 1000000;
} elseif ( $div<1000 && abs($valRatio['r'.$numRatio])/1000>0 ){
$unite = 'K EUR';
$div = 1000;
}
}
}
$xPrec = $index;
}
$nbRatio++;
//Ratios de projection
} elseif ( preg_match($patternP, $ratio, $matches) ){
$titreRatio = $matches[1];
$dataInfos[$nbRatio] = array(
'titre' => $titreRatio.' - Projection',
'style' => $matches[2],
);
$tabRatio = $tabProjection[$titreRatio.'_Y'];
krsort($tabRatio);
foreach ( $tabRatio as $key => $valRatio ){
$index = (int)substr($key,0,4);
$data[$index][$nbRatio] = $valRatio;
}
$nbRatio++;
}
}
if ( !isset($unite) ) { $unite = 'EUR'; }
//FB::log($data, 'data');
//Graphique
if ( count($data)<=1 ){
$output = false;
} else {
ksort($data);
$labelsX = array_keys($data);
$valuesY = array();
for ( $i = 0; $i < $nbRatio ; $i++ ){
foreach ( $data as $values ){
$valuesY[$i][] = isset($values[$i]) ? (int)$values[$i]/$div : 0;
}
}
//Génération du graphique
$c = new XYChart(548, 350);
$c->addTitle($titre, 'times.ttf', 10);
$c->yAxis->setTitle($unite);
$c->yAxis->setWidth(2);
$c->xAxis->setTitle('Années');
$c->xAxis->setWidth(2);
$c->xAxis->setLabels($labelsX);
$legendObj = $c->addLegend2(25, 25, -2, "times.ttf", 9);
$legendObj->setBackground(Transparent, Transparent);
$c->setPlotArea(110, 90, 300, 200);
//Application layer pour line
$lineLayer = $c->addLineLayer2(Side);
$lineLayer->setBorderColor(Transparent, softLighting(Left));
$lineLayer->setLineWidth(2);
//Application layer pour bar
$barLayer = $c->addBarLayer2(Side);
$barLayer->setBorderColor(Transparent, softLighting(Left));
for ( $i = 0; $i < $nbRatio ; $i++ ){
//FB::log($couleurs[$i], 'Couleur');
if ( $dataInfos[$i]['style'] == 'H' ){
$barLayer->addDataSet($valuesY[$i], $couleurs[$i], $dataInfos[$i]['titre']);
} else if ( $dataInfos[$i]['style'] == 'L' ){
$dataSetObj = $lineLayer->addDataSet($valuesY[$i], $couleurs[$i], $dataInfos[$i]['titre']);
$dataSetObj->setDataSymbol(SquareSymbol, 7);
}
}
if( $c->makeChart($this->pathImage.$file) === true ){
$output = $file;
} else {
$output = false;
}
}
}
return $output;
}
protected function graph_radar($id, $titre, $strRatios)
{
$file = $id.'.png';
if( cache_graph($this->pathImage.$file) && 0 ){
$output = $file;
} else {
if(count($this->graphCouleurs)>0){
$couleurs = $this->graphCouleurs;
} else {
$couleurs = array(0x9999ff);
}
//FB::log($couleurs, 'RADAR-couleur');
$ratios = explode(',', $strRatios);
$data = array();
$pattern = '/(Rs?)\[(.*)]#(.{1})/';
foreach($ratios as $ratio){
if( preg_match($pattern, $ratio, $matches) ){
$typeRatio = $matches[1];
$numRatio = $matches[2];
$titreRatio = $this->graph_lib($numRatio);
$supPattern = '/\(.*?\)/';
$titreRatio = preg_replace($supPattern, '', $titreRatio);
$supPattern = '/[ ]/';
$titreRatio = preg_replace($supPattern, '<*br*>', $titreRatio);
$data['y'][] = array(
'titre' => $titreRatio,
'data' => $this->graph_y($typeRatio, $numRatio),
);
}
}
$data['x'] = $this->graph_x();
//Graphique
if(count($data)<=1){
$output = 0;
}else{
//Tri des données
foreach($data['y'] as $yKey => $y){
$i = 0;
foreach($y['data'] as $val){
$dataTri[$data['x'][$i]] = $val;
$i++;
}
ksort($dataTri);
$data['y'][$yKey]['data'] = array_values($dataTri);
}
sort($data['x']);
foreach($data['y'] as $val){
$num = count($val['data'])-1;
$gdata[] = $val['data'][$num];
$labels[] = $val['titre'];
}
}
//FB::log($gdata, 'DATA');
$c = new PolarChart(548, 480);
$c->setPlotArea(270, 240, 150);
$c->addAreaLayer($gdata, $couleurs[0]);
$c->angularAxis->setLabels($labels);
if( $c->makeChart($this->pathImage.$file) === true ){
$output = $file;
} else {
$output = false;
}
}
return $output;
}
protected function graph_lib($numRatio)
{
global $ratiosInfos;
return htmlspecialchars_decode(
$ratiosInfos['r'.$numRatio]['libelle'],
ENT_QUOTES);
}
protected function graphp_x($ratio)
{
global $tabProjection;
$output = array();
$tabRatio = $tabProjection[$ratio.'_Y'];
krsort($tabRatio);
foreach($tabRatio as $key => $val){
$output[] = (int)substr($key,0,4);
}
return $output;
}
protected function dateBilanAAAA($value)
{
return (int)substr($value, 0, 4);
}
protected function graph_x()
{
global $bilansInfos;
$output = array();
foreach($bilansInfos as $bilan){
$output[] = $bilan['dateCloture'];
}
return $output;
}
protected function graph_y($typeRatio, $numRatio)
{
//@todo VARIABLE
global $ratiosEntrep, $ratiosSecteur, $tabProjection;
switch ($typeRatio) {
case 'R' : $tabRatio = $ratiosEntrep; break;
case 'Rs' : $tabRatio = $ratiosSecteur; break;
case 'projection' : $tabRatio = $tabProjection; break;
}
$output = array();
if ($typeRatio != 'projection') {
foreach($tabRatio as $key => $val){
$value = $val['r'.$numRatio];
if (is_numeric($value)) {
$output[$key] = $value;
} elseif (is_string($value) && substr($value,0,1)=='>') {
$output[$key] = (int) substr($value,1);
}
}
} else {
$tabRatio = $tabRatio[$numRatio.'_Y'];
krsort($tabRatio);
foreach($tabRatio as $val){
$output[] = $val;
}
}
return $output;
}
}