Geo_Location::FindLocation PHP Method

FindLocation() public static method

Looks whether the location is llready at the database NOTE: the 'location' ('center') parameters should be array with points (a point) with lat/lon values
public static FindLocation ( array $p_location, string $p_type, integer $p_style, array $p_center, integer $p_radius ) : integer
$p_location array
$p_type string
$p_style integer
$p_center array
$p_radius integer
return integer
    public static function FindLocation($p_location, $p_type, $p_style, $p_center, $p_radius)
    {
        global $g_ado_db;
        if ('point' != $p_type) {
            return null;
        }
        $queryStr_point = 'SELECT id FROM ' . self::TABLE . ' WHERE poi_location = GeomFromText(\'POINT(%%poi_lat%% %%poi_lon%%)\') AND poi_type = "point" ';
        $queryStr_point .= "AND poi_type_style = ? AND poi_center = PointFromText('POINT(%%cen_lat%% %%cen_lon%%)') AND poi_radius = ?";
        $loc_id = 0;
        // here checking for points; nothing else yet
        if ('point' == $p_type) {
            try {
                $loc_latitude = '' . $p_location[0]['latitude'];
                $loc_longitude = '' . $p_location[0]['longitude'];
                $cen_latitude = '' . $p_center['latitude'];
                $cen_longitude = '' . $p_center['longitude'];
                $correct_coords = true;
                if (!is_numeric($loc_latitude)) {
                    $correct_coords = false;
                }
                if (!is_numeric($loc_longitude)) {
                    $correct_coords = false;
                }
                if (!is_numeric($cen_latitude)) {
                    $correct_coords = false;
                }
                if (!is_numeric($cen_longitude)) {
                    $correct_coords = false;
                }
                if (!$correct_coords) {
                    return 0;
                }
                $queryStr_point = str_replace('%%poi_lat%%', $loc_latitude, $queryStr_point);
                $queryStr_point = str_replace('%%poi_lon%%', $loc_longitude, $queryStr_point);
                $queryStr_point = str_replace('%%cen_lat%%', $cen_latitude, $queryStr_point);
                $queryStr_point = str_replace('%%cen_lon%%', $cen_longitude, $queryStr_point);
                $sql_params = array();
                $sql_params[] = 0 + $p_style;
                $sql_params[] = 0 + $p_radius;
                $rows = $g_ado_db->GetAll($queryStr_point, $sql_params);
                if (is_array($rows)) {
                    foreach ($rows as $row) {
                        $loc_id = $row['id'];
                        if ($loc_id && 0 < $loc_id) {
                            break;
                        }
                    }
                }
            } catch (Exception $exc) {
                return false;
            }
        }
        return $loc_id;
    }

Usage Example

