From f52ae23b803a5d305277ec8c2f32315cfd3b6f9e Mon Sep 17 00:00:00 2001 From: "vincent@cubedesigners.com" Date: Mon, 13 Feb 2012 13:22:17 +0000 Subject: [PATCH] --- inc/ws/Controlleur/class.ws.stats.php | 133 ++++++++++++++++++++++---- inc/ws/Controlleur/class.ws.url.php | 5 +- 2 files changed, 121 insertions(+), 17 deletions(-) diff --git a/inc/ws/Controlleur/class.ws.stats.php b/inc/ws/Controlleur/class.ws.stats.php index 9e4575a31..94dc50697 100644 --- a/inc/ws/Controlleur/class.ws.stats.php +++ b/inc/ws/Controlleur/class.ws.stats.php @@ -4,11 +4,54 @@ class wsStats { public static $datestocorrect = array(array(22, 11, 2009), array(23, 11, 2009), array(8, 12, 2009), array(9, 12, 2009)); + /** + * @var PHPExcel + */ + public static $xls; + + public static function exportXLS($bid, $annee = null, $mois = null) { + $xname = self::_getXLSFile($bid, $annee, $mois); + $fname = 'stats_' . $bid; + if (!is_null($annee)) { + $fname.='_' . $annee; + if (!is_null($mois)) { + $fname.='_' . $mois; + } + } + $fname.='.xlsx'; + + files::$mimeType['xlsx'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; + + cubeHTTP::downloadFile($xname, $fname); + } + + protected static function _getXLSFile($bid, $annee = null, $mois = null) { + $xname = '/home/stats/www/xls/' . $bid; + if (!is_null($annee)) { + $xname.='/' . $annee; + if (!is_null($mois)) { + $xname.='/' . $mois; + } + } + + $xname.='.xlsx'; + + $dir = dirname($xname); + if (!file_exists($dir)) { + mkdir($dir, 0777, true); + } + + return $xname; + } + public static function display($bid, $annee = null, $mois = null) { global $core; cubePHP::set_memory('512M'); + self::$xls = new PHPExcel(); + self::$xls->removeSheetByIndex(0); + $global = self::load_stats($bid); $exporturl = SITE_PATH . 'statsxls/' . $bid; @@ -19,6 +62,8 @@ class wsStats { } } + + $actions = array(); $actions[] = '' . $core->typo->Ajouter('Exporter au format Excel') . ''; @@ -63,24 +108,14 @@ class wsStats { $res .= self::vue_mensuelle($bid, $annee, $mois); } $res .= commonPage::bMain(); - return $res; - } - public static function exportXLS($bid, $annee = null, $mois = null) { - $xls = new PHPExcel(); - if (is_null($annee)) { - // Export global - } else if (is_null($mois)) { - // Export annee - } else { - // Export mois - } - cubeHTTP::forceDownload('stats_' . $bid . '.xls', false); - } + self::$xls->setActiveSheetIndex(0); + $writer = new PHPExcel_Writer_Excel2007(); + $writer->setPHPExcel(self::$xls); + $writer->save(self::_getXLSFile($bid, $annee, $mois)); - public static function addSheetFromTable() { - + return $res; } public static function getExtra($extra) { @@ -316,7 +351,20 @@ class wsStats { return $res; } + /** + * + * @param string $name + * @return PHPExcel_Worksheet + */ + protected static function _createSheet($name) { + $s = self::$xls->createSheet(); + $s->setTitle($name); + return $s; + } + public static function globalDatas($titre, $date_creation, $book, $stats, $afriend, $aprint, $adown, $adownp, $aextra) { + global $xls; + $colsTitles = array('', ''); $lines = array(); $class = array('liste', 'max'); @@ -343,6 +391,8 @@ class wsStats { $lines[] = array(__('Accès à la rubrique') . ' ' . $extra, $stats['extras']); } + self::setSheetFromDatas(__('Résumé'), null, $lines); + return self::table($titre, $colsTitles, $lines, $class); } @@ -467,7 +517,6 @@ class wsStats { public static function detailPeriode($book, $stats, $bid, $annee) { self::getActives($stats, $book, $aextra, $adown, $adownp, $aprint, $afriend); - $title = ''; $colsTitles = array(__('Période'), __('Lieux de visite'), __('Visiteurs uniques'), __('Visites'), __('Pages vues')); @@ -508,9 +557,42 @@ class wsStats { } } + self::setSheetFromDatas(__('Détail'), $colsTitles, $lines); + return self::table($title, $colsTitles, $lines, array('max', 'liste'), true, false, $noOdd); } + protected static function setSheetFromDatas($title, $columns, $lines) { + $s = self::_createSheet($title); + $offset = 1; + if (!is_null($columns)) { + $offset = 2; + foreach ($columns as $k => $c) { + + $s->setCellValueByColumnAndRow($k, 1, $c); + $s->getStyleByColumnAndRow($k, 1)->getFont()->setBold(true); + + if ($k == 0) { + $s->getStyleByColumnAndRow($k, 1)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); + } else { + $s->getStyleByColumnAndRow($k, 1)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); + } + } + } + foreach ($lines as $k => $l) { + foreach ($l as $c => $v) { + $s->getColumnDimensionByColumn($c)->setAutoSize(true); + $s->setCellValueByColumnAndRow($c, $k + $offset, strip_tags($v)); + + if ($c == 0) { + $s->getStyleByColumnAndRow($c, $k + $offset)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); + } else { + $s->getStyleByColumnAndRow($c, $k + $offset)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); + } + } + } + } + public static function linePeriod($stats, $bid, $afriend, $aprint, $adown, $adownp, $aextra) { $line = array(); $url = SITE_PATH . 'stats/' . $bid; @@ -552,6 +634,7 @@ class wsStats { public static function detailJour($book, $annee, $mois, $stats) { $colsTitles = array(__('Jour'), __('Lieux de visite'), __('Visiteurs uniques'), __('Visites'), __('Pages vues'), __('Liens cliqués')); $lines = array(); + $xlines = array(); foreach ($stats->days->day as $day) { $lines[] = array(strftime('%A %d', mktime(0, 0, 0, $mois, (string) $day['day'], $annee)), @@ -560,9 +643,17 @@ class wsStats { $day['visits'], $day['views'], $day['links']); + $xlines[] = array(strftime('%d', mktime(0, 0, 0, $mois, (string) $day['day'], $annee)), + $day['places'], + $day['visitors'], + $day['visits'], + $day['views'], + $day['links']); } $time_page = mktime(0, 0, 0, $mois, 15, $annee); + self::setSheetFromDatas(__('Jours'), $colsTitles, $xlines); + return self::table(__('Détail par jour') . ' - ' . strftime('%B %Y', $time_page), $colsTitles, $lines, array('liste', 'max'), true); } @@ -613,12 +704,16 @@ class wsStats { $lines[] = $line; } $time_page = mktime(0, 0, 0, $mois, 15, $annee); + + self::setSheetFromDatas(__('Pages'), $colsTitles, $lines); + return self::table(__('Détail par page') . ' - ' . strftime('%B %Y', $time_page), $colsTitles, $lines, array('liste', 'max')); } public static function detailLiens($book, $stats) { $colsTitles = array(__('URL'), __('Clics')); $lines = array(); + $xlines = array(); foreach ($stats->links->link as $link) { $uurl = (string) $link['url']; @@ -633,8 +728,10 @@ class wsStats { } $lines[] = array('' . $url . '', $nb); + $xlines[] = array($uurl, $nb); } + self::setSheetFromDatas(__('Liens'), $colsTitles, $xlines); return self::table(__('Liens les plus cliqués'), $colsTitles, $lines, array('liste', 'max')); } @@ -649,12 +746,14 @@ class wsStats { } $lines[] = array($q, $search['count']); } + self::setSheetFromDatas(__('Recherches'), $colsTitles, $lines); return self::table(__('Mots les plus recherchés'), $colsTitles, $lines, array('liste', 'max')); } public static function detailOrigine($book, $stats) { $colsTitles = array(__('Pays'), __('Visiteurs')); $lines = array(); + $xlines = array(); $i = 0; foreach ($stats->countries->country as $country) { @@ -664,8 +763,10 @@ class wsStats { $c = __('Origine inconnue'); } $lines[] = array(cubeCountry::getFlag($pays) . ' ' . ucfirst(mb_strtolower($c)), $country['visitors']); + $xlines[] = array(ucfirst(mb_strtolower($c)), $country['visitors']); $i++; } + self::setSheetFromDatas(__('Origines'), $colsTitles, $xlines); return self::table(__('Origine des visiteurs'), $colsTitles, $lines, array('liste', 'max')); } diff --git a/inc/ws/Controlleur/class.ws.url.php b/inc/ws/Controlleur/class.ws.url.php index 69d988863..8b803e7d9 100644 --- a/inc/ws/Controlleur/class.ws.url.php +++ b/inc/ws/Controlleur/class.ws.url.php @@ -846,7 +846,9 @@ html{height:100%}' . "\n"; public static function statsxls($args) { $bid = (isset($args[1])) ? $args[1] : null; - wsStats::exportXLS($bid); + $annee = (isset($args[2])) ? $args[2] : null; + $mois = (isset($args[3])) ? $args[3] : null; + wsStats::exportXLS($bid, $annee, $mois); } public static function chooseExistingBook() { @@ -1525,6 +1527,7 @@ html{height:100%}' . "\n"; $res .= commonPage::bMain(); return $res; } + } ?> \ No newline at end of file -- 2.39.5