changeset 114:c803235bd0f9

Implement flag config option type.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 20 Mar 2017 15:37:19 +0200
parents 4f95ed80583c
children a3bf93b85758
files mgallery.inc.php mgtool.php
diffstat 2 files changed, 46 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/mgallery.inc.php	Mon Mar 20 14:47:56 2017 +0200
+++ b/mgallery.inc.php	Mon Mar 20 15:37:19 2017 +0200
@@ -17,6 +17,7 @@
 //
 define("MGF_JAVASCRIPT"   , 0x01);
 define("MGF_BREADCRUMBS"  , 0x10);
+define("MGF_CAPTIONS"     , 0x20);
 
 //
 // Constants for different value types
@@ -25,12 +26,20 @@
 define("MG_INT", 2);
 define("MG_DVA", 3);
 define("MG_BOOL", 4);
+define("MG_FLAGS", 5);
 
 
 define("yes", 1);
 define("no", 0);
 
 
+$mgGFlags = [
+  "javascript"      => MGF_JAVASCRIPT,
+  "breadcrumbs"     => MGF_BREADCRUMBS,
+  "captions"        => MGF_CAPTIONS,
+];
+
+
 function mgPathName($path)
 {
   $tmp = mgCleanPathArray(TRUE, 0, func_num_args(), func_get_args());
@@ -66,8 +75,9 @@
   "urchin_file"      => array(MG_STR, FALSE),
   "album_icon"       => array(MG_STR, "album_sm.png"),
 
-  "image_flags"      => array(MG_FLAGS, MGF_JAVASCRIPT | MGF_BREADCRUMBS),
-  "album_flags"      => array(MG_FLAGS, MGF_JAVASCRIPT | MGF_BREADCRUMBS),
+  "global_flags"     => array(MG_FLAGS, MGF_JAVASCRIPT | MGF_BREADCRUMBS | MGF_CAPTIONS, &$mgGFlags),
+  "image_flags"      => array(MG_FLAGS, MGF_JAVASCRIPT | MGF_BREADCRUMBS, &$mgGFlags),
+  "album_flags"      => array(MG_FLAGS, MGF_JAVASCRIPT | MGF_BREADCRUMBS, &$mgGFlags),
   "album_row_limit"  => array(MG_INT, 5),
 
   "med_suffix"       => array(MG_STR, ".med"),
@@ -148,6 +158,25 @@
       mgFatal("Setting '".$key."' is not set, but is required to be configured.\n");
   }
 
+  switch ($mgDefaults[$key][0])
+  {
+    case MG_FLAGS:
+      if (is_string($val))
+      {
+        $flags = $mgDefaults[$key][2];
+        $cval = preg_split("/\s*[,|]\s*/", strtolower($val), -1, PREG_SPLIT_NO_EMPTY);
+        $nval = 0;
+        foreach ($cval as $qval)
+        {
+          if (array_key_exists($qval, $flags))
+            $nval |= $flags[$qval];
+          else
+            mgFatal("Invalid flag value for '".$key."': '".$qval."'.");
+        }
+      }
+      break;
+  }
+
   return $val;
 }
 
@@ -297,12 +326,22 @@
 }
 
 
-function mgGetDValStr($type, $val)
+function mgGetDValStr($mdef, $val)
 {
-  switch ($type)
+  switch ($mdef[0])
   {
     case MG_STR  : return "\"".$val."\"";
     case MG_BOOL : return $val ? "yes" : "no";
+    case MG_FLAGS:
+      {
+        $mstr = [];
+        foreach ($mdef[2] as $vkey => $vval)
+        {
+          if ($val & $vval)
+            $mstr[] = $vkey;
+        }
+        return implode($mstr, " | ");
+      }
     case MG_INT  :
     default      : return (string) $val;
   }
--- a/mgtool.php	Mon Mar 20 14:47:56 2017 +0200
+++ b/mgtool.php	Mon Mar 20 15:37:19 2017 +0200
@@ -853,14 +853,14 @@
       $sval = mgGetSetting($key);
       if ($cmd == "dump")
       {
-        printf("%-20s = %s\n", $key, mgGetDValStr($dval[0], $sval));
+        printf("%-20s = %s\n", $key, mgGetDValStr($dval, $sval));
       }
       else
       {
         printf("%-20s = %s%s\n",
           $key,
-          mgGetDValStr($dval[0], $sval),
-          ($dval[1] !== NULL && $sval !== $dval[1]) ? " (default: ".mgGetDValStr($dval[0], $dval[1]).")" : "");
+          mgGetDValStr($dval, $sval),
+          ($dval[1] !== NULL && $sval !== $dval[1]) ? " (default: ".mgGetDValStr($dval, $dval[1]).")" : "");
       }
     }
     break;