Example #1
0
 /**
  * Inserts points (with locations and other contents) into the map.
  * NB: The result indices are used at the point order updating, since that order-updating
  * would not know id's of the new points otherwise.
  *
  * @param int $p_mapId
  * @param int $p_languageId
  * @param int $p_articleNumber
  * @param array $p_insertion
  * @param array $p_indices
  *
  * @return array
  */
 public static function InsertPoints($p_mapId, $p_languageId, $p_articleNumber, $p_insertion, &$p_indices)
 {
     global $g_ado_db;
     global $g_user;
     Geo_MapLocation::CleanFound();
     // this should not happen
     if (0 == $p_mapId) {
         return array();
     }
     /*
         A)
             1) given article_number, language_id, map_id, list of new data
             2) read languages of the article
     
         B)
             cycle:
                 1) insert into Locations the new position
                 2) get inserted id as location_id
                 3) insert into LocationContents the most of the new data,
                 4) get inserted id as content_id
                 5) insert into MapLocations (map_id, location_id, data:style, rank=0)
                 6) get inserted id as maplocation_id
                 7) insert into MapLocationLanguages (maplocation_id, language_id, content_id, data:display)
                 ad 7) this for all languages, with display=false for the other ones
     */
     // ad B 1)
     $queryStr_loc_in = 'INSERT INTO Locations (poi_location, poi_type, poi_type_style, poi_center, poi_radius, IdUser) VALUES (';
     $queryStr_loc_in .= "GeomFromText('POINT(? ?)'), 'point', 0, PointFromText('POINT(? ?)'), 0, %%user_id%%";
     $queryStr_loc_in .= ')';
     // ad B 3)
     // ad B 5)
     $queryStr_maploc = 'INSERT INTO MapLocations (fk_map_id, fk_location_id, poi_style, rank) ';
     $queryStr_maploc .= 'VALUES (?, ?, ?, 0)';
     // ad B 7)
     $queryStr_maploclan = 'INSERT INTO MapLocationLanguages (fk_maplocation_id, fk_language_id, fk_content_id, poi_display) ';
     $queryStr_maploclan .= 'VALUES (?, ?, ?, ?)';
     if ($p_articleNumber) {
         $languages = Geo_Map::ReadLanguagesByArticle($p_articleNumber);
     } else {
         $languages = Geo_Map::ReadLanguagesByMap($p_mapId);
     }
     foreach ($p_insertion as $poi) {
         if (is_object($poi)) {
             $poi = get_object_vars($poi);
         }
         $loc_id = null;
         $new_loc = array();
         $new_loc[] = array('latitude' => $poi['latitude'], 'longitude' => $poi['longitude']);
         $new_cen = array('latitude' => $poi['latitude'], 'longitude' => $poi['longitude']);
         $new_style = 0;
         $new_radius = 0;
         $reuse_id = Geo_Location::FindLocation($new_loc, 'point', $new_style, $new_cen, $new_radius);
         if ($reuse_id && 0 < $reuse_id) {
             $loc_id = $reuse_id;
         } else {
             // ad B 1)
             $loc_in_params = array();
             $loc_in_params[] = $poi['latitude'];
             $loc_in_params[] = $poi['longitude'];
             $loc_in_params[] = $poi['latitude'];
             $loc_in_params[] = $poi['longitude'];
             // the POI itself insertion
             $queryStr_loc_in = str_replace('%%user_id%%', $g_user->getUserId(), $queryStr_loc_in);
             $success = $g_ado_db->Execute($queryStr_loc_in, $loc_in_params);
             // ad B 2)
             // taking its ID for the next processing
             $loc_id = $g_ado_db->Insert_ID();
         }
         // ad B 3/4)
         $con_id = Geo_MapLocationContent::InsertContent($poi);
         // ad B 5)
         $maploc_params = array();
         $maploc_params[] = $p_mapId;
         $maploc_params[] = $loc_id;
         $maploc_params[] = '' . $poi['style'];
         // the map-point link insertion
         $success = $g_ado_db->Execute($queryStr_maploc, $maploc_params);
         // ad B 6)
         $maploc_id = $g_ado_db->Insert_ID();
         Geo_Multimedia::InsertMultimedia($maploc_id, $poi);
         // ad B 7)
         $maploclan_params = array();
         $maploclan_params[] = $maploc_id;
         $maploclan_params[] = 0 + $p_languageId;
         $maploclan_params[] = $con_id;
         $maploclan_params[] = 0 + $poi['display'];
         // the map-point link insertion
         $success = $g_ado_db->Execute($queryStr_maploclan, $maploclan_params);
         $poi_index = $poi['index'];
         $p_indices[$poi_index] = array('maploc' => $maploc_id);
         // insert the POI content for the other article's languages
         foreach ($languages as $one_lang) {
             if ($one_lang == $p_languageId) {
                 continue;
             }
             $maploclan_params[1] = $one_lang;
             $maploclan_params[3] = 0;
             // false; // display;
             $success = $g_ado_db->Execute($queryStr_maploclan, $maploclan_params);
         }
         // if a new POI, then that's all for it here
         continue;
     }
     return $p_indices;
 }