d1c82531fdb1f0833584404c75e3c977508e454f
tdreszer
  Tue Mar 8 16:23:55 2011 -0800
Added lib routine for creating a single/multiple drop down, where the multiple will be expected to be converted into a ui-dropdownchecklist.js type multi-select
diff --git src/lib/cheapcgi.c src/lib/cheapcgi.c
index d338e3c..9574d27 100644
--- src/lib/cheapcgi.c
+++ src/lib/cheapcgi.c
@@ -1646,30 +1646,104 @@
     {
     printf("<SELECT NAME=\"%s\">\n", name);
     }
 
 for (i=0; i<menuSize; ++i)
     {
     if (sameWord(values[i], checked))
         selString = " SELECTED";
     else
         selString = "";
     printf("<OPTION%s VALUE=\"%s\">%s</OPTION>\n", selString, values[i], menu[i]);
     }
 printf("</SELECT>\n");
 }
 
+char *cgiMakeSelectDropList(boolean multiple, char *name, struct slPair *valsAndLabels,char *selected, char *anyAll,char *extraClasses, char *extraHtml)
+// Returns allocated string of HTML defining a drop-down select (if multiple, REQUIRES ui-dropdownchecklist.js)
+// In valsAndLabels, val (pair->name) must be filled in but label (pair->val) may be NULL.
+// selected, if not NULL is a val found in the valsAndLabels (multiple then comma delimited list).  If null and anyAll not NULL, that will be selected
+// anyAll, if not NULL is the string for an initial option.  It can contain val and label, delimited by a comma
+// extraHtml, if not NULL contains id, javascript calls and style.  It does NOT contain class definitions
+{
+struct dyString *output = dyStringNew(1024);
+boolean checked = FALSE;
+
+dyStringPrintf(output,"<SELECT name='%s'",name);
+if (multiple)
+    dyStringAppend(output," MULTIPLE");
+if (extraClasses != NULL)
+    dyStringPrintf(output," class='%s%s'",extraClasses,(multiple?" filterBy":""));
+else if (multiple)
+    dyStringAppend(output," class='filterBy'");
+
+if (extraHtml != NULL)
+    dyStringPrintf(output," %s",extraHtml);
+dyStringAppend(output,">\n");
+
+// Handle initial option "Any" or "All"
+if (anyAll != NULL)
+    {
+    char *val = anyAll;  // Could contain a label after the value
+    char *label = strchr(val,',');  // Could contain a label after the value
+    if (label != NULL)
+        {
+        val = cloneString(anyAll);
+        label = strchr(val,',');  // again because this is new mem
+        *label = '\0';
+        label = label+1;
+        }
+    else
+        label = val;
+    checked = TRUE; // The default case
+    if (selected != NULL)
+        {
+        if (multiple)
+            checked = (findWordByDelimiter(val,',', selected) != NULL);
+        else
+            checked = sameString(val,selected);
+        }
+    dyStringPrintf(output, "<OPTION%s VALUE='%s'>%s</OPTION>\n",(checked?" SELECTED":""),
+                   val, javaScriptLiteralEncode(label));
+    if (label != val)
+        freeMem(val);
+    }
+
+// All other options
+struct slPair *valPair = valsAndLabels;
+for (; valPair != NULL; valPair = valPair->next)
+    {
+    checked = FALSE;
+    if (selected != NULL)
+        {
+        if (multiple)
+            checked = (findWordByDelimiter(valPair->name,',', selected) != NULL);
+        else
+            checked = sameString(valPair->name,selected);
+        }
+    char *label = valPair->name;
+    if (valPair->val != NULL)
+        label = valPair->val;
+    dyStringPrintf(output, "<OPTION%s VALUE='%s'>%s</OPTION>\n",(checked?" SELECTED":""),
+                   (char *)valPair->name, javaScriptLiteralEncode(label));
+    }
+
+dyStringPrintf(output,"</SELECT>\n");
+
+return dyStringCannibalize(&output);
+}
+
 void cgiMakeDropListWithVals(char *name, char *menu[], char *values[],
                          int menuSize, char *checked)
 /* Make a drop-down list with names and values. In this case checked
  * corresponds to a value, not a menu. */
 {
 int i;
 char *selString;
 if (checked == NULL) checked = values[0];
 
 printf("<SELECT NAME=\"%s\">\n", name);
 for (i=0; i<menuSize; ++i)
     {
     if (sameWord(values[i], checked))
         selString = " SELECTED";
     else