geo.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 2 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 <http://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/class/dolgraph.class.php");
  24. require_once(DOL_DOCUMENT_ROOT."/lib/member.lib.php");
  25. $graphwidth = 700;
  26. $mapratio = 0.5;
  27. $graphheight = round($graphwidth * $mapratio);
  28. $mode=GETPOST('mode')?GETPOST('mode'):'';
  29. // Security check
  30. if ($user->societe_id > 0)
  31. {
  32. $action = '';
  33. $socid = $user->societe_id;
  34. }
  35. if (! $user->rights->adherent->cotisation->lire)
  36. accessforbidden();
  37. $year = strftime("%Y", time());
  38. $startyear=$year-2;
  39. $endyear=$year;
  40. /*
  41. * View
  42. */
  43. llxHeader('','','','',0,0,array('http://www.google.com/jsapi'));
  44. $title=$langs->trans("Statistics");
  45. if ($mode == 'memberbycountry') $title=$langs->trans("MembersStatisticsByCountries");
  46. if ($mode == 'memberbystate') $title=$langs->trans("MembersStatisticsByState");
  47. if ($mode == 'memberbytown') $title=$langs->trans("MembersStatisticsByTown");
  48. print_fiche_titre($title, $mesg);
  49. create_exdir($dir);
  50. if ($mode)
  51. {
  52. // Define sql
  53. if ($mode == 'memberbycountry')
  54. {
  55. $label=$langs->trans("Country");
  56. $tab='statscountry';
  57. $data = array();
  58. $sql.="SELECT COUNT(d.rowid) as nb, MAX(d.datevalid) as lastdate, c.code, c.libelle as label";
  59. $sql.=" FROM ".MAIN_DB_PREFIX."adherent as d LEFT JOIN ".MAIN_DB_PREFIX."c_pays as c on d.pays = c.rowid";
  60. $sql.=" WHERE d.statut = 1";
  61. $sql.=" GROUP BY c.libelle, c.code";
  62. //print $sql;
  63. }
  64. if ($mode == 'memberbystate')
  65. {
  66. $label=$langs->trans("Country");
  67. $label2=$langs->trans("State");
  68. $tab='statsstate';
  69. $data = array();
  70. $sql.="SELECT COUNT(d.rowid) as nb, MAX(d.datevalid) as lastdate, p.code, p.libelle as label, c.nom as label2";
  71. $sql.=" FROM ".MAIN_DB_PREFIX."adherent as d LEFT JOIN ".MAIN_DB_PREFIX."c_departements as c on d.fk_departement = c.rowid";
  72. $sql.=" LEFT JOIN ".MAIN_DB_PREFIX."c_regions as r on c.fk_region = r.code_region";
  73. $sql.=" LEFT JOIN ".MAIN_DB_PREFIX."c_pays as p on d.pays = p.rowid";
  74. $sql.=" WHERE d.statut = 1";
  75. $sql.=" GROUP BY p.libelle, p.code, c.nom";
  76. //print $sql;
  77. }
  78. if ($mode == 'memberbytown')
  79. {
  80. $label=$langs->trans("Country");
  81. $label2=$langs->trans("Town");
  82. $tab='statstown';
  83. $data = array();
  84. $sql.="SELECT COUNT(d.rowid) as nb, MAX(d.datevalid) as lastdate, p.code, p.libelle as label, d.ville as label2";
  85. $sql.=" FROM ".MAIN_DB_PREFIX."adherent as d";
  86. $sql.=" LEFT JOIN ".MAIN_DB_PREFIX."c_pays as p on d.pays = p.rowid";
  87. $sql.=" WHERE d.statut = 1";
  88. $sql.=" GROUP BY p.libelle, p.code, d.ville";
  89. //print $sql;
  90. }
  91. $langsen=new Translate('',$conf);
  92. $langsen->setDefaultLang('en_US');
  93. $langsen->load("dict");
  94. //print $langsen->trans("Country"."FI");exit;
  95. // Define $data array
  96. dol_syslog("Count member sql=".$sql);
  97. $resql=$db->query($sql);
  98. if ($resql)
  99. {
  100. $num=$db->num_rows($resql);
  101. $i=0;
  102. while ($i < $num)
  103. {
  104. $obj=$db->fetch_object($resql);
  105. if ($mode == 'memberbycountry')
  106. {
  107. $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"))),
  108. 'label_en'=>(($obj->code && $langsen->transnoentitiesnoconv("Country".$obj->code)!="Country".$obj->code)?$langsen->transnoentitiesnoconv("Country".$obj->code):($obj->label?$obj->label:$langs->trans("Unknown"))),
  109. 'code'=>$obj->code,
  110. 'nb'=>$obj->nb,
  111. 'lastdate'=>$db->jdate($obj->lastdate)
  112. );
  113. }
  114. if ($mode == 'memberbystate')
  115. {
  116. $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"))),
  117. 'label_en'=>(($obj->code && $langsen->transnoentitiesnoconv("Country".$obj->code)!="Country".$obj->code)?$langsen->transnoentitiesnoconv("Country".$obj->code):($obj->label?$obj->label:$langs->trans("Unknown"))),
  118. 'label2'=>($obj->label2?$obj->label2:$langs->trans("Unknown")),
  119. 'nb'=>$obj->nb,
  120. 'lastdate'=>$db->jdate($obj->lastdate)
  121. );
  122. }
  123. if ($mode == 'memberbytown')
  124. {
  125. $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"))),
  126. 'label_en'=>(($obj->code && $langsen->transnoentitiesnoconv("Country".$obj->code)!="Country".$obj->code)?$langsen->transnoentitiesnoconv("Country".$obj->code):($obj->label?$obj->label:$langs->trans("Unknown"))),
  127. 'label2'=>($obj->label2?$obj->label2:$langs->trans("Unknown")),
  128. 'nb'=>$obj->nb,
  129. 'lastdate'=>$db->jdate($obj->lastdate)
  130. );
  131. }
  132. $i++;
  133. }
  134. $db->free($resql);
  135. }
  136. else
  137. {
  138. dol_print_error($db);
  139. }
  140. }
  141. $head = member_stats_prepare_head($adh);
  142. dol_fiche_head($head, $tab, $langs->trans("Statistics"), 0, 'user');
  143. // Print title
  144. if ($mode && ! sizeof($data))
  145. {
  146. print $langs->trans("NoValidatedMemberYet").'<br>';
  147. print '<br>';
  148. }
  149. else
  150. {
  151. if ($mode == 'memberbycountry') print $langs->trans("MembersByCountryDesc").'<br>';
  152. else if ($mode == 'memberbystate') print $langs->trans("MembersByStateDesc").'<br>';
  153. else if ($mode == 'memberbytown') print $langs->trans("MembersByTownDesc").'<br>';
  154. else
  155. {
  156. print $langs->trans("MembersStatisticsDesc").'<br>';
  157. print '<br>';
  158. print '<a href="'.$_SERVER["PHP_SELF"].'?mode=memberbycountry">'.$langs->trans("MembersStatisticsByCountries").'</a><br>';
  159. print '<br>';
  160. print '<a href="'.$_SERVER["PHP_SELF"].'?mode=memberbystate">'.$langs->trans("MembersStatisticsByState").'</a><br>';
  161. print '<br>';
  162. print '<a href="'.$_SERVER["PHP_SELF"].'?mode=memberbytown">'.$langs->trans("MembersStatisticsByTown").'</a><br>';
  163. }
  164. print '<br>';
  165. }
  166. // Show graphics
  167. if ($mode == 'memberbycountry')
  168. {
  169. // Assume we've already included the proper headers so just call our script inline
  170. print "\n<script type='text/javascript'>\n";
  171. print "google.load('visualization', '1', {'packages': ['geomap']});\n";
  172. print "google.setOnLoadCallback(drawMap);\n";
  173. print "function drawMap() {\n\tvar data = new google.visualization.DataTable();\n";
  174. // Get the total number of rows
  175. print "\tdata.addRows(".sizeof($data).");\n";
  176. print "\tdata.addColumn('string', 'Country');\n";
  177. print "\tdata.addColumn('number', 'Number');\n";
  178. // loop and dump
  179. $i=0;
  180. foreach($data as $val)
  181. {
  182. //$valcountry=ucfirst($val['code']);
  183. $valcountry=ucfirst($val['label_en']);
  184. // fix case of uk
  185. if ($valcountry == 'Great Britain') { $valcountry = 'United Kingdom'; }
  186. print "\tdata.setValue(".$i.", 0, \"".$valcountry."\");\n";
  187. print "\tdata.setValue(".$i.", 1, ".$val['nb'].");\n";
  188. // Google's Geomap only supports up to 400 entries
  189. if ($i >= 400){ break; }
  190. $i++;
  191. }
  192. print "\tvar options = {};\n";
  193. print "\toptions['dataMode'] = 'regions';\n";
  194. print "\toptions['showZoomOut'] = false;\n";
  195. //print "\toptions['zoomOutLabel'] = '".dol_escape_js($langs->transnoentitiesnoconv("Numbers"))."';\n";
  196. print "\toptions['width'] = ".$graphwidth.";\n";
  197. print "\toptions['height'] = ".$graphheight.";\n";
  198. print "\tvar container = document.getElementById('".$mode."');\n";
  199. print "\tvar geomap = new google.visualization.GeoMap(container);\n";
  200. print "\tgeomap.draw(data, options);\n";
  201. print "};\n";
  202. print "</script>\n";
  203. // print the div tag that will contain the map
  204. print '<div align="center" id="'.$mode.'"></div>'."\n";
  205. print '<br>';
  206. }
  207. if ($mode)
  208. {
  209. // Print array
  210. print '<table class="border" width="100%">';
  211. print '<tr class="liste_titre">';
  212. print '<td align="center">'.$label.'</td>';
  213. if ($label2) print '<td align="center">'.$label2.'</td>';
  214. print '<td align="center">'.$langs->trans("NbOfMembers").'</td>';
  215. print '<td align="center">'.$langs->trans("LastMemberDate").'</td>';
  216. print '</tr>';
  217. $oldyear=0;
  218. $var=true;
  219. foreach ($data as $val)
  220. {
  221. $year = $val['year'];
  222. $var=!$var;
  223. print '<tr '.$bc[$var].'>';
  224. print '<td align="center">'.$val['label'].'</td>';
  225. if ($label2) print '<td align="center">'.$val['label2'].'</td>';
  226. print '<td align="right">'.$val['nb'].'</td>';
  227. print '<td align="right">'.dol_print_date($val['lastdate'],'dayhour').'</td>';
  228. print '</tr>';
  229. $oldyear=$year;
  230. }
  231. print '</table>';
  232. }
  233. dol_fiche_end();
  234. $db->close();
  235. llxFooter();
  236. ?>