Posted By

rccc on 03/02/10


Tagged

widget symfony


Versions (?)

sfWidgetFormGeocoder / sfValidatorGeocoder


 / Published in: PHP
 

i use this link : http://www.symfony-project.org/more-with-symfony/1_4/fr/05-Custom-Widgets-and-Validators , but this one is lighest and don't require jquery

  1. class sfWidgetFormGeocoder extends sfWidgetForm
  2. {
  3. public function configure($options = array(), $attributes = array())
  4. {
  5. $this->addOption('address.options', array('style' => 'width:400px'));
  6.  
  7. $this->setOption('default', array(
  8. 'address' => '',
  9. 'lon' => '1.913899',
  10. 'lat' => '47.900486'
  11. ));
  12.  
  13. $this->addOption('div.class', 'geocoder-widget');
  14. $this->addOption('map.height', '300px');
  15. $this->addOption('map.width', '500px');
  16. $this->addOption('map.style', "");
  17. $this->addOption('lookup.name', "");
  18.  
  19. $this->addOption('template.html', '
  20. <div id="{div.id}" class="{div.class}">
  21. {input.search} <input type="submit" value="{input.lookup.name}" id="{input.lookup.id}" /> <br />
  22. {input.lon}
  23. {input.lat}
  24. <div id="{map.id}" style="width:{map.width};height:{map.height};{map.style}"></div>
  25. </div>
  26. ');
  27.  
  28. $this->addOption('template.javascript', '
  29. <script type="text/javascript">
  30.  
  31. (function() {
  32.  
  33. var $submit = document.getElementById("{input.lookup.id}");
  34. var $address = document.getElementById("{input.address.id}");
  35. var $lat = document.getElementById("{input.lat.id}");
  36. var $lon = document.getElementById("{input.lon.id}");
  37.  
  38. var map = new GMap2(document.getElementById("{map.id}"));
  39. var center = new GLatLng( $lat.value||47.900486, $lon.value||1.913899);
  40. var marker = new GMarker(center, {draggable: true});
  41. var geocoder = new GClientGeocoder();
  42.  
  43. map.addControl(new GSmallMapControl());
  44. map.addControl(new GMapTypeControl());
  45. map.setCenter(center, 5);
  46. map.addOverlay(marker);
  47. setPosition(center);
  48.  
  49. GEvent.addListener(marker, "dragend", function() {
  50. var point = marker.getPoint();
  51. map.panTo(point);
  52. setPosition(point);
  53. });
  54.  
  55. GEvent.addListener(map, "moveend", function() {
  56. var point = map.getCenter()
  57. marker.setLatLng(point);
  58. setPosition(point);
  59. });
  60.  
  61. $submit.onclick = function(){showAddress($address.value)};
  62.  
  63. function showAddress() {
  64. if(!arguments[0]) return false;
  65. var address = arguments[0];
  66. if (geocoder) {
  67. geocoder.getLatLng(address, function(point) {
  68. if (!point) {
  69. alert(address + " not found");
  70. }else{
  71. setPosition(point);
  72. map.setCenter(point, 14);
  73. }
  74. });
  75. }
  76. }
  77.  
  78. function setPosition(){
  79. $lat.value = arguments[0].lat();
  80. $lon.value = arguments[0].lng();
  81. }
  82.  
  83. })();
  84. </script>
  85. ');
  86. }
  87.  
  88. public function getJavascripts()
  89. {
  90. return array('');
  91. }
  92.  
  93. public function render($name, $value = null, $attributes = array(), $errors = array())
  94. {
  95. // define main template variables
  96. $template_vars = array(
  97. '{div.id}' => $this->generateId($name),
  98. '{div.class}' => $this->getOption('div.class'),
  99. '{map.id}' => $this->generateId($name.'[map]'),
  100. '{map.style}' => $this->getOption('map.style'),
  101. '{map.height}' => $this->getOption('map.height'),
  102. '{map.width}' => $this->getOption('map.width'),
  103. '{input.lookup.id}' => $this->generateId($name.'[lookup]'),
  104. '{input.lookup.name}' => $this->getOption('lookup.name'),
  105. '{input.address.id}' => $this->generateId($name.'[address]'),
  106. '{input.lat.id}' => $this->generateId($name.'[lat]'),
  107. '{input.lon.id}' => $this->generateId($name.'[lon]'),
  108. );
  109.  
  110. // v���©rifie si la valeur est valide
  111. $value = !is_array($value) ? array() : $value;
  112. $value['address'] = isset($value['address']) ? $value['address'] : '';
  113. $value['lon'] = isset($value['long']) ? $value['lon'] : '';
  114. $value['lat'] = isset($value['lat']) ? $value['lat'] : '';
  115.  
  116. // d���©finit le widget pour le champ adresse
  117. $address = new sfWidgetFormInputText(array(), $this->getOption('address.options'));
  118. $template_vars['{input.search}'] = $address->render($name.'[address]', $value['address']);
  119.  
  120. // d���©finit les widgets pour les champs : longitude et latitude
  121. $hidden = new sfWidgetFormInputText;
  122. $template_vars['{input.lon}'] = $hidden->render($name.'[lon', $value['lon']);
  123. $template_vars['{input.lat}'] = $hidden->render($name.'[lat]', $value['lat']);
  124.  
  125. // assemble le mod���¨le avec les valeurs
  126. return strtr(
  127. $this->getOption('template.html').$this->getOption('template.javascript'),
  128. $template_vars
  129. );
  130. }
  131. }
  132.  
  133.  
  134. class sfValidatorGeocoder extends sfValidatorBase
  135. {
  136. protected function doClean($value)
  137. {
  138. if (!is_array($value))
  139. {
  140. throw new sfValidatorError($this, 'invalid');
  141. }
  142.  
  143. try
  144. {
  145. $latitude = new sfValidatorNumber(array( 'min' => -90, 'max' => 90, 'required' => true ));
  146. $value['lat'] = $latitude->clean(isset($value['lat']) ? $value['lat'] : null);
  147.  
  148. $longitude = new sfValidatorNumber(array( 'min' => -180, 'max' => 180, 'required' => true ));
  149. $value['lon'] = $longitude->clean(isset($value['lon]) ? $value['lon'] : null);
  150.  
  151. $address = new sfValidatorString(array( 'min_length' => 10, 'max_length' => 255, 'required' => true ));
  152. $value['address'] = $address->clean(isset($value['address']) ? $value['address'] : null);
  153. }
  154. catch(sfValidatorError $e)
  155. {
  156. throw new sfValidatorError($this, 'invalid');
  157. }
  158.  
  159. return $value;
  160. }
  161. }

Report this snippet  

You need to login to post a comment.