Source: site.view [edit]
Function name: searchProperties
Arguments: sessionkey,type,address,price,beds,baths,rooms,size,lotSize,features,description
Description: Searches for properties matching criteria
Page type: webl
Render function:  
Module: maybeathome

Page source:

var ret = nil;  // [sessionkey,type,address,price,beds,baths,rooms,size,lotSize,features,description];

var msg = "";
var geocode = nil;
var list = [];

var aaddress = Str_Trim(address);
var pprice = Wub_ReplaceAll(Wub_ReplaceAll(Str_Trim(price), ",", ""), "$", "");
var ssize = Wub_ReplaceAll(Str_Trim(size), ",", "");
var llotSize = Wub_ReplaceAll(Str_Trim(lotSize), ",", "");
var bbeds = Str_Trim(beds);
var bbaths = Str_Trim(baths);
var rrooms = Str_Trim(rooms);
var ddescription = Str_Trim(description);

if (Str_Trim(aaddress) != "") then
   geocode = WubCall("geocode", [aaddress]);
end;
  

if (geocode == nil) then
   msg = "Please enter a valid search location.";
   ret = WubCall("maybeathome.results", [ [. "args" = [sessionkey, msg, type, address, price, beds, baths, rooms, size, lotSize, features, description], "listings" = list .] ])
   
elsif (pprice != "" and (ToInt(pprice) ? -1) <= 0) then
   msg = "Please correct 'price'";
   ret = WubCall("maybeathome.results", [ [. "args" = [sessionkey, msg, type, address, price, beds, baths, rooms, size, lotSize, features, description], "listings" = list .] ])

elsif (ssize != "" and (ToInt(ssize) ? -1) <= 10) then
   msg = "Please correct 'size'";
   ret = WubCall("maybeathome.results", [ [. "args" = [sessionkey, msg, type, address, price, beds, baths, rooms, size, lotSize, features, description], "listings" = list .] ])

elsif (llotSize != "" and (ToInt(llotSize) ? -1) <= 10) then
   msg = "Please correct 'lot size'";
   ret = WubCall("maybeathome.results", [ [. "args" = [sessionkey, msg, type, address, price, beds, baths, rooms, size, lotSize, features, description], "listings" = list .] ])

elsif (bbeds != "" and ((ToInt(beds) ? -1) <= 0) or (ToInt(bbeds) ? -1) > 20) then
   msg = "Please correct 'beds'";
   ret = WubCall("maybeathome.results", [ [. "args" = [sessionkey, msg, type, address, price, beds, baths, rooms, size, lotSize, features, description], "listings" = list .] ])

elsif (rrooms != "" and ((ToInt(rooms) ? -1) <= 0) or (ToInt(rooms) ? -1) > 40) then
   msg = "Please correct 'rooms'";
   ret = WubCall("maybeathome.results", [ [. "args" = [sessionkey, msg, type, address, price, beds, baths, rooms, size, lotSize, features, description], "listings" = list .] ])

elsif (bbaths != "" and ((ToInt(bbaths) ? -1) <= 0) or (ToInt(bbaths) ? -1) > 20) then
   msg = "Please correct 'baths'";
   ret = WubCall("maybeathome.results", [ [. "args" = [sessionkey, msg, type, address, price, beds, baths, rooms, size, lotSize, features, description], "listings" = list .] ])
end;   

// No errors: add it
if (ret == nil) then
   var db = Wub_GetDB("maybeathome");
   var coll = Wub_GetCollection(db, "listings");
  
   var searchParams = [. .];
  
   if (geocode.postalCode != nil) then
      searchParams.zip := geocode.postalCode;
   end;
   if (geocode.city != nil) then
      searchParams.city := geocode.city;
   end;
   // This should be added back, but our data is so old, state is not matching for our demo properties
   //  Google must have changed the state from "Ile-De-France" to "IDF".  This hack is the fastest way to get the demo working again.
   // if (geocode.state != nil) then
      // searchParams.state := geocode.state;
   // end;
   if (geocode.country != nil) then
      searchParams.country := geocode.country;
   end;
  
   if (type != nil and type != "") then
      if Stringp(type) then
         searchParams.type := type; 
      end
   end;
  
   if (beds != nil and beds != "") then      
      searchParams.beds := [. "$gte"=ToReal(bbeds) .]; 
      // beds = WubCall("formatNum", [. "int", beds .] );
   end;
   if (baths != nil and baths != "") then      
      searchParams.baths := [. "$gte"=ToReal(bbaths) .]; 
      // baths = WubCall("formatNum", [. "int", baths .] );
   end;
   if (rooms != nil and rooms != "") then      
      searchParams.rooms := [. "$gte"=ToReal(rrooms) .]; 
      // rooms = WubCall("formatNum", [. "int", rooms .] );
   end;
   if (size != nil and size != "") then      
      searchParams.size := [. "$gte"=ToReal(ssize) .]; 
      // size = WubCall("formatNum", [. "int", size .] );
   end;
   if (lotSize != nil and lotSize != "") then      
      searchParams.lotSize := [. "$gte"=ToReal(llotSize) .]; 
      // lotSize = WubCall("formatNum", [. "int", lotSize .] );
   end;
   if (price != nil and price != "") then      
      searchParams.price := [. "$lte"=ToReal(pprice) .];
      // price = WubCall("formatNum", [. "USD", price .] );
   end;
 
   var dbobj = Wub_NewDBObject(searchParams); 
  
   var res = Wub_QueryDB(coll, dbobj, 30);
   
   if (Size(res) == 0) then
       // should update
       msg = "No results found.";
       ret = WubCall("maybeathome.results", [ [. "args" = [sessionkey, msg, type, address, price, beds, baths, rooms, size, lotSize,features, description], "listings" = list .] ])
   end;

   if (ret == nil) then
      every r in res do
         list = list + [ r ];
      end;
      // ret = list;
      ret = WubCall("maybeathome.results", [ [. "args" = [sessionkey,"",type,address,price,beds,baths,rooms,size,lotSize,features,description], "listings" = list .] ])
   end
   
  
end;

ret;