NxTIC/manager/module/call_log.php

267 lines
8.4 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace manager\module;
use \manager\Database;
use \manager\sessionManager;
use \manager\ResourceDispatcher;
use \manager\ManagerError;
use \manager\Repo;
class call_log{
/* DESERIALISATION D'UN JOURNAL D'APPEL
*
* @number<String> Numéro de téléphone du propriétaire du journal d'appel
*
* @return logs<Array> Retourne un tableau associatif les logs
* @return directory<Array> Retourne un tableau associatif contenant l'annuaire des contacts
*
*/
public static function unserialize($params){
extract($params);
// On formatte le numéro de téléphone
$phone_number = Database::formatNumber($phone_number);
/* [0] On récupère dans la config le chemin du fichier
=========================================================*/
/* (1) On récupère le fichier */
$uploadAuth = ResourceDispatcher::getResource('f/json/upload-auth/conf');
/* (2) Si une erreur pour le fichier de conf */
if( $uploadAuth === false )
return ManagerError::UnreachableResource;
/* (3) On récupère la config sous forme de tableau */
$uploadAuth = json_decode( $uploadAuth, true );
/* (4) Si erreur de PARSAGE */
if( !is_array($uploadAuth) )
return ManagerError::ParsingFailed;
/* (5) On construit le chemin du fichier */
$prefix = 'call_log'; $extension = 'xml';
$path = __ROOT__.$uploadAuth['root']."/$prefix/".$_SESSION['username'].".$extension";
/* [1] On récupère le dictionnaire des valeurs
=========================================================*/
/* (1) On récupère le fichier */
$dict = ResourceDispatcher::getResource('f/json/dictionary/upload/phone_storage');
/* (2) Si une erreur pour le fichier de conf */
if( $dict === false )
return ManagerError::UnreachableResource;
/* (3) On récupère la config sous forme de tableau */
$dict = json_decode( $dict, true );
/* (4) Si erreur de PARSAGE */
if( !is_array($dict) )
return ManagerError::ParsingFailed;
/* [2] Fonction pour mettre la clé associée dans la dictionnaire
=========================================================*/
function dParse($dict, $path, $value){
// {0} On met en forme la clé //
$path = explode(':', $path);
// {1} Si mauvaise taille, on quitte //
if( count($path) != 2 )
return $value;
// {2} Si l'entrée n'existe pas, on retourne la valeur //
if( !isset($dict[$path[0]][$path[1]]) )
return $value;
// {3} On charche parmi les valeurs possibles //
foreach($dict[$path[0]][$path[1]] as $k=>$v)
if( $v == $value ) // Si on trouve la valeur, on retourne la clé associée
return $k;
// {4} On retourne la valeur tel quel si on ne trouve pas //
return $value;
};
/* [3] On parse/récupère le xml
=========================================================*/
// Si le fichier n'existe pas, on quitte
if( !file_exists($path) )
return array('ModuleError' => ManagerError::UnreachableResource);
$file_content = file_get_contents($path);
if( $file_content === false )
return array('ModuleError' => ManagerError::UnreachableResource);
$xml = simplexml_load_string( $file_content );
// Si le format XML n'est pas bon, on retourne une erreur
if( $xml === false )
return array('ModuleError' => ManagerError::ParsingFailed);
/* [4] On lit chaque élément
=========================================================*/
$id = array(); // Contiendra les correspondances numéro->id
$phone_directory = array(); // Contiendra les données des contacts (par id)
$call_logs = array(); // Contiendra nos logs (appels/sms)
foreach($xml->Item as $log){
/* (1) On formatte le numéro */
$number = Database::formatNumber($log['Number']);
/* (2) On enregistre le contact dans l'annuaire s'il y est pas déjà */
if( !isset($id[$number]) ){
$id[$number] = count($id);
$cur_id = $id[$number];
$phone_directory[$cur_id] = array(
'id' => $cur_id,
'number' => $number,
'name' => (string) $log['Name'],
'call' => 0,
'sms' => 0
);
}
// On récupère l'id courant
$cur_id = $id[$number];
/* (3) Si c'est un appel, on incrémente le compteur pour ce numéro */
if( strtolower($log['Type']) == 'phone' ) $phone_directory[$cur_id]['call']++;
/* (4) Si c'est un sms, on incrémente le compteur pour ce numéro */
else $phone_directory[$cur_id]['sms']++;
/* (5) On complète le log */
$call_log = array(
'id' => $cur_id,
'direction' => dParse($dict, 'logs:direction', $log['Direction']),
'type' => dParse($dict, 'logs:type', strtoupper($log['Type'])),
'date' => strtotime($log['Date']),
'duration' => (int) $log['Duration']
);
array_push($call_logs, $call_log);
}
/* [5] On trie les contacts par nombre d'apparition d'APPEL
=========================================================*/
$tmp = $phone_directory; // Permet de ne pas efface $phone_directory
$maxId = -1; // Contiendra l'id du plus gros
$maxVal = null; // Contiendra la valeur max (total calls)
$call_sorted = array(); // Contiendra l'annuaire trié par nombre d'interraction
/* (1) Tant qu'on a pas tout trié */
while( count($call_sorted) < 10 ){
$maxId = -1;
$maxVal = null;
/* (2) On parcours toutes les entrées puor trouver le plus proche */
foreach($tmp as $cur_id=>$data)
if( $data['call'] > $maxVal || is_null($maxVal) ){
// On met à jour la valeur max
$maxVal = $data['call'];
// On met à jour l'indice
$maxId = $cur_id;
}
// Si on a rien trouvé, on arrête
if( $maxId == -1 )
break;
/* (3) On supprime le plus proche qu'on a trouvé et on l'ajoute au tableau trié */
array_push($call_sorted, $maxId);
unset($tmp[$maxId]);
}
/* [6] On trie les contacts par nombre d'apparition de SMS/MMS
=========================================================*/
$tmp = $phone_directory; // Permet de ne pas efface $phone_directory
$maxId = -1; // Contiendra le numéro du plus gros
$maxVal = null; // Contiendra la valeur max (total calls)
$sms_sorted = array(); // Contiendra l'annuaire trié par nombre d'interraction
/* (1) Tant qu'on a pas tout trié */
while( count($sms_sorted) < 10 ){
$maxId = -1;
$maxVal = null;
/* (2) On parcours toutes les entrées puor trouver le plus proche */
foreach($tmp as $cur_id=>$data)
if( $data['sms'] > $maxVal || is_null($maxVal) ){
// On met à jour la valeur max
$maxVal = $data['sms'];
// On met à jour l'indice
$maxId = $cur_id;
}
// Si on a rien trouvé, on arrête
if( $maxId == -1 )
break;
/* (3) On supprime le plus proche qu'on a trouvé et on l'ajoute au tableau trié */
array_push($sms_sorted, $maxId);
unset($tmp[$maxId]);
}
/* [7] Enregistrement du journal d'appel au format de sortie
=========================================================*/
// on cržée un id unique pour cet utilisateur
$subject_id = sessionManager::sha1($phone_number);
/* (1) On construit le contenu */
$phone_storage = array(
'logs' => $call_logs, // Le journal d'appel
'subject' => '', // sera complété lors de l'envoi du formulaire (numéro de tel)
'contacts' => array() // sera complété lors de l'envoi du formulaire
);
/* (2) On met le contenu en json */
$phone_storage = json_encode($phone_storage);
/* (3) On l'enregistre dans le fichier */
$written = file_put_contents( __ROOT__.'/tmp/phone_storage_'.$subject_id.'.json', $phone_storage);
// Si erreur d'écriture
if( $written === false )
return array( 'ModuleError' => ManagerError::ModuleError );
/* [8] Gestion du retour
=========================================================*/
return array(
'ModuleError' => ManagerError::Success,
'tmp_id' => $subject_id,
'directory' => $phone_directory,
'call' => $call_sorted,
'sms' => $sms_sorted
);
}
}
?>