unification of card editing functions
[confetti] / templates / edit_client.sh
index b2331aa..f2c5afb 100755 (executable)
@@ -40,17 +40,36 @@ teltype() {
        EOF
 }
 
-n_last="$(printf %s "$values[N]" |sed -rn 's:^([^;]*;){0} *([^;]*).*$:\2:p')"
-n_first="$(printf %s "$values[N]" |sed -rn 's:^([^;]*;){1} *([^;]*).*$:\2:p')"
-n_middle="$(printf %s "$values[N]" |sed -rn 's:^([^;]*;){2} *([^;]*).*$:\2:p')"
-n_pre="$(printf %s "$values[N]" |sed -rn 's:^([^;]*;){3} *([^;]*).*$:\2:p')"
-n_post="$(printf %s "$values[N]" |sed -rn 's:^([^;]*;){4} *([^;]*)*$:\2:p')"
-
-hi_company="$(printf %s "${values[X-HEALTH-INSURANCE]}" |cut -d\; -f1)"
-hi_number="$(printf %s "${values[X-HEALTH-INSURANCE]}" |cut -d\; -f2)"
-hi_status="$(printf %s "${values[X-HEALTH-INSURANCE]}" |cut -d\; -f3)"
+list_items(){
+  item="$1"
+  placeholder="$2"
+  [ -n "${values[$item]+x}" ] && printf '<h3>%s</h3>\n' "$(l10n $item)"
+  for n in "$item" "$item"{0..100}; do
+    if [ -n "${values[$n]+x}" ]; then
+      case "$item" in
+        (ADR|NOTE)
+          printf '<textarea class="item %s" name="%s">%s</textarea>\n' \
+            "$item" "$item" "$(htmlsafe ${values[$n]})"
+          ;;
+        (TEL)
+          teltype "${values[${n}_TYPE]}"
+          printf '<input class="item %s" name="%s" value="%s" />\n' \
+            "$item" "$item" "$(attribsafe ${values[$n]})"
+          ;;
+        (*)
+          printf '<input class="item %s" name="%s" value="%s" placeholder="%s"/>\n' \
+            "$item" "$item" "$(attribsafe ${values[$n]})" "$placeholder"
+          ;;
+      esac
+    else
+      break
+    fi
+  done
+}
 
-SUP_FIELDS=(N NICKNAME GENDER BDAY ADR TEL EMAIL X-HEALTH-INSURANCE X-HEALTH-INSURANCE-NOCONTRIB IMPP URL NOTE X-CLIENT-REFERRAL)
+hi_company="${values[X-HEALTH-INSURANCE+1]}"
+hi_number= "${values[X-HEALTH-INSURANCE+2]}"
+hi_status= "${values[X-HEALTH-INSURANCE+3]}"
 
 [ -z "$values[UID]" ] && values[UID]="$(uuidgenerator)"
 
@@ -59,11 +78,11 @@ cat <<END_HTML
   <input type="hidden" name="card" value="${id}"/>
   <div class="section basic">
     <h3>$(l10n N)</h3>
-    <input class="item N" name="3N" placeholder="$(l10n n_pre)" value="$n_pre" />
-    <input class="item N" name="1N" placeholder="$(l10n n_first)" value="$n_first" />
-    <input class="item N" name="2N" placeholder="$(l10n n_middle)" value="$n_middle" />
-    <input class="item N" name="0N" placeholder="$(l10n n_last)" value="$n_last" />
-    <input class="item N" name="4N" placeholder="$(l10n n_post)" value="$n_post" />
+    <input class="item N" name="3N" placeholder="$(l10n n_pre)" value="$values[N+3]" />
+    <input class="item N" name="1N" placeholder="$(l10n n_first)" value="$values[N+1]" />
+    <input class="item N" name="2N" placeholder="$(l10n n_middle)" value="$values[N+2]" />
+    <input class="item N" name="0N" placeholder="$(l10n n_last)" value="$values[N+0]" />
+    <input class="item N" name="4N" placeholder="$(l10n n_post)" value="$values[N+4]" />
 
     <select class="item GENDER" name="GENDER">
       <option value="" disabled="disabled">$(l10n GENDER)</option>
@@ -74,49 +93,32 @@ cat <<END_HTML
     </select>
 
     $(for n in NICKNAME NICKNAME{0..10}; do
-      echo "${values[$n]:+<input class="item NICKNAME" name="NICKNAME" placeholder="$(l10n NICKNAME)" value="$values[$n]" />}"
+      printf %s\\n "${values[$n]+<input class="item NICKNAME" name="NICKNAME" placeholder="$(l10n NICKNAME)" value="$values[$n]" />}"
     done)
 
-    <h3>$(l10n BDAY)</h3>
-    <input class="item BDAY" name="BDAY" value="$values[BDAY]" placeholder="YYYY-MM-DD" />
+    $(list_items BDAY YYYY-MM-DD)
+
+    $(list_items X-ZACK-JOINDATE YYYY-MM-DD)
+    $(list_items X-ZACK-LEAVEDATE YYYY-MM-DD)
 
