]> git.plutz.net Git - serve0/blob - widgets.sh
simplification and sanitizing
[serve0] / widgets.sh
1 #!/bin/sh
2
3 [ -n "$include_widgets" ] && return 0
4 include_widgets="$0"
5
6 . "$_EXEC/cgilite/storage.sh"
7
8 w_refuri="$(HTML "$REQUEST_URI")"
9
10 w_tags="$(find "$_DATA/" -path '*/.index/meta' -print0 \
11         | xargs -r0 cut -f4 \
12         | cut -d= -f2- |tr , '\n' | sort -u)"
13 w_tags="$(UNSTRING "$w_tags")"
14 w_tagcategories="$(printf %s "$w_tags" |cut -sd: -f1 |sort -u)"
15
16 CHECK(){ [ "$1" = "$2" ] && printf checked; }
17
18 w_search(){
19   printf '
20   [form #search method=GET action=?
21     [select name=o size=1 
22       [option disabled=disabled Order By]
23       [option value=Name %s Name]
24       [option value=Date %s Date]
25       [option value=Length %s Length]
26     ]
27     [input name=s placeholder=Search value="%s"]
28   ]
29   ' \
30   "$(CHECK "$ORDER" Name)" \
31   "$(CHECK "$ORDER" Date)" \
32   "$(CHECK "$ORDER" Length)" \
33   "$(HTML "$SEARCH")"
34 }
35
36 w_prefs(){
37   local fakemp4 mode
38   fakemp4="$(COOKIE fakemp4)"
39   mode="$(COOKIE mode)"
40   printf '
41   [form #prefs method="POST" action="?a=setprefs"
42     [a href="#" x]
43     [hidden "ref" "%s"]
44     [label for=prefs_ps Pagesize]
45     [input #prefs_ps type=number name=pagesize value="%s"][br]
46     [radio "mode" "browse" %s #prefs_modebrowse] [label for=prefs_modebrowse Browse Folders][br]
47     [radio "mode" "index"  %s #prefs_modeindex ] [label for=prefs_modeindex View Full Index][br]
48     [checkbox "fakemp4" "yes" %s #prefs_fmp4] [label for=prefs_fmp4 Fake .MP4 file ending][br]
49     [submit "store" "store" Set Cookie]
50   ]
51   ' \
52   "$w_refuri" "$LISTSIZE" \
53   "$([ "$mode" = index  ] || CHECK)" \
54   "$(CHECK "$mode" index)" \
55   "$(CHECK "$fakemp4" yes)"
56 }
57
58 w_index(){
59   [ -d "$_DATA/$ITEM/.index" ] || printf '
60   [form #index method="POST" action="?a=spawnindex"
61     [hidden "ref" "%s"]
62     [label Set up for Index view: ]
63     [checkbox "recursive" "yes" #spawn_recursive] [label for=spawn_recursive Include subdirectories]
64     [submit "spawn" "spawn" Set up]
65   ]
66   ' "$w_refuri"
67 }
68
69 w_advsearch(){
70   local n lbid tag category tn filter f
71   filter="${FILTER}^"
72
73   printf '[form #advsearch action=?a=advsearch method=POST
74             [a href="#" Hide]
75             [p .help Select multiple tags from each category by holding down the [strong Ctrl] key on your keyboard.[br]
76             Refine the search further by setting additional search tags using the [strong "+and"] button.]
77             [hidden "ref" "%s"]
78          ' "$w_refuri"
79
80   for n in 1 2 3 4 5 6 7 8 9 10; do
81     f="${filter%%^*}"; filter="${filter#*^}"
82
83     lbid="$(HTML "cat_${n}_(none)")"
84     printf '[input .and type=checkbox name=and id="and_%i" %s][label for="and_%i" +and
85             ][fieldset .select
86             [radio "pol_%i" "pos" .pol %s #pol_pos_%i"][label for=pol_pos_%i Any]
87             [radio "pol_%i" "neg" .pol %s #pol_neg_%i"][label for=pol_neg_%i None]
88             [label .head Category:]' \
89             $n "$([ "$f" ] && printf checked)" $n \
90             $n "$([ "$f" -a ! "${f%%~*}" ] || printf checked)" $n $n \
91             $n "$([ "$f" -a ! "${f%%~*}" ] && printf checked)" $n $n
92     f="|${f#~}|"
93
94     printf '*\n%s\n' "$w_tagcategories" \
95     | while read -r category; do
96       catn="$(HTML "$category")"
97       lbid="cat_${n}_${catn}"
98       printf '[radio "cat_%i" "%s" .cat id="%s" %s][label for="%s" %s]
99               [select name=tag_%s size=10 multiple' \
100               $n "$catn" "$lbid" \
101               "$([ ! "${f%%*|${category}:*}" -o ! "${category%\*}" ] && printf checked)" \
102               "$lbid" "$catn" $n
103
104       printf %s "$w_tags" \
105       | { [ "$category" = '*' ] && grep -vF ':' || grep -wF "${category}"; } \
106       | while read -r tag; do
107         [ ! "$tag" ] && continue
108         tg="$(HTML "$tag")"; tn="${tg#*:}"
109         printf '[option %s value="%s"\n%s]' \
110           "$([ ! "${f%%*|${tag}|*}" ] && printf selected)" \
111           "$tg" "$tn"
112       done
113       printf '\n]'
114     done
115     printf ']'
116   done
117
118   printf '[fieldset .submit [select name=order
119             [option disabled=disabled Order By]
120             [option value=Name %s Name]
121             [option value=Date %s Date]
122             [option value=Length %s Length]
123           ][button type=submit Apply Filter]]
124           ]' \
125           "$(CHECK "$ORDER" Name)" \
126           "$(CHECK "$ORDER" Date)" \
127           "$(CHECK "$ORDER" Length)"
128 }
129
130 w_tagging(){
131   local tag category
132   printf '[div #multitag [input type="hidden" name="ref" value="%s"]' "$w_refuri"
133   printf '[a href="#" Hide][br]'
134
135   printf 'Tags\n%s\n' "$w_tagcategories" \
136   | while read -r category; do
137     printf '[fieldset [legend %s:][select name=tag size=4 multiple' "$(HTML "$category")"
138     printf %s "$w_tags" \
139     | { [ "$category" = 'Tags' ] && grep -vF ':' || grep -wF "${category}"; } \
140     | while read -r tag; do
141       [ ! "$tag" ] && continue
142       tag="$(HTML "$tag")"; tn="${tag#*:}"
143       printf '[option value="%s"\n%s]' "$tag" "$tn"
144     done
145     printf ']]'
146   done
147
148   printf '[fieldset [legend New:][textarea name=newtag\n][button type=Submit Add Tags]]]'
149 }