˙Ř˙ŕ JFIF ` ` ˙ţxŘ
| Server IP : 109.234.164.53 / Your IP : 216.73.216.110 Web Server : Apache System : Linux cervelle.o2switch.net 4.18.0-553.32.1.lve.el8.x86_64 #1 SMP Thu Dec 19 13:14:03 UTC 2024 x86_64 User : computer3 ( 1098) PHP Version : 7.1.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /home/computer3/lachocolateriedevirginie.fr/cp-admin/ |
Upload File : |
<?php
require_once('includes/header.php');
require_once('includes/menu.php');
/********************************************/
/************ OUTILS ************************/
/********************************************/
function avis_clean_txt($txt)
{
$txt = strip_tags($txt);
$txt = html_entity_decode($txt, ENT_QUOTES, 'UTF-8');
$txt = str_replace("\r", "\n", $txt);
$txt = preg_replace("/[ \t]+/", " ", $txt);
$txt = preg_replace("/\n+/", "\n", $txt);
return trim($txt);
}
function avis_normalize($txt)
{
$txt = strtolower(strip_tags($txt));
$txt = html_entity_decode($txt, ENT_QUOTES, 'UTF-8');
$txt = preg_replace('/[^a-z0-9à âäéèêëîïôöùûüç ]/iu', ' ', $txt);
$txt = preg_replace('/\s+/', ' ', $txt);
return trim($txt);
}
function avis_initial($nom)
{
$nom = trim($nom);
if ($nom == '') return '';
$parts = preg_split('/\s+/', $nom);
$ini = '';
foreach ($parts as $p) {
if ($p != '') {
$ini .= strtoupper(substr($p, 0, 1));
}
// STOP Ă 2 lettres max
if (strlen($ini) >= 2) {
break;
}
}
return $ini;
}
function avis_relative_date_to_sql($txt)
{
$txt = strtolower(trim($txt));
if (strpos($txt, 'hier') !== false) return date('Y-m-d', strtotime('-1 day'));
if (strpos($txt, 'aujourd') !== false) return date('Y-m-d');
if (preg_match('/il y a\s+un\s+jour/i', $txt)) {
return date('Y-m-d', strtotime('-1 day'));
}
if (preg_match('/il y a\s+une\s+semaine/i', $txt)) {
return date('Y-m-d', strtotime('-1 week'));
}
if (preg_match('/il y a\s+un\s+mois/i', $txt)) {
return date('Y-m-d', strtotime('-1 month'));
}
if (preg_match('/il y a\s+un\s+an/i', $txt)) {
return date('Y-m-d', strtotime('-1 year'));
}
if (preg_match('/il y a\s+([0-9]+)\s+jour(s)?/i', $txt, $m)) {
return date('Y-m-d', strtotime('-' . (int)$m[1] . ' days'));
}
if (preg_match('/il y a\s+([0-9]+)\s+semaine(s)?/i', $txt, $m)) {
return date('Y-m-d', strtotime('-' . ((int)$m[1] * 7) . ' days'));
}
if (preg_match('/il y a\s+([0-9]+)\s+mois/i', $txt, $m)) {
return date('Y-m-d', strtotime('-' . (int)$m[1] . ' months'));
}
if (preg_match('/il y a\s+([0-9]+)\s+an(s)?/i', $txt, $m)) {
return date('Y-m-d', strtotime('-' . (int)$m[1] . ' years'));
}
return date('Y-m-d');
}
function avis_visit_date_to_sql($txt)
{
$txt = strtolower(trim($txt));
$mois = array(
'janvier' => '01',
'février' => '02',
'fevrier' => '02',
'mars' => '03',
'avril' => '04',
'mai' => '05',
'juin' => '06',
'juillet' => '07',
'août' => '08',
'aout' => '08',
'septembre' => '09',
'octobre' => '10',
'novembre' => '11',
'décembre' => '12',
'decembre' => '12'
);
if (preg_match('/visité en\s+([a-zéèêëà âîïôöûüç]+)\s+([0-9]{4})/iu', $txt, $m)) {
$mois_nom = strtolower($m[1]);
$annee = $m[2];
if (isset($mois[$mois_nom])) {
return $annee . '-' . $mois[$mois_nom] . '-01';
}
}
return '';
}
function avis_is_noise($line)
{
$l = strtolower(trim($line));
if ($l == '') return true;
if ($l == 'google') return true;
if ($l == 'avis') return true;
if ($l == 'avis de google') return true;
if ($l == 'photo') return true;
if ($l == 'photos') return true;
if ($l == 'partager') return true;
if ($l == 'répondre') return true;
if ($l == 'repondre') return true;
if ($l == 'j’aime') return true;
if ($l == 'j aime') return true;
if ($l == 'jaime') return true;
if ($l == 'plus') return true;
if ($l == 'moins') return true;
if (strpos($l, 'local guide') !== false) return true;
if (strpos($l, 'avis google') !== false) return true;
return false;
}
/********************************************/
/************ PARSE 1 AVIS ******************/
/********************************************/
function avis_parse_bloc($bloc)
{
$bloc = avis_clean_txt($bloc);
$lines = explode("\n", $bloc);
$clean = array();
foreach ($lines as $line) {
$line = trim($line);
if (avis_is_noise($line)) continue;
$clean[] = $line;
}
$nom = '';
$note = 5.0;
$date_avis = date('Y-m-d');
$date_visite = '';
$texte_parts = array();
foreach ($clean as $line) {
$l = strtolower($line);
// NOTE EXACTE GOOGLE : 1/5, 2/5, 3/5, 4/5, 4,0/5, 4.0/5, 4,5/5
if (preg_match('/([1-5])([,.][0-9])?\s*\/\s*5/i', $line, $m)) {
$note_txt = $m[1];
if (isset($m[2]) && $m[2] != '') {
$note_txt .= str_replace(',', '.', $m[2]);
}
$note = (float)$note_txt;
continue;
}
if (preg_match('/^visité en /i', $line)) {
$dvisite = avis_visit_date_to_sql($line);
if ($dvisite != '') {
$date_visite = $dvisite;
}
continue;
}
if (strpos($l, 'il y a') !== false || strpos($l, 'hier') !== false || strpos($l, 'aujourd') !== false) {
$date_avis = avis_relative_date_to_sql($line);
}
if (strpos($l, 'il y a') !== false || strpos($l, 'hier') !== false || strpos($l, 'aujourd') !== false) {
continue;
}
if ($nom == '') {
$nom = $line;
continue;
}
if (strlen($line) > 2) {
$texte_parts[] = $line;
}
}
$texte = trim(implode("\n", $texte_parts));
return array(
'nom' => $nom,
'initial' => avis_initial($nom),
'note' => $note,
'texte' => $texte,
'source' => 'Google',
'date_avis' => $date_avis,
'date_visite' => $date_visite
);
}
/********************************************/
/************ SPLIT GOOGLE ******************/
/********************************************/
function avis_split_google($raw)
{
$raw = avis_clean_txt($raw);
$lines = explode("\n", $raw);
$starts = array();
for ($i = 0; $i < count($lines); $i++) {
$line = trim($lines[$i]);
if (strcasecmp($line, 'Avis de Google') == 0 && $i > 0) {
$starts[] = $i - 1; // ligne du nom juste avant
}
}
$blocs = array();
for ($i = 0; $i < count($starts); $i++) {
$start = $starts[$i];
if (isset($starts[$i + 1])) {
$end = $starts[$i + 1];
} else {
$end = count($lines);
}
$bloc_lines = array();
for ($j = $start; $j < $end; $j++) {
$bloc_lines[] = $lines[$j];
}
$bloc = trim(implode("\n", $bloc_lines));
if ($bloc != '') {
$blocs[] = $bloc;
}
}
if (empty($blocs)) {
$blocs[] = $raw;
}
return $blocs;
}
/********************************************/
/************ DOUBLONS **********************/
/********************************************/
function avis_is_duplicate($bdd, $table, $nom, $texte)
{
if (trim($texte) == 'Avis Google sans commentaire.') {
return false;
}
$nom_n = avis_normalize($nom);
$txt_n = avis_normalize($texte);
$q = $bdd->prepare("SELECT nom, texte FROM " . $table . " WHERE source = 'Google' ORDER BY id DESC LIMIT 300");
$q->execute();
$rows = $q->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $r) {
$db_nom = avis_normalize($r['nom']);
$db_txt = avis_normalize($r['texte']);
if ($db_nom == $nom_n && $db_txt == $txt_n) {
return true;
}
similar_text($txt_n, $db_txt, $pct);
if ($pct >= 88) {
return true;
}
}
return false;
}
/********************************************/
/************ VARIABLES *********************/
/********************************************/
$raw_google = '';
$preview = array();
$message_import = '';
/********************************************/
/************ ANALYSE ***********************/
/********************************************/
if (isset($_POST['analyser_google'])) {
$raw_google = isset($_POST['raw_google']) ? $_POST['raw_google'] : '';
$blocs = avis_split_google($raw_google);
foreach ($blocs as $bloc) {
$a = avis_parse_bloc($bloc);
if ($a['nom'] == '') continue;
/* Avis sans commentaire accepté */
if (trim($a['texte']) == '') {
$a['texte'] = 'Avis Google sans commentaire.';
}
$a['doublon'] = avis_is_duplicate($bdd, $GLOBALS['db_table']['avis_g'], $a['nom'], $a['texte']) ? 1 : 0;
$preview[] = $a;
}
}
/********************************************/
/************ IMPORT SELECTION **************/
/********************************************/
if (isset($_POST['import_selected'])) {
$nb_ok = 0;
$nb_skip = 0;
$nb_doublon = 0;
if (isset($_POST['avis_select']) && is_array($_POST['avis_select'])) {
foreach ($_POST['avis_select'] as $idx) {
$idx = (int)$idx;
$nom = isset($_POST['avis_nom'][$idx]) ? trim($_POST['avis_nom'][$idx]) : '';
$texte = isset($_POST['avis_texte'][$idx]) ? trim($_POST['avis_texte'][$idx]) : '';
if ($nom == '' || $texte == '') {
$nb_skip++;
continue;
}
if (avis_is_duplicate($bdd, $GLOBALS['db_table']['avis_g'], $nom, $texte)) {
$nb_doublon++;
continue;
}
$sql = "INSERT INTO " . $GLOBALS['db_table']['avis_g'] . "
(initial, nom, note, texte, source, date_avis, date_visite)
VALUES
(:initial, :nom, :note, :texte, :source, :date_avis, :date_visite)";
$q = $bdd->prepare($sql);
$q->execute(array(
'initial' => isset($_POST['avis_initial'][$idx]) ? $_POST['avis_initial'][$idx] : avis_initial($nom),
'nom' => $nom,
'note' => isset($_POST['avis_note'][$idx]) ? (float)str_replace(',', '.', $_POST['avis_note'][$idx]) : 5.0,
'texte' => $texte,
'source' => 'Google',
'date_avis' => isset($_POST['avis_date'][$idx]) ? $_POST['avis_date'][$idx] : date('Y-m-d'),
'date_visite' => isset($_POST['avis_visite'][$idx]) && $_POST['avis_visite'][$idx] != '' ? $_POST['avis_visite'][$idx] : null
));
$nb_ok++;
}
}
echo '<script>';
echo 'window.location = "./avis_g.php";';
echo '</script>';
exit;
}
?>
<style>
.import-google-box{
background:#f7f7f7;
border:1px solid #ddd;
padding:20px;
margin-bottom:25px;
border-radius:6px;
}
.import-google-box textarea{
width:100%;
min-height:180px;
}
.preview-avis{
background:#fff;
border:1px solid #ddd;
padding:12px;
margin-bottom:10px;
border-radius:5px;
}
.preview-avis.doublon{
background:#fff3f3;
border-color:#d9534f;
}
.preview-avis-ok{
background:#f4fff4;
border-color:#5cb85c;
}
.preview-head{
display:flex;
align-items:center;
gap:10px;
flex-wrap:wrap;
}
.preview-note{
color:#f7c600;
font-size:18px;
}
.preview-tools{
margin-bottom:15px;
}
.import-report{
margin-top:10px;
color:#777;
}
.preview-head input[type="checkbox"].avis-check{
-webkit-appearance: checkbox !important;
-moz-appearance: checkbox !important;
appearance: checkbox !important;
opacity:1 !important;
visibility:visible !important;
display:inline-block !important;
width:22px !important;
height:22px !important;
min-width:22px !important;
min-height:22px !important;
margin:0 10px 0 0 !important;
background:#fff !important;
cursor:pointer !important;
}
.preview-head input[type="checkbox"].avis-check:checked{
outline:3px solid #5cb85c !important;
box-shadow:0 0 0 4px rgba(92,184,92,0.25) !important;
}
.preview-avis.selected{
background:#eaffea !important;
border-color:#5cb85c !important;
}
</style>
<div class="container">
<div style="margin-top:60px">
<strong>Mode d’emploi :</strong>
<ul>
<li>Aller sur la fiche Google de l’entreprise</li>
<li>Cliquer sur Avis</li>
<li>Descendre pour charger les avis</li>
<li>Sélectionner depuis le nom du premier client jusqu’à la fin du dernier avis</li>
<li>Coller ici puis cliquer sur Analyser</li>
</ul>
</div>
<?php echo $message_import; ?>
<div class="import-google-box">
<form method="post" action="add_avis_g.php" id="form-google-import">
<h3>Import Google intelligent</h3>
<textarea name="raw_google" id="raw_google" placeholder="Colle ici les avis Google copiés brut"><?php echo htmlspecialchars($raw_google, ENT_QUOTES, 'UTF-8'); ?></textarea>
<br><br>
<button type="button" onclick="pasteGoogleAndAnalyze()" class="btn btn-info">
Coller depuis Google + analyser
</button>
<button type="submit" name="analyser_google" class="btn btn-warning">
Analyser
</button>
<a href="avis_g.php" class="btn btn-primary">Retour</a>
</form>
</div>
<?php if (!empty($preview)) { ?>
<form method="post" action="add_avis_g.php">
<div class="import-google-box">
<h3>Prévisualisation</h3>
<div class="preview-tools">
<button type="button" class="btn" onclick="checkAllAvis(true); return false;">Tout cocher</button>
<button type="button" class="btn" onclick="checkAllAvis(false); return false;">Tout décocher</button>
<button type="button" class="btn btn-danger" onclick="uncheckDoublons(); return false;">Décocher les doublons</button>
</div>
<?php
$nb_detectes = 0;
$nb_doublons = 0;
$idx = 0;
foreach ($preview as $avis) {
$nb_detectes++;
$is_doublon = !empty($avis['doublon']) ? 1 : 0;
if ($is_doublon) $nb_doublons++;
$class = $is_doublon ? 'preview-avis doublon' : 'preview-avis preview-avis-ok';
$checked = $is_doublon ? '' : 'checked="checked"';
?>
<div class="<?php echo $class; ?>">
<div class="preview-head">
<input
type="checkbox"
name="avis_select[]"
class="avis-check"
data-doublon="<?php echo $is_doublon; ?>"
value="<?php echo $idx; ?>"
<?php echo $checked; ?>
/>
<input type="hidden" name="avis_nom[<?php echo $idx; ?>]" value="<?php echo htmlspecialchars($avis['nom'], ENT_QUOTES, 'UTF-8'); ?>">
<input type="hidden" name="avis_initial[<?php echo $idx; ?>]" value="<?php echo htmlspecialchars($avis['initial'], ENT_QUOTES, 'UTF-8'); ?>">
<input type="hidden" name="avis_note[<?php echo $idx; ?>]" value="<?php echo htmlspecialchars($avis['note'], ENT_QUOTES, 'UTF-8'); ?>">
<input type="hidden" name="avis_date[<?php echo $idx; ?>]" value="<?php echo htmlspecialchars($avis['date_avis'], ENT_QUOTES, 'UTF-8'); ?>">
<input type="hidden" name="avis_visite[<?php echo $idx; ?>]" value="<?php echo htmlspecialchars($avis['date_visite'], ENT_QUOTES, 'UTF-8'); ?>">
<input type="hidden" name="avis_texte[<?php echo $idx; ?>]" value="<?php echo htmlspecialchars($avis['texte'], ENT_QUOTES, 'UTF-8'); ?>">
<strong><?php echo htmlspecialchars($avis['nom'], ENT_QUOTES, 'UTF-8'); ?></strong>
<span class="preview-note">
<?php
$noteAff = (float)$avis['note'];
echo htmlspecialchars($noteAff, ENT_QUOTES, 'UTF-8') . '/5';
?>
</span>
<small>
avis : <?php echo htmlspecialchars($avis['date_avis'], ENT_QUOTES, 'UTF-8'); ?>
<?php if (!empty($avis['date_visite'])) { ?>
— visite : <?php echo htmlspecialchars($avis['date_visite'], ENT_QUOTES, 'UTF-8'); ?>
<?php } ?>
</small>
<?php if ($is_doublon) { ?>
<span class="label label-important">Doublon probable</span>
<?php } else { ?>
<span class="label label-success">OK</span>
<?php } ?>
</div>
<p><?php echo nl2br(htmlspecialchars($avis['texte'], ENT_QUOTES, 'UTF-8')); ?></p>
</div>
<?php
$idx++;
}
?>
<div class="import-report">
<?php echo $nb_detectes; ?> avis détecté(s), <?php echo $nb_doublons; ?> doublon(s) probable(s).
<br>
<strong><span id="nbChecked">0</span> avis sélectionné(s)</strong>
</div>
<br>
<button type="submit" name="import_selected" class="btn btn-success">
Importer les avis sélectionnés
</button>
</div>
</form>
<?php } ?>
</div>
<script>
function cleanGoogleClipboardText(text){
text = text.replace(/Local Guide/gi, '');
text = text.replace(/Partager/gi, '');
text = text.replace(/Photos?/gi, '');
text = text.replace(/J.?aime/gi, '');
text = text.replace(/Répondre/gi, '');
text = text.replace(/Repondre/gi, '');
text = text.replace(/\r/g, "\n");
text = text.replace(/[ \t]+/g, " ");
text = text.replace(/\n\s*\n\s*\n+/g, "\n");
return text.trim();
}
function pasteGoogleAndAnalyze(){
if (!navigator.clipboard) {
alert("Collage automatique bloqué. Coller manuellement puis cliquer sur Analyser.");
return;
}
navigator.clipboard.readText().then(function(text){
text = cleanGoogleClipboardText(text);
document.getElementById('raw_google').value = text;
var form = document.getElementById('form-google-import');
var input = document.createElement('input');
input.type = 'hidden';
input.name = 'analyser_google';
input.value = '1';
form.appendChild(input);
form.submit();
}).catch(function(){
alert("Accès presse-papier refusé. Coller manuellement puis cliquer sur Analyser.");
});
}
function refreshSelectedCards(){
var cbs = document.getElementsByTagName('input');
for(var i=0;i<cbs.length;i++){
if(cbs[i].type == 'checkbox' && cbs[i].name == 'avis_select[]'){
var parent = cbs[i].parentNode;
while(parent && parent.className.indexOf('preview-avis') === -1){
parent = parent.parentNode;
}
if(parent){
if(cbs[i].checked){
if(parent.className.indexOf('selected') === -1){
parent.className += ' selected';
}
} else {
parent.className = parent.className.replace(' selected','');
}
}
}
}
}
function updateCheckedCount(){
var cbs = document.getElementsByTagName('input');
var total = 0;
for(var i=0;i<cbs.length;i++){
if(cbs[i].type == 'checkbox' && cbs[i].name == 'avis_select[]'){
if(cbs[i].checked) total++;
}
}
if(document.getElementById('nbChecked')){
document.getElementById('nbChecked').innerHTML = total;
}
refreshSelectedCards();
}
window.onload = function(){
var cbs = document.getElementsByTagName('input');
for(var i=0;i<cbs.length;i++){
if(cbs[i].type == 'checkbox' && cbs[i].name == 'avis_select[]'){
cbs[i].onclick = updateCheckedCount;
}
}
updateCheckedCount();
};
function checkAllAvis(state){
var cbs = document.getElementsByTagName('input');
for(var i=0;i<cbs.length;i++){
if(cbs[i].type == 'checkbox' && cbs[i].name == 'avis_select[]'){
cbs[i].checked = state;
}
}
updateCheckedCount();
}
function uncheckDoublons(){
var cbs = document.getElementsByTagName('input');
for(var i=0;i<cbs.length;i++){
if(cbs[i].type == 'checkbox' && cbs[i].name == 'avis_select[]'){
if(cbs[i].getAttribute('data-doublon') == '1'){
cbs[i].checked = false;
}
}
}
updateCheckedCount();
}
</script>
<?php
include_once('includes/footer.php');
?>