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