function init($conf)
 {
     $this->conf = $conf;
     $this->pi_setPiVarDefaults();
     $this->pi_loadLL();
     $this->pi_initPIflexForm();
     // Init FlexForm configuration for plugin
     $this->hooks = array();
     if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['ods_osm']['class.tx_odsosm_pi1.php'])) {
         foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['ods_osm']['class.tx_odsosm_pi1.php'] as $classRef) {
             $this->hooks[] =& t3lib_div::getUserObj($classRef);
         }
     }
     /* --------------------------------------------------
     			Configuration (order of priority)
     			- FlexForm
     			- TypoScript
     			- Extension
     		-------------------------------------------------- */
     $flex = array();
     $options = array('height', 'lat', 'layer', 'leaflet_layer', 'library', 'lon', 'marker', 'marker_popup_initial', 'mouse_navigation', 'openlayers_layer', 'openlayers3_layer', 'position', 'show_layerswitcher', 'show_scalebar', 'show_pan_zoom', 'show_popups', 'static_layer', 'use_coords_only_nomarker', 'width', 'zoom');
     foreach ($options as $option) {
         $value = $this->pi_getFFvalue($this->cObj->data['pi_flexform'], $option, 'sDEF');
         if ($value) {
             switch ($option) {
                 case 'marker':
                 case 'marker_popup_initial':
                     $flex[$option] = $this->splitGroup($value, 'tt_address');
                     break;
                 default:
                     $flex[$option] = $value;
                     break;
             }
         }
     }
     if ($flex['library']) {
         $flex['layer'] = $flex[$flex['library'] . '_layer'];
     }
     $this->config = array_merge(tx_odsosm_div::getConfig(), $conf, $flex);
     if (!is_array($this->config['marker'])) {
         $this->config['marker'] = array();
     }
     if (is_array($conf['marker.'])) {
         foreach ($conf['marker.'] as $name => $value) {
             if (!empty($value)) {
                 if (!is_array($this->config['marker'][$name])) {
                     $this->config['marker'][$name] = array();
                 }
                 $this->config['marker'][$name] = $this->config['marker'][$name] + explode(',', $value);
             }
         }
     }
     $this->config['layer'] = explode(',', $this->config['layer']);
     if (is_numeric($this->config['height'])) {
         $this->config['height'] .= 'px';
     }
     if (is_numeric($this->config['width'])) {
         $this->config['width'] .= 'px';
     }
     if ($this->config['show_layerswitcher']) {
         $this->config['layers_visible'] = array();
     } else {
         $this->config['layers_visible'] = $this->config['layer'];
     }
     if ($this->config['external_control']) {
         if (t3lib_div::_GP('lon')) {
             $this->config['lon'] = t3lib_div::_GP('lon');
         }
         if (t3lib_div::_GP('lat')) {
             $this->config['lat'] = t3lib_div::_GP('lat');
         }
         if (t3lib_div::_GP('zoom')) {
             $this->config['zoom'] = t3lib_div::_GP('zoom');
         }
         if (t3lib_div::_GP('layers')) {
             $this->config['layers_visible'] = explode(',', t3lib_div::_GP('layers'));
         }
         if (t3lib_div::_GP('records')) {
             $this->config['marker'] = $this->splitGroup(t3lib_div::_GP('records'), 'tt_address');
         }
     }
     $this->config['id'] = 'osm_' . $this->cObj->data['uid'];
     $this->config['marker'] = $this->extractGroup($this->config['marker']);
     // Show this marker's popup intially
     if (is_array($this->config['marker_popup_initial'])) {
         foreach ($this->config['marker_popup_initial'] as $table => $records) {
             foreach ($records as $uid) {
                 if (isset($this->config['marker'][$table][$uid])) {
                     $this->config['marker'][$table][$uid]['initial_popup'] = true;
                 }
             }
         }
     }
     // Library
     if (empty($this->config['library'])) {
         $this->config['library'] = 'leaflet';
     }
     require_once t3lib_extMgm::extPath('ods_osm') . 'class.tx_odsosm_' . $this->config['library'] . '.php';
     $this->library = t3lib_div::makeInstance('tx_odsosm_' . $this->config['library']);
     $this->library->init($this->config);
     $this->library->cObj = $this->cObj;
     // Get marker records
     $this->library->markers = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'tx_odsosm_marker', '1' . $this->cObj->enableFields('tx_odsosm_marker'), '', '', '', 'uid');
 }
 function processDatamap_postProcessFieldArray($status, $table, $id, &$fieldArray, $obj)
 {
     switch ($table) {
         case 'fe_users':
         case 'tt_address':
             $config = tx_odsosm_div::getConfig(array('autocomplete'));
             // Search coordinates
             if ($config['autocomplete'] && ($fieldArray['zip'] || $fieldArray['city'] || $fieldArray['address'])) {
                 $address = $obj->datamap[$table][$id];
                 if ($config['autocomplete'] == 2 || floatval($address['tx_odsosm_lon']) == 0) {
                     $ll = tx_odsosm_div::updateAddress($address);
                     if ($ll) {
                         $fieldArray['tx_odsosm_lon'] = sprintf('%01.6f', $address['lon']);
                         $fieldArray['tx_odsosm_lat'] = sprintf('%01.6f', $address['lat']);
                         if ($address['street']) {
                             $fieldArray['address'] = $address['street'];
                             if ($address['housenumber']) {
                                 $fieldArray['address'] .= ' ' . $address['housenumber'];
                             }
                         }
                         if ($address['zip']) {
                             $fieldArray['zip'] = $address['zip'];
                         }
                         if ($address['city']) {
                             $fieldArray['city'] = $address['city'];
                         }
                         if ($address['state'] && $table == 'tt_address') {
                             $fieldArray['region'] = $address['state'];
                         }
                         if ($address['country']) {
                             $fieldArray['country'] = $address['country'];
                         }
                     }
                 }
             }
             break;
         case 'tx_odsosm_track':
             $filename = PATH_site . 'uploads/tx_odsosm/' . $fieldArray['file'];
             if ($fieldArray['file'] && file_exists($filename)) {
                 require_once t3lib_extMgm::extPath('ods_osm', 'res/geoPHP/geoPHP.inc');
                 $polygon = geoPHP::load(file_get_contents($filename), pathinfo($filename, PATHINFO_EXTENSION));
                 $box = $polygon->getBBox();
                 $fieldArray['min_lon'] = sprintf('%01.6f', $box['minx']);
                 $fieldArray['min_lat'] = sprintf('%01.6f', $box['miny']);
                 $fieldArray['max_lon'] = sprintf('%01.6f', $box['maxx']);
                 $fieldArray['max_lat'] = sprintf('%01.6f', $box['maxy']);
             }
             break;
         case 'tx_odsosm_marker':
             if ($fieldArray['icon'] && file_exists(PATH_site . 'uploads/tx_odsosm/' . $fieldArray['icon'])) {
                 $size = getimagesize(PATH_site . 'uploads/tx_odsosm/' . $fieldArray['icon']);
                 $fieldArray['size_x'] = $size[0];
                 $fieldArray['size_y'] = $size[1];
                 $fieldArray['offset_x'] = -round($size[0] / 2);
                 $fieldArray['offset_y'] = -$size[1];
             }
             break;
         case 'tx_odsosm_vector':
             if ($fieldArray['data']) {
                 $this->lon = array();
                 $this->lat = array();
                 $vector = json_decode($fieldArray['data']);
                 foreach ($vector->geometry->coordinates[0] as $coordinates) {
                     $this->lon[] = $coordinates[0];
                     $this->lat[] = $coordinates[1];
                 }
             }
             $fieldArray['min_lon'] = sprintf('%01.6f', min($this->lon));
             $fieldArray['min_lat'] = sprintf('%01.6f', min($this->lat));
             $fieldArray['max_lon'] = sprintf('%01.6f', max($this->lon));
             $fieldArray['max_lat'] = sprintf('%01.6f', max($this->lat));
             break;
     }
 }
 /**
  * Search for the given address in one of the geocoding services and update
  * its data.
  *
  * Data lat, lon, zip and city may get updated.
  *
  * @param array   &$address Address record from database
  * @param integer $service  Geocoding service to use
  *						  - 0: internal caching database table
  *						  - 1: geonames.org
  *						  - 2: nominatim.openstreetmap.org
  *
  * @return boolean True if the address got updated, false if not.
  */
 function searchAddress(&$address, $service = 0)
 {
     $config = tx_odsosm_div::getConfig(array('default_country', 'geo_service_email', 'geo_service_user'));
     $ll = false;
     $country = strtoupper(strlen($address['country']) == 2 ? $address['country'] : $config['default_country']);
     $email = t3lib_div::validEmail($config['geo_service_email']) ? $config['geo_service_email'] : $_SERVER['SERVER_ADMIN'];
     if (TYPO3_DLOG) {
         $service_names = array(0 => 'cache', 1 => 'geonames', 2 => 'nominatim');
         t3lib_div::devLog('Search address using ' . $service_names[$service], 'ods_osm', 0, $address);
     }
     switch ($service) {
         case 0:
             // cache
             $where = array();
             if ($country) {
                 $where[] = 'country=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($country, 'tx_odsosm_geocache');
             }
             if ($address['city']) {
                 $where[] = '(city=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($address['city'], 'tx_odsosm_geocache') . ' OR search_city=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($address['city'], 'tx_odsosm_geocache') . ')';
             }
             if ($address['zip']) {
                 $where[] = 'zip=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($address['zip'], 'tx_odsosm_geocache');
             }
             if ($address['street']) {
                 $where[] = 'street=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($address['street'], 'tx_odsosm_geocache');
             }
             if ($address['housenumber']) {
                 $where[] = 'housenumber=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($address['housenumber'], 'tx_odsosm_geocache');
             }
             if ($where) {
                 $where[] = 'deleted=0';
                 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'tx_odsosm_geocache', implode(' AND ', $where));
                 $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
                 if ($row) {
                     $ll = true;
                     $set = array('tstamp' => time(), 'cache_hit' => $row['cache_hit'] + 1);
                     $GLOBALS['TYPO3_DB']->exec_UPDATEquery('tx_odsosm_geocache', 'uid=' . $row['uid'], $set);
                     $address['lat'] = $row['lat'];
                     $address['lon'] = $row['lon'];
                     if ($row['zip']) {
                         $address['zip'] = $row['zip'];
                     }
                     if ($row['city']) {
                         $address['city'] = $row['city'];
                     }
                     if ($row['state']) {
                         $address['state'] = $row['state'];
                     }
                     if (empty($address['country'])) {
                         $address['country'] = $row['country'];
                     }
                 }
             }
             break;
         case 1:
             // http://www.geonames.org/
             if ($country) {
                 $query['country'] = $country;
             }
             if ($address['city']) {
                 $query['placename'] = $address['city'];
             }
             if ($address['zip']) {
                 $query['postalcode'] = $address['zip'];
             }
             if ($query) {
                 $query['maxRows'] = 1;
                 $query['username'] = $config['geo_service_user'];
                 $xml = t3lib_div::getURL('http://api.geonames.org/postalCodeSearch?' . http_build_query($query, '', '&'), false, 'User-Agent: TYPO3 extension ods_osm/' . t3lib_extMgm::getExtensionVersion('ods_osm'));
                 if (TYPO3_DLOG && $xml === false) {
                     t3lib_div::devLog('t3lib_div::getURL failed', "ods_osm", 3);
                 }
                 if ($xml) {
                     $xmlobj = new SimpleXMLElement($xml);
                     if ($xmlobj->status) {
                         if (TYPO3_DLOG) {
                             t3lib_div::devLog('GeoNames message', 'ods_osm', 2, (array) $xmlobj->status->attributes());
                         }
                         $o_flashMessage = t3lib_div::makeInstance('t3lib_FlashMessage', (string) $xmlobj->status->attributes()->message, 'GeoNames message', t3lib_FlashMessage::WARNING);
                         t3lib_FlashMessageQueue::addMessage($o_flashMessage);
                     }
                     if ($xmlobj->code) {
                         $ll = true;
                         $address['lat'] = (string) $xmlobj->code->lat;
                         $address['lon'] = (string) $xmlobj->code->lng;
                         if ($xmlobj->code->postalcode) {
                             $address['zip'] = (string) $xmlobj->code->postalcode;
                         }
                         if ($xmlobj->code->name) {
                             $address['city'] = (string) $xmlobj->code->name;
                         }
                         if (empty($address['country'])) {
                             $address['country'] = (string) $xmlobj->code->countryCode;
                         }
                     }
                 }
             }
             break;
         case 2:
             // http://nominatim.openstreetmap.org/
             $query['country'] = $country;
             $query['email'] = $email;
             $query['addressdetails'] = 1;
             $query['format'] = 'xml';
             if ($this->address_type == 'structured') {
                 if ($address['city']) {
                     $query['city'] = $address['city'];
                 }
                 if ($address['zip']) {
                     $query['postalcode'] = $address['zip'];
                 }
                 if ($address['street']) {
                     $query['street'] = $address['street'];
                 }
                 if ($address['housenumber']) {
                     $query['street'] = $address['housenumber'] . ' ' . $query['street'];
                 }
                 if (TYPO3_DLOG) {
                     t3lib_div::devLog('Nominatim structured', 'ods_osm', -1, $query);
                 }
                 $ll = tx_odsosm_div::searchAddressNominatim($query, $address);
                 if (!$ll && $query['postalcode']) {
                     unset($query['postalcode']);
                     if (TYPO3_DLOG) {
                         t3lib_div::devLog('Nominatim retrying without zip', 'ods_osm', -1, $query);
                     }
                     $ll = tx_odsosm_div::searchAddressNominatim($query, $address);
                 }
             }
             if ($this->address_type == 'unstructured') {
                 $query['q'] = $address['address'];
                 if (TYPO3_DLOG) {
                     t3lib_div::devLog('Nominatim unstructured', 'ods_osm', -1, $query);
                 }
                 $ll = tx_odsosm_div::searchAddressNominatim($query, $address);
             }
             break;
     }
     if (TYPO3_DLOG) {
         if ($ll) {
             t3lib_div::devLog('Return address', 'ods_osm', 0, $address);
         } else {
             t3lib_div::devLog('No address found', 'ods_osm', 0);
         }
     }
     return $ll;
 }