geo.php 14 KB

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