Geo_Location::UpdateLocations PHP Method

UpdateLocations() public static method

Updates the location, the COW way
public static UpdateLocations ( integer $p_mapId, array $p_locations ) : boolean
$p_mapId integer
$p_locations array
return boolean
    public static function UpdateLocations($p_mapId, $p_locations)
    {
        global $g_ado_db;
        global $g_user;
        /*
            A)
                1) given article_number, language_id, map_id, list of map_loc_id / new locations
            B)
                cycle:
                    1) read location_id (as old_loc_id) of the map_loc_id
                    2) insert new location with new positions
                    3) get the inserted id into new_loc_id
                    4) update maplocations into the new_loc_id for the map_loc_id
                    6) delete location of old_loc_id if none maplocation with a link into the old_loc_id
        */
        // ad B 1)
        $queryStr_loc_id = 'SELECT fk_location_id AS loc FROM ' . Geo_MapLocation::TABLE . ' WHERE id = ?';
        // ad B 2)
        $queryStr_loc_in = 'INSERT INTO ' . self::TABLE . ' (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 4)
        $queryStr_map_up = 'UPDATE ' . Geo_MapLocation::TABLE . ' SET fk_location_id = ? WHERE id = ?';
        // ad B 6)
        $queryStr_loc_rm = 'DELETE FROM ' . self::TABLE . ' WHERE id = ? AND NOT EXISTS (SELECT id FROM MapLocations WHERE fk_location_id = ?)';
        // updating current POIs, inserting new POIs
        foreach ($p_locations as $poi_obj) {
            $poi = get_object_vars($poi_obj);
            // ad B 1)
            $loc_old_id = null;
            try {
                $maploc_sel_params = array();
                $maploc_sel_params[] = $poi['id'];
                $rows = $g_ado_db->GetAll($queryStr_loc_id, $maploc_sel_params);
                if (is_array($rows)) {
                    foreach ($rows as $row) {
                        $loc_old_id = $row['loc'];
                    }
                }
            } catch (Exception $exc) {
                return false;
            }
            if (null === $loc_old_id) {
                continue;
            }
            $loc_new_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 = self::FindLocation($new_loc, 'point', $new_style, $new_cen, $new_radius);
            if ($reuse_id && 0 < $reuse_id) {
                $loc_new_id = $reuse_id;
            } else {
                $loc_in_params = array();
                $loc_in_params[] = $poi['latitude'];
                $loc_in_params[] = $poi['longitude'];
                $loc_in_params[] = $poi['latitude'];
                $loc_in_params[] = $poi['longitude'];
                $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 3)
                // taking its ID for the next processing
                $loc_new_id = $g_ado_db->Insert_ID();
            }
            $map_up_params = array();
            $map_up_params[] = $loc_new_id;
            $map_up_params[] = $poi['id'];
            $success = $g_ado_db->Execute($queryStr_map_up, $map_up_params);
            // ad B 6)
            try {
                $loc_rm_params = array();
                $loc_rm_params[] = $loc_old_id;
                $loc_rm_params[] = $loc_old_id;
                $success = $g_ado_db->Execute($queryStr_loc_rm, $loc_rm_params);
            } catch (Exception $exc) {
                return false;
            }
        }
        return true;
    }

Usage Example

