/**
  * Update the location for this user
  *
  * @return User object (this) (or Error object)
  */
 function updateLocation($location = "", $loccountry = "")
 {
     global $DB, $CFG, $USER, $HUB_SQL, $HUB_CACHE;
     if (isset($HUB_CACHE)) {
         $HUB_CACHE->deleteData($this->userid . $this->style);
     }
     $params = array();
     $params[0] = $location;
     $params[1] = $loccountry;
     $params[2] = $this->userid;
     $res = $DB->insert($HUB_SQL->DATAMODEL_USER_LOCATION_UPDATE, $params);
     if ($res) {
         //try to geocode
         if ($location != "" && $loccountry != "" && ($location != $this->location || $loccountry != $this->countrycode)) {
             $coords = geoCode($location, $loccountry);
             if ($coords["lat"] != "" && $coords["lng"] != "") {
                 $params = array();
                 $params[0] = $coords["lat"];
                 $params[1] = $coords["lng"];
                 $params[2] = $this->userid;
                 $res = $DB->insert($HUB_SQL->DATAMODEL_USER_LATLONG_UPDATE, $params);
             } else {
                 $params = array();
                 $params[0] = null;
                 $params[1] = null;
                 $params[2] = $this->userid;
                 $res = $DB->insert($HUB_SQL->DATAMODEL_USER_LATLONG_UPDATE, $params);
             }
         }
         return $this->load();
     } else {
         return database_error();
     }
 }
function scrapeRM()
{
    $max = 90;
    for ($index = 0; $index < $max; $index += 30) {
        $url = "http://www.redmascotera.com/see_pet.php?ini=" . $index . "&bu_pais=1";
        $html_content = scraperWiki::scrape($url);
        $html = str_get_html($html_content);
        $pets_data = array();
        foreach ($html->find("div.cont_m") as $html_el) {
            $pet_data = array();
            $code = $html_el->find("h3 strong", 0)->plaintext;
            $title = $html_el->find("h3", 0)->plaintext;
            $title = str_replace($code, '', $title);
            $pet_data["Codigo"] = utf8_encode(trim($code));
            $pet_data["Titulo"] = utf8_encode(trim($title));
            $link = $html_el->find("a", 0)->href;
            $detail_url = "http://www.redmascotera.com/" . $link;
            $detail_content = scraperWiki::scrape($detail_url);
            $detail = str_get_html($detail_content);
            $pet_info = $detail->find("div.bkg_presentacion", 0);
            foreach ($pet_info->find("h5") as $pet_el) {
                $tags = explode(":", $pet_el->plaintext);
                $key = utf8_encode(trim($tags[0]));
                $value = utf8_encode(trim($tags[1]));
                if ($key == "Ingresada") {
                    $value = date_create_from_format('d/m/Y', $value);
                }
                if ($key == "Provincia" && $value == "Capital y GBA") {
                    $value = "Buenos Aires";
                }
                $pet_data[$key] = $value;
            }
            foreach ($pet_info->find("p") as $contact_el) {
                $tags = explode(":", $contact_el->plaintext);
                $key = utf8_encode(trim($tags[0]));
                $value = utf8_encode(trim($tags[1]));
                $pet_data[$key] = $value;
            }
            $pet_extra = $detail->find("div.bkg_presentacion", 1);
            $more_info = $pet_extra->find("p", 0)->plaintext;
            $pet_data["Mas_info"] = utf8_encode(trim($more_info));
            $pet_data["URL"] = utf8_encode($detail_url);
            $geo_data = geoCode($pet_data["Calle"] . ", " . $pet_data["Barrio"] . ", " . $pet_data["Localidad"] . ", " . $pet_data["Provincia"] . ", Argentina");
            $pet_data["Lat"] = $geo_data["lat"];
            $pet_data["Lng"] = $geo_data["lng"];
            $pet_data["Direccion"] = $geo_data["formatted_address"];
            scraperwiki::save(array('Codigo'), $pet_data);
            $pets_data[] = $pet_data;
            $img = $html_el->find("img", 0)->src;
            $p = $html_el->find("p", 0);
            #print $title . " - " .$code . " - " .$link . " - " .$img. " - " .$p."\n";
        }
    }
}