geo.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. /**
  18. * \file htdocs/adherents/stats/geo.php
  19. * \ingroup member
  20. * \brief Page with geographical statistics on members
  21. */
  22. require '../../main.inc.php';
  23. require_once DOL_DOCUMENT_ROOT.'/core/lib/member.lib.php';
  24. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
  26. $graphwidth = DolGraph::getDefaultGraphSizeForStats('width', 700);
  27. $mapratio = 0.5;
  28. $graphheight = round($graphwidth * $mapratio);
  29. $mode = GETPOST('mode') ?GETPOST('mode') : '';
  30. // Security check
  31. if ($user->socid > 0)
  32. {
  33. $action = '';
  34. $socid = $user->socid;
  35. }
  36. $result = restrictedArea($user, 'adherent', '', '', 'cotisation');
  37. $year = strftime("%Y", time());
  38. $startyear = $year - 2;
  39. $endyear = $year;
  40. // Load translation files required by the page
  41. $langs->loadLangs(array("companies", "members"));
  42. /*
  43. * View
  44. */
  45. $arrayjs = array('https://www.google.com/jsapi');
  46. if (!empty($conf->dol_use_jmobile)) $arrayjs = array();
  47. $title = $langs->trans("Statistics");
  48. if ($mode == 'memberbycountry') $title = $langs->trans("MembersStatisticsByCountries");
  49. if ($mode == 'memberbystate') $title = $langs->trans("MembersStatisticsByState");
  50. if ($mode == 'memberbytown') $title = $langs->trans("MembersStatisticsByTown");
  51. if ($mode == 'memberbyregion') $title = $langs->trans("MembersStatisticsByRegion");
  52. llxHeader('', $title, '', '', 0, 0, $arrayjs);
  53. print load_fiche_titre($title, $mesg);
  54. dol_mkdir($dir);
  55. if ($mode)
  56. {
  57. // Define sql
  58. if ($mode == 'memberbycountry')
  59. {
  60. $label = $langs->trans("Country");
  61. $tab = 'statscountry';
  62. $data = array();
  63. $sql .= "SELECT COUNT(d.rowid) as nb, MAX(d.datevalid) as lastdate, MAX(s.dateadh) as lastsubscriptiondate, c.code, c.label";
  64. $sql .= " FROM ".MAIN_DB_PREFIX."adherent as d";
  65. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c on d.country = c.rowid";
  66. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."subscription as s ON s.fk_adherent = d.rowid";
  67. $sql .= " WHERE d.entity IN (".getEntity('adherent').")";
  68. $sql .= " AND d.statut = 1";
  69. $sql .= " GROUP BY c.label, c.code";
  70. //print $sql;
  71. }
  72. if ($mode == 'memberbystate')
  73. {
  74. $label = $langs->trans("Country");
  75. $label2 = $langs->trans("State");
  76. $tab = 'statsstate';
  77. $data = array();
  78. $sql .= "SELECT COUNT(d.rowid) as nb, MAX(d.datevalid) as lastdate, MAX(s.dateadh) as lastsubscriptiondate, co.code, co.label, c.nom as label2"; //
  79. $sql .= " FROM ".MAIN_DB_PREFIX."adherent as d";
  80. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as c on d.state_id = c.rowid";
  81. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_regions as r on c.fk_region = r.code_region";
  82. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co on d.country = co.rowid";
  83. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."subscription as s ON s.fk_adherent = d.rowid";
  84. $sql .= " WHERE d.entity IN (".getEntity('adherent').")";
  85. $sql .= " AND d.statut = 1";
  86. $sql .= " GROUP BY co.label, co.code, c.nom";
  87. //print $sql;
  88. }
  89. if ($mode == 'memberbyregion') //
  90. {
  91. $label = $langs->trans("Country");
  92. $label2 = $langs->trans("Region"); //département
  93. $tab = 'statsregion'; //onglet
  94. $data = array(); //tableau de donnée
  95. $sql .= "SELECT COUNT(d.rowid) as nb, MAX(d.datevalid) as lastdate, MAX(s.dateadh) as lastsubscriptiondate, co.code, co.label, r.nom as label2";
  96. $sql .= " FROM ".MAIN_DB_PREFIX."adherent as d";
  97. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as c on d.state_id = c.rowid";
  98. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_regions as r on c.fk_region = r.code_region";
  99. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co on d.country = co.rowid";
  100. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."subscription as s ON s.fk_adherent = d.rowid";
  101. $sql .= " WHERE d.entity IN (".getEntity('adherent').")";
  102. $sql .= " AND d.statut = 1";
  103. $sql .= " GROUP BY co.label, co.code, r.nom"; //+
  104. //print $sql;
  105. }
  106. if ($mode == 'memberbytown')
  107. {
  108. $label = $langs->trans("Country");
  109. $label2 = $langs->trans("Town");
  110. $tab = 'statstown';
  111. $data = array();
  112. $sql .= "SELECT COUNT(d.rowid) as nb, MAX(d.datevalid) as lastdate, MAX(s.dateadh) as lastsubscriptiondate, c.code, c.label, d.town as label2";
  113. $sql .= " FROM ".MAIN_DB_PREFIX."adherent as d";
  114. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c on d.country = c.rowid";
  115. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."subscription as s ON s.fk_adherent = d.rowid";
  116. $sql .= " WHERE d.entity IN (".getEntity('adherent').")";
  117. $sql .= " AND d.statut = 1";
  118. $sql .= " GROUP BY c.label, c.code, d.town";
  119. //print $sql;
  120. }
  121. $langsen = new Translate('', $conf);
  122. $langsen->setDefaultLang('en_US');
  123. $langsen->load("dict");
  124. //print $langsen->trans("Country"."FI");exit;
  125. // Define $data array
  126. dol_syslog("Count member", LOG_DEBUG);
  127. $resql = $db->query($sql);
  128. if ($resql)
  129. {
  130. $num = $db->num_rows($resql);
  131. $i = 0;
  132. while ($i < $num)
  133. {
  134. $obj = $db->fetch_object($resql);
  135. if ($mode == 'memberbycountry')
  136. {
  137. $data[] = array('label'=>(($obj->code && $langs->trans("Country".$obj->code) != "Country".$obj->code) ? $langs->trans("Country".$obj->code) : ($obj->label ? $obj->label : $langs->trans("Unknown"))),
  138. 'label_en'=>(($obj->code && $langsen->transnoentitiesnoconv("Country".$obj->code) != "Country".$obj->code) ? $langsen->transnoentitiesnoconv("Country".$obj->code) : ($obj->label ? $obj->label : $langs->trans("Unknown"))),
  139. 'code'=>$obj->code,
  140. 'nb'=>$obj->nb,
  141. 'lastdate'=>$db->jdate($obj->lastdate),
  142. 'lastsubscriptiondate'=>$db->jdate($obj->lastsubscriptiondate)
  143. );
  144. }
  145. if ($mode == 'memberbyregion') //+
  146. {
  147. $data[] = array(
  148. 'label'=>(($obj->code && $langs->trans("Country".$obj->code) != "Country".$obj->code) ? $langs->trans("Country".$obj->code) : ($obj->label ? $obj->label : $langs->trans("Unknown"))),
  149. 'label_en'=>(($obj->code && $langsen->transnoentitiesnoconv("Country".$obj->code) != "Country".$obj->code) ? $langsen->transnoentitiesnoconv("Country".$obj->code) : ($obj->label ? $obj->label : $langs->trans("Unknown"))),
  150. 'label2'=>($obj->label2 ? $obj->label2 : $langs->trans("Unknown")),
  151. 'nb'=>$obj->nb,
  152. 'lastdate'=>$db->jdate($obj->lastdate),
  153. 'lastsubscriptiondate'=>$db->jdate($obj->lastsubscriptiondate)
  154. );
  155. }
  156. if ($mode == 'memberbystate')
  157. {
  158. $data[] = array('label'=>(($obj->code && $langs->trans("Country".$obj->code) != "Country".$obj->code) ? $langs->trans("Country".$obj->code) : ($obj->label ? $obj->label : $langs->trans("Unknown"))),
  159. 'label_en'=>(($obj->code && $langsen->transnoentitiesnoconv("Country".$obj->code) != "Country".$obj->code) ? $langsen->transnoentitiesnoconv("Country".$obj->code) : ($obj->label ? $obj->label : $langs->trans("Unknown"))),
  160. 'label2'=>($obj->label2 ? $obj->label2 : $langs->trans("Unknown")),
  161. 'nb'=>$obj->nb,
  162. 'lastdate'=>$db->jdate($obj->lastdate),
  163. 'lastsubscriptiondate'=>$db->jdate($obj->lastsubscriptiondate)
  164. );
  165. }
  166. if ($mode == 'memberbytown')
  167. {
  168. $data[] = array('label'=>(($obj->code && $langs->trans("Country".$obj->code) != "Country".$obj->code) ? $langs->trans("Country".$obj->code) : ($obj->label ? $obj->label : $langs->trans("Unknown"))),
  169. 'label_en'=>(($obj->code && $langsen->transnoentitiesnoconv("Country".$obj->code) != "Country".$obj->code) ? $langsen->transnoentitiesnoconv("Country".$obj->code) : ($obj->label ? $obj->label : $langs->trans("Unknown"))),
  170. 'label2'=>($obj->label2 ? $obj->label2 : $langs->trans("Unknown")),
  171. 'nb'=>$obj->nb,
  172. 'lastdate'=>$db->jdate($obj->lastdate),
  173. 'lastsubscriptiondate'=>$db->jdate($obj->lastsubscriptiondate)
  174. );
  175. }
  176. $i++;
  177. }
  178. $db->free($resql);
  179. }
  180. else
  181. {
  182. dol_print_error($db);
  183. }
  184. }
  185. $head = member_stats_prepare_head($adh);
  186. dol_fiche_head($head, $tab, $langs->trans("Statistics"), -1, 'user');
  187. // Print title
  188. if ($mode && !count($data))
  189. {
  190. print $langs->trans("NoValidatedMemberYet").'<br>';
  191. print '<br>';
  192. }
  193. else
  194. {
  195. if ($mode == 'memberbycountry') print $langs->trans("MembersByCountryDesc").'<br>';
  196. elseif ($mode == 'memberbystate') print $langs->trans("MembersByStateDesc").'<br>';
  197. elseif ($mode == 'memberbytown') print $langs->trans("MembersByTownDesc").'<br>';
  198. elseif ($mode == 'memberbyregion') print $langs->trans("MembersByRegion").'<br>'; //+
  199. else
  200. {
  201. print $langs->trans("MembersStatisticsDesc").'<br>';
  202. print '<br>';
  203. print '<a href="'.$_SERVER["PHP_SELF"].'?mode=memberbycountry">'.$langs->trans("MembersStatisticsByCountries").'</a><br>';
  204. print '<br>';
  205. print '<a href="'.$_SERVER["PHP_SELF"].'?mode=memberbystate">'.$langs->trans("MembersStatisticsByState").'</a><br>';
  206. print '<br>';
  207. print '<a href="'.$_SERVER["PHP_SELF"].'?mode=memberbytown">'.$langs->trans("MembersStatisticsByTown").'</a><br>';
  208. print '<br>'; //+
  209. print '<a href="'.$_SERVER["PHP_SELF"].'?mode=memberbyregion">'.$langs->trans("MembersStatisticsByRegion").'</a><br>'; //+
  210. }
  211. print '<br>';
  212. }
  213. // Show graphics
  214. if (count($arrayjs) && $mode == 'memberbycountry')
  215. {
  216. $color_file = DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/theme_vars.inc.php';
  217. if (is_readable($color_file)) include_once $color_file;
  218. // Assume we've already included the proper headers so just call our script inline
  219. // More doc: https://developers.google.com/chart/interactive/docs/gallery/geomap?hl=fr-FR
  220. print "\n<script type='text/javascript'>\n";
  221. print "google.load('visualization', '1', {'packages': ['geomap']});\n";
  222. print "google.setOnLoadCallback(drawMap);\n";
  223. print "function drawMap() {\n\tvar data = new google.visualization.DataTable();\n";
  224. // Get the total number of rows
  225. print "\tdata.addRows(".count($data).");\n";
  226. print "\tdata.addColumn('string', 'Country');\n";
  227. print "\tdata.addColumn('number', 'Number');\n";
  228. // loop and dump
  229. $i = 0;
  230. foreach ($data as $val)
  231. {
  232. $valcountry = strtoupper($val['code']); // Should be ISO-3166 code (faster)
  233. //$valcountry=ucfirst($val['label_en']);
  234. if ($valcountry == 'Great Britain') { $valcountry = 'United Kingdom'; } // fix case of uk (when we use labels)
  235. print "\tdata.setValue(".$i.", 0, \"".$valcountry."\");\n";
  236. print "\tdata.setValue(".$i.", 1, ".$val['nb'].");\n";
  237. // Google's Geomap only supports up to 400 entries
  238. if ($i >= 400) { break; }
  239. $i++;
  240. }
  241. print "\tvar options = {};\n";
  242. print "\toptions['dataMode'] = 'regions';\n";
  243. print "\toptions['showZoomOut'] = false;\n";
  244. //print "\toptions['zoomOutLabel'] = '".dol_escape_js($langs->transnoentitiesnoconv("Numbers"))."';\n";
  245. print "\toptions['width'] = ".$graphwidth.";\n";
  246. print "\toptions['height'] = ".$graphheight.";\n";
  247. print "\toptions['colors'] = [0x".colorArrayToHex($theme_datacolor[1], 'BBBBBB').", 0x".colorArrayToHex($theme_datacolor[0], '444444')."];\n";
  248. print "\tvar container = document.getElementById('".$mode."');\n";
  249. print "\tvar geomap = new google.visualization.GeoMap(container);\n";
  250. print "\tgeomap.draw(data, options);\n";
  251. print "};\n";
  252. print "</script>\n";
  253. // print the div tag that will contain the map
  254. print '<div class="center" id="'.$mode.'"></div>'."\n";
  255. print '<br>';
  256. }
  257. if ($mode)
  258. {
  259. // Print array / Affiche le tableau
  260. print '<table class="liste centpercent">';
  261. print '<tr class="liste_titre">';
  262. print '<td>'.$label.'</td>';
  263. if ($label2) print '<td class="center">'.$label2.'</td>';
  264. print '<td class="right">'.$langs->trans("NbOfMembers").'</td>';
  265. print '<td class="center">'.$langs->trans("LastMemberDate").'</td>';
  266. print '<td class="center">'.$langs->trans("LatestSubscriptionDate").'</td>';
  267. print '</tr>';
  268. foreach ($data as $val)
  269. {
  270. $year = $val['year'];
  271. print '<tr class="oddeven">';
  272. print '<td>'.$val['label'].'</td>';
  273. if ($label2) print '<td class="center">'.$val['label2'].'</td>';
  274. print '<td class="right">'.$val['nb'].'</td>';
  275. print '<td class="center">'.dol_print_date($val['lastdate'], 'dayhour').'</td>';
  276. print '<td class="center">'.dol_print_date($val['lastsubscriptiondate'], 'dayhour').'</td>';
  277. print '</tr>';
  278. }
  279. print '</table>';
  280. }
  281. dol_fiche_end();
  282. // End of page
  283. llxFooter();
  284. $db->close();