*
* 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
* (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
* along with this program. If not, see .
*/
/**
* \file htdocs/adherents/stats/geo.php
* \ingroup member
* \brief Page with geographical statistics on members
*/
// Load Dolibarr environment
require '../../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/member.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
$graphwidth = DolGraph::getDefaultGraphSizeForStats('width', 700);
$mapratio = 0.5;
$graphheight = round($graphwidth * $mapratio);
$mode = GETPOST('mode') ?GETPOST('mode') : '';
// Security check
if ($user->socid > 0) {
$action = '';
$socid = $user->socid;
}
$result = restrictedArea($user, 'adherent', '', '', 'cotisation');
$year = dol_print_date(dol_now('gmt'), "%Y", 'gmt');
$startyear = $year - (empty($conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS) ? 2 : max(1, min(10, $conf->global->MAIN_STATS_GRAPHS_SHOW_N_YEARS)));
$endyear = $year;
// Load translation files required by the page
$langs->loadLangs(array("companies", "members", "banks"));
/*
* View
*/
$memberstatic = new Adherent($db);
$arrayjs = array('https://www.google.com/jsapi');
if (!empty($conf->dol_use_jmobile)) {
$arrayjs = array();
}
$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);
print load_fiche_titre($title, '', $memberstatic->picto);
//dol_mkdir($dir);
if ($mode) {
// Define sql
if ($mode == 'memberbycountry') {
$label = $langs->trans("Country");
$tab = 'statscountry';
$data = array();
$sql = "SELECT COUNT(DISTINCT d.rowid) as nb, COUNT(s.rowid) as nbsubscriptions, 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 <> ".Adherent::STATUS_DRAFT;
$sql .= " GROUP BY c.label, c.code";
//print $sql;
}
if ($mode == 'memberbystate') {
$label = $langs->trans("Country");
$label2 = $langs->trans("State");
$tab = 'statsstate';
$data = array();
$sql = "SELECT COUNT(DISTINCT d.rowid) as nb, COUNT(s.rowid) as nbsubscriptions, 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 <> ".Adherent::STATUS_DRAFT;
$sql .= " GROUP BY co.label, co.code, c.nom";
//print $sql;
}
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(DISTINCT d.rowid) as nb, COUNT(s.rowid) as nbsubscriptions, 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 <> ".Adherent::STATUS_DRAFT;
$sql .= " GROUP BY co.label, co.code, r.nom"; //+
//print $sql;
}
if ($mode == 'memberbytown') {
$label = $langs->trans("Country");
$label2 = $langs->trans("Town");
$tab = 'statstown';
$data = array();
$sql = "SELECT COUNT(DISTINCT d.rowid) as nb, COUNT(s.rowid) as nbsubscriptions, 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 <> ".Adherent::STATUS_DRAFT;
$sql .= " GROUP BY c.label, c.code, d.town";
//print $sql;
}
$langsen = new Translate('', $conf);
$langsen->setDefaultLang('en_US');
$langsen->load("dict");
//print $langsen->trans("Country"."FI");exit;
// Define $data array
dol_syslog("Count member", LOG_DEBUG);
$resql = $db->query($sql);
if ($resql) {
$num = $db->num_rows($resql);
$i = 0;
while ($i < $num) {
$obj = $db->fetch_object($resql);
if ($mode == 'memberbycountry') {
$data[] = array('label'=>(($obj->code && $langs->trans("Country".$obj->code) != "Country".$obj->code) ? img_picto('', DOL_URL_ROOT.'/theme/common/flags/'.strtolower($obj->code).'.png', '', 1).' '.$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").'')),
'code'=>$obj->code,
'nb'=>$obj->nb,
'lastdate'=>$db->jdate($obj->lastdate),
'lastsubscriptiondate'=>$db->jdate($obj->lastsubscriptiondate)
);
}
if ($mode == 'memberbyregion') { //+
$data[] = array(
'label'=>(($obj->code && $langs->trans("Country".$obj->code) != "Country".$obj->code) ? img_picto('', DOL_URL_ROOT.'/theme/common/flags/'.strtolower($obj->code).'.png', '', 1).' '.$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)
);
}
if ($mode == 'memberbystate') {
$data[] = array('label'=>(($obj->code && $langs->trans("Country".$obj->code) != "Country".$obj->code) ? img_picto('', DOL_URL_ROOT.'/theme/common/flags/'.strtolower($obj->code).'.png', '', 1).' '.$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)
);
}
if ($mode == 'memberbytown') {
$data[] = array('label'=>(($obj->code && $langs->trans("Country".$obj->code) != "Country".$obj->code) ? img_picto('', DOL_URL_ROOT.'/theme/common/flags/'.strtolower($obj->code).'.png', '', 1).' '.$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)
);
}
$i++;
}
$db->free($resql);
} else {
dol_print_error($db);
}
}
$head = member_stats_prepare_head($memberstatic);
print dol_get_fiche_head($head, $tab, '', -1, '');
// Print title
if ($mode && !count($data)) {
print $langs->trans("NoValidatedMemberYet").'
';
print '
';
} else {
if ($mode == 'memberbycountry') {
print ''.$langs->trans("MembersByCountryDesc").'
';
} elseif ($mode == 'memberbystate') {
print ''.$langs->trans("MembersByStateDesc").'
';
} elseif ($mode == 'memberbytown') {
print ''.$langs->trans("MembersByTownDesc").'
';
} elseif ($mode == 'memberbyregion') {
print ''.$langs->trans("MembersByRegion").'
'; //+
} else {
print ''.$langs->trans("MembersStatisticsDesc").'
';
print '
';
print ''.$langs->trans("MembersStatisticsByCountries").'
';
print '
';
print ''.$langs->trans("MembersStatisticsByState").'
';
print '
';
print ''.$langs->trans("MembersStatisticsByTown").'
';
print '
'; //+
print ''.$langs->trans("MembersStatisticsByRegion").'
'; //+
}
print '
';
}
// Show graphics
if (count($arrayjs) && $mode == 'memberbycountry') {
$color_file = DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/theme_vars.inc.php';
if (is_readable($color_file)) {
include $color_file;
}
// 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
print "\n\n";
// print the div tag that will contain the map
print '
'."\n";
}
if ($mode) {
// Print array
print ''; // You can use div-table-responsive-no-min if you dont need reserved height for your table
print '
';
print '';
print '| '.$label.' | ';
if (isset($label2)) {
print ''.$label2.' | ';
}
print ''.$langs->trans("NbOfMembers").' ('.$langs->trans("AllTime").') | ';
print ''.$langs->trans("LastMemberDate").' | ';
print ''.$langs->trans("LatestSubscriptionDate").' | ';
print '
';
foreach ($data as $val) {
$year = isset($val['year']) ? $val['year'] : '';
print '';
print '| '.$val['label'].' | ';
if (isset($label2)) {
print ''.$val['label2'].' | ';
}
print ''.$val['nb'].' | ';
print ''.dol_print_date($val['lastdate'], 'dayhour').' | ';
print ''.dol_print_date($val['lastsubscriptiondate'], 'dayhour').' | ';
print '
';
}
print '
';
print '
';
}
print dol_get_fiche_end();
// End of page
llxFooter();
$db->close();