PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/application/views/admin/settings_js.php

https://github.com/yamamoto123/Ushahidi_Web
PHP | 275 lines | 193 code | 41 blank | 41 comment | 31 complexity | 8850f9d087a8b246c5f7b056a3a87808 MD5 | raw file
  1. /**
  2. * Settings js file.
  3. *
  4. * Handles javascript stuff related to settings function.
  5. *
  6. * PHP version 5
  7. * LICENSE: This source file is subject to LGPL license
  8. * that is available through the world-wide-web at the following URI:
  9. * http://www.gnu.org/copyleft/lesser.html
  10. * @author Ushahidi Team <team@ushahidi.com>
  11. * @package Ushahidi - http://source.ushahididev.com
  12. * @module API Controller
  13. * @copyright Ushahidi - http://www.ushahidi.com
  14. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
  15. */
  16. // Map JS
  17. $(document).ready(
  18. function()
  19. {
  20. var markers;
  21. var marker;
  22. var myPoint;
  23. var lonlat;
  24. var DispProj = new OpenLayers.Projection("EPSG:4326");
  25. var MapProj = new OpenLayers.Projection("EPSG:900913");
  26. var options = {
  27. maxResolution: 156543.0339
  28. , units: "m"
  29. , projection: MapProj
  30. , 'displayProjection': DispProj
  31. , maxExtent: new OpenLayers.Bounds(-20037508.34, -20037508.34, 20037508.34, 20037508.34)
  32. , controls: [ new OpenLayers.Control.Navigation(),
  33. new OpenLayers.Control.MouseDefaults(),
  34. new OpenLayers.Control.PanZoom(),
  35. new OpenLayers.Control.ArgParser(),
  36. new OpenLayers.Control.MousePosition(),
  37. new OpenLayers.Control.LoadingPanel({minSize: new OpenLayers.Size(573, 366)}) ]
  38. };
  39. var map = new OpenLayers.Map('map', options);
  40. <?php echo map::layers_js(TRUE); ?>
  41. map.addLayers(<?php echo map::layers_array(TRUE); ?>);
  42. // Transform feature point coordinate to Spherical Mercator
  43. preFeatureInsert = function(feature) {
  44. var point = new OpenLayers.Geometry.Point(feature.geometry.x, feature.geometry.y);
  45. OpenLayers.Projection.transform(point, DispProj, MapProj);
  46. };
  47. // Create the markers layer
  48. markers = new OpenLayers.Layer.Markers("Markers", {
  49. preFeatureInsert:preFeatureInsert,
  50. projection: DispProj
  51. });
  52. map.addLayer(markers);
  53. // create myPoint, a lat/lon object
  54. myPoint = new OpenLayers.LonLat(<?php echo $default_lon; ?>, <?php echo $default_lat; ?>).transform(DispProj, MapProj);
  55. // create a marker using the myPoint lat/lon object
  56. marker = new OpenLayers.Marker(myPoint);
  57. markers.addMarker(marker);
  58. // set map center and zoom in to default zoom level
  59. map.setCenter(myPoint, <?php echo $default_zoom; ?>);
  60. // add info bubble to the marker
  61. // popup = new OpenLayers.Popup.Anchored("test", myPoint,new OpenLayers.Size(200,200),"Hello!", true);
  62. // create new marker at map click location
  63. map.events.register("click", map, function(e){
  64. // Update the myPoint global
  65. myPoint = map.getLonLatFromViewPortPx(e.xy);
  66. lonlat = map.getLonLatFromViewPortPx(e.xy);
  67. markers.removeMarker(marker);
  68. marker = new OpenLayers.Marker(lonlat);
  69. markers.addMarker(marker);
  70. // Update form values (jQuery)
  71. lonlat = lonlat.transform(MapProj,DispProj);
  72. $("#default_lat").attr("value", lonlat.lat);
  73. $("#default_lon").attr("value", lonlat.lon);
  74. });
  75. // When we change the zoom level on the map control itself, also change the slider
  76. // which is where the value is actually being passed from when we save the map position
  77. map.events.on({
  78. zoomend: function(e) {
  79. $('select#default_zoom').val(map.getZoom());
  80. $('select#default_zoom').trigger('click');
  81. }
  82. });
  83. // zoom slider detection
  84. $('select#default_zoom').selectToUISlider({
  85. labels: 5,
  86. sliderOptions: {
  87. change:function(e, ui) {
  88. var new_zoom = parseInt($("#default_zoom").val());
  89. $('#zoom_level').html('"' + new_zoom + '"');
  90. map.setCenter(myPoint, new_zoom);
  91. markers.removeMarker(marker);
  92. marker = new OpenLayers.Marker(myPoint);
  93. markers.addMarker(marker);
  94. }
  95. }
  96. }).hide();
  97. // detect country dropdown change, then zoom to selected country
  98. $('#default_country').change(function(){
  99. address = $('#default_country :selected').text();
  100. //>
  101. //> 25/03/2011 - E.Kala <emmanuel(at)ushahidi.com>
  102. //> Switched to Google v3 API
  103. //>
  104. var geocoder = new google.maps.Geocoder();
  105. if (geocoder) {
  106. geocoder.geocode({ 'address': address },
  107. function(results, status) {
  108. if (status == google.maps.GeocoderStatus.ZERO_RESULTS) {
  109. alert(address + " not found");
  110. } else if (status == google.maps.GeocoderStatus.OK) {
  111. // Get the lat/lon from the result; accuracy of the map center is off because the lookup
  112. // address does not include the name of the capital city
  113. var point = results[0].geometry.location;
  114. myPoint = new OpenLayers.LonLat(point.lng(), point.lat()).transform(DispProj, MapProj);
  115. map.setCenter(myPoint, map.getZoom());
  116. markers.removeMarker(marker);
  117. marker = new OpenLayers.Marker(myPoint);
  118. markers.addMarker(marker);
  119. // Update form lat/lon values
  120. $("#default_lat").attr("value", point.lat());
  121. $("#default_lon").attr("value", point.lng());
  122. }
  123. });
  124. }
  125. });
  126. // Provider Select JS
  127. // This could be cleaner ;)
  128. var i;
  129. var api_go;
  130. default_map = '<?php print $default_map ?>';
  131. all_maps = <?php echo $all_maps_json; ?>;
  132. for (var i in all_maps)
  133. {
  134. target = map.getLayersByName(all_maps[i].title);
  135. if (i == default_map) {
  136. target[0].setVisibility(true);
  137. map.setBaseLayer(target[0]);
  138. if (all_maps[i].api_signup) {
  139. $('#api_link').attr('href', all_maps[i].api_signup);
  140. $('#api_link').attr('target', '_blank');
  141. } else {
  142. $('#api_link').attr('href', 'javascript:alert(\'Your current selection does not require an API key!\')');
  143. $('#api_link').attr('target', '_top');
  144. }
  145. if (all_maps[i].openlayers == 'Google') {
  146. $("#api_div_google").show();
  147. }
  148. else
  149. {
  150. $("#api_div_google").hide();
  151. }
  152. if (all_maps[i].openlayers == 'Yahoo') {
  153. $("#api_div_yahoo").show();
  154. }
  155. else
  156. {
  157. $("#api_div_yahoo").hide();
  158. }
  159. }
  160. else
  161. {
  162. if (target[0]) {
  163. target[0].setVisibility(false);
  164. };
  165. }
  166. };
  167. // detect map provider dropdown change
  168. $('#default_map').change(function(){
  169. selected_map = $('#default_map option:selected').val();
  170. for (var i in all_maps)
  171. {
  172. target = map.getLayersByName(all_maps[i].title);
  173. if (i == selected_map)
  174. {
  175. if (target[0]) {
  176. target[0].setVisibility(true);
  177. map.setBaseLayer(target[0]);
  178. if (all_maps[i].api_signup) {
  179. $('#api_link').attr('href', all_maps[i].api_signup);
  180. $('#api_link').attr('target', '_blank');
  181. } else {
  182. $('#api_link').attr('href', 'javascript:alert(\'Your current selection does not require an API key!\')');
  183. $('#api_link').attr('target', '_top');
  184. }
  185. if (all_maps[i].openlayers == 'Google') {
  186. $("#api_div_google").show();
  187. }
  188. else
  189. {
  190. $("#api_div_google").hide();
  191. }
  192. if (all_maps[i].openlayers == 'Yahoo') {
  193. $("#api_div_yahoo").show();
  194. }
  195. else
  196. {
  197. $("#api_div_yahoo").hide();
  198. }
  199. }
  200. }
  201. else
  202. {
  203. if (target[0]) {
  204. target[0].setVisibility(false);
  205. };
  206. }
  207. };
  208. });
  209. }
  210. );
  211. // Retrieve Cities From Geonames DB (Ajax)
  212. function retrieveCities()
  213. {
  214. var selected = $("#default_country option[selected]");
  215. country = selected.val();
  216. if (!country || country =='') {
  217. alert('Please select a country from the dropdown');
  218. }
  219. else
  220. {
  221. $('#cities_loading').html('<img src="<?php echo url::file_loc('img')."media/img/loading_g.gif"; ?>">');
  222. $.getJSON("<?php echo url::site() . 'admin/settings/updateCities/' ?>" + country,
  223. function(data){
  224. if (data.status == 'success'){
  225. $('#city_count').show();
  226. $('#city_count').html(data.response);
  227. $('#cities_loading').html('');
  228. } else {
  229. alert(data.response);
  230. }
  231. $('#cities_loading').html('');
  232. }, "json");
  233. }
  234. }