@@ -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 /**
3133 * @param array<string, mixed> $datas
3234 */
@@ -52,6 +54,9 @@ public function getcode(): string
5254 */
5355 public function listhtml (array $ pagelist , Page $ currentpage ): string
5456 {
57+ $ pagecount = count ($ pagelist );
58+ $ html = '' ;
59+
5560 if ($ this ->hidecurrent && key_exists ($ currentpage ->id (), $ pagelist )) {
5661 unset($ pagelist [$ currentpage ->id ()]);
5762 }
@@ -137,16 +142,63 @@ public function listhtml(array $pagelist, Page $currentpage): string
137142 $ thumbnail ->setAttribute ('alt ' , htmlspecialchars ($ page ->title ()));
138143 $ parent ->appendChild ($ thumbnail );
139144 }
145+ if ($ this ->checkbox ) {
146+ $ tags = $ page ->tag ();
147+ $ this ->addusefulltags ($ tags );
148+ foreach ($ tags as $ tag ) {
149+ $ parent ->setAttribute ("data-tag_ $ tag " , '1 ' );
150+ }
151+ }
140152 }
141-
142153 $ dom ->appendChild ($ ul );
143154
144- return $ dom ->saveHTML ($ dom ->documentElement );
155+ if ($ this ->checkbox ) {
156+ $ domform = new DOMDocument ('1.0 ' , 'UTF-8 ' );
157+ $ form = $ domform ->createElement ('form ' );
158+ foreach ($ this ->usefulltags as $ tag => $ count ) {
159+ if ($ count === $ pagecount ) {
160+ continue ; // skip this tag as it's used by all pages
161+ }
162+ $ span = $ domform ->createElement ('span ' );
163+ $ id = "checkbox-tag_ $ tag " ;
164+ $ input = $ domform ->createElement ('input ' );
165+ $ input ->setAttribute ('id ' , $ id );
166+ $ input ->setAttribute ('value ' , $ tag );
167+ $ input ->setAttribute ('type ' , 'checkbox ' );
168+ $ label = $ domform ->createElement ('label ' , $ tag );
169+ $ label ->setAttribute ('for ' , $ id );
170+ $ span ->appendChild ($ input );
171+ $ span ->appendChild ($ label );
172+ $ form ->appendChild ($ span );
173+ }
174+ $ domform ->appendChild ($ form );
175+ $ html .= $ domform ->saveHTML ($ domform ->documentElement );
176+ $ html .= "\n" ;
177+ }
178+
179+ $ html .= $ dom ->saveHTML ($ dom ->documentElement );
180+
181+ return $ html ;
145182 } catch (DOMException $ e ) {
146183 throw new LogicException ('bad DOM node used ' , 0 , $ e );
147184 }
148185 }
149186
187+ /**
188+ * merge list of tags within the list of usefull tags.
189+ * Tag names are stored as key and value count the time it's used.
190+ */
191+ private function addusefulltags (array $ tags ): void
192+ {
193+ foreach ($ tags as $ tag ) {
194+ if (key_exists ($ tag , $ this ->usefulltags )) {
195+ $ this ->usefulltags [$ tag ] ++;
196+ } else {
197+ $ this ->usefulltags [$ tag ] = 1 ;
198+ }
199+ }
200+ }
201+
150202
151203
152204
@@ -188,6 +240,11 @@ public function hidecurrent(): bool
188240 return $ this ->hidecurrent ;
189241 }
190242
243+ public function checkbox (): bool
244+ {
245+ return $ this ->checkbox ;
246+ }
247+
191248 public function style (): string
192249 {
193250 return $ this ->style ;
@@ -231,7 +288,12 @@ public function sethidecurrent(bool $hidecurrent): void
231288 $ this ->hidecurrent = $ hidecurrent ;
232289 }
233290
234- public function setstyle (string $ style ): void
291+ public function setcheckbox ($ checkbox )
292+ {
293+ $ this ->checkbox = boolval ($ checkbox );
294+ }
295+
296+ public function setstyle (string $ style )
235297 {
236298 if (key_exists ($ style , self ::STYLES )) {
237299 $ this ->style = $ style ;
0 commit comments