Source: site.view [edit]
Function name: addListing
Arguments: sessionkey,type,address,price,size,lotSize,beds,baths,features,description
Description: Add a new listing
Page type: webl
Render function:  
Module: maybeathome

Page source:

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

address = Str_Trim(address);
price = Wub_ReplaceAll(Wub_ReplaceAll(Str_Trim(price), ",", ""), "$", "");
size = Wub_ReplaceAll(Str_Trim(size), ",", "");
lotSize = Wub_ReplaceAll(Str_Trim(lotSize), ",", "");
beds = Str_Trim(beds);
baths = Str_Trim(baths);
description = Str_Trim(description);

var userInfo = WubCall("siteutil.getSessionInfo", ["maybeathome", sessionkey]);

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

if (sessionkey == "nil" or sessionkey == "") then
   ret = WubCall("maybeathome.login", [])

elsif (userInfo.arg0 == "buyer") then
   msg = "You must be logged in with a seller or realtor account to add a listing";
   ret = WubCall("maybeathome.sell", [msg, sessionkey, type, address, price, size, lotSize, beds, baths, features, description])

elsif (address == "") then
   msg = "What is the address of your property?";
   ret = WubCall("maybeathome.sell", [msg, sessionkey, type, address, price, size, lotSize, beds, baths, features, description])

elsif (geocode != nil and geocode.streetNumber == nil) then
   msg = "I couldn't find that address.";
   ret = WubCall("maybeathome.sell", [msg, sessionkey, type, address, price, size, lotSize, beds, baths, features, description])
   
elsif (price == "" or (ToInt(price) ? -1) <= 0) then
   msg = "Please correct 'price'";
   ret = WubCall("maybeathome.sell", [msg, sessionkey, type, address, price, size, lotSize, beds, baths, features, description])

elsif (size == "" or (ToInt(size) ? -1) <= 10) then
   msg = "Please correct 'size'";
   ret = WubCall("maybeathome.sell", [msg, sessionkey, type, address, price, size, lotSize, beds, baths, features, description])

elsif (lotSize == "" or (ToInt(lotSize) ? -1) <= 10) then
   msg = "Please correct 'lot size'";
   ret = WubCall("maybeathome.sell", [msg, sessionkey, type, address, price, size, lotSize, beds, baths, features, description])

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

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

end;   

// No errors: add it
if (ret == nil) then
   var db = Wub_GetDB("maybeathome");
   var coll = Wub_GetCollection(db, "listings");

   var dbobj = Wub_NewDBObject([. subpremise = geocode.subpremise, streetNumber = geocode.streetNumber, street = geocode.street, 
                                  postalCode = geocode.postalCode  .]); 
   var res = Wub_QueryDB(coll, dbobj, 20);
   if (Size(res) > 0) then
       // should update
       msg = "Listing already added.";
       ret = WubCall("maybeathome.sell", [msg, sessionkey, type, address, price, size, lotSize, beds, baths, features, description])
   end;

   if (ret == nil) then
      var data = [. subpremise = geocode.subpremise, streetNumber = geocode.streetNumber, street = geocode.street, postalCode = geocode.postalCode,
                    city = geocode.city, state = geocode.state, county = geocode.county, country = geocode.country,
                    lat = geocode.lat, lon = geocode.long, formatted = geocode.formatted, 
                    sessionkey = sessionkey, owner = userInfo.username, ownertype = userInfo.arg0, 
                    price = ToInt(price), size = ToInt(size), lotSize = ToInt(lotSize), 
                    beds = ToInt(beds), baths = ToInt(baths), features = features, description = description .];
  
      // add user's information  
      dbobj = Wub_NewDBObject(data); 
      Wub_InsertDB(coll, dbobj);
      msg = "Listing added!";
      ret = WubCall("maybeathome.sell", [msg, sessionkey, type, address, price, size, lotSize, beds, baths, features, description])
   end
end;
  
ret;