The Code - The View

Example 1.8. View.inc.php
<?php
/**
 * Description of 
 * File: View.inc.php
 * Superclass with common methods for several views
 *
 * @author nml
 * @version 2015-11-26 V0.9
 * 
 */

abstract class View {
    private $title;
 
    public function printPage() {
        $s = "";
        $s .= $this->htmlTop();
        $s .= $this->pageHeader();
        $s .= $this->pageMain();
        $s .= $this->pageFooter();
        return $s;
    }
    
    private function htmlTop() {
        $s = "";
        $s .= "<!doctype html>\n";
        $s .= "<html>\n";
        $s .= "    <head>\n";
        $s .= "        <meta charset='utf-8'/>\n";
        $s .= "        <link rel='stylesheet' href='css/html5reset-1.6.1.css'/>\n";
        $s .= "        <link rel='stylesheet' href='css/styles.css'/>\n";
        $s .= sprintf("        <title>%s</title>\n", $this->getTitle());
        return $s;
    }
    
    private function pageHeader() {
        $s = "";
        $s .= "    </head>\n";
        $s .= "    <body>\n";
        $s .= "        <header>\n        <nav>\n";
        $s .= "            <h1>Demo MVC</h1>\n";
        $s .= sprintf("%s", $this->headerMenu());
        $s .= "        </nav>\n        </header>\n";
        $s .= "        <main>\n";
        return $s;
    }
    
    private function pageFooter() {
        $s = "";
        $s .= "        </main>\n";
        $s .= "        <footer>\n";
        $s .= sprintf("%s", $this->footerCont());
        $s .= "        </footer>\n";
        $s .= "    </body>\n";
        $s .= "</html>\n";
        return $s;
    }

    private function headerMenu() {
        $s = "";
        $s .= "            <ul id='menu'>\n";
        $s .= "                <li><a href='./index.php'>Home</a></li>\n";
        $s .= "                <li><a href='#'>Page1</a></li>\n";
        $s .= "                <li><a href='#'>Page2</a></li>\n";
        $s .= "                <li><a href='index.php?func=newitemform'>New Item</a></li>\n";
        if (!Authentication::isAuthenticated()) {
            $s .= "                <li><a href='index.php?func=login'>Login</a></li>\n";
        } else {
            $s .= "                <li><a href='index.php?func=logout'>Logout</a></li>\n";
        }
        $s .= "            </ul>\n";
        return $s;
    }

    private function footerCont() {
        $s = "";
        $s .= "            <address>\n";
        $s .= "                &copy; NML, 2015\n";
        $s .= "            </address>\n";
        return $s;
    }
   
    public function getTitle () {
        return $this->title;
    }
    
    public function setTitle ($title) {
        $this->title = $title;
    }
    
    /*
     * convert category to class
     */
    protected static function catConvert($in) {
        $cl = ['L', 'S', 'M'];
        return $cl[$in % count($cl)];   
    }
    
    /*
     * convert priority to class
     */
    protected static function priConvert($in) {
        $pl = ['B', 'I', 'N'];
        return $pl[$in % count($pl)];   
    }
    
    abstract protected function pageMain();
}

Example 1.9. ViewItems.inc.php
<?php
/**
 * Description of ViewItems
 *
 * @author nml
 */

require_once './views/View.inc.php';

class ViewItems extends View {
    const COLS = 3;
    private $model;
    
    public function __construct($model) {
        $this->model = $model;
    }
    
    protected function pageMain () {
        $arr = $this->model->getItemsArr();
        $s = "";
        $j = 0;
        for($i = 0; $i < count($arr); $i++) {
            $item = $arr[$i];
            $x = "";
            if ($j % self::COLS ===  0) {
                $x = "<div class='R'>";
            }
            if ($i < count($arr) - 1) {
                $j += $item->getCategory(); // + $arr[$i+1]->getCategory();
            } else {
                $j = self::COLS;
            }
            $y = "";
            if ($j % self::COLS ===  0) {
                $y = "</div>\n\n";
                $j = 0;
            }
            $z = "";
            if (strlen($item->getItem()) > 128) {
                $z = sprintf("<p class='right'><a href='index.php?item=%s'>Read more.</a></p>\n"
                        , $item->getId());
            }
            
            $s .= sprintf("        %s\n"
                    . "            <article class='%s'>\n"
                    . "                <h1 class='%s'>%s</h1>\n"
                    . "                <p>%s</p>\n"
                    . "                %s"
                    . "                <p><span class='clock'>%s</span>, %s, %s %s</p>\n"
                    . "            </article>\n"
                    . "        %s"
                , $x
                , self::catConvert($item->getCategory())
                , self::priConvert($item->getPriority())
                , $item->getHeader()
                , $item->getItem()
                , $z
                , $item->getWhen()
                , $item->getCategory()
                , $item->getPriority()
                , $item->getId()
                , $y);
        }
        return $s;
    }
}