form.html.twig 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. {% extends 'SonataAdminBundle:CRUD:base_edit.html.twig' %}
  2. {% block formactions %}
  3. {{ parent() }}
  4. <script type="text/javascript">
  5. var modificacion = false;
  6. $(function () {
  7. // buscar la direccion del cliente y la muestra en el mapa.
  8. var objSelectClient = $('input:hidden[id$="clientId"]');
  9. if (objSelectClient.val() != "" && parseInt(objSelectClient.val()) > 0) {
  10. modificacion = true;
  11. callbackClientId(objSelectClient.val());
  12. } else {
  13. if (_MAP_LATITUDE == 0 && _MAP_LONGITUDE == 0) {
  14. setCoordinatesFromConfig(true);
  15. } else {
  16. drawMap(_MAP_LATITUDE, _MAP_LONGITUDE);
  17. }
  18. $("div[id*='_nap']").find(".select2-chosen").html("");
  19. $("select[id*='_nap']").val(0);
  20. }
  21. });
  22. function callbackClientId(clientId) {
  23. $.ajax({
  24. url: '{{ path('ajax_client_data') }}',
  25. dataType: 'json',
  26. delay: 250,
  27. data: {
  28. q: clientId
  29. }
  30. }).done(function (data) {
  31. var extradata = null;
  32. if (data.results[0].location) {
  33. try {
  34. extradata = JSON.parse(data.results[0].location.extraData);
  35. if (extradata.lat == undefined || extradata.lng == undefined) {
  36. extradata = null;
  37. }
  38. } catch (ignore) {
  39. }
  40. }
  41. if (extradata === null) {
  42. googleSearchDirectionAndShowMaps(data.results[0].address);
  43. } else {
  44. $('input[class="search-input"]').val(data.results[0].address);
  45. drawMap(extradata.lat, extradata.lng);
  46. }
  47. //calcularDistanciaNap();
  48. });
  49. }
  50. /**
  51. * funcion que busca una direccion y la muestra en el mapa.
  52. * @param address Contiene la direccion.
  53. */
  54. function googleSearchDirectionAndShowMaps(address) {
  55. $('input[class="search-input"]').val(address);
  56. $.ajax({
  57. url: "http://maps.googleapis.com/maps/api/geocode/json?address='" + address + "'",
  58. type: "POST"
  59. }).done(function (res) {
  60. if (res != undefined && res.status == google.maps.GeocoderStatus.OK) {
  61. drawMap(res.results[0].geometry.location.lat, res.results[0].geometry.location.lng);
  62. } else {
  63. // direccion no encontrada por google
  64. var msg = "{{ 'error.address_not_found'|trans({}, 'FTTHBundle') }}";
  65. showError(msg.replace('%client_address%', address));
  66. }
  67. }).error(function (res) {
  68. // direccion no encontrada por google, esto se puede deber a la cantidad de consultas que se
  69. // realizan al webservice de google
  70. });
  71. }
  72. /**
  73. * Funcion que dibuja el mapa.
  74. * @param lat
  75. * @param lng
  76. */
  77. function drawMap(lat, lng) {
  78. loc = new L.latLng(lat, lng);
  79. drawMarker();
  80. setDataValue();
  81. }
  82. /**
  83. * Busca y calcula la distancia a los nap y la cantidad de puertos libres.
  84. */
  85. function calcularDistanciaNap() {
  86. var origin = null;
  87. try {
  88. origin = JSON.parse($("input[id*='_location_extraData']").attr('value'));
  89. } catch (ignore) {
  90. }
  91. if (origin != null) {
  92. $.ajax({
  93. url: '{{ path('ajax_distance_nap_onu') }}',
  94. dataType: 'json',
  95. type: "GET",
  96. data: {
  97. lat: origin.lat,
  98. lng: origin.lng,
  99. napId: modificacion ? $("select[id*='_nap']").val() : 0
  100. }
  101. }).done(function (res) {
  102. var select = $("select[id*='_nap']");
  103. var span = $("div[id*='_nap']").find(".select2-chosen");
  104. var codigoNap = select.val(), nap = "";
  105. span.html("");
  106. select.find("option").remove().end();
  107. $.each(res, function (i, obj) {
  108. obj = JSON.parse(obj);
  109. for (var i = 0; i < obj.length; i++) {
  110. if (obj[i].distance != -1) {
  111. var tmp = obj[i].name +
  112. " ({{ 'Free Port'|trans({}, 'FTTHBundle') }}: " + obj[i].freePort + " - " +
  113. "OLT: " + obj[i].olt + " - " +
  114. "Slot: " + obj[i].slot + " - " +
  115. "Link: " + obj[i].link + " - " +
  116. "{{ 'Distance'|trans({}, 'FTTHBundle') }}: " + obj[i].distance + " KM. - " +
  117. "{{ 'Address'|trans({}, 'FTTHBundle') }}: " + obj[i].address + " )";
  118. select.append(
  119. '<option value="' + obj[i].id + '" >' + tmp +
  120. '</option>'
  121. );
  122. if (codigoNap == obj[i].id) {
  123. nap = tmp;
  124. }
  125. }
  126. }
  127. for (var i = 0; i < obj.length; i++) {
  128. if (obj[i].distance == -1) {
  129. var tmp = obj[i].name + " ({{ 'Free Port'|trans({}, 'FTTHBundle') }}: " + obj[i].freePort + " - ";
  130. tmp +=
  131. "OLT: " + obj[i].olt + " - " +
  132. "Slot: " + obj[i].slot + " - " +
  133. "Link: " + obj[i].link + " - ";
  134. tmp += "{{ 'Distance'|trans({}, 'FTTHBundle') }}: " + obj[i].distanceMessage;
  135. if (obj[i].address != null && obj[i].address.length > 0) {
  136. tmp += " - {{ 'Address'|trans({}, 'FTTHBundle') }}: " + obj[i].address;
  137. }
  138. tmp += ")";
  139. select.append(
  140. '<option value="' + obj[i].id + '" >' + tmp +
  141. '</option>'
  142. );
  143. if (codigoNap == obj[i].id) {
  144. nap = tmp;
  145. }
  146. }
  147. }
  148. });
  149. span.html(nap);
  150. select.val(codigoNap);
  151. });
  152. }
  153. }
  154. </script>
  155. {% endblock %}