dolibarr/htdocs/adherents/stats/geo.php

305 lines
14 KiB
PHP
Raw Normal View History

2016-02-08 16:51:29 +01:00
<?php
2011-08-08 18:00:16 +02:00
/* Copyright (c) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
2011-06-21 14:39:09 +02:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
2011-06-21 14:39:09 +02:00
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2011-06-21 14:39:09 +02:00
*/
/**
2011-08-08 18:00:16 +02:00
* \file htdocs/adherents/stats/geo.php
2011-06-21 14:39:09 +02:00
* \ingroup member
2011-08-08 18:00:16 +02:00
* \brief Page with geographical statistics on members
*/
2011-06-21 14:39:09 +02:00
require '../../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/member.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
2013-06-05 16:24:32 +02:00
require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
2011-06-21 14:39:09 +02:00
$graphwidth = DolGraph::getDefaultGraphSizeForStats('width', 700);
2011-06-21 14:39:09 +02:00
$mapratio = 0.5;
$graphheight = round($graphwidth * $mapratio);
$mode = GETPOST('mode') ?GETPOST('mode') : '';
2011-06-21 14:39:09 +02:00
// Security check
2020-05-21 09:12:18 +02:00
if ($user->socid > 0) {
2011-08-08 18:00:16 +02:00
$action = '';
$socid = $user->socid;
2011-06-21 14:39:09 +02:00
}
$result = restrictedArea($user, 'adherent', '', '', 'cotisation');
2011-06-21 14:39:09 +02:00
$year = strftime("%Y", time());
$startyear = $year - 2;
$endyear = $year;
2011-06-21 14:39:09 +02:00
2018-09-07 10:40:36 +02:00
// Load translation files required by the page
$langs->loadLangs(array("companies", "members"));
2011-06-21 14:39:09 +02:00
/*
* View
*/
$arrayjs = array('https://www.google.com/jsapi');
if (!empty($conf->dol_use_jmobile)) $arrayjs = array();
2011-06-21 14:39:09 +02:00
$title = $langs->trans("Statistics");
if ($mode == 'memberbycountry') $title = $langs->trans("MembersStatisticsByCountries");
if ($mode == 'memberbystate') $title = $langs->trans("MembersStatisticsByState");
if ($mode == 'memberbytown') $title = $langs->trans("MembersStatisticsByTown");
if ($mode == 'memberbyregion') $title = $langs->trans("MembersStatisticsByRegion");
llxHeader('', $title, '', '', 0, 0, $arrayjs);
2011-06-21 14:39:09 +02:00
2020-04-12 18:41:42 +02:00
print load_fiche_titre($title, '', 'object_group');
2011-06-21 14:39:09 +02:00
dol_mkdir($dir);
2011-06-21 14:39:09 +02:00
2020-05-21 09:12:18 +02:00
if ($mode) {
// Define sql
2020-05-21 09:12:18 +02:00
if ($mode == 'memberbycountry') {
$label = $langs->trans("Country");
$tab = 'statscountry';
2011-06-21 14:39:09 +02:00
2011-08-08 18:00:16 +02:00
$data = array();
$sql .= "SELECT COUNT(d.rowid) as nb, MAX(d.datevalid) as lastdate, MAX(s.dateadh) as lastsubscriptiondate, c.code, c.label";
$sql .= " FROM ".MAIN_DB_PREFIX."adherent as d";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c on d.country = c.rowid";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."subscription as s ON s.fk_adherent = d.rowid";
$sql .= " WHERE d.entity IN (".getEntity('adherent').")";
$sql .= " AND d.statut = 1";
$sql .= " GROUP BY c.label, c.code";
2011-08-08 18:00:16 +02:00
//print $sql;
}
2020-05-21 09:12:18 +02:00
if ($mode == 'memberbystate') {
$label = $langs->trans("Country");
$label2 = $langs->trans("State");
$tab = 'statsstate';
2011-06-21 14:39:09 +02:00
2011-08-08 18:00:16 +02:00
$data = array();
$sql .= "SELECT COUNT(d.rowid) as nb, MAX(d.datevalid) as lastdate, MAX(s.dateadh) as lastsubscriptiondate, co.code, co.label, c.nom as label2"; //
$sql .= " FROM ".MAIN_DB_PREFIX."adherent as d";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as c on d.state_id = c.rowid";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_regions as r on c.fk_region = r.code_region";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co on d.country = co.rowid";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."subscription as s ON s.fk_adherent = d.rowid";
$sql .= " WHERE d.entity IN (".getEntity('adherent').")";
$sql .= " AND d.statut = 1";
$sql .= " GROUP BY co.label, co.code, c.nom";
2011-08-08 18:00:16 +02:00
//print $sql;
}
2020-05-21 09:12:18 +02:00
if ($mode == 'memberbyregion') { //
$label = $langs->trans("Country");
$label2 = $langs->trans("Region"); //département
$tab = 'statsregion'; //onglet
$data = array(); //tableau de donnée
$sql .= "SELECT COUNT(d.rowid) as nb, MAX(d.datevalid) as lastdate, MAX(s.dateadh) as lastsubscriptiondate, co.code, co.label, r.nom as label2";
$sql .= " FROM ".MAIN_DB_PREFIX."adherent as d";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as c on d.state_id = c.rowid";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_regions as r on c.fk_region = r.code_region";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co on d.country = co.rowid";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."subscription as s ON s.fk_adherent = d.rowid";
$sql .= " WHERE d.entity IN (".getEntity('adherent').")";
$sql .= " AND d.statut = 1";
$sql .= " GROUP BY co.label, co.code, r.nom"; //+
//print $sql;
}
2020-05-21 09:12:18 +02:00
if ($mode == 'memberbytown') {
$label = $langs->trans("Country");
$label2 = $langs->trans("Town");
$tab = 'statstown';
2011-06-21 14:39:09 +02:00
$data = array();
$sql .= "SELECT COUNT(d.rowid) as nb, MAX(d.datevalid) as lastdate, MAX(s.dateadh) as lastsubscriptiondate, c.code, c.label, d.town as label2";
$sql .= " FROM ".MAIN_DB_PREFIX."adherent as d";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c on d.country = c.rowid";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."subscription as s ON s.fk_adherent = d.rowid";
$sql .= " WHERE d.entity IN (".getEntity('adherent').")";
$sql .= " AND d.statut = 1";
$sql .= " GROUP BY c.label, c.code, d.town";
2011-06-21 14:39:09 +02:00
//print $sql;
}
$langsen = new Translate('', $conf);
2011-06-21 14:39:09 +02:00
$langsen->setDefaultLang('en_US');
$langsen->load("dict");
//print $langsen->trans("Country"."FI");exit;
2011-08-08 18:00:16 +02:00
// Define $data array
2014-06-12 11:31:53 +02:00
dol_syslog("Count member", LOG_DEBUG);
$resql = $db->query($sql);
2020-05-21 09:12:18 +02:00
if ($resql) {
$num = $db->num_rows($resql);
$i = 0;
2020-05-21 09:12:18 +02:00
while ($i < $num) {
$obj = $db->fetch_object($resql);
2020-05-21 09:12:18 +02:00
if ($mode == 'memberbycountry') {
$data[] = array('label'=>(($obj->code && $langs->trans("Country".$obj->code) != "Country".$obj->code) ? $langs->trans("Country".$obj->code) : ($obj->label ? $obj->label : $langs->trans("Unknown"))),
'label_en'=>(($obj->code && $langsen->transnoentitiesnoconv("Country".$obj->code) != "Country".$obj->code) ? $langsen->transnoentitiesnoconv("Country".$obj->code) : ($obj->label ? $obj->label : $langs->trans("Unknown"))),
2011-06-21 14:39:09 +02:00
'code'=>$obj->code,
'nb'=>$obj->nb,
'lastdate'=>$db->jdate($obj->lastdate),
'lastsubscriptiondate'=>$db->jdate($obj->lastsubscriptiondate)
2011-08-08 18:00:16 +02:00
);
}
2020-05-21 09:12:18 +02:00
if ($mode == 'memberbyregion') { //+
$data[] = array(
'label'=>(($obj->code && $langs->trans("Country".$obj->code) != "Country".$obj->code) ? $langs->trans("Country".$obj->code) : ($obj->label ? $obj->label : $langs->trans("Unknown"))),
'label_en'=>(($obj->code && $langsen->transnoentitiesnoconv("Country".$obj->code) != "Country".$obj->code) ? $langsen->transnoentitiesnoconv("Country".$obj->code) : ($obj->label ? $obj->label : $langs->trans("Unknown"))),
'label2'=>($obj->label2 ? $obj->label2 : $langs->trans("Unknown")),
'nb'=>$obj->nb,
'lastdate'=>$db->jdate($obj->lastdate),
'lastsubscriptiondate'=>$db->jdate($obj->lastsubscriptiondate)
);
}
2020-05-21 09:12:18 +02:00
if ($mode == 'memberbystate') {
$data[] = array('label'=>(($obj->code && $langs->trans("Country".$obj->code) != "Country".$obj->code) ? $langs->trans("Country".$obj->code) : ($obj->label ? $obj->label : $langs->trans("Unknown"))),
'label_en'=>(($obj->code && $langsen->transnoentitiesnoconv("Country".$obj->code) != "Country".$obj->code) ? $langsen->transnoentitiesnoconv("Country".$obj->code) : ($obj->label ? $obj->label : $langs->trans("Unknown"))),
'label2'=>($obj->label2 ? $obj->label2 : $langs->trans("Unknown")),
2011-06-21 14:39:09 +02:00
'nb'=>$obj->nb,
'lastdate'=>$db->jdate($obj->lastdate),
'lastsubscriptiondate'=>$db->jdate($obj->lastsubscriptiondate)
2011-08-08 18:00:16 +02:00
);
}
2020-05-21 09:12:18 +02:00
if ($mode == 'memberbytown') {
$data[] = array('label'=>(($obj->code && $langs->trans("Country".$obj->code) != "Country".$obj->code) ? $langs->trans("Country".$obj->code) : ($obj->label ? $obj->label : $langs->trans("Unknown"))),
'label_en'=>(($obj->code && $langsen->transnoentitiesnoconv("Country".$obj->code) != "Country".$obj->code) ? $langsen->transnoentitiesnoconv("Country".$obj->code) : ($obj->label ? $obj->label : $langs->trans("Unknown"))),
'label2'=>($obj->label2 ? $obj->label2 : $langs->trans("Unknown")),
2011-06-21 14:39:09 +02:00
'nb'=>$obj->nb,
'lastdate'=>$db->jdate($obj->lastdate),
'lastsubscriptiondate'=>$db->jdate($obj->lastsubscriptiondate)
2011-06-21 14:39:09 +02:00
);
}
2011-08-08 18:00:16 +02:00
$i++;
}
$db->free($resql);
2020-05-21 00:47:16 +02:00
} else {
2011-08-08 18:00:16 +02:00
dol_print_error($db);
}
2011-06-21 14:39:09 +02:00
}
$head = member_stats_prepare_head($adh);
2017-07-04 13:05:02 +02:00
dol_fiche_head($head, $tab, $langs->trans("Statistics"), -1, 'user');
2011-06-21 14:39:09 +02:00
// Print title
2020-05-21 09:12:18 +02:00
if ($mode && !count($data)) {
2011-08-08 18:00:16 +02:00
print $langs->trans("NoValidatedMemberYet").'<br>';
print '<br>';
2020-05-21 00:47:16 +02:00
} else {
2011-08-08 18:00:16 +02:00
if ($mode == 'memberbycountry') print $langs->trans("MembersByCountryDesc").'<br>';
2019-01-27 10:49:34 +01:00
elseif ($mode == 'memberbystate') print $langs->trans("MembersByStateDesc").'<br>';
elseif ($mode == 'memberbytown') print $langs->trans("MembersByTownDesc").'<br>';
elseif ($mode == 'memberbyregion') print $langs->trans("MembersByRegion").'<br>'; //+
2020-05-21 00:47:16 +02:00
else {
2011-08-08 18:00:16 +02:00
print $langs->trans("MembersStatisticsDesc").'<br>';
print '<br>';
print '<a href="'.$_SERVER["PHP_SELF"].'?mode=memberbycountry">'.$langs->trans("MembersStatisticsByCountries").'</a><br>';
print '<br>';
print '<a href="'.$_SERVER["PHP_SELF"].'?mode=memberbystate">'.$langs->trans("MembersStatisticsByState").'</a><br>';
2011-06-21 14:39:09 +02:00
print '<br>';
print '<a href="'.$_SERVER["PHP_SELF"].'?mode=memberbytown">'.$langs->trans("MembersStatisticsByTown").'</a><br>';
print '<br>'; //+
print '<a href="'.$_SERVER["PHP_SELF"].'?mode=memberbyregion">'.$langs->trans("MembersStatisticsByRegion").'</a><br>'; //+
2011-08-08 18:00:16 +02:00
}
print '<br>';
2011-06-21 14:39:09 +02:00
}
// Show graphics
2020-05-21 09:12:18 +02:00
if (count($arrayjs) && $mode == 'memberbycountry') {
$color_file = DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/theme_vars.inc.php';
if (is_readable($color_file)) include_once $color_file;
2011-08-08 18:00:16 +02:00
// Assume we've already included the proper headers so just call our script inline
// More doc: https://developers.google.com/chart/interactive/docs/gallery/geomap?hl=fr-FR
2011-08-08 18:00:16 +02:00
print "\n<script type='text/javascript'>\n";
print "google.load('visualization', '1', {'packages': ['geomap']});\n";
print "google.setOnLoadCallback(drawMap);\n";
print "function drawMap() {\n\tvar data = new google.visualization.DataTable();\n";
// Get the total number of rows
print "\tdata.addRows(".count($data).");\n";
2011-08-08 18:00:16 +02:00
print "\tdata.addColumn('string', 'Country');\n";
print "\tdata.addColumn('number', 'Number');\n";
// loop and dump
$i = 0;
2020-05-21 09:12:18 +02:00
foreach ($data as $val) {
$valcountry = strtoupper($val['code']); // Should be ISO-3166 code (faster)
//$valcountry=ucfirst($val['label_en']);
if ($valcountry == 'Great Britain') { $valcountry = 'United Kingdom'; } // fix case of uk (when we use labels)
2011-08-08 18:00:16 +02:00
print "\tdata.setValue(".$i.", 0, \"".$valcountry."\");\n";
print "\tdata.setValue(".$i.", 1, ".$val['nb'].");\n";
// Google's Geomap only supports up to 400 entries
if ($i >= 400) { break; }
2011-08-08 18:00:16 +02:00
$i++;
}
print "\tvar options = {};\n";
2011-06-21 14:39:09 +02:00
print "\toptions['dataMode'] = 'regions';\n";
print "\toptions['showZoomOut'] = false;\n";
//print "\toptions['zoomOutLabel'] = '".dol_escape_js($langs->transnoentitiesnoconv("Numbers"))."';\n";
2011-08-08 18:00:16 +02:00
print "\toptions['width'] = ".$graphwidth.";\n";
print "\toptions['height'] = ".$graphheight.";\n";
print "\toptions['colors'] = [0x".colorArrayToHex($theme_datacolor[1], 'BBBBBB').", 0x".colorArrayToHex($theme_datacolor[0], '444444')."];\n";
2011-08-08 18:00:16 +02:00
print "\tvar container = document.getElementById('".$mode."');\n";
print "\tvar geomap = new google.visualization.GeoMap(container);\n";
print "\tgeomap.draw(data, options);\n";
print "};\n";
print "</script>\n";
// print the div tag that will contain the map
2019-03-19 13:53:04 +01:00
print '<div class="center" id="'.$mode.'"></div>'."\n";
2011-08-08 18:00:16 +02:00
print '<br>';
2011-06-21 14:39:09 +02:00
}
2020-05-21 09:12:18 +02:00
if ($mode) {
// Print array / Affiche le tableau
2019-11-05 21:24:41 +01:00
print '<table class="liste centpercent">';
2011-08-08 18:00:16 +02:00
print '<tr class="liste_titre">';
2016-02-08 16:51:29 +01:00
print '<td>'.$label.'</td>';
2019-03-19 13:53:04 +01:00
if ($label2) print '<td class="center">'.$label2.'</td>';
2019-01-21 22:02:11 +01:00
print '<td class="right">'.$langs->trans("NbOfMembers").'</td>';
2019-03-19 13:53:04 +01:00
print '<td class="center">'.$langs->trans("LastMemberDate").'</td>';
print '<td class="center">'.$langs->trans("LatestSubscriptionDate").'</td>';
2011-08-08 18:00:16 +02:00
print '</tr>';
2020-05-21 09:12:18 +02:00
foreach ($data as $val) {
2011-08-08 18:00:16 +02:00
$year = $val['year'];
2017-04-12 10:28:05 +02:00
print '<tr class="oddeven">';
2016-02-08 16:51:29 +01:00
print '<td>'.$val['label'].'</td>';
2019-03-19 13:53:04 +01:00
if ($label2) print '<td class="center">'.$val['label2'].'</td>';
2019-01-21 22:02:11 +01:00
print '<td class="right">'.$val['nb'].'</td>';
2019-03-19 13:53:04 +01:00
print '<td class="center">'.dol_print_date($val['lastdate'], 'dayhour').'</td>';
print '<td class="center">'.dol_print_date($val['lastsubscriptiondate'], 'dayhour').'</td>';
2011-08-08 18:00:16 +02:00
print '</tr>';
}
2011-06-21 14:39:09 +02:00
2011-08-08 18:00:16 +02:00
print '</table>';
2011-06-21 14:39:09 +02:00
}
dol_fiche_end();
2018-07-28 14:29:28 +02:00
// End of page
llxFooter();
$db->close();