dolibarr 24.0.0-beta
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-2026 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');
46if (empty($mode)) {
47 $mode = 'memberbycountry';
48}
49
50// Security check
51if ($user->socid > 0) {
52 $action = '';
53 $socid = $user->socid;
54}
55restrictedArea($user, 'adherent', '', '', 'cotisation');
56
57$year = (int) dol_print_date(dol_now('gmt'), "%Y", 'gmt');
58$startyear = $year - (getDolGlobalString('MAIN_STATS_GRAPHS_SHOW_N_YEARS') ? max(1, min(10, getDolGlobalString('MAIN_STATS_GRAPHS_SHOW_N_YEARS'))) : 2);
59$endyear = $year;
60
61// Load translation files required by the page
62$langs->loadLangs(array("companies", "members", "banks"));
63
64
65/*
66 * View
67 */
68
69$memberstatic = new Adherent($db);
70
71$arrayjs = array('https://www.google.com/jsapi');
72if (!empty($conf->dol_use_jmobile)) {
73 $arrayjs = array();
74}
75
76$title = $langs->trans("Members");
77
78$help_url = 'EN:Module_Services_En|FR:Module_Services|ES:M&oacute;dulo_Servicios|DE:Modul_Mitglieder';
79
80llxHeader('', $title, $help_url, '', 0, 0, $arrayjs, '', '', 'mod-member page-stats_geo');
81
82$param = '';
83
84$newcardbutton = '';
85$queryforbutton = array();
86$queryforbutton['mode'] = 'common';
87$newcardbutton .= dolGetButtonTitle($langs->trans('ViewList'), '', 'fa fa-bars imgforviewmode', dolBuildUrl(DOL_URL_ROOT.'/adherents/list.php', $queryforbutton), '', ((empty($mode) || $mode == 'common') ? 2 : 1), array('morecss' => 'reposition'));
88$queryforbutton['mode'] = 'kanban';
89$newcardbutton .= dolGetButtonTitle($langs->trans('ViewKanban'), '', 'fa fa-th-list imgforviewmode', dolBuildUrl(DOL_URL_ROOT.'/adherents/list.php', $queryforbutton), '', ($mode == 'kanban' ? 2 : 1), array('morecss' => 'reposition'));
90$newcardbutton .= dolGetButtonTitle($langs->trans('Statistics'), '', 'fa fa-chart-bar imgforviewmode', dol_buildpath('/adherents/stats/index.php', 1).'?objecttype=adherent@adherent'.preg_replace('/(&|\?)*(mode|groupby)=[^&]+/', '', $param), '', 2, array('morecss' => 'reposition'));
91$newcardbutton .= dolGetButtonTitleSeparator();
92$newcardbutton .= dolGetButtonTitle($langs->trans('NewMember'), '', 'fa fa-plus-circle', dolBuildUrl(DOL_URL_ROOT.'/adherents/card.php', ['action' => 'create']), '', $user->hasRight('adherent', 'creer'));
93
94print_barre_liste($title, 0, $_SERVER["PHP_SELF"], $param, '', '', '', 0, $langs->trans("Statistics"), $memberstatic->picto, 0, $newcardbutton, '', 0, 0, 0, 1);
95
96//dol_mkdir($dir);
97$data = array();
98$tab = null;
99$label = '';
100
101// Define sql
102$sql = null;
103if ($mode == 'memberbycountry') {
104 $label = $langs->trans("Country");
105 $tab = 'statscountry';
106
107 $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";
108 $sql .= " FROM ".MAIN_DB_PREFIX."adherent as d";
109 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c on d.country = c.rowid";
110 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."subscription as s ON s.fk_adherent = d.rowid";
111 $sql .= " WHERE d.entity IN (".getEntity('adherent').")";
112 $sql .= " AND d.statut <> ".Adherent::STATUS_DRAFT;
113 $sql .= " GROUP BY c.label, c.code";
114} elseif ($mode == 'memberbystate') {
115 $label = $langs->trans("Country");
116 $label2 = $langs->trans("State");
117 $tab = 'statsstate';
118
119 $data = array();
120 $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"; //
121 $sql .= " FROM ".MAIN_DB_PREFIX."adherent as d";
122 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as c on d.state_id = c.rowid";
123 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_regions as r on c.fk_region = r.code_region";
124 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co on d.country = co.rowid";
125 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."subscription as s ON s.fk_adherent = d.rowid";
126 $sql .= " WHERE d.entity IN (".getEntity('adherent').")";
127 $sql .= " AND d.statut <> ".Adherent::STATUS_DRAFT;
128 $sql .= " GROUP BY co.label, co.code, c.nom";
129 //print $sql;
130} elseif ($mode == 'memberbyregion') {
131 $label = $langs->trans("Country");
132 $label2 = $langs->trans("Region"); //département
133 $tab = 'statsregion'; //onglet
134
135 $data = array(); //tableau de donnée
136 $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";
137 $sql .= " FROM ".MAIN_DB_PREFIX."adherent as d";
138 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as c on d.state_id = c.rowid";
139 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_regions as r on c.fk_region = r.code_region";
140 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co on d.country = co.rowid";
141 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."subscription as s ON s.fk_adherent = d.rowid";
142 $sql .= " WHERE d.entity IN (".getEntity('adherent').")";
143 $sql .= " AND d.statut <> ".Adherent::STATUS_DRAFT;
144 $sql .= " GROUP BY co.label, co.code, r.nom"; //+
145} elseif ($mode == 'memberbytown') {
146 $label = $langs->trans("Country");
147 $label2 = $langs->trans("Town");
148 $tab = 'statstown';
149
150 $data = array();
151 $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";
152 $sql .= " FROM ".MAIN_DB_PREFIX."adherent as d";
153 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c on d.country = c.rowid";
154 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."subscription as s ON s.fk_adherent = d.rowid";
155 $sql .= " WHERE d.entity IN (".getEntity('adherent').")";
156 $sql .= " AND d.statut <> ".Adherent::STATUS_DRAFT;
157 $sql .= " GROUP BY c.label, c.code, d.town";
158}
159
160$langsen = new Translate('', $conf);
161$langsen->setDefaultLang('en_US');
162$langsen->load("dict");
163//print $langsen->trans("Country"."FI");exit;
164
165// Define $data array
166dol_syslog("Count member", LOG_DEBUG);
167if ($sql != null) {
168 $resql = $db->query($sql);
169} else {
170 $resql = false;
171 dol_syslog(__FILE__.":No SQL, invalid mode '$mode'", LOG_ERR);
172}
173if ($resql) {
174 $num = $db->num_rows($resql);
175 $i = 0;
176 while ($i < $num) {
177 $obj = $db->fetch_object($resql);
178 if ($mode == 'memberbycountry') {
179 $data[] = array(
180 '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>')),
181 '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>')),
182 'code' => (string) $obj->code,
183 'nb' => (int) $obj->nb,
184 'lastdate' => $db->jdate($obj->lastdate),
185 'lastsubscriptiondate' => $db->jdate($obj->lastsubscriptiondate)
186 );
187 } elseif ($mode == 'memberbyregion') { //+
188 $data[] = array(
189 '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>')),
190 '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>')),
191 'label2' => ($obj->label2 ? $obj->label2 : '<span class="opacitymedium">'.$langs->trans("Unknown").'</span>'),
192 'nb' => (int) $obj->nb,
193 'lastdate' => $db->jdate($obj->lastdate),
194 'lastsubscriptiondate' => $db->jdate($obj->lastsubscriptiondate)
195 );
196 } elseif ($mode == 'memberbystate') {
197 $data[] = array(
198 '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>')),
199 '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>')),
200 'label2' => ($obj->label2 ? $obj->label2 : '<span class="opacitymedium">'.$langs->trans("Unknown").'</span>'),
201 'nb' => (int) $obj->nb,
202 'lastdate' => $db->jdate($obj->lastdate),
203 'lastsubscriptiondate' => $db->jdate($obj->lastsubscriptiondate)
204 );
205 } elseif ($mode == 'memberbytown') {
206 $data[] = array(
207 '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>')),
208 '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>')),
209 'label2' => (string) ($obj->label2 ? $obj->label2 : '<span class="opacitymedium">'.$langs->trans("Unknown").'</span>'),
210 'nb' => (int) $obj->nb,
211 'lastdate' => $db->jdate($obj->lastdate),
212 'lastsubscriptiondate' => $db->jdate($obj->lastsubscriptiondate)
213 );
214 }
215
216 $i++;
217 }
218 $db->free($resql);
219} else {
221}
222
223
224$head = member_stats_prepare_head($memberstatic);
225
226print dol_get_fiche_head($head, (string) $tab, '', -1, '');
227
228
229// Print title
230if (!count($data)) {
231 print '<span class="opacitymedium">'.$langs->trans("NoValidatedMemberYet").'</span><br>';
232 print '<br>';
233} else {
234 if (empty($mode) || $mode == 'memberbycountry') {
235 print '<span class="opacitymedium">'.$langs->trans("MembersByCountryDesc");
236 if (getDolGlobalString("GOOGLE_SHOW_COUNTRY_GRAPH")) {
237 print $langs->trans("MembersByCountryDesc2");
238 }
239 print '</span><br>';
240 } elseif ($mode == 'memberbystate') {
241 print '<span class="opacitymedium">'.$langs->trans("MembersByStateDesc").'</span><br>';
242 } elseif ($mode == 'memberbytown') {
243 print '<span class="opacitymedium">'.$langs->trans("MembersByTownDesc").'</span><br>';
244 } elseif ($mode == 'memberbyregion') {
245 print '<span class="opacitymedium">'.$langs->trans("MembersByRegion").'</span><br>'; //+
246 }
247 print '<span class="opacitymedium">'.$langs->trans("DraftMembersAreExcluded").'</span><br>';
248 print '<br>';
249}
250
251
252// Show graphics
253if (getDolGlobalString("GOOGLE_SHOW_COUNTRY_GRAPH") && $mode == 'memberbycountry') {
254 global $theme_bordercolor, $theme_datacolor, $theme_bgcolor, $theme_bgcoloronglet;
255 $color_file = DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/theme_vars.inc.php';
256 if (is_readable($color_file)) {
257 include $color_file;
258 }
259
260 // Assume we've already included the proper headers so just call our script inline
261 // More doc: https://developers.google.com/chart/interactive/docs/gallery/geomap?hl=fr-FR
262 print "\n<script type='text/javascript'>\n";
263 print "google.load('visualization', '1', {'packages': ['geomap']});\n";
264 print "google.setOnLoadCallback(drawMap);\n";
265 print "function drawMap() {\n\tvar data = new google.visualization.DataTable();\n";
266
267 // Get the total number of rows
268 print "\tdata.addRows(".count($data).");\n";
269 print "\tdata.addColumn('string', 'Country');\n";
270 print "\tdata.addColumn('number', 'Number');\n";
271
272 // loop and dump
273 $i = 0;
274 foreach ($data as $val) {
275 $valcountry = strtoupper($val['code']); // Should be ISO-3166 code (faster)
276 // $valcountry = ucfirst($val['label_en']);
277 // if ($valcountry == 'Great Britain') {
278 // $valcountry = 'United Kingdom';
279 // } // fix case of uk (when we use labels)
280 print "\tdata.setValue(".$i.", 0, \"".$valcountry."\");\n";
281 print "\tdata.setValue(".$i.", 1, ".$val['nb'].");\n";
282 // Google's Geomap only supports up to 400 entries
283 if ($i >= 400) {
284 break;
285 }
286 $i++;
287 }
288
289 print "\tvar options = {};\n";
290 print "\toptions['dataMode'] = 'regions';\n";
291 print "\toptions['showZoomOut'] = false;\n";
292 //print "\toptions['zoomOutLabel'] = '".dol_escape_js($langs->transnoentitiesnoconv("Numbers"))."';\n";
293 print "\toptions['width'] = ".$graphwidth.";\n";
294 print "\toptions['height'] = ".$graphheight.";\n";
295 print "\toptions['colors'] = [0x".colorArrayToHex($theme_datacolor[1], 'BBBBBB').", 0x".colorArrayToHex($theme_datacolor[0], '444444')."];\n";
296 print "\tvar container = document.getElementById('".$mode."');\n";
297 print "\tvar geomap = new google.visualization.GeoMap(container);\n";
298 print "\tgeomap.draw(data, options);\n";
299 print "}\n";
300 print "</script>\n";
301
302 // print the div tag that will contain the map
303 print '<div class="center" id="'.$mode.'"></div>'."\n";
304}
305
306// Print array
307print '<div class="div-table-responsive">'; // You can use div-table-responsive-no-min if you don't need reserved height for your table
308print '<table class="liste centpercent noborder">';
309print '<tr class="liste_titre">';
310print '<th>'.$label.'</th>';
311if (isset($label2)) {
312 print '<th class="center">'.$label2.'</th>';
313}
314print '<th class="right">'.$langs->trans("NbOfMembers").' <span class="opacitymedium">('.$langs->trans("AllTime").')</span></th>';
315print '<th class="center">'.$langs->trans("LastMemberDate").'</th>';
316print '<th class="center">'.$langs->trans("LatestSubscriptionDate").'</th>';
317print '</tr>';
318
319foreach ($data as $val) {
320 print '<tr class="oddeven">';
321 print '<td>'.$val['label'].'</td>';
322 if (isset($label2)) {
323 print '<td class="center">'.$val['label2'].'</td>';
324 }
325 print '<td class="right">'.$val['nb'].'</td>';
326 print '<td class="center">'.dol_print_date($val['lastdate'], 'dayhour', 'auto', null, false, 1).'</td>';
327 print '<td class="center">'.dol_print_date($val['lastsubscriptiondate'], 'dayhour', 'auto', null, false, 1).'</td>';
328 print '</tr>';
329}
330
331print '</table>';
332print '</div>';
333
334
335print dol_get_fiche_end();
336
337// End of page
338llxFooter();
339$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:1600
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
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.
print_barre_liste($title, $page, $file, $options='', $sortfield='', $sortorder='', $morehtmlcenter='', $num=-1, $totalnboflines='', $picto='generic', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limit=-1, $selectlimitsuffix=0, $hidenavigation=0, $pagenavastextinput=0, $morehtmlrightbeforearrow='')
Print a title with navigation controls for pagination.
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.
dolBuildUrl($url, $params=[], $addtoken=false, $anchor='')
Return path of url.
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_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
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...
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.
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string
buildzip.php
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.