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