@@ -17,7 +17,7 @@ class Optlist extends Optcode
1717 protected bool $ time = false ;
1818 protected bool $ author = false ;
1919 protected bool $ hidecurrent = false ;
20- protected bool $ tags = false ;
20+ protected bool $ checkbox = false ;
2121 protected string $ style = self ::LIST ;
2222
2323 public const LIST = 'list ' ;
@@ -27,6 +27,8 @@ class Optlist extends Optcode
2727 self ::CARD , self ::CARD ,
2828 ];
2929
30+ protected array $ usefulltags = [];
31+
3032 public function __construct (array $ datas = [])
3133 {
3234 parent ::__construct ($ datas );
@@ -49,6 +51,9 @@ public function getcode(): string
4951 */
5052 public function listhtml (array $ pagelist , Page $ currentpage ): string
5153 {
54+ $ pagecount = count ($ pagelist );
55+ $ html = '' ;
56+
5257 if ($ this ->hidecurrent && key_exists ($ currentpage ->id (), $ pagelist )) {
5358 unset($ pagelist [$ currentpage ->id ()]);
5459 }
@@ -59,7 +64,6 @@ public function listhtml(array $pagelist, Page $currentpage): string
5964 $ timeformatter = new IntlDateFormatter ($ lang , IntlDateFormatter::NONE , IntlDateFormatter::SHORT );
6065
6166 try {
62- $ dom = new DOMDocument ();
6367 $ dom = new DOMDocument ('1.0 ' , 'UTF-8 ' );
6468
6569 $ ul = $ dom ->createElement ('ul ' );
@@ -135,16 +139,63 @@ public function listhtml(array $pagelist, Page $currentpage): string
135139 $ thumbnail ->setAttribute ('alt ' , htmlspecialchars ($ page ->title ()));
136140 $ parent ->appendChild ($ thumbnail );
137141 }
142+ if ($ this ->checkbox ) {
143+ $ tags = $ page ->tag ();
144+ $ this ->addusefulltags ($ tags );
145+ foreach ($ tags as $ tag ) {
146+ $ parent ->setAttribute ("data-tag_ $ tag " , '1 ' );
147+ }
148+ }
138149 }
139-
140150 $ dom ->appendChild ($ ul );
141151
142- return $ dom ->saveHTML ($ dom ->documentElement );
152+ if ($ this ->checkbox ) {
153+ $ domform = new DOMDocument ('1.0 ' , 'UTF-8 ' );
154+ $ form = $ domform ->createElement ('form ' );
155+ foreach ($ this ->usefulltags as $ tag => $ count ) {
156+ if ($ count === $ pagecount ) {
157+ continue ; // skip this tag as it's used by all pages
158+ }
159+ $ span = $ domform ->createElement ('span ' );
160+ $ id = "checkbox-tag_ $ tag " ;
161+ $ input = $ domform ->createElement ('input ' );
162+ $ input ->setAttribute ('id ' , $ id );
163+ $ input ->setAttribute ('value ' , $ tag );
164+ $ input ->setAttribute ('type ' , 'checkbox ' );
165+ $ label = $ domform ->createElement ('label ' , $ tag );
166+ $ label ->setAttribute ('for ' , $ id );
167+ $ span ->appendChild ($ input );
168+ $ span ->appendChild ($ label );
169+ $ form ->appendChild ($ span );
170+ }
171+ $ domform ->appendChild ($ form );
172+ $ html .= $ domform ->saveHTML ($ domform ->documentElement );
173+ $ html .= "\n" ;
174+ }
175+
176+ $ html .= $ dom ->saveHTML ($ dom ->documentElement );
177+
178+ return $ html ;
143179 } catch (DOMException $ e ) {
144180 throw new LogicException ('bad DOM node used ' , 0 , $ e );
145181 }
146182 }
147183
184+ /**
185+ * merge list of tags within the list of usefull tags.
186+ * Tag names are stored as key and value count the time it's used.
187+ */
188+ private function addusefulltags (array $ tags ): void
189+ {
190+ foreach ($ tags as $ tag ) {
191+ if (key_exists ($ tag , $ this ->usefulltags )) {
192+ $ this ->usefulltags [$ tag ] ++;
193+ } else {
194+ $ this ->usefulltags [$ tag ] = 1 ;
195+ }
196+ }
197+ }
198+
148199
149200
150201
@@ -186,6 +237,11 @@ public function hidecurrent(): bool
186237 return $ this ->hidecurrent ;
187238 }
188239
240+ public function checkbox (): bool
241+ {
242+ return $ this ->checkbox ;
243+ }
244+
189245 public function style ()
190246 {
191247 return $ this ->style ;
@@ -229,6 +285,11 @@ public function sethidecurrent($hidecurrent)
229285 $ this ->hidecurrent = boolval ($ hidecurrent );
230286 }
231287
288+ public function setcheckbox ($ checkbox )
289+ {
290+ $ this ->checkbox = boolval ($ checkbox );
291+ }
292+
232293 public function setstyle ($ style )
233294 {
234295 if (is_string ($ style ) && key_exists ($ style , self ::STYLES )) {
0 commit comments