PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/assets/plugins/managermanager/widgets/ddymap/ddymap.php

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