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