page: move placeholder_include() to widget method
authorMischa POSLAWSKY <perl@shiar.org>
Thu, 3 Dec 2020 21:28:09 +0000 (22:28 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Sat, 19 Dec 2020 02:01:43 +0000 (03:01 +0100)
article.inc.php
issue/index.php
login/edit/index.php
nieuws/index.php
page.php

index acd219d164aab81397d9da33ef104148f3e23574..b676dd0c5e34537bb17732e13dd5a6b5f35628ff 100644 (file)
@@ -170,6 +170,38 @@ class ArchiveArticle
                        $this->image
                );
        }
+
+       function widget($name, $params = [])
+       {
+               $path = stream_resolve_include_path("widget/$name.php");
+               if (!file_exists($path)) {
+                       return '<strong class="warn"><em>'.$name.'</em> ontbreekt</strong>';
+               }
+
+               ob_start();
+               $Page = clone $this;
+               $Page->handler = $Page->handler . $Page->path; // .= with explicit getter
+               $Page->path = '';
+               $Place = $GLOBALS['Place'];
+               foreach ($params as $param) {
+                       if ($set = strpos($param, '=')) {
+                               $Place[ substr($param, 0, $set) ] = substr($param, $set + 1);
+                       }
+                       elseif (!empty($param)) {
+                               $Page->path .= '/'.$param;
+                       }
+               }
+               $Page->link .= $Page->path;
+               try {
+                       include "widget/$name.php";
+                       return ob_get_clean();
+               }
+               catch (Exception $e) {
+                       return sprintf('<strong class="warn">%s</strong>',
+                               "fout in <em>$name</em>: {$e->getMessage()}"
+                       );
+               }
+       }
 }
 
 class PageSearch
index 41d37491378b22eac9d670ab7dbb8cf4dfb355f6..181d5b73e6380fec368787929fe291dace82030b 100644 (file)
@@ -11,7 +11,7 @@ if ($id and ctype_digit($id)) {
        )->fetch();
        if (!$Issue) throw new Exception('Issuenummer niet gevonden');
 
-       $replies = placeholder_include('reply');  # handle updates
+       $replies = $Page->widget('reply');  # handle updates
 
        $Page->title .= ': '.htmlspecialchars($Issue->subject);
        $Page->teaser = $Issue->body;
index 3360ec7bab093356a19be45f4313ad3faaf22204..2ba0cf38711729942f86ba353ab8db1c59207055 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 if ($Page->path and $User->admin('user')) {
        $user = new User(strtolower("profile{$Page->path}"), FALSE);
-       print placeholder_include('login/admin');
+       print $Page->widget('login/admin');
        $Page->title = "Profiel <em>{$user->login}</em>";
        print "<h2>{$Page->title}</h2>\n";
-       print placeholder_include('login/edit');
+       print $Page->widget('login/edit');
        return;
 }
index 08400153ff15dbaffdf2130d66078b16df0a5971..0bb72f9142ff3363c26f0209edf6df697e74f3fd 100644 (file)
@@ -41,7 +41,7 @@ if ($page and !is_numeric($page)) {
                }
        }
        if ($replyform) {
-               print placeholder_include('reply');
+               print $Page->widget('reply');
        }
        return;
 }
@@ -67,6 +67,6 @@ if ($year) {
        }
 
        print "<h2>$title</h2>\n\n";
-       print placeholder_include('nieuws', [$match]);
+       print $Page->widget('nieuws', [$match]);
        return;
 }
index a469a9d865cec5b765c99f4d7293aae122c17d6f..d5c78cdecc9005285a2df589197c6cd087c4453a 100644 (file)
--- a/page.php
+++ b/page.php
@@ -8,38 +8,6 @@ function abort($body, $status = NULL) {
        exit;
 }
 
-function placeholder_include($name, $params = [])
-{
-       $path = stream_resolve_include_path("widget/$name.php");
-       if (!file_exists($path)) {
-               return '<strong class="warn"><em>'.$name.'</em> ontbreekt</strong>';
-       }
-
-       ob_start();
-       $Page = clone $GLOBALS['Page'];
-       $Page->handler = $Page->handler . $Page->path; // .= with explicit getter
-       $Page->path = '';
-       $Place = $GLOBALS['Place'];
-       foreach ($params as $param) {
-               if ($set = strpos($param, '=')) {
-                       $Place[ substr($param, 0, $set) ] = substr($param, $set + 1);
-               }
-               elseif (!empty($param)) {
-                       $Page->path .= '/'.$param;
-               }
-       }
-       $Page->link .= $Page->path;
-       try {
-               include "widget/$name.php";
-               return ob_get_clean();
-       }
-       catch (Exception $e) {
-               return sprintf('<strong class="warn">%s</strong>',
-                       "fout in <em>$name</em>: {$e->getMessage()}"
-               );
-       }
-}
-
 function getoutput($blocks = [])
 {
        $doc = ob_get_clean();