6082b925a9a70897583ac4251c96006a24b6c1c8
tdreszer
  Thu Apr 25 12:52:24 2013 -0700
Added cartTimeoutBoolean, which allows setting a var to true and having it timeout after a given number of hours.
diff --git src/hg/lib/cart.c src/hg/lib/cart.c
index f5a79e4..2039bb8 100644
--- src/hg/lib/cart.c
+++ src/hg/lib/cart.c
@@ -1135,30 +1135,61 @@
 {
 if (cgiBooleanDefined(var))
     return cgiBoolean(var);
 if (cart != NULL)
     return cgiBoolean(var) || cartUsualBoolean(cart, var, usual);
 return(usual);
 }
 
 void cartSetBoolean(struct cart *cart, char *var, boolean val)
 /* Set boolean value. */
 {
 // Be explicit because some cartBools overloaded with negative "disabled" values
 cartSetInt(cart,var,(val?1:0));
 }
 
+boolean cartTimeoutBoolean(struct cart *cart, char *var, int hours)
+// Returns true if a cart var was set to non-zero less than hours ago
+// If the var has expired or val=0, it will be deleted.
+// If val is non-zero and not a time_t, (e.g. 'set') then the timer is started.
+{
+char *s = cartOptionalString(cart, var);
+if (s == NULL)
+    return FALSE;
+if (sameString(s,"0"))
+    {
+    cartRemove(cart, var);
+    return FALSE;
+    }
+
+time_t seconds = clock1();
+time_t val = (time_t)atoll(s);
+if (val < 1000)
+    {
+    char buf[64];
+    safef(buf, sizeof(buf), "%ld", seconds);
+    cartSetString(cart, var, buf);
+    return TRUE;
+    }
+if (val + (hours * 3600) < seconds)
+    {
+    cartRemove(cart, var);
+    return FALSE;
+    }
+return TRUE;
+}
+
 void cartMakeTextVar(struct cart *cart, char *var, char *defaultVal, int charSize)
 /* Make a text control filled with value from cart if it exists or
  * default value otherwise.  If charSize is zero it's calculated to fit
  * current value.  Default value may be NULL. */
 {
 cgiMakeTextVar(var, cartUsualString(cart, var, defaultVal), charSize);
 }
 
 void cartMakeIntVar(struct cart *cart, char *var, int defaultVal, int maxDigits)
 /* Make a text control filled with integer value - from cart if available
  * otherwise default.  */
 {
 cgiMakeIntVar(var, cartUsualInt(cart, var, defaultVal), maxDigits);
 }