Example #1
0
 /**
  * The main dispatcher for ajax based editing of maps
  *
  * @param int $p_mapId
  * @param int $p_languageId
  * @param int $p_articleNumber
  * @param mixed $p_map
  * @param mixed $p_remove
  * @param mixed $p_insert
  * @param mixed $p_locations
  * @param mixed $p_contents
  * @param mixed $p_order
  *
  * @return array
  */
 public static function StoreMapData($p_mapId, $p_languageId, $p_articleNumber, $p_map = '', $p_remove = '', $p_insert = '', $p_locations = '', $p_contents = '', $p_order = '')
 {
     Geo_MapLocation::CleanFound();
     $security_problem = array('status' => '403', 'description' => 'Invalid security token!');
     $unknown_request = array('status' => '404', 'description' => 'Unknown request!');
     $data_wrong = array('status' => '404', 'description' => 'Wrong data.');
     $status = true;
     if ('' != $p_map) {
         $map_data = array();
         try {
             $p_map = str_replace('%2B', '+', $p_map);
             $p_map = str_replace('%2F', '/', $p_map);
             $map_json = base64_decode($p_map);
             $map_data = json_decode($map_json);
         } catch (Exception $exc) {
             $status = false;
         }
         if ($status) {
             $status = Geo_Map::UpdateMap($p_mapId, $p_articleNumber, $map_data);
         }
     }
     if (!$status) {
         return $data_wrong;
     }
     if ('' != $p_remove) {
         $remove_data = array();
         try {
             $p_remove = str_replace('%2B', '+', $p_remove);
             $p_remove = str_replace('%2F', '/', $p_remove);
             $remove_json = base64_decode($p_remove);
             $remove_data = json_decode($remove_json);
         } catch (Exception $exc) {
             $status = false;
         }
         if ($status) {
             $status = Geo_Map::RemovePoints($p_mapId, $remove_data);
         }
     }
     if (!$status) {
         return $data_wrong;
     }
     $new_ids = array();
     if ('' != $p_insert) {
         $insert_data = array();
         try {
             $p_insert = str_replace('%2B', '+', $p_insert);
             $p_insert = str_replace('%2F', '/', $p_insert);
             $insert_json = base64_decode($p_insert);
             $insert_data = json_decode($insert_json);
         } catch (Exception $exc) {
             $status = false;
         }
         if ($status) {
             $status = Geo_Map::InsertPoints($p_mapId, $p_languageId, $p_articleNumber, $insert_data, $new_ids);
         }
     }
     if (!$status) {
         return $data_wrong;
     }
     if ('' != $p_locations) {
         $locations_data = array();
         try {
             $p_locations = str_replace('%2B', '+', $p_locations);
             $p_locations = str_replace('%2F', '/', $p_locations);
             $locations_json = base64_decode($p_locations);
             $locations_data = json_decode($locations_json);
         } catch (Exception $exc) {
             $status = false;
         }
         if ($status) {
             $status = Geo_Location::UpdateLocations($p_mapId, $locations_data);
         }
     }
     if (!$status) {
         return $data_wrong;
     }
     if ('' != $p_contents) {
         $contents_data = array();
         try {
             $p_contents = str_replace('%2B', '+', $p_contents);
             $p_contents = str_replace('%2F', '/', $p_contents);
             $contents_json = base64_decode($p_contents);
             $contents_data = json_decode($contents_json);
         } catch (Exception $exc) {
             $status = false;
         }
         if ($status) {
             $status = Geo_Location::UpdateContents($p_mapId, $contents_data);
         }
     }
     if (!$status) {
         return $data_wrong;
     }
     if ('' != $p_order) {
         $order_data = array();
         try {
             $p_order = str_replace('%2B', '+', $p_order);
             $p_order = str_replace('%2F', '/', $p_order);
             $order_json = base64_decode($p_order);
             $order_data = json_decode($order_json);
         } catch (Exception $exc) {
             $status = false;
         }
         if ($status) {
             $status = Geo_Location::UpdateOrder($p_mapId, $order_data, $new_ids);
         }
     }
     if (!$status) {
         return $data_wrong;
     }
     $geo_map_usage = Geo_Map::ReadMapInfo('map', $p_mapId);
     $poi_count = 0;
     $p_constraints = array();
     $leftOperand = 'as_array';
     $rightOperand = true;
     $operator = new Operator('is', 'php');
     $constraint = new ComparisonOperation($leftOperand, $operator, $rightOperand);
     $p_constraints[] = $constraint;
     $leftOperand = 'preview';
     $rightOperand = false;
     $operator = new Operator('is', 'php');
     $constraint = new ComparisonOperation($leftOperand, $operator, $rightOperand);
     $p_constraints[] = $constraint;
     $leftOperand = 'text_only';
     $rightOperand = false;
     $operator = new Operator('is', 'php');
     $constraint = new ComparisonOperation($leftOperand, $operator, $rightOperand);
     $p_constraints[] = $constraint;
     $leftOperand = 'language';
     $rightOperand = $p_languageId;
     $operator = new Operator('is', 'php');
     $constraint = new ComparisonOperation($leftOperand, $operator, $rightOperand);
     $p_constraints[] = $constraint;
     $leftOperand = 'map';
     $rightOperand = $p_mapId;
     $operator = new Operator('is', 'php');
     $constraint = new ComparisonOperation($leftOperand, $operator, $rightOperand);
     $p_constraints[] = $constraint;
     $found_list = array();
     $found_objs = Geo_MapLocation::GetListExt($p_constraints, (array) null, 0, 0, $poi_count, false, $found_list);
     $res_array = array('status' => '200', 'pois' => $found_list, 'map' => $geo_map_usage);
     return $res_array;
 }
All Usage Examples Of Geo_Location::UpdateLocations