function update_point($id_list, $address_list, $ltlg_list, $title_list, $desc_list, $icon_list, $thumb_list)
 {
     $i = 0;
     foreach ($id_list as $id) {
         $new_point = new GMSHC_Point();
         $new_point->create_point($id, $address_list[$i], $ltlg_list[$i], $title_list[$i], $desc_list[$i], $icon_list[$i], $thumb_list[$i], $this->post_id);
         if (!gmshc_update_db_point($new_point)) {
             return false;
         }
         $i++;
     }
     return true;
 }
function gmshc_tab_process()
{
    gmshc_head();
    if (!isset($_REQUEST['map'])) {
        media_upload_header();
        $options = get_gmshc_options();
        $post_id = $_REQUEST["post_id"];
        $custom_fieds = get_post_custom($post_id);
        $address = isset($_REQUEST['new_address']) ? gmshc_clean_string($_REQUEST['new_address']) : "";
        $latitude = isset($_REQUEST['latitude']) ? gmshc_clean_string($_REQUEST['latitude']) : "";
        $longitude = isset($_REQUEST['longitude']) ? gmshc_clean_string($_REQUEST['longitude']) : "";
        $verify = isset($_REQUEST['verify']) ? true : false;
        $verify_point = true;
        if ($latitude != "" && $longitude != "") {
            $ltlg = $latitude . "," . $longitude;
            if ($verify) {
                $verify_point = true;
            } else {
                $verify_point = false;
            }
        } else {
            $ltlg = "";
        }
        $title = isset($_REQUEST['new_title']) ? gmshc_clean_string($_REQUEST['new_title']) : get_the_title($post_id);
        $description = isset($_REQUEST['new_description']) ? gmshc_clean_string($_REQUEST['new_description']) : "";
        $icon = isset($_REQUEST['default_icon']) ? stripslashes($_REQUEST['default_icon']) : "";
        $selected_thumbnail = isset($_REQUEST['selected_thumbnail']) ? stripslashes($_REQUEST['selected_thumbnail']) : "";
        $add_point = isset($_REQUEST['add_point']) ? $_REQUEST['add_point'] : '';
        $del_point = isset($_REQUEST['delp']) ? $_REQUEST['delp'] : '';
        $update_point = isset($_REQUEST['update']) ? $_REQUEST['update'] : '';
        $width = isset($_REQUEST['width']) ? $_REQUEST['width'] : $options['width'];
        $height = isset($_REQUEST['height']) ? $_REQUEST['height'] : $options['height'];
        $margin = isset($_REQUEST['margin']) ? $_REQUEST['margin'] : $options['margin'];
        $align = isset($_REQUEST['align']) ? $_REQUEST['align'] : $options['align'];
        $zoom = isset($_REQUEST['zoom']) ? $_REQUEST['zoom'] : $options['zoom'];
        $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : $options['type'];
        $focus = isset($_REQUEST['focus']) ? $_REQUEST['focus'] : $options['focus'];
        $focus_type = isset($_REQUEST['focus_type']) ? $_REQUEST['focus_type'] : $options['focus_type'];
        $id_list = isset($_REQUEST['pid']) ? $_REQUEST['pid'] : "";
        $address_list = isset($_REQUEST['addr']) ? gmshc_stripslashes_deep($_REQUEST['addr']) : "";
        $title_list = isset($_REQUEST['title']) ? gmshc_stripslashes_deep($_REQUEST['title']) : "";
        $desc_list = isset($_REQUEST['desc']) ? gmshc_stripslashes_deep($_REQUEST['desc']) : "";
        $ltlg_list = isset($_REQUEST['ltlg']) ? $_REQUEST['ltlg'] : "";
        $icon_list = isset($_REQUEST['icon']) ? $_REQUEST['icon'] : "";
        $thumb_list = isset($_REQUEST['thumb']) ? $_REQUEST['thumb'] : "";
        $post_points = new GMSHC_Post_Map();
        $post_points->create_post_map($post_id);
        if (!empty($add_point)) {
            $new_point = new GMSHC_Point();
            if ($new_point->create_point($ltlg, $address, $ltlg, $title, $description, $icon, $selected_thumbnail, $post_id, $verify_point)) {
                if ($post_points->add_point($new_point)) {
                    echo "<div class='updated'><p>" . __("The Point was added.", "google-map-sc") . "</p></div>";
                } else {
                    echo "<div class='error'><p>" . __("The point can't be saved or already exist.", "google-map-sc") . "</p></div>";
                }
            } else {
                echo "<div class='error'><p>" . __("The Address can't be located.", "google-map-sc") . "</p></div>";
            }
        } else {
            if (!empty($update_point)) {
                if ($post_points->update_point($id_list, $address_list, $ltlg_list, $title_list, $desc_list, $icon_list, $thumb_list)) {
                    echo "<div class='updated'><p>" . __("The Point was updated.", "google-map-sc") . "</p></div>";
                } else {
                    echo "<div class='error'><p>" . __("The Points can't be updated.", "google-map-sc") . "</p></div>";
                }
            } else {
                if ($del_point != "") {
                    if ($post_points->delete_point($del_point)) {
                        echo "<div class='updated'><p>" . __("The Point was deleted.", "google-map-sc") . "</p></div>";
                    }
                }
            }
        }
        ?>
    
    <?php 
        $post_points->load_points();
        ?>
        
    <script type="text/javascript" src="<?php 
        echo GMSC_PLUGIN_URL;
        ?>
/js/gmshc-admin.js"></script>
        
    <link href="<?php 
        echo GMSC_PLUGIN_URL;
        ?>
/styles/gmshc-admin-styles.css" rel="stylesheet" type="text/css"/>       
       
       
        <div style="width:620px; margin:10px auto">
       
        <?php 
        echo gmshc_plugin_menu();
        ?>
    
        <form  action="#" method="post">
           <input id="default_width" type="hidden" value="<?php 
        echo $width;
        ?>
"/>
           <input id="default_height" type="hidden" value="<?php 
        echo $height;
        ?>
"/>
           <input id="default_margin" type="hidden" value="<?php 
        echo $margin;
        ?>
"/>
           <input id="default_align" type="hidden" value="<?php 
        echo $align;
        ?>
"/>
           <input id="default_zoom" type="hidden" value="<?php 
        echo $zoom;
        ?>
"/>
           <input id="default_focus" type="hidden" value="<?php 
        echo $focus;
        ?>
"/>
           <input id="default_focus_type" type="hidden" value="<?php 
        echo $focus_type;
        ?>
"/>           
           <input id="default_type" type="hidden" value="<?php 
        echo $type;
        ?>
"/>
           
           <table width="620" border="0" cellspacing="5" cellpadding="5">
            <tr>
                <td colspan="2">
                <h3 class="gmshc_editor"><?php 
        _e("Add New Point", "google-map-sc");
        ?>
</h3>
                <p><?php 
        _e("Points can be added using Address or Lat/Long.", "google-map-sc");
        ?>
</p>
                </td>
           </tr> 
            <tr>
                <td align="right" valign="top">
                <strong><?php 
        _e("Full Address", "google-map-sc");
        ?>
</strong>
                </td>
				<td valign="top">    
				<textarea name="new_address" cols="32" rows="2" id="new_address"></textarea>
				</td>
            </tr>           
            <tr>
                <td align="right" valign="top">
                <strong><?php 
        _e("Latitude ", "google-map-sc");
        ?>
</strong>
                </td>
				<td valign="top">    
				<input name="latitude" size="35" id="latitude" />
				</td>
            </tr>
            <tr>
                <td align="right" valign="top">
                <strong><?php 
        _e("Longitude", "google-map-sc");
        ?>
</strong>
                </td>
				<td valign="top">    
				<input name="longitude" size="35" id="longitude" />
				</td>
            </tr>
            <tr>
                <td align="right" valign="top">
                <input type="checkbox" value="1" name="verify" />
                </td>
				<td valign="top">    
				<?php 
        _e("Verify this latitude and longitude using Geocoding. This could overwrite the point address.", "google-map-sc");
        ?>
				</td>
            </tr>            
            <tr>
                <td align="right" valign="top">
                <strong><?php 
        _e("Title", "google-map-sc");
        ?>
</strong>
                </td>
				<td valign="top">    
				<input name="new_title"  size="54" id="new_title" value="<?php 
        echo $title;
        ?>
" />
				</td>
            </tr> 
            <tr>
                <td align="right" valign="top">
                <strong><?php 
        _e("Description", "google-map-sc");
        ?>
</strong>
                </td>
				<td valign="top">    
				<textarea name="new_description" cols="47" rows="2" id="new_description"></textarea>
				</td>
            </tr>	

            <tr>
				<td align="right" valign="top" colspan="2">
					<?php 
        gmshc_deploy_icons();
        ?>
				</td>
            </tr> 
            <tr>
				<td align="center" valign="top" colspan="2">
                	<?php 
        $thumbnail_list = gmshc_all_post_thumb($post_id);
        if (count($thumbnail_list) > 0) {
            ?>
                        <div class="gmshc_label">
                            <?php 
            _e("Select the thumbnail by clicking on the images", "google-map-sc");
            ?>
                        </div>
                        <div id="gmshc_thumb_cont">
                        <input type="hidden" name="selected_thumbnail" value="<?php 
            echo $default_icon;
            ?>
" id="selected_thumbnail" />
                        <?php 
            foreach ($thumbnail_list as $thumbnail) {
                ?>
                            <div class="gmshc_thumb">
                                <img src="<?php 
                echo $thumbnail;
                ?>
" width="40" height="40" />
                            </div>
                        <?php 
            }
            ?>
                        </div>
					<?php 
        } else {
            ?>
                        <div class="gmshc_label">
                            <strong><?php 
            _e("Thumbnail: ", "google-map-sc");
            ?>
</strong><?php 
            _e("If you want to attach an image to the point you need to upload it first to the post gallery", "google-map-sc");
            ?>
                        </div> 
                    <?php 
        }
        ?>
 
                    <p align="left"><a class="button" href = "?post_id=<?php 
        echo $post_id;
        ?>
&type=image" title="Upload Images"><?php 
        _e("Upload Images", "google-map-sc");
        ?>
</a></p>                     
				    <p class="endbox"><input class="button-primary" value="<?php 
        _e("Add Point", "google-map-sc");
        ?>
" name="add_point" type="submit"></p>
                </td>
            </tr>
            <?php 
        if ($post_points->points_number > 0) {
            ?>
            
            <tr>
                <td colspan="2">
                <h3 class="gmshc_editor"><?php 
            _e("Map Configuration", "google-map-sc");
            ?>
</h3>
                </td>
            </tr>  
            <tr>
                <td align="right"><?php 
            _e("Width", "google-map-sc");
            ?>
</td>
                <td valign="top"><input name="width" type="text" id="width" size="10" value = "<?php 
            echo $width;
            ?>
"/></td>
            </tr>  
            <tr>
                <td align="right"><?php 
            _e("Height", "google-map-sc");
            ?>
</td>
                <td valign="top"><input name="height" type="text" id="height" size="10" value = "<?php 
            echo $height;
            ?>
" /></td>
            </tr>
            <tr>
              <td align="right"><?php 
            _e("Margin", "google-map-sc");
            ?>
</td>
              <td><input name="margin" id="margin" type="text" size="6" value="<?php 
            echo $margin;
            ?>
" /></td>
            </tr>  
            <tr>
              <td align="right"><?php 
            _e("Align", "google-map-sc");
            ?>
</td>
              <td>
                  <input name="align" type="radio" id="aleft" value="left" <?php 
            echo $align == "left" ? "checked = 'checked'" : "";
            ?>
 /> <?php 
            _e("left", "google-map-sc");
            ?>
                  <input name="align" type="radio" id="acenter" value="center" <?php 
            echo $align == "center" ? "checked = 'checked'" : "";
            ?>
 /> <?php 
            _e("center", "google-map-sc");
            ?>
                  <input name="align" type="radio" id="aright" value="right" <?php 
            echo $align == "right" ? "checked = 'checked'" : "";
            ?>
 /> <?php 
            _e("right", "google-map-sc");
            ?>
        
            </tr>            
            <tr>
              <td align="right"><?php 
            _e("Zoom", "google-map-sc");
            ?>
</td>
              <td>              
              <select name="zoom" id="zoom">              
                  <?php 
            for ($i = 0; $i <= 20; $i++) {
                ?>
                      <option value="<?php 
                echo $i;
                ?>
" <?php 
                echo $i == $zoom ? "selected" : "";
                ?>
 ><?php 
                echo $i;
                ?>
</option>
                  <?php 
            }
            ?>
              </select>         
            </tr> 
            <tr>
              <td align="right"><?php 
            _e("Maps Type", "google-map-sc");
            ?>
</td>
              <td>
                  <select name="type" id="type">
                      <option value="ROADMAP" <?php 
            if ($type == "ROADMAP") {
                echo "selected";
            }
            ?>
><?php 
            _e("ROADMAP - Displays a normal street map", "google-map-sc");
            ?>
</option>
                      <option value="SATELLITE" <?php 
            if ($type == "SATELLITE") {
                echo "selected";
            }
            ?>
><?php 
            _e("SATELLITE - Displays satellite images", "google-map-sc");
            ?>
</option>
                      <option value="TERRAIN" <?php 
            if ($type == "TERRAIN") {
                echo "selected";
            }
            ?>
><?php 
            _e("TERRAIN - Displays maps with physical features such as terrain and vegetation", "google-map-sc");
            ?>
</option>
                      <option value="HYBRID" <?php 
            if ($type == "HYBRID") {
                echo "selected";
            }
            ?>
><?php 
            _e("HYBRID - Displays a transparent layer of major streets on satellite images", "google-map-sc");
            ?>
</option>
                  </select>
              </td>        
            </tr>  
            <tr>
              <td align="right"><?php 
            _e("Focus", "google-map-sc");
            ?>
</td>
              <td>
                  <select name="focus" id="focus">
                      <option value="0" <?php 
            if ($focus == 0) {
                echo "selected";
            }
            ?>
><?php 
            _e("None", "google-map-sc");
            ?>
</option>
                      <?php 
            for ($i = 1; $i <= $post_points->points_number; $i++) {
                ?>
                      	<option value="<?php 
                echo $i;
                ?>
" <?php 
                if ($focus == $i) {
                    echo "selected";
                }
                ?>
><?php 
                echo $i;
                ?>
</option>
                      <?php 
            }
            ?>
                      <?php 
            if ($post_points->points_number > 1) {
                ?>
                      <option value="all" <?php 
                if ($focus == "all") {
                    echo "selected";
                }
                ?>
><?php 
                _e("All", "google-map-sc");
                ?>
</option>
                      <?php 
            }
            ?>
                      
                  </select>
                  <em><?php 
            _e("Select the point to be focused after loading the map", "google-map-sc");
            ?>
</em>
              </td>        
            </tr> 
            <tr>
              <td align="right" valign="top"><?php 
            _e("Focus Type", "google-map-sc");
            ?>
</td>
              <td>
              <select name="focus_type" id="focus_type">
                 <option value="open" <?php 
            echo $focus_type == "open" ? "selected" : "";
            ?>
 ><?php 
            _e("Open Markers", "google-map-sc");
            ?>
</option>
                 <option value="center" <?php 
            echo $focus_type == "center" ? "selected" : "";
            ?>
 ><?php 
            _e("Center Markers", "google-map-sc");
            ?>
</option>
              </select>
              </td>        
            </tr>                                   
            <?php 
        }
        ?>
			
            </table>
            
			<?php 
        if ($post_points->points_number > 0) {
            ?>
  
                
                <p class="endbox"><input class="button-primary insert_map" value="<?php 
            _e("Insert Map", "google-map-sc");
            ?>
" type="button" \> 
                   <a class="button gmshc_show" href="#gmshc_map" show="<?php 
            _e("Show Map", "google-map-sc");
            ?>
" hide="<?php 
            _e("Hide Map", "google-map-sc");
            ?>
">
                    <?php 
            _e("Show Map", "google-map-sc");
            ?>
                   </a>&nbsp;  
                   <a class="button gmshc_refresh" href="#gmshc_map">
                    <?php 
            _e("Refresh Map", "google-map-sc");
            ?>
                   </a>
                </p>
                                     
                <p><?php 
            echo $post_points->points_number . " " . __("Points Added", "google-map-sc");
            ?>
</p>                    
            <?php 
        }
        ?>
            
			<?php 
        if (count($post_points->points) > 0) {
            rsort($post_points->points);
            ?>
     
            <table class="widefat" cellspacing="0">
                <thead>
                <tr>
                <th><?php 
            _e("#");
            ?>
</th>
                <th><?php 
            _e("Marker", "google-map-sc");
            ?>
</th>
                <th><?php 
            _e("Thumbnail", "google-map-sc");
            ?>
</th>
                <th><?php 
            _e("Title/Description", "google-map-sc");
            ?>
</th>
                <th width="140"><?php 
            _e("Address/LtLg", "google-map-sc");
            ?>
</th>
                </tr>
                </thead>
                <tbody class="media-item-info">   
                
                <?php 
            $i = 0;
            foreach ($post_points->points as $point) {
                ?>
                     
                    <tr>
                      <td><?php 
                echo $post_points->points_number - $i;
                ?>
</td>	
                      <td>
                      	<img src="<?php 
                echo $point->icon;
                ?>
" atl="<?php 
                _e("Icon", "google-map-sc");
                ?>
" />
                        <input name="icon[]" type="hidden" id="icon_<?php 
                echo $i;
                ?>
" size="30" value = "<?php 
                echo $point->icon;
                ?>
"/>
                        <input name="pid[]" type="hidden" id="id_<?php 
                echo $i;
                ?>
" size="30" value = "<?php 
                echo $point->id;
                ?>
"/>
                      </td>                    
                      <td>
                      	<div class="gmshc_thumb gmshc_selected">
                        <?php 
                if ($point->thumbnail != "") {
                    ?>
						       
                      	<img src="<?php 
                    echo $point->thumbnail;
                    ?>
" atl="<?php 
                    _e("Thumbnail", "google-map-sc");
                    ?>
" width = "40" height="40" />
                        <input name="thumb[]" type="hidden" id="thumb_<?php 
                    echo $i;
                    ?>
" size="30" value = "<?php 
                    echo $point->thumbnail;
                    ?>
"/>                        
                         <?php 
                }
                ?>
                         </div>                  
                      </td>
                      <td>
						<input name="title[]" type="text" id="title_<?php 
                echo $i;
                ?>
" size="33" value = "<?php 
                echo $point->title;
                ?>
"/>
                        <textarea name="desc[]" cols="30" rows="2" id="desc_<?php 
                echo $i;
                ?>
"><?php 
                echo $point->description;
                ?>
</textarea>							
                      </td>
                      <td>
                      	<textarea name="addr[]" cols="30" rows="2" id="addr_<?php 
                echo $i;
                ?>
"><?php 
                echo $point->address;
                ?>
</textarea>
						<input name="ltlg[]" type="hidden" id="ltlg_<?php 
                echo $i;
                ?>
" size="30" value = "<?php 
                echo $point->ltlg;
                ?>
"/>
                        <p><?php 
                echo $point->ltlg;
                ?>
</p>                        
                        <div>                        
                        <input class="button" value="<?php 
                _e("Update", "google-map-sc");
                ?>
" name="update" type="submit"> 
                        <a href="?post_id=<?php 
                echo $post_id;
                ?>
&tab=gmshc&delp=<?php 
                echo $point->id;
                ?>
" class="delete_point" onclick="if(confirm('<?php 
                _e("You will not be able to roll back deletion. Are you sure?", "google-map-sc");
                ?>
')) return true; else return false"><?php 
                _e("Delete", "google-map-sc");
                ?>
</a>
                        </div>
                      </td>
                    </tr>
                 <?php 
                $i++;
            }
            ?>
          
                </tbody> 	    
            </table>
        
   	    <p><input class="button-primary insert_map" value="<?php 
            _e("Insert Map", "google-map-sc");
            ?>
" type="button" \> 
           <a class="button gmshc_show" href="#gmshc_map" show="<?php 
            _e("Show Map", "google-map-sc");
            ?>
" hide="<?php 
            _e("Hide Map", "google-map-sc");
            ?>
">
		   	<?php 
            _e("Show Map", "google-map-sc");
            ?>
           </a>&nbsp; 
           <a class="button gmshc_refresh" href="#gmshc_map">
		   	<?php 
            _e("Refresh Map", "google-map-sc");
            ?>
           </a>
        </p>        
        </div>
		
        <div id="gmshc_map" style="height:0px; overflow:hidden;" tabindex="100">
        <input type="hidden" id="iframe_url" value="?post_id=<?php 
            echo $post_id;
            ?>
&tab=gmshc&"  />
        <iframe id = "iframe_sc" src="" width="600" height="420" scrolling="no" />        
        </div>
        <br />        
			<?php 
        }
        ?>
		</form>
        <?php 
    } else {
        $map_attr = array("post_id", "type", "zoom", "focus", "focus_type");
        $shc_attr = "";
        foreach ($map_attr as $attr) {
            if (isset($_REQUEST[$attr])) {
                $shc_attr .= " " . ($attr == "post_id" ? "id" : $attr) . "=" . $_REQUEST[$attr];
            }
        }
        echo do_shortcode("[google-map-sc width=600 height=420 align=center margin=0" . $shc_attr . "]");
    }
}
/**
 * Get post points form the post custom field 'google-map-sc'
 */
function gmshc_get_points($post_id)
{
    // check for old custom google-map-sc
    $post_data = get_post_meta($post_id, 'google-map-sc', true);
    $post_points = array();
    if ($post_data != "") {
        $points = json_decode(urldecode($post_data), true);
        if (is_array($points)) {
            foreach ($points as $point) {
                $point_obj = new GMSHC_Point();
                if ($point_obj->create_point("", $point['address'], $point['ltlg'], $point['title'], $point['description'], $point['icon'], $point['thumbnail'], $post_id)) {
                    gmshc_insert_db_point($point_obj);
                }
            }
        }
    }
    /**  checking for old custom fields google-map-sc-address **/
    $post_data_address = get_post_meta($post_id, 'google-map-sc-address');
    $options = get_gmshc_options();
    $default_icon = $options['default_icon'];
    $post_title = get_the_title($post_id);
    if (count($post_data_address) > 0) {
        foreach ($post_data_address as $point_address) {
            $point_obj_address = new GMSHC_Point();
            if ($point_obj_address->create_point("", gmshc_clean_string($point_address), "", $post_title, "", $default_icon, "", $post_id, true)) {
                gmshc_insert_db_point($point_obj_address);
            }
        }
    }
    /**  checking for old custom fields google-map-sc-latlng **/
    $post_data_ltlg = get_post_meta($post_id, 'google-map-sc-latlng');
    if (count($post_data_ltlg) > 0) {
        foreach ($post_data_ltlg as $point_ltlg) {
            $point_obj_ltlg = new GMSHC_Point();
            if ($point_obj_ltlg->create_point("", "", $point_ltlg, $post_title, "", $default_icon, "", $post_id)) {
                gmshc_insert_db_point($point_obj_ltlg);
            }
        }
    }
    $db_points = gmshc_get_bd_points($post_id);
    if (count($db_points) > 0) {
        foreach ($db_points as $point) {
            $point_obj = new GMSHC_Point();
            if ($point_obj->create_point($point['id'], $point['address'], $point['ltlg'], $point['title'], $point['description'], $point['icon'], $point['thumbnail'], $post_id)) {
                array_push($post_points, $point_obj);
            }
        }
    }
    return $post_points;
}