<?php
namespace TheatreCore\Repository;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use TheatreCore\Entity\Stats;
use TheatreCore\Traits\TheatreTrait;
use App\Service\IdCookie;
/**
* StatsRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class StatsRepository extends ServiceEntityRepository
{
use TheatreTrait;
private $em;
public $__table;
public $__table_object;
public $__idtable;
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Stats::class);
$this->__table = $this->getClassMetadata()->getTableName();
$this->__table_object = $this->getClassMetadata()->name;
$this->__idtable = $this->getClassMetadata()->getSingleIdentifierFieldName();
}
// {{{ setStats()
/** Enregistrement d'une statistique
*
* Permet l'enregistrement d'une statistique dans une table
*
* @access public
* @param string $table Nom de la table de relation
* @param string $key Clé dans la tableau
* @param string $col Colonne
* @param array $array_values Valeur à enregistrer
* @param int $id Id de l'enregistrement principal
* @return bool
*/
public function setStats($infos, $source = null)
{
/* Vérification rapide des données passées */
if (empty($infos)) return;
if ($infos['type'] != 'article' && $infos['type'] != 'video') {
error_log('setStats: type incorrect: ' . $infos['type']);
return;
}
if (empty($infos['module'])) {
error_log('setStats: module non spécifié');
return;
}
$lng = strtolower($GLOBALS['lng']);
if (strlen($lng) != 2) {
error_log('setStats: code langue incorrect: ' . $lng);
return;
}
$idcookie = new IdCookie('stats');
$uniqid = $idcookie->get(1 * 60); /* 12 heures */
$md5url = md5($infos['url']);
if ($uniqid) {
$data_uniqid = $this->findBy(['remoteAddr' => $uniqid, 'idcontent' => (int)$infos['idcontent'], 'md5url' => $md5url]);
if (!empty($data_uniqid)) {
return $data_uniqid[0];
}
}
// if (!$this->debug) $source = null;
// on supprime les paramètres le cas échéant
$querypos = strpos($infos['url'], '?');
if ($querypos !== false) {
$infos['url'] = substr($infos['url'], 0, $querypos);
}
//tableau pour insertion dans la table stats
$values = array(
'type' => $infos['type'],
'module' => $infos['module'],
'url' => $infos['url'],
'md5url' => $md5url,
'idcontent' => (int)$infos['idcontent'],
'object' => $infos['object'],
'idobject' => (int)$infos['idobject'],
'video' => (int)$infos['video'],
'lng' => $GLOBALS['lng'],
'remote_addr' => $uniqid,
'source' => $source
);
//print_r($values);
// champ a mettre a jour
$this->setUpdateFields($values);
//debug only
//if ($this->debug) return $this->setData(null,true);
// on insère ou on update
$ret = false;
try {
// dump('à refaire ici');
$ret = $this->setData(null, true);
} catch (\Exception $e) {
//on accepte les erreurs ici (double insertion)
//$error = $e->getError();
}
return $ret;
}
// }}}
// {{{ logVideoStats()
/** Insère les statistiques pour une video
*
*
* @param mixed $module
* @access public
* @return void
*/
public function logVideoStats($module)
{
// deboguage: mettre à 1 le paramètre dbg dans makePlayerConf()
if (!empty($_GET['dbg'])) {
$debug_file = '/tmp/logstats.txt';
} else {
$debug_file = false;
}
if ($debug_file) {
file_put_contents($debug_file, date('Ymd His') . ' ' . print_r($_REQUEST, true), FILE_APPEND);
}
// verification de la clé, prévention flood
if ($_GET['key'] != md5(URL_THTV . date('Ymd'))) return;
$infos = array();
// parametre envoyes par le lecteur
if (isset($_GET['url'])) {
$infos['url'] = urldecode($_GET['url']);
}
if (isset($_GET['idcontent'])) {
$infos['idcontent'] = urldecode($_GET['idcontent']);
}
if (isset($_GET['object'])) {
$infos['object'] = urldecode($_GET['object']);
}
if (isset($_GET['idobject'])) {
$infos['idobject'] = urldecode($_GET['idobject']);
}
$infos['type'] = 'video';
$infos['video'] = 1;
$infos['module'] = $module;
//insérer la statistique
if (!empty($infos['object']) && !empty($infos['idobject'])) {
$res = $this->setStats($infos, __FILE__ . ':' . __LINE__);
if ($debug_file) {
file_put_contents($debug_file, date('Ymd His') . ' setStats(): ' . print_r($res, true) . "\n", FILE_APPEND);
}
}
}
// }}}
}