-    $(if [ -n "$values[SOUND]" ]; then
-      echo '    <audio controls="controls" class="item SOUND">'
-      echo '      <source type="audio/ogg" src="data:audio/ogg;base64,'$values[SOUND]'" />'
-      echo '    </audio>'
-    fi)
-    ${values[PHOTO]:+<img class="item PHOTO" src="data:image/$values[PHOTO_TYPE];base64,$values[PHOTO]" />}
-    ${values[LOGO]:+<img class="item LOGO" src="data:image/$values[LOGO_TYPE];base64,$values[LOGO]" />}
+    $([ -n "$values[SOUND]" ] && printf '
+      <audio controls="controls" class="item SOUND">
+        <source type="audio/ogg" src="data:audio/ogg;base64,%s" />
+      </audio>' "$values[SOUND]"
+    )
+    ${values[PHOTO]+<img class="item PHOTO" src="data:image/$values[PHOTO_TYPE];base64,$values[PHOTO]" />}
+    ${values[LOGO]+<img class="item LOGO" src="data:image/$values[LOGO_TYPE];base64,$values[LOGO]" />}
   </div>
 
   <div class="section address">
-    <h3>$(l10n ADR)</h3>
-    <textarea class="item ADR" name="ADR">$values[ADR]</textarea>
-    $(for n in ADR{0..10}; do
-      echo "${values[$n]:+<textarea class="item ADR" name="ADR">$values[$n]</textarea>}"
-    done)
-    ${values[URL]:+<h3>$(l10n URL)</h3>}
-    $(for n in URL URL{0..10}; do
-      echo "${values[$n]:+<input class="item URL" name="URL" value="$values[$n]" />}"
-    done)
+    $(list_items ADR)
   </div>
 
   <div class="section phone">
-    <h3>$(l10n TEL)</h3>
-    $(teltype "${values[TEL_TYPE]}")<input class="item TEL" name="TEL" value="$values[TEL]" />
-    $(for n in TEL{0..10}; do
-      printf "${values[$n]:+$(teltype "${values[${n}_TYPE]}")<input class="item TEL" name="TEL" value="'$values[$n]'" />}"
-    done)
-
-    <h3>$(l10n EMAIL)</h3>
-    <input class="item EMAIL" name="EMAIL" value="$values[EMAIL]" />
-    $(for n in EMAIL{0..10}; do
-      printf "${values[$n]:+<input class="item EMAIL" name="EMAIL" value="$values[$n]" />}"
-    done)
-    ${values[IMPP]:+<h3>$(l10n IMPP)</h3>}
-    $(for n in IMPP IMPP{0..10}; do
-      echo "${values[$n]:+<input class="item IMPP" name="IMPP" value="$values[$n]" />}"
-    done)
+    $(list_items TEL)
+    $(list_items EMAIL)
+    $(list_items IMPP)
+    $(list_items URL)
   </div>
 
   <div class="section insurance">
@@ -138,27 +140,19 @@ cat <<END_HTML
   </div>
 
   <div class="section note">
-    <h3>$(l10n NOTE)</h3>
-    <textarea class="item NOTE" name="NOTE">$values[NOTE]</textarea>
-    $(for n in NOTE{0..10}; do
-      echo "${values[$n]:+<textarea class="item NOTE" name="NOTE">$values[$n]</textarea>}"
-    done)
-    <h3>$(l10n X-CLIENT-REFERRAL)</h3>
-    <input class="item X-CLIENT-REFERRAL" name="X-CLIENT-REFERRAL" value="$values[X-CLIENT-REFERRAL]" />
-    $(for n in X-CLIENT-REFERRAL{0..10}; do
-      echo "${values[$n]:+<input class="item X-CLIENT-REFERRAL" name="X-CLIENT-REFERRAL" value="'$values[$n]'" />}"
-    done)
+    $(list_items NOTE)
+    $(list_items X-CLIENT-REFERRAL)
   </div>
 
   <div class="control">
     <select class="item" name="newfield">
       <option value="" disabled="disabled" selected="selected">$(l10n edit_addfieldtext)</option>
-      $(for f in $SUP_FIELDS; do echo "<option value=\"$f\">$(l10n $f)</option>"; done)
+      $(for f in $SUP_FIELDS; do printf %s\\n "<option value=\"$f\">$(l10n $f)</option>"; done)
     </select
     ><button class="item" type="submit" name="action" value="addfield">$(l10n edit_addfield)</button>
     <button class="item" type="submit" name="action" value="update">$(l10n edit_update)</button>
     <input type="checkbox" id="delete"><label class="item" for="delete">$(l10n edit_delete)</label>
-    <button class="item" type="submit" name="action" value="delete">$(l10n edit_delete)</button>
+      <button class="item" type="submit" name="action" value="delete">$(l10n edit_delete)</button>
     <button class="item" type="submit" name="action" value="cancel">$(l10n edit_cancel)</button>
   </div>
   <input type="hidden" name="UID" value="$values[UID]"/>