odea/application/controllers/DashboardController.php
2015-01-23 14:39:25 +00:00

481 lines
16 KiB
PHP

<?php
class DashboardController extends Zend_Controller_Action
{
public function init()
{
$this->view->headLink()
->appendStylesheet('/themes/default/styles/dashboard.css', 'all');
$this->view->headScript()
->appendFile('/themes/default/scripts/dashboard.js', 'text/javascript');
}
public function indexAction()
{
$auth = Zend_Auth::getInstance();
$user = $auth->getIdentity();
$criteresM = new Application_Model_CiblageCriteres();
$sql = $criteresM->select()
->from($criteresM, array('id', 'reference', 'dateAjout'))
->where("idClient = ?", $user->idClient)
->where("login = ?", $user->username)
->order('dateAjout DESC')
->limit(5);
$rows = $criteresM->fetchAll($sql);
//
$results = array();
$comptagesM = new Application_Model_CiblageComptages();
foreach($rows->toArray() as $item)
{
$info = array(
'id' => $item['id'],
'reference' => $item['reference'],
'dateCriteres' => $item['dateAjout'],
);
//Recherche des comptages
$sql = $comptagesM->select()
->from($comptagesM, array('resultat', 'uniteInsee', "DATE_FORMAT(dateAjout, '%Y/%m/%d %H:%i:%s') as dateAjout"))
->where('idDefinition = ?', $item['id'])
->order('dateAjout DESC')->limit(1);
$comptage = $comptagesM->fetchAll($sql)->toArray();
if (count($comptage)>0){
$info['resultat'] = $comptage[0]['resultat'];
$info['uniteInsee'] = $comptage[0]['uniteInsee'];
$info['dateComptage'] = $comptage[0]['dateAjout'];
}
$results[] = $info;
}
$this->view->comptages = $results;
//
$enrichissements = new Application_Model_CiblageEnrichissementIdentifiants();
$sql = $enrichissements->select()
->setIntegrityCheck(false)
->from(
array('i' => 'enrichissement_identifiants'),
array('id', 'reference', 'fichier', 'nbLigneTotales', 'nbLigneTraites', 'error', 'dateAdded', 'dateStart', 'dateStop')
)
->join(
array('c' => 'ciblage_criteres'), 'i.idCriteres = c.id',
array('')
)
->where('c.idClient = ?', $user->idClient)
->where('c.login = ?', $user->username)
->order('i.dateAdded DESC');
$this->view->enrichissements = $enrichissements->fetchAll($sql);
}
public function ciblagesAction()
{
$request = $this->getRequest();
$page = $request->getParam('page', 1);
$offset = 20;
//Liste des ciblages par paquet de n
$auth = Zend_Auth::getInstance();
$user = $auth->getIdentity();
$criteresM = new Application_Model_CiblageCriteres();
//Compter le nombre de page
$sql = $criteresM->select()
->from($criteresM, array('nb' => 'COUNT(*)'))
->where("idClient = ?", $user->idClient)
->where("login = ?", $user->username);
$count = $criteresM->fetchRow($sql);
$nbCiblage = $count->nb;
//Récupérer les informations
$position = ($page-1)*$offset+1;
$sql = $criteresM->select()
->from($criteresM, array('id', 'reference', 'dateAjout'))
->where("idClient = ?", $user->idClient)
->where("login = ?", $user->username)
->order('dateAjout DESC')
->limitPage($position, $offset);
$rows = $criteresM->fetchAll($sql);
$results = array();
$comptagesM = new Application_Model_CiblageComptages();
foreach($rows->toArray() as $item)
{
$info = array(
'id' => $item['id'],
'reference' => $item['reference'],
'dateCriteres' => $item['dateAjout'],
);
//Recherche des comptages
$sql = $comptagesM->select()
->from($comptagesM, array('resultat', 'uniteInsee', "DATE_FORMAT(dateAjout, '%d/%m/%Y %H:%i:%s') as dateAjout"))
->where('idDefinition = ?', $item['id'])
->order('dateAjout DESC')->limit(1);
$comptage = $comptagesM->fetchAll($sql)->toArray();
if (count($comptage)>0){
$info['resultat'] = $comptage[0]['resultat'];
$info['uniteInsee'] = $comptage[0]['uniteInsee'];
$info['dateComptage'] = $comptage[0]['dateAjout'];
}
$results[] = $info;
}
$this->view->ciblages = $results;
$this->view->nbCiblage = $nbCiblage;
$this->view->page = $page;
$this->view->nbPages = ceil($nbCiblage/$offset);
}
public function ciblageAction()
{
$request = $this->getRequest();
$auth = Zend_Auth::getInstance();
$user = $auth->getIdentity();
$comptageId = $request->getParam('id');
//Lecture des paramètres du ciblage
$criteresM = new Application_Model_CiblageCriteres();
$sql = $criteresM->select()
->where('idClient = ?', $user->idClient)
->where('login = ?', $user->username)
->where('id = ?', $comptageId);
$criteres = $criteresM->fetchRow($sql)->toArray();
$infosCriteres = array();
if ($criteres != null) {
$comptageRef = $criteres['reference'];
$decodeCriteres = json_decode($criteres['criteres'], true);
$fields = new Scores_Ciblage_FieldList();
$sessionValue = new Scores_Ciblage_Session($fields);
$sessionValue->setSelectedValues($decodeCriteres, false);
//Construction affichage des critères
$infosCriteres = array();
$allFields = $fields->getItems(true);
foreach ( $allFields as $key => $params )
{
$valueSelected = $sessionValue->getSelectedValue($key);
if ( $valueSelected !== null ) {
$isMultiple = $fields->isMultiple($key);
$inValue = array();
if ( array_key_exists('in', $valueSelected) ) {
$inValue = $valueSelected['in'];
}
$exValue = array();
if ( array_key_exists('in', $valueSelected) ) {
$exValue = $valueSelected['ex'];
}
Zend_Registry::get('firebug')->info($key);
Zend_Registry::get('firebug')->info($inValue);
$inLabel = array();
if ( is_array($inValue) && count($inValue) > 0 ) {
foreach ( $inValue as $value ) {
if ( $isMultiple === true ) {
$inLabel[] = $fields->labelValueDetail($key, $value);
}
else if ( array_key_exists($value, $params['values']) ) {
$inLabel[] = $params['values'][$value];
}
}
} else {
if ( array_key_exists($inValue, $params['values']) ) {
$inLabel[] = $params['values'][$inValue];
}
}
$exLabel = array();
if ( is_array($exValue) && count($exValue)>0 ) {
foreach ( $exValue as $value ) {
if ( $isMultiple === true ) {
$inLabel[] = $fields->labelValueDetail($key, $value);
}
else if ( array_key_exists($value, $params['values']) ) {
$exLabel[] = $params['values'][$value];
}
}
} else {
if ( array_key_exists($exValue, $params['values']) ) {
$exLabel[] = $params['values'][$inValue];
}
}
$infosCriteres[$key] = array(
'label' => $params['label'],
'in' => $inLabel,
'ex' => $exLabel,
);
}
}
//Lecture des comptages
$comptageM = new Application_Model_CiblageComptages();
$sql = $comptageM->select()
->where('idDefinition = ?', $comptageId)
->order('dateAjout DESC');
$comptages = $comptageM->fetchAll($sql)->toArray();
//Lecture enrichissement existe
$enrichissementsM = new Application_Model_CiblageEnrichissementIdentifiants();
$sql = $enrichissementsM->select()
->where('idCriteres = ?', $comptageId);
$enrichissements = $enrichissementsM->fetchAll($sql);
//Affichage
$this->view->comptageId = $comptageId;
$this->view->comptageRef = $comptageRef;
$this->view->criteres = $infosCriteres;
$this->view->comptages = $comptages;
$this->view->enrichissements = $enrichissements;
}
}
public function rcomptageAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
$request = $this->getRequest();
$q = $request->getParam('q');
$auth = Zend_Auth::getInstance();
$user = $auth->getIdentity();
$criteresM = new Application_Model_CiblageCriteres();
$sql = $criteresM->select()
->from($criteresM, array('id', 'reference', "DATE_FORMAT(dateAjout, '%d/%m/%Y') as date"))
->where("idClient = ?", $user->idClient)
->where("login = ?", $user->username)
->where("reference LIKE ?", $q.'%');
$rows = $criteresM->fetchAll($sql);
if (count($rows)>0){
$separator = " , ";
foreach ($rows as $item) {
$output[] = array(
'label' => $item->reference . $separator . $item->date,
'value' => $item->reference,
'url' => $this->view->url(array('controller'=>'dashboard', 'action'=>'ciblage', 'id'=>$item->id)),
);
}
}
echo json_encode($output);
}
/**
* Liste des enrichissements
*/
public function enrichissementsAction()
{
$auth = Zend_Auth::getInstance();
$user = $auth->getIdentity();
//Criteres => Comptages (last) => enrichissement_identifiants => enrichissement_commandes
$enrichissementsM = new Application_Model_CiblageEnrichissementIdentifiants();
// Pending
$sql = $enrichissementsM->select()
->setIntegrityCheck(false)
->from(
array('i' => 'enrichissement_identifiants'),
array('id', 'reference', 'fichier', 'nbLigneTotales', 'nbLigneTraites', 'error', 'dateAdded', 'dateStart', 'dateStop')
)
->join(
array('c' => 'ciblage_comptages'), 'i.idComptage = c.id',
array('')
)
->join(
array('criteres' => 'ciblage_criteres'), 'i.idCriteres = criteres.id',
array('')
);
$sql->where('i.dateStop = ?', 0)
->where('criteres.idClient = ?', $user->idClient)
->where('criteres.login = ?', $user->username);
$encours = $enrichissementsM->fetchAll($sql);
$this->view->encours = $encours;
$sql = $enrichissementsM->select()
->setIntegrityCheck(false)
->from(
array('i' => 'enrichissement_identifiants'),
array('id', 'reference', 'fichier', 'nbLigneTotales', 'nbLigneTraites', 'error', 'dateAdded', 'dateStart', 'dateStop')
)
->join(
array('c' => 'ciblage_comptages'), 'i.idComptage = c.id',
array('')
)
->join(
array('criteres' => 'ciblage_criteres'), 'i.idCriteres = criteres.id',
array('')
);
$sql->where('criteres.idClient = ?', $user->idClient)
->where('criteres.login = ?', $user->username)
->where('i.dateStart != ?', '0000-00-00 00:00:00');
$fini = $enrichissementsM->fetchAll($sql)->toArray();
$this->view->fini = $fini;
$this->view->pathfile = $config->path->data;
}
/**
* Détail d'un enrichissment
*/
public function enrichissementAction()
{
}
/**
* Display information about user's profil
*/
public function configurationAction()
{
//User's params
$auth = Zend_Auth::getInstance();
$user = $auth->getIdentity();
if (array_key_exists('filter', $user->preferences) && count($user->preferences['filter'])>0) {
$liste = $user->preferences['filter'];
foreach ( $liste as $name => $value ){
$this->view->assign('prefFilter'.ucfirst($name), $value);
}
}
if (array_key_exists('interface', $user->preferences) && count($user->preferences['interface'])>0) {
$liste = $user->preferences['interface'];
foreach ( $liste as $name => $value ){
$this->view->assign('prefInterface'.ucfirst($name), $value);
}
}
$timestamp = strtotime( $user->dateContrat );
$dateBegin = date( 'd/m/Y', $timestamp );
$timestamp = mktime(0,0,0,substr($dateBegin,3,2)+$user->periodContrat,substr($dateBegin,0,2), substr($dateBegin,6,4));
$dateEnd = date( 'd/m/Y', $timestamp );
$this->view->assign('dateBegin', $dateBegin);
$this->view->assign('dateEnd', $dateEnd);
//Customer params have an option on the RNCS filter
$this->view->assign('filterRNCS', $user->filterRNCS);
$this->view->assign('licenceINSEE', $user->licenceINSEE);
$this->view->assign('immediatExtract', $user->immediatExtract);
$tarifText = '';
//Tarifs
if ($user->forfait!=0) {
//Forfait de
$tarifText = 'Forfait de '.$user->forfait.' euros';
} else {
$tarifText = 'Tarif unitaire à la ligne de '.$user->priceLine.' euros';
}
//Paramètres
if ( $user->limitFiles!=0 && $user->limitLines!=0 ) {
$tarifText.= ' limité à '.$user->limitFiles.' fichiers et à '.$user->limitLines.' lignes par fichier.';
} elseif ( $user->limitFiles!=0 && $user->limitLines==0 ) {
$tarifText.= ' limité à '.$user->limitFiles.' fichiers';
} elseif ( $user->limitFiles==0 && $user->limitLines!=à ) {
$tarifText.= ' limité à '.$user->limitLines.' lignes par fichier.';
}
$this->view->assign('tarifText', $tarifText);
//List of data to extract
$profilsM = new Application_Model_CiblageEnrichissementProfils();
$sql = $profilsM->select()
->where('login = ?', $user->username)
->where('idClient = ?', $user->idClient);
$result = $profilsM->fetchRow($sql);
if ($result) {
$criteres = json_decode($result['criteres'], true);
$enrichissement = new Scores_Ciblage_Extract();
$data = $enrichissement->getFields();
$profilLabels = array();
foreach ( $data as $key => $item ) {
if (in_array($key, $criteres)) {
$profilLabels[] = $item['label'];
}
}
$profil = new stdClass();
$profil->labels = $profilLabels;
$profil->reference = $result['reference'];
$this->view->assign('profil', $profil);
}
}
/**
* Set preferences
*/
public function prefAction()
{
$this->_helper->layout()->disableLayout();
$auth = Zend_Auth::getInstance();
$user = $auth->getIdentity();
//Read prefs table to load actual config
$prefsM = new Application_Model_CiblagePrefs();
$userPrefs = $prefsM->find($user->username);
if ($userPrefs) {
$data = json_decode($userPrefs->current()->json, true);
} else {
$data = array('interface'=>array(),'filter'=>array());
$prefsM->insert(array('login'=>$user->username, 'json'=>json_encode($data)));
}
$request = $this->getRequest();
$sendParam = $request->getParams();
$prefsFilter = array('rncs');
$prefsInterface = array('insee');
$filter = array();
foreach ( $prefsFilter as $item ) {
if (array_key_exists($item, $sendParam)) {
$filter = array($item => $sendParam[$item]);
}
}
if (array_key_exists('filter', $data)) {
$filter = array_merge($data['filter'], $filter);
}
$data['filter'] = $filter;
$interface = array();
foreach ( $prefsInterface as $item ) {
if (array_key_exists($item, $sendParam)) {
$interface = array($item => $sendParam[$item]);
}
}
if (array_key_exists('interface', $data)) {
$interface = array_merge($data['interface'], $interface);
}
$data['interface'] = $interface;
$newjson = json_encode($data);
$nbRow = $prefsM->update(array('json'=>$newjson), "login='".$user->username."'");
//Don't forget to save new preferences in session
$this->view->assign('nbRow', $nbRow);
}
}