dolibarr 23.0.3
geo.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024 Alexandre Spangaro <alexandre@inovea-conseil.com>
5 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
27// Load Dolibarr environment
28require '../../main.inc.php';
36require_once DOL_DOCUMENT_ROOT.'/core/lib/member.lib.php';
37require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
38require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
39require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
40
41$graphwidth = DolGraph::getDefaultGraphSizeForStats('width', '700');
42$mapratio = 0.5;
43$graphheight = round($graphwidth * $mapratio);
44
45$mode = GETPOST('mode');
46
47
48// Security check
49if ($user->socid > 0) {
50 $action = '';
51 $socid = $user->socid;
52}
53restrictedArea($user, 'adherent', '', '', 'cotisation');
54
55$year = (int) dol_print_date(dol_now('gmt'), "%Y", 'gmt');
56$startyear = $year - (getDolGlobalString('MAIN_STATS_GRAPHS_SHOW_N_YEARS') ? max(1, min(10, getDolGlobalString('MAIN_STATS_GRAPHS_SHOW_N_YEARS'))) : 2);
57$endyear = $year;
58
59// Load translation files required by the page
60$langs->loadLangs(array("companies", "members", "banks"));
61
62
63/*
64 * View
65 */
66
67$memberstatic = new Adherent($db);
68
69$arrayjs = array('https://www.google.com/jsapi');
70if (!empty($conf->dol_use_jmobile)) {
71 $arrayjs = array();
72}
73
74$title = $langs->trans("MembershipStatistics");
75
76$help_url = 'EN:Module_Services_En|FR:Module_Services|ES:M&oacute;dulo_Servicios|DE:Modul_Mitglieder';
77
78llxHeader('', $title, $help_url, '', 0, 0, $arrayjs, '', '', 'mod-member page-stats_geo');
79
80print load_fiche_titre($title, '', $memberstatic->picto);
81
82//dol_mkdir($dir);
83$data = array();
84$tab = null;
85$label = '';
86
87if ($mode) {
88 // Define sql
89 $sql = null;
90 if ($mode == 'memberbycountry') {
91 $label = $langs->trans("Country");
92 $tab = 'statscountry';
93
94 $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";
95 $sql .= " FROM ".MAIN_DB_PREFIX."adherent as d";
96 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c on d.country = c.rowid";
97 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."subscription as s ON s.fk_adherent = d.rowid";
98 $sql .= " WHERE d.entity IN (".getEntity('adherent').")";
99 $sql .= " AND d.statut <> ".Adherent::STATUS_DRAFT;
100 $sql .= " GROUP BY c.label, c.code";
101 //print $sql;
102 }
103
104 if ($mode == 'memberbystate') {
105 $label = $langs->trans("Country");
106 $label2 = $langs->trans("State");
107 $tab = 'statsstate';
108
109 $data = array();
110 $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"; //
111 $sql .= " FROM ".MAIN_DB_PREFIX."adherent as d";
112 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as c on d.state_id = c.rowid";
113 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_regions as r on c.fk_region = r.code_region";
114 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co on d.country = co.rowid";
115 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."subscription as s ON s.fk_adherent = d.rowid";
116 $sql .= " WHERE d.entity IN (".getEntity('adherent').")";
117 $sql .= " AND d.statut <> ".Adherent::STATUS_DRAFT;
118 $sql .= " GROUP BY co.label, co.code, c.nom";
119 //print $sql;
120 }
121 if ($mode == 'memberbyregion') { //
122 $label = $langs->trans("Country");
123 $label2 = $langs->trans("Region"); //département
124 $tab = 'statsregion'; //onglet
125
126 $data = array(); //tableau de donnée
127 $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";
128 $sql .= " FROM ".MAIN_DB_PREFIX."adherent as d";
129 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as c on d.state_id = c.rowid";
130 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_regions as r on c.fk_region = r.code_region";
131 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co on d.country = co.rowid";
132 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."subscription as s ON s.fk_adherent = d.rowid";
133 $sql .= " WHERE d.entity IN (".getEntity('adherent').")";
134 $sql .= " AND d.statut <> ".Adherent::STATUS_DRAFT;
135 $sql .= " GROUP BY co.label, co.code, r.nom"; //+
136 //print $sql;
137 }
138 if ($mode == 'memberbytown') {
139 $label = $langs->trans("Country");
140 $label2 = $langs->trans("Town");
141 $tab = 'statstown';
142
143 $data = array();
144 $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";
145 $sql .= " FROM ".MAIN_DB_PREFIX."adherent as d";
146 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c on d.country = c.rowid";
147 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."subscription as s ON s.fk_adherent = d.rowid";
148 $sql .= " WHERE d.entity IN (".getEntity('adherent').")";
149 $sql .= " AND d.statut <> ".Adherent::STATUS_DRAFT;
150 $sql .= " GROUP BY c.label, c.code, d.town";
151 //print $sql;
152 }
153
154 $langsen = new Translate('', $conf);
155 $langsen->setDefaultLang('en_US');
156 $langsen->load("dict");
157 //print $langsen->trans("Country"."FI");exit;
158
159 // Define $data array
160 dol_syslog("Count member", LOG_DEBUG);
161 if ($sql != null) {
162 $resql = $db->query($sql);
163 } else {
164 $resql = false;
165 dol_syslog(__FILE__.":No SQL, invalid mode '$mode'", LOG_ERR);
166 }
167 if ($resql) {
168 $num = $db->num_rows($resql);
169 $i = 0;
170 while ($i < $num) {
171 $obj = $db->fetch_object($resql);
172 if ($mode == 'memberbycountry') {
173 $data[] = array(
174 'label' => (string) (($obj->code && $langs->trans("Country".$obj->code) != "Country".$obj->code) ? picto_from_langcode($obj->code, 'class="saturatemedium paddingrightonly"', 1).' '.$langs->trans("Country".$obj->code) : ($obj->label ? $obj->label : '<span class="opacitymedium">'.$langs->trans("Unknown").'</span>')),
175 'label_en' => (string) (($obj->code && $langsen->transnoentitiesnoconv("Country".$obj->code) != "Country".$obj->code) ? $langsen->transnoentitiesnoconv("Country".$obj->code) : ($obj->label ? $obj->label : '<span class="opacitymedium">'.$langs->trans("Unknown").'</span>')),
176 'code' => (string) $obj->code,
177 'nb' => (int) $obj->nb,
178 'lastdate' => $db->jdate($obj->lastdate),
179 'lastsubscriptiondate' => $db->jdate($obj->lastsubscriptiondate)
180 );
181 }
182 if ($mode == 'memberbyregion') { //+
183 $data[] = array(
184 'label' => (string) (($obj->code && $langs->trans("Country".$obj->code) != "Country".$obj->code) ? picto_from_langcode($obj->code, 'class="saturatemedium paddingrightonly"', 1).' '.$langs->trans("Country".$obj->code) : ($obj->label ? $obj->label : '<span class="opacitymedium">'.$langs->trans("Unknown").'</span>')),
185 'label_en' => (string) (($obj->code && $langsen->transnoentitiesnoconv("Country".$obj->code) != "Country".$obj->code) ? $langsen->transnoentitiesnoconv("Country".$obj->code) : ($obj->label ? $obj->label : '<span class="opacitymedium">'.$langs->trans("Unknown").'</span>')),
186 'label2' => ($obj->label2 ? $obj->label2 : '<span class="opacitymedium">'.$langs->trans("Unknown").'</span>'),
187 'nb' => (int) $obj->nb,
188 'lastdate' => $db->jdate($obj->lastdate),
189 'lastsubscriptiondate' => $db->jdate($obj->lastsubscriptiondate)
190 );
191 }
192 if ($mode == 'memberbystate') {
193 $data[] = array(
194 'label' => (string) (($obj->code && $langs->trans("Country".$obj->code) != "Country".$obj->code) ? picto_from_langcode($obj->code, 'class="saturatemedium paddingrightonly"', 1).' '.$langs->trans("Country".$obj->code) : ($obj->label ? $obj->label : '<span class="opacitymedium">'.$langs->trans("Unknown").'</span>')),
195 'label_en' => (string) (($obj->code && $langsen->transnoentitiesnoconv("Country".$obj->code) != "Country".$obj->code) ? $langsen->transnoentitiesnoconv("Country".$obj->code) : ($obj->label ? $obj->label : '<span class="opacitymedium">'.$langs->trans("Unknown").'</span>')),
196 'label2' => ($obj->label2 ? $obj->label2 : '<span class="opacitymedium">'.$langs->trans("Unknown").'</span>'),
197 'nb' => (int) $obj->nb,
198 'lastdate' => $db->jdate($obj->lastdate),
199 'lastsubscriptiondate' => $db->jdate($obj->lastsubscriptiondate)
200 );
201 }
202 if ($mode == 'memberbytown') {
203 $data[] = array(
204 'label' => (string) (($obj->code && $langs->trans("Country".$obj->code) != "Country".$obj->code) ? picto_from_langcode($obj->code, 'class="saturatemedium paddingrightonly"', 1).' '.$langs->trans("Country".$obj->code) : ($obj->label ? $obj->label : '<span class="opacitymedium">'.$langs->trans("Unknown").'</span>')),
205 'label_en' => (string) (($obj->code && $langsen->transnoentitiesnoconv("Country".$obj->code) != "Country".$obj->code) ? $langsen->transnoentitiesnoconv("Country".$obj->code) : ($obj->label ? $obj->label : '<span class="opacitymedium">'.$langs->trans("Unknown").'</span>')),
206 'label2' => (string) ($obj->label2 ? $obj->label2 : '<span class="opacitymedium">'.$langs->trans("Unknown").'</span>'),
207 'nb' => (int) $obj->nb,
208 'lastdate' => $db->jdate($obj->lastdate),
209 'lastsubscriptiondate' => $db->jdate($obj->lastsubscriptiondate)
210 );
211 }
212
213 $i++;
214 }
215 $db->free($resql);
216 } else {
217 dol_print_error($db);
218 }
219}
220
221
222$head = member_stats_prepare_head($memberstatic);
223
224print dol_get_fiche_head($head, (string) $tab, '', -1, '');
225
226
227// Print title
228if ($mode && !count($data)) {
229 print '<span class="opacitymedium">'.$langs->trans("NoValidatedMemberYet").'</span><br>';
230 print '<br>';
231} else {
232 if (empty($mode) || $mode == 'memberbycountry') {
233 print '<span class="opacitymedium">'.$langs->trans("MembersByCountryDesc");
234 if (getDolGlobalString("GOOGLE_SHOW_COUNTRY_GRAPH")) {
235 print $langs->trans("MembersByCountryDesc2");
236 }
237 print '</span><br>';
238 } elseif ($mode == 'memberbystate') {
239 print '<span class="opacitymedium">'.$langs->trans("MembersByStateDesc").'</span><br>';
240 } elseif ($mode == 'memberbytown') {
241 print '<span class="opacitymedium">'.$langs->trans("MembersByTownDesc").'</span><br>';
242 } elseif ($mode == 'memberbyregion') {
243 print '<span class="opacitymedium">'.$langs->trans("MembersByRegion").'</span><br>'; //+
244 }
245 print '<br>';
246}
247
248
249// Show graphics
250if (getDolGlobalString("GOOGLE_SHOW_COUNTRY_GRAPH") && $mode == 'memberbycountry') {
251 global $theme_bordercolor, $theme_datacolor, $theme_bgcolor, $theme_bgcoloronglet;
252 $color_file = DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/theme_vars.inc.php';
253 if (is_readable($color_file)) {
254 include $color_file;
255 }
256
257 // Assume we've already included the proper headers so just call our script inline
258 // More doc: https://developers.google.com/chart/interactive/docs/gallery/geomap?hl=fr-FR
259 print "\n<script type='text/javascript'>\n";
260 print "google.load('visualization', '1', {'packages': ['geomap']});\n";
261 print "google.setOnLoadCallback(drawMap);\n";
262 print "function drawMap() {\n\tvar data = new google.visualization.DataTable();\n";
263
264 // Get the total number of rows
265 print "\tdata.addRows(".count($data).");\n";
266 print "\tdata.addColumn('string', 'Country');\n";
267 print "\tdata.addColumn('number', 'Number');\n";
268
269 // loop and dump
270 $i = 0;
271 foreach ($data as $val) {
272 $valcountry = strtoupper($val['code']); // Should be ISO-3166 code (faster)
273 // $valcountry = ucfirst($val['label_en']);
274 // if ($valcountry == 'Great Britain') {
275 // $valcountry = 'United Kingdom';
276 // } // fix case of uk (when we use labels)
277 print "\tdata.setValue(".$i.", 0, \"".$valcountry."\");\n";
278 print "\tdata.setValue(".$i.", 1, ".$val['nb'].");\n";
279 // Google's Geomap only supports up to 400 entries
280 if ($i >= 400) {
281 break;
282 }
283 $i++;
284 }
285
286 print "\tvar options = {};\n";
287 print "\toptions['dataMode'] = 'regions';\n";
288 print "\toptions['showZoomOut'] = false;\n";
289 //print "\toptions['zoomOutLabel'] = '".dol_escape_js($langs->transnoentitiesnoconv("Numbers"))."';\n";
290 print "\toptions['width'] = ".$graphwidth.";\n";
291 print "\toptions['height'] = ".$graphheight.";\n";
292 print "\toptions['colors'] = [0x".colorArrayToHex($theme_datacolor[1], 'BBBBBB').", 0x".colorArrayToHex($theme_datacolor[0], '444444')."];\n";
293 print "\tvar container = document.getElementById('".$mode."');\n";
294 print "\tvar geomap = new google.visualization.GeoMap(container);\n";
295 print "\tgeomap.draw(data, options);\n";
296 print "}\n";
297 print "</script>\n";
298
299 // print the div tag that will contain the map
300 print '<div class="center" id="'.$mode.'"></div>'."\n";
301}
302
303if ($mode) {
304 // Print array
305 print '<div class="div-table-responsive">'; // You can use div-table-responsive-no-min if you don't need reserved height for your table
306 print '<table class="liste centpercent noborder">';
307 print '<tr class="liste_titre">';
308 print '<th>'.$label.'</th>';
309 if (isset($label2)) {
310 print '<th class="center">'.$label2.'</th>';
311 }
312 print '<th class="right">'.$langs->trans("NbOfMembers").' <span class="opacitymedium">('.$langs->trans("AllTime").')</span></th>';
313 print '<th class="center">'.$langs->trans("LastMemberDate").'</th>';
314 print '<th class="center">'.$langs->trans("LatestSubscriptionDate").'</th>';
315 print '</tr>';
316
317 foreach ($data as $val) {
318 print '<tr class="oddeven">';
319 print '<td>'.$val['label'].'</td>';
320 if (isset($label2)) {
321 print '<td class="center">'.$val['label2'].'</td>';
322 }
323 print '<td class="right">'.$val['nb'].'</td>';
324 print '<td class="center">'.dol_print_date($val['lastdate'], 'dayhour', 'auto', null, false, 1).'</td>';
325 print '<td class="center">'.dol_print_date($val['lastsubscriptiondate'], 'dayhour', 'auto', null, false, 1).'</td>';
326 print '</tr>';
327 }
328
329 print '</table>';
330 print '</div>';
331}
332
333
334print dol_get_fiche_end();
335
336// End of page
337llxFooter();
338$db->close();
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
Class to manage members of a foundation.
static getDefaultGraphSizeForStats($direction, $defaultsize='')
getDefaultGraphSizeForStats
Class to manage translations.
$theme_datacolor
Definition index.php:1597
colorArrayToHex($arraycolor, $colorifnotfound='888888')
Convert an array with RGB value into hex RGB value.
dol_now($mode='gmt')
Return date for now.
picto_from_langcode($codelang, $moreatt='', $notitlealt=0)
Return img flag of country for a language code or country code.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
member_stats_prepare_head($object)
Return array head with list of tabs to view object stats information.
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.