[ Index ]

MailPress 544

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/class/options/form/field_types/geotag/ -> geotag.php (source)

   1  <?php
   2  class MP_Form_field_type_geotag extends MP_form_field_type_
   3  {
   4      var $file    = __FILE__;
   5  
   6      var $id    = 'geotag';
   7  
   8      var $category = 'composite';
   9      var $order    = 96;
  10  
  11  	function get_name($field) { return (isset($field->settings['options']['is'])) ? $this->prefix . '[' . $field->form_id . ']['. $field->id . '][' . $field->settings['options']['is'] . ']' : $this->prefix . '[' . $field->form_id . ']['. $field->id . ']'; }
  12  	function get_id($field)   { return (isset($field->settings['options']['is'])) ? $this->prefix .       $field->form_id .  '_'. $field->id .  '_' . $field->settings['options']['is']       : $this->prefix .       $field->form_id .  '_'. $field->id; }
  13  
  14  	function submitted($field)
  15      {
  16          $options = $field->settings['googlemap'];
  17  
  18          $value    = $_POST[$this->prefix][$field->form_id][$field->id];
  19  
  20          $required     = (isset($field->settings['controls']['required']) && $field->settings['controls']['required']);
  21          $empty     = ( empty($value['lat']) || empty($value['lng']) );
  22          $geotag_ok     = true;
  23  
  24          if ($required && $empty)
  25          {
  26              $field->submitted['on_error'] = 1;
  27              return $field;
  28          }
  29  
  30          if (isset($options['lat_lng']) && !isset($options['lat_lng_disabled'])) if (($value['lat'] != (string)(float)$value['lat']) || ($value['lng'] != (string)(float)$value['lng'])) $geotag_ok = false;
  31  
  32          if (!$geotag_ok)
  33          {
  34              $field->submitted['on_error'] = 2;
  35              return $field;
  36          }
  37  
  38          $value['reverse_geocoding'] = MP_Ip::get_address($value['lat'], $value['lng']);
  39  
  40          $width  = (float) $field->settings['googlemap']['width'];
  41          $height = (float) $field->settings['googlemap']['height'];
  42          if ($width  > 640) $width  = 640;
  43          if ($height > 640) $height = 640;
  44          $static_map  = 'http://maps.googleapis.com/maps/api/staticmap?';
  45          $static_map .= 'center=' . $value['center_lat'] . ',' . $value['center_lng'];
  46          $static_map .= '&zoom=' . $value['zoomlevel'];
  47          $static_map .= "&size=$width" . 'x' . $height;
  48          $static_map .= '&maptype='; 
  49          switch ($value['maptype']) { case 'SATELLITE' : $static_map .= 'satellite'; break; case 'HYBRID' : $static_map .= 'hybrid'; break; case 'PHYSICAL' : $static_map .= 'terrain'; break; case 'TERRAIN' : $static_map .= 'terrain'; break; default : $static_map .= 'roadmap'; break; }
  50          $static_map .= '&markers=' . $value['lat'] . ',' . $value['lng'];
  51          $static_map .= '&sensor=false';
  52  
  53          $field->submitted['value'] = $value;
  54          $field->submitted['text']  = '';
  55          $field->submitted['text']  = 'lat : ' . $value['lat'] . ' lng : ' . $value['lng'] . "<br />\n\r";
  56          $field->submitted['text'] .= __('Reverse geocoding :', MP_TXTDOM) . ' ' . ( (!empty($value['reverse_geocoding'])) ? $value['reverse_geocoding'] : '<small>[<i>' . __('empty', MP_TXTDOM) . '</i>]</small>' ) . "<br />\n\r";
  57          if (isset($value['geocode'])) $field->submitted['text'] .= ( (!empty($value['geocode'])) ? 'geocode : ' . $value['geocode'] : '<small>[<i>' . __('empty', MP_TXTDOM) . '</i>]</small>' ) . "<br />\n\r";
  58          $field->submitted['map']   = $static_map;
  59  
  60          return $field;
  61      }
  62  
  63  	function attributes_filter($no_reset)
  64      {
  65          $ip = $_SERVER['REMOTE_ADDR'];
  66  
  67          $xlatlng = MP_Ip::get_latlng($ip);
  68          $options = $this->field->settings['googlemap'];
  69  
  70          $options['lat'] = $options['center_lat'] = ($xlatlng) ? (float) trim($xlatlng['lat']) : (float) trim($options['lat']);
  71          $options['lng'] = $options['center_lng'] = ($xlatlng) ? (float) trim($xlatlng['lng']) : (float) trim($options['lng']);
  72  
  73          if (isset($options['geocode'])) $options['geocode_value'] = MP_Ip::get_address($options['lat'], $options['lng']);
  74          $this->field->settings['googlemap'] = $options;
  75  
  76          if (!$no_reset) return;
  77  
  78          $Post = $_POST[$this->prefix][$this->field->form_id][$this->field->id];
  79          $options['lat']         = (float) $Post['lat'];
  80          $options['lng']         = (float) $Post['lng'];
  81          $options['center_lat']     = (float) $Post['center_lat'];
  82          $options['center_lng']     = (float) $Post['center_lng'];
  83          $options['zoomlevel']     = $Post['zoomlevel'];
  84          $options['maptype']     = $Post['maptype'];
  85  
  86          if (isset($options['geocode'])) $options['geocode_value'] =  esc_attr($_POST[$this->prefix][$this->field->form_id][$this->field->id]['geocode']);
  87          
  88          $this->field->settings['googlemap'] = $options;
  89  
  90          $this->attributes_filter_css();
  91      }
  92  
  93  	function build_tag()
  94      {
  95          $options = $this->field->settings['googlemap'];
  96      //map
  97          $this->field->settings['options']['is'] = 'map';
  98          $id_map    = $this->get_id($this->field);
  99          $style    = " style='overflow:hidden;width:" . trim($options['width']) . ';height:' . trim($options['height']) . ";'";
 100          $tag_map    = "<div id='$id_map'$style></div>";
 101          //zoomlevel
 102              $this->field->settings['attributes']['type'] = 'hidden';
 103  
 104              $this->field->settings['attributes']['value'] = $options['zoomlevel'];
 105              $this->field->settings['options']['is'] = 'zoomlevel';
 106              $tag_map     .= parent::build_tag();
 107          //maptype
 108              $this->field->settings['attributes']['value'] = $options['maptype'];
 109              $this->field->settings['options']['is'] = 'maptype';
 110              $tag_map     .= parent::build_tag();
 111          //center_lat
 112              $this->field->settings['attributes']['value'] = $options['center_lat'];
 113              $this->field->settings['options']['is'] = 'center_lat';
 114              $tag_map     .= parent::build_tag();
 115          //center_lng
 116              $this->field->settings['attributes']['value'] = $options['center_lng'];
 117              $this->field->settings['options']['is'] = 'center_lng';
 118              $tag_map     .= parent::build_tag();
 119  
 120      // lat, lng
 121          $tag_lat = $tag_lng = $id_lat_d = $id_lng_d = '';
 122          $this->field->type = 'text';
 123          if (isset($options['lat_lng']))
 124          {
 125              if (!isset($options['lat_lng_disabled']))
 126              {
 127                  // lat lng text
 128                  $this->field->settings['attributes']['type'] = 'text';
 129                  $this->field->settings['attributes']['size']  = $options['lat_lng_size'];
 130                  $this->field->settings['attributes']['value'] = $options['lat'];
 131  
 132                  $this->field->settings['options']['is'] = 'lat';
 133                  $id_lat    = $this->get_id($this->field);
 134                  $tag_lat    = parent::build_tag();
 135  
 136                  $this->field->settings['attributes']['value'] = $options['lng'];
 137  
 138                  $this->field->settings['options']['is'] = 'lng';
 139                  $id_lng    = $this->get_id($this->field);
 140                  $tag_lng    = parent::build_tag();
 141              }
 142              else
 143              {
 144                  // lat lng text             id et name differents
 145                  $this->field->settings['attributes']['type'] = 'text';
 146                  $this->field->settings['attributes']['size']  = $options['lat_lng_size'];
 147                  $this->field->settings['attributes']['value'] = $options['lat'];
 148                  $this->field->settings['attributes']['disabled'] = 'disabled';
 149  
 150                  $this->field->settings['options']['is'] = 'lat_d';
 151                  $id_lat_d    = $this->get_id($this->field);
 152                  $tag_lat    = parent::build_tag();
 153  
 154                  $this->field->settings['attributes']['value'] = $options['lng'];
 155  
 156                  $this->field->settings['options']['is'] = 'lng_d';
 157                  $id_lng_d    = $this->get_id($this->field);
 158                  $tag_lng    = parent::build_tag();
 159  
 160                  // lat lng hidden
 161                  unset($this->field->settings['attributes']['disabled'], $this->field->settings['attributes']['size']);
 162  
 163                  $this->field->settings['attributes']['type'] = 'hidden';
 164                  $this->field->settings['attributes']['value'] = $options['lat'];
 165  
 166                  $this->field->settings['options']['is'] = 'lat';
 167                  $id_lat    = $this->get_id($this->field);
 168                  $tag_map    .= parent::build_tag();
 169  
 170                  $this->field->settings['attributes']['value'] = $options['lng'];
 171  
 172                  $this->field->settings['options']['is'] = 'lng';
 173                  $id_lng    = $this->get_id($this->field);
 174                  $tag_map    .= parent::build_tag();
 175              }
 176          }
 177          else
 178          {
 179              // lat lng hidden
 180              unset($this->field->settings['attributes']['disabled'], $this->field->settings['attributes']['size']);
 181  
 182              $this->field->settings['attributes']['type'] = 'hidden';
 183              $this->field->settings['attributes']['value'] = $options['lat'];
 184  
 185              $this->field->settings['options']['is'] = 'lat';
 186              $id_lat    = $this->get_id($this->field);
 187              $tag_map    .= parent::build_tag();
 188  
 189              $this->field->settings['attributes']['value'] = $options['lng'];
 190  
 191              $this->field->settings['options']['is'] = 'lng';
 192              $id_lng    = $this->get_id($this->field);
 193              $tag_map    .= parent::build_tag();
 194          }
 195  
 196      // geocode
 197          $id_geocode = $tag_geocode = $id_geocode_button    = $tag_geocode_button = '';
 198          if (isset($options['geocode'])) 
 199          {
 200          // input text
 201              unset($this->field->settings['attributes']['disabled']);
 202  
 203              $this->field->settings['attributes']['type']  = 'text';
 204              $this->field->settings['attributes']['size']  = $options['geocode_size'];
 205              $this->field->settings['attributes']['value'] = (isset($options['geocode_value'])) ? $options['geocode_value'] : '';
 206  
 207              $this->field->settings['options']['is'] = 'geocode';
 208              $id_geocode        = $this->get_id($this->field);
 209              $tag_geocode    = parent::build_tag();
 210  
 211          // button
 212              $this->field->type = 'button';
 213              unset($this->field->settings['attributes']['size']);
 214  
 215              $this->field->settings['attributes']['type'] = 'button';
 216              $this->field->settings['attributes']['value'] = $options['geocode_button'];
 217  
 218              $this->field->settings['options']['is'] = 'geocode_button';
 219              $id_geocode_button    = $this->get_id($this->field);
 220              $tag_geocode_button    = parent::build_tag();
 221          }
 222  
 223      // javascript
 224          $js = '';
 225          if (!defined('MP_FORM_GEOTAG'))
 226          {
 227              define ('MP_FORM_GEOTAG', true);
 228              if (!isset($options['gmap']))   $js .= "\n<script type='text/javascript' src='http://maps.googleapis.com/maps/api/js?sensor=false'></script>";
 229              if (!isset($options['jQuery'])) $js .= "\n<script type='text/javascript' src='" . site_url() . "/wp-includes/js/jquery/jquery.js'></script>";
 230  
 231              $m = array( 'mp_gmapL10n'    => array(    'url'        => site_url() . '/' . MP_PATH . 'mp-admin/images/', 
 232                                          'center'    => esc_js(__('Center on marker', MP_TXTDOM)), 
 233                                          'rgeocode'    => esc_js(__('Find marker address', MP_TXTDOM)), 
 234                                          'changemap'    => esc_js(__('Change map', MP_TXTDOM))
 235                                  )
 236                  );
 237              $js .= "\n<script type='text/javascript'>\n/* <![CDATA[ */\n";
 238              foreach ( $m as $var => $val ) $js .= "var $var = " . MP_::print_scripts_l10n_val($val);
 239              $js .= ";\n/* ]]> */\n</script>";
 240              $js .= "\n<script type='text/javascript' src='" . site_url() . '/' . MP_PATH . "mp-includes/js/mp_field_type_geotag.js'></script>\n";
 241          }
 242  
 243          $x = array();
 244          $x['form'] = $this->field->form_id; $x['field'] = $this->field->id;
 245          foreach (array('lat', 'lng', 'center_lat', 'center_lng', 'maptype', 'zoomlevel', 'zoom', 'changemap', 'center', 'lat_lng', 'lat_lng_disabled', 'rgeocode') as $opt) $x[$opt] = (isset($options[$opt])) ? $options[$opt] : '0';
 246          $m = array( 'mp_field_type_geotag_' . $this->field->form_id .  '_'. $this->field->id => $x );
 247          $js .= "\n<script type='text/javascript'>\n/* <![CDATA[ */\n";
 248          foreach ( $m as $var => $val ) $js .= "var $var = " . MP_::print_scripts_l10n_val($val);
 249          $js .= ";\njQuery(document).ready( function() { var mp_form_" . $this->field->form_id . '_' . $this->field->id  . " = new mp_field_type_geotag(mp_field_type_geotag_" . $this->field->form_id .  '_'. $this->field->id . "); } );\n/* ]]> */\n</script>";
 250  
 251      //end
 252          $this->field->type = $this->id;
 253  
 254          $sf  = '';
 255          $sf  = (isset($options['lat_lng'])) ? 'latlng' : '';
 256          $sf .= (isset($options['geocode'])) ? ( (empty($sf)) ? 'geocode' : '_geocode' ) : '';
 257          if (empty($sf)) $sf = 'alone';
 258  
 259          $form_formats['alone']        =  '{{map}}';
 260          $form_formats['latlng']        =  '{{map}}lat:{{lat}}&#160;lng:{{lng}}';
 261          $form_formats['geocode']    =  '{{map}}{{geocode}}&#160;{{geocode_button}}';
 262          $form_formats['latlng_geocode']=  '{{map}}lat:{{lat}}&#160;lng:{{lng}}<br />{{geocode}}&#160;{{geocode_button}}';
 263  
 264          $form_formats = $this->get_formats($form_formats);
 265  
 266          $search[] = '{{map}}';            $replace[] = '%1$s';
 267          $search[] = '{{id_map}}';        $replace[] = '%2$s';
 268          $search[] = '{{lat}}';             $replace[] = '%3$s';
 269          $search[] = '{{id_lat}}';         $replace[] = '%4$s';
 270          $search[] = '{{lng}}';             $replace[] = '%5$s';
 271          $search[] = '{{id_lng}}';        $replace[] = '%6$s';
 272          $search[] = '{{geocode}}';        $replace[] = '%7$s';
 273          $search[] = '{{id_geocode}}';        $replace[] = '%8$s';
 274          $search[] = '{{geocode_button}}';    $replace[] = '%9$s';
 275          $search[] = '{{id_geocode_button}}';$replace[] = '%10$s';
 276             $search[] = '{{id_lat_dis}}';        $replace[] = '%11$s';
 277          $search[] = '{{id_lng_dis}}';        $replace[] = '%12$s';
 278  
 279          $html = str_replace($search, $replace, $form_formats[$sf]);
 280          return sprintf($html, $tag_map, $id_map, $tag_lat, $id_lat, $tag_lng, $id_lng, $tag_geocode, $id_geocode, $tag_geocode_button, $id_geocode_button, $id_lat_d, $id_lng_d) . "\n$js\n";
 281      }
 282  }
 283  new MP_Form_field_type_geotag(__('Geotag', MP_TXTDOM));


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1