PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/assets/plugins/managermanager/widgets/ddgmap/ddgmap.php

https://github.com/good-web-master/modx.evo.custom
PHP | 112 lines | 75 code | 13 blank | 24 comment | 7 complexity | d1cb20c5c609e8d5a434269253c9755b MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0, MIT, BSD-3-Clause
  1. <?php
  2. /**
  3. * mm_ddGMap
  4. * @version 1.0.2 (2012-02-12)
  5. *
  6. * Позволяет интегрировать карту Google Maps для получения координат.
  7. *
  8. * @copyright 2012, DivanDesign
  9. * http://www.DivanDesign.ru
  10. */
  11. function mm_ddGMap($tvs, $roles='', $templates='', $w='auto', $h='400') {
  12. global $modx, $content, $mm_fields, $modx_lang_attribute;
  13. $e = &$modx->Event;
  14. if ($e->name == 'OnDocFormRender' && useThisRule($roles, $templates)){
  15. // Your output should be stored in a string, which is outputted at the end
  16. // It will be inserted as a Javascript block (with jQuery), which is executed on document ready
  17. $output = '';
  18. // if we've been supplied with a string, convert it into an array
  19. $tvs = makeArray($tvs);
  20. // You might want to check whether the current page's template uses the TVs that have been
  21. // supplied, to save processing page which don't contain them
  22. // Which template is this page using?
  23. if (isset($content['template'])) {
  24. $page_template = $content['template'];
  25. } else {
  26. // If no content is set, it's likely we're adding a new page at top level.
  27. // So use the site default template. This may need some work as it might interfere with a default template set by MM?
  28. $page_template = $modx->config['default_template'];
  29. }
  30. $tvs = tplUseTvs($content['template'], $tvs);
  31. if ($tvs == false) {
  32. return;
  33. }
  34. $style = 'width: '.$w.'px; height: '.$h.'px; position: relative; border: 1px solid #c3c3c3;';
  35. // We always put a JS comment, which makes debugging much easier
  36. $output .= "// -------------- mm_ddGMap :: Begin ------------- \n";
  37. // We have functions to include JS or CSS external files you might need
  38. // The standard ModX API methods don't work here
  39. //$output .= includeJs('http://maps.google.com/maps/api/js?sensor=false');
  40. // Do something for each of the fields supplied
  41. foreach ($tvs as $tv) {
  42. // If it's a TV, we may need to map the field name, to what it's ID is.
  43. // This can be obtained from the mm_fields array
  44. $tv_id = 'tv'.$tv['id'];
  45. $output .= '
  46. var coordinatesField = $j("#'.$tv_id.'");//TV с координатами
  47. var ddLatLng = coordinatesField.val();//Координаты
  48. //Скрываем поле, запоминаем название поля
  49. var sectionName = coordinatesField.parents("tr:first").hide().find(".warning").text();
  50. coordinatesField.parents("tr:first").prev("tr").hide();
  51. //Контейнер для карты
  52. var sectionConteiner = $j("<div class=\"sectionHeader\">"+sectionName+"</div><div class=\"sectionBody tmplvars\"><div class=\"ddGMap\" style=\"'.$style.'\"></div></div>");
  53. //Добавляем контейнер
  54. coordinatesField.parents(".tab-page:first").append(sectionConteiner);
  55. //Если координаты не заданны, то задаём дефолт
  56. if(ddLatLng == "") ddLatLng = "55.19396010947335,61.3670539855957";
  57. ddLatLng = ddLatLng.split(",");
  58. //Callback функция для GM
  59. window.ddgminitialize = function(){
  60. var GM = google.maps;
  61. var myOptions = {
  62. zoom: 15,
  63. center: new GM.LatLng(ddLatLng[0],ddLatLng[1]),
  64. mapTypeId: GM.MapTypeId.ROADMAP,
  65. streetViewControl: false,
  66. scrollwheel: false
  67. };
  68. var map = new GM.Map(sectionConteiner.find(".ddGMap").get(0), myOptions);
  69. //Добавляем маркер на карту
  70. var GMMarker = new GM.Marker({
  71. position: new GM.LatLng(ddLatLng[0],ddLatLng[1]),
  72. map: map,
  73. draggable: true
  74. });
  75. //При перетаскивании маркера
  76. GM.event.addListener(GMMarker, "drag", function(event){
  77. var position = event.latLng;//Координаты
  78. coordinatesField.val(position.lat() + "," + position.lng());//Сохраняем значение в поле
  79. });
  80. //При клике на карте
  81. GM.event.addListener(map, "click", function(event){
  82. var position = event.latLng;//Новые координаты
  83. GMMarker.setPosition(position);//Меняем позицию маркера
  84. map.setCenter(position);//Центрируем карту на маркере
  85. coordinatesField.val(position.lat() + "," + position.lng());//Сохраняем значение в поле
  86. });
  87. };
  88. //Подключаем карту, вызываем callback функцию
  89. $j(window).on("load.ddEvents", function(){
  90. $j("body").append("<script type=\"text/javascript\" src=\"http://maps.google.com/maps/api/js?sensor=false&hl='.$modx_lang_attribute.'&callback=ddgminitialize\">");
  91. });
  92. ';
  93. }
  94. $output .= "// -------------- mm_ddGMap :: End ------------- \n";
  95. $e->output($output . "\n"); // Send the output to the browser
  96. } // end if
  97. }
  98. ?>