[ Index ]

MailPress 7.0.1

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

title

Body

[close]

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

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


Generated: Mon Jan 28 00:07:10 2019 Cross-referenced by PHPXref 0.7.1