dolibarr 21.0.0-alpha
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 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024 Alexandre Spangaro <alexandre@inovea-conseil.com>
5 * Copyright (C) 2024 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';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/member.lib.php';
30require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
31require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
32require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
33
34$graphwidth = DolGraph::getDefaultGraphSizeForStats('width', '700');
35$mapratio = 0.5;
36$graphheight = round($graphwidth * $mapratio);
37
38$mode = GETPOST('mode') ? GETPOST('mode') : '';
39
40
41// Security check
42if ($user->socid > 0) {
43 $action = '';
44 $socid = $user->socid;
45}
46$result = restrictedArea($user, 'adherent', '', '', 'cotisation');
47
48$year = (int) dol_print_date(dol_now('gmt'), "%Y", 'gmt');
49$startyear = $year - (!getDolGlobalString('MAIN_STATS_GRAPHS_SHOW_N_YEARS') ? 2 : max(1, min(10, getDolGlobalString('MAIN_STATS_GRAPHS_SHOW_N_YEARS'))));
50$endyear = $year;
51
52// Load translation files required by the page
53$langs->loadLangs(array("companies", "members", "banks"));
54
55
56/*
57 * View
58 */
59
60$memberstatic = new Adherent($db);
61
62$arrayjs = array('https://www.google.com/jsapi');
63if (!empty($conf->dol_use_jmobile)) {
64 $arrayjs = array();
65}
66
67$title = $langs->trans("Statistics");
68if ($mode == 'memberbycountry') {
69 $title = $langs->trans("MembersStatisticsByCountries");
70}
71if ($mode == 'memberbystate') {
72 $title = $langs->trans("MembersStatisticsByState");
73}
74if ($mode == 'memberbytown') {
75 $title = $langs->trans("MembersStatisticsByTown");
76}
77if ($mode == 'memberbyregion') {
78 $title = $langs->trans("MembersStatisticsByRegion");
79}
80
81$help_url = 'EN:Module_Services_En|FR:Module_Services|ES:M&oacute;dulo_Servicios|DE:Modul_Mitglieder';
82
83llxHeader('', $title, $help_url, '', 0, 0, $arrayjs, '', '', 'mod-member page-stats_geo');
84
85print load_fiche_titre($title, '', $memberstatic->picto);
86
87//dol_mkdir($dir);
88$data = array();
89$tab = null;
90
91if ($mode) {
92 // Define sql
93 $sql = null;
94 if ($mode == 'memberbycountry') {
95 $label = $langs->trans("Country");
96 $tab = 'statscountry';
97
98 $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";
99 $sql .= " FROM ".MAIN_DB_PREFIX."adherent as d";
100 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c on d.country = c.rowid";
101 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."subscription as s ON s.fk_adherent = d.rowid";
102 $sql .= " WHERE d.entity IN (".getEntity('adherent').")";
103 $sql .= " AND d.statut <> ".Adherent::STATUS_DRAFT;
104 $sql .= " GROUP BY c.label, c.code";
105 //print $sql;
106 }
107
108 if ($mode == 'memberbystate') {
109 $label = $langs->trans("Country");
110 $label2 = $langs->trans("State");
111 $tab = 'statsstate';
112
113 $data = array();
114 $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"; //
115 $sql .= " FROM ".MAIN_DB_PREFIX."adherent as d";
116 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as c on d.state_id = c.rowid";
117 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_regions as r on c.fk_region = r.code_region";
118 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co on d.country = co.rowid";
119 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."subscription as s ON s.fk_adherent = d.rowid";
120 $sql .= " WHERE d.entity IN (".getEntity('adherent').")";
121 $sql .= " AND d.statut <> ".Adherent::STATUS_DRAFT;
122 $sql .= " GROUP BY co.label, co.code, c.nom";
123 //print $sql;
124 }
125 if ($mode == 'memberbyregion') { //
126 $label = $langs->trans("Country");
127 $label2 = $langs->trans("Region"); //département
128 $tab = 'statsregion'; //onglet
129
130 $data = array(); //tableau de donnée
131 $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";
132 $sql .= " FROM ".MAIN_DB_PREFIX."adherent as d";
133 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as c on d.state_id = c.rowid";
134 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_regions as r on c.fk_region = r.code_region";
135 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co on d.country = co.rowid";
136 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."subscription as s ON s.fk_adherent = d.rowid";
137 $sql .= " WHERE d.entity IN (".getEntity('adherent').")";
138 $sql .= " AND d.statut <> ".Adherent::STATUS_DRAFT;
139 $sql .= " GROUP BY co.label, co.code, r.nom"; //+
140 //print $sql;
141 }
142 if ($mode == 'memberbytown') {
143 $label = $langs->trans("Country");
144 $label2 = $langs->trans("Town");
145 $tab = 'statstown';
146
147 $data = array();
148 $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";
149 $sql .= " FROM ".MAIN_DB_PREFIX."adherent as d";
150 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c on d.country = c.rowid";
151 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."subscription as s ON s.fk_adherent = d.rowid";
152 $sql .= " WHERE d.entity IN (".getEntity('adherent').")";
153 $sql .= " AND d.statut <> ".Adherent::STATUS_DRAFT;
154 $sql .= " GROUP BY c.label, c.code, d.town";
155 //print $sql;
156 }
157
158 $langsen = new Translate('', $conf);
159 $langsen->setDefaultLang('en_US');
160 $langsen->load("dict");
161 //print $langsen->trans("Country"."FI");exit;
162
163 // Define $data array
164 dol_syslog("Count member", LOG_DEBUG);
165 if ($sql != null) {
166 $resql = $db->query($sql);
167 } else {
168 $resql = false;
169 dol_syslog(__FILE__.":No SQL, invalid mode '$mode'", LOG_ERR);
170 }
171 if ($resql) {
172 $num = $db->num_rows($resql);
173 $i = 0;
174 while ($i < $num) {
175 $obj = $db->fetch_object($resql);
176 if ($mode == 'memberbycountry') {
177 $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 : '<span class="opacitymedium">'.$langs->trans("Unknown").'</span>')),
178 'label_en' => (($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>')),
179 'code' => $obj->code,
180 'nb' => $obj->nb,
181 'lastdate' => $db->jdate($obj->lastdate),
182 'lastsubscriptiondate' => $db->jdate($obj->lastsubscriptiondate)
183 );
184 }
185 if ($mode == 'memberbyregion') { //+
186 $data[] = array(
187 '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 : '<span class="opacitymedium">'.$langs->trans("Unknown").'</span>')),
188 'label_en' => (($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>')),
189 'label2' => ($obj->label2 ? $obj->label2 : '<span class="opacitymedium">'.$langs->trans("Unknown").'</span>'),
190 'nb' => $obj->nb,
191 'lastdate' => $db->jdate($obj->lastdate),
192 'lastsubscriptiondate' => $db->jdate($obj->lastsubscriptiondate)
193 );
194 }
195 if ($mode == 'memberbystate') {
196 $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 : '<span class="opacitymedium">'.$langs->trans("Unknown").'</span>')),
197 'label_en' => (($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>')),
198 'label2' => ($obj->label2 ? $obj->label2 : '<span class="opacitymedium">'.$langs->trans("Unknown").'</span>'),
199 'nb' => $obj->nb,
200 'lastdate' => $db->jdate($obj->lastdate),
201 'lastsubscriptiondate' => $db->jdate($obj->lastsubscriptiondate)
202 );
203 }
204 if ($mode == 'memberbytown') {
205 $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 : '<span class="opacitymedium">'.$langs->trans("Unknown").'</span>')),
206 'label_en' => (($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>')),
207 'label2' => ($obj->label2 ? $obj->label2 : '<span class="opacitymedium">'.$langs->trans("Unknown").'</span>'),
208 'nb' => $obj->nb,
209 'lastdate' => $db->jdate($obj->lastdate),
210 'lastsubscriptiondate' => $db->jdate($obj->lastsubscriptiondate)
211 );
212 }
213
214 $i++;
215 }
216 $db->free($resql);
217 } else {
218 dol_print_error($db);
219 }
220}
221
222
223$head = member_stats_prepare_head($memberstatic);
224
225print dol_get_fiche_head($head, $tab, '', -1, '');
226
227
228// Print title
229if ($mode && !count($data)) {
230 print $langs->trans("NoValidatedMemberYet").'<br>';
231 print '<br>';
232} else {
233 if ($mode == 'memberbycountry') {
234 print '<span class="opacitymedium">'.$langs->trans("MembersByCountryDesc").'</span><br>';
235 } elseif ($mode == 'memberbystate') {
236 print '<span class="opacitymedium">'.$langs->trans("MembersByStateDesc").'</span><br>';
237 } elseif ($mode == 'memberbytown') {
238 print '<span class="opacitymedium">'.$langs->trans("MembersByTownDesc").'</span><br>';
239 } elseif ($mode == 'memberbyregion') {
240 print '<span class="opacitymedium">'.$langs->trans("MembersByRegion").'</span><br>'; //+
241 } else {
242 print '<span class="opacitymedium">'.$langs->trans("MembersStatisticsDesc").'</span><br>';
243 print '<br>';
244 print '<a href="'.$_SERVER["PHP_SELF"].'?mode=memberbycountry">'.$langs->trans("MembersStatisticsByCountries").'</a><br>';
245 print '<br>';
246 print '<a href="'.$_SERVER["PHP_SELF"].'?mode=memberbystate">'.$langs->trans("MembersStatisticsByState").'</a><br>';
247 print '<br>';
248 print '<a href="'.$_SERVER["PHP_SELF"].'?mode=memberbytown">'.$langs->trans("MembersStatisticsByTown").'</a><br>';
249 print '<br>'; //+
250 print '<a href="'.$_SERVER["PHP_SELF"].'?mode=memberbyregion">'.$langs->trans("MembersStatisticsByRegion").'</a><br>'; //+
251 }
252 print '<br>';
253}
254
255
256// Show graphics
257if (count($arrayjs) && $mode == 'memberbycountry') {
258 $color_file = DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/theme_vars.inc.php';
259 if (is_readable($color_file)) {
260 include $color_file;
261 }
262
263 // Assume we've already included the proper headers so just call our script inline
264 // More doc: https://developers.google.com/chart/interactive/docs/gallery/geomap?hl=fr-FR
265 print "\n<script type='text/javascript'>\n";
266 print "google.load('visualization', '1', {'packages': ['geomap']});\n";
267 print "google.setOnLoadCallback(drawMap);\n";
268 print "function drawMap() {\n\tvar data = new google.visualization.DataTable();\n";
269
270 // Get the total number of rows
271 print "\tdata.addRows(".count($data).");\n";
272 print "\tdata.addColumn('string', 'Country');\n";
273 print "\tdata.addColumn('number', 'Number');\n";
274
275 // loop and dump
276 $i = 0;
277 foreach ($data as $val) {
278 $valcountry = strtoupper($val['code']); // Should be ISO-3166 code (faster)
279 //$valcountry=ucfirst($val['label_en']);
280 if ($valcountry == 'Great Britain') {
281 $valcountry = 'United Kingdom';
282 } // fix case of uk (when we use labels)
283 print "\tdata.setValue(".$i.", 0, \"".$valcountry."\");\n";
284 print "\tdata.setValue(".$i.", 1, ".$val['nb'].");\n";
285 // Google's Geomap only supports up to 400 entries
286 if ($i >= 400) {
287 break;
288 }
289 $i++;
290 }
291
292 print "\tvar options = {};\n";
293 print "\toptions['dataMode'] = 'regions';\n";
294 print "\toptions['showZoomOut'] = false;\n";
295 //print "\toptions['zoomOutLabel'] = '".dol_escape_js($langs->transnoentitiesnoconv("Numbers"))."';\n";
296 print "\toptions['width'] = ".$graphwidth.";\n";
297 print "\toptions['height'] = ".$graphheight.";\n";
298 print "\toptions['colors'] = [0x".colorArrayToHex($theme_datacolor[1], 'BBBBBB').", 0x".colorArrayToHex($theme_datacolor[0], '444444')."];\n";
299 print "\tvar container = document.getElementById('".$mode."');\n";
300 print "\tvar geomap = new google.visualization.GeoMap(container);\n";
301 print "\tgeomap.draw(data, options);\n";
302 print "}\n";
303 print "</script>\n";
304
305 // print the div tag that will contain the map
306 print '<div class="center" id="'.$mode.'"></div>'."\n";
307}
308
309if ($mode) {
310 // Print array
311 print '<div class="div-table-responsive">'; // You can use div-table-responsive-no-min if you don't need reserved height for your table
312 print '<table class="liste centpercent noborder">';
313 print '<tr class="liste_titre">';
314 print '<th>'.$label.'</th>';
315 if (isset($label2)) {
316 print '<th class="center">'.$label2.'</th>';
317 }
318 print '<th class="right">'.$langs->trans("NbOfMembers").' <span class="opacitymedium">('.$langs->trans("AllTime").')</span></th>';
319 print '<th class="center">'.$langs->trans("LastMemberDate").'</th>';
320 print '<th class="center">'.$langs->trans("LatestSubscriptionDate").'</th>';
321 print '</tr>';
322
323 foreach ($data as $val) {
324 print '<tr class="oddeven">';
325 print '<td>'.$val['label'].'</td>';
326 if (isset($label2)) {
327 print '<td class="center">'.$val['label2'].'</td>';
328 }
329 print '<td class="right">'.$val['nb'].'</td>';
330 print '<td class="center">'.dol_print_date($val['lastdate'], 'dayhour').'</td>';
331 print '<td class="center">'.dol_print_date($val['lastsubscriptiondate'], 'dayhour').'</td>';
332 print '</tr>';
333 }
334
335 print '</table>';
336 print '</div>';
337}
338
339
340print dol_get_fiche_end();
341
342// End of page
343llxFooter();
344$db->close();
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:70
Class to manage members of a foundation.
static getDefaultGraphSizeForStats($direction, $defaultsize='')
getDefaultGraphSizeForStats
Class to manage translations.
llxFooter()
Footer empty.
Definition document.php:107
colorArrayToHex($arraycolor, $colorifnotfound='888888')
Convert an array with RGB value into hex RGB value.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0)
Show tabs of a record.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
dol_now($mode='auto')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
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.
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.