Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

7. Cvičení - Řešení

Kompletní řešení: ZIP

Svátky

<?php
 
include ("autoload.php");
 
// vyrobime si Smarty objekt
$smarty = new MySmarty();
 
$error = null;
$name = "";

$month = $day = $year = null;

if (isset($_GET['submit'])) {

    $month = $_GET['month'];
    $day = $_GET['day'];
    $year = $_GET['year'];

    if (!Calendar::dateExists($day, $month, $year)) {
        $error = "Chybné datum, prosím opravte ho.";
    } else {
    // volame model jak je treba
    // zjistime poradove cislo dnu v roce
        $dayKey = intval($day).'.'.intval($month).'.';
        // kalendar ve forme pole, kde klic je den.mesic.
        $names = DataFactory::getNames();
        // konkretni svatek pro dany den
        $name = $names[$dayKey];
    }
} else {
// formular nebyl odeslan, zobraz jen text
}
 
$smarty->assign("error", $error);
$smarty->assign("name", $name);
$smarty->assign("formattedDate",
    Calendar::getFormattedDate($day, $month, $year,'d.m.Y'));
 
// pro znovuzobrazeni poli ve formulari
$smarty->assign("day", $day);
$smarty->assign("year", $year);
$smarty->assign("month", $month);
 
$smarty->display("svatky.xhtml");
 
?>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Smarty svátky</title>
    </head>
    <body>
        <h1>Svátky</h1>
        {if $name != ""}
        Svátek {$formattedDate} má {$name}
        {/if}
 
        {if $error != null}
        {$error|escape:html}
        {/if}
        <form action="{$smarty.server.PHP_SELF}" method="get">
            <label for="day">Den</label>
            <input type="text" id="day" name="day" value="{$day|escape:html}" /><br/>
            <label for="month">Měsíc</label>
            <input type="text" id="month" name="month" value="{$month|escape:html}"/><br/>
            <label for="year">Rok</label>
            <input type="text" id="year" name="year" value="{$year|escape:html}"/><br/>
            <input type="submit" name="submit" value="Odeslat"/>
        </form>
    </body>
</html>

Články

<?php

include ("autoload.php");
define("TEMPLATE_NAME","clanky.xhtml");

// zjistime, zda jsme admini
$isadmin = false;
if (isset($_GET['isadmin']) && $_GET['isadmin'] == 1) {
    $isadmin = true;
}

// vyrobime si Smarty objekt
$smarty = new MySmarty();

// zapneme podporu cache
$smarty->caching = 1;

// nastavime delku platnosti cache
if ($isadmin) {
    $smarty->cache_lifetime = 0;
} else {
    $smarty->cache_lifetime = 60;
}

// v pripade, ze je cachovano, tak nevolame model
if (!$smarty->is_cached(TEMPLATE_NAME, $isadmin)) {
    $articles = DataFactory::getArticles();
    $smarty->assign_by_ref("articles", $articles);
}

$smarty->display(TEMPLATE_NAME, $isadmin);

?>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Smarty články</title>
        {literal}
        <style type="text/css">
            table, th, td {border-collapse:collapse; border: 2px solid black;}
            th {background-color: silver;}
            .even {background-color: Lavender;}
            .odd {background-color: transparent;}
        </style>
        {/literal}
        {debug}
    </head>
    <body>
        <h1>Zde je seznam aktuálních článků</h1>

        <table>
            <thead>
                <tr>
                    <th>Autor</th>
                    <th>Titulek</th>
                    <th>Náhled</th>
                </tr>
            </thead>
            <tbody>
                {section name="article" loop=$articles}
                <tr class="{cycle values='even,odd'}">
                    <td>{$articles[article].Author|escape:"html"}</td>
                    <td>{$articles[article].Title|escape:"html"}</td>
                    <td>{$articles[article].Text|truncate:100:"...":false|escape:"html"}</td>
                </tr>
                {/section}
            </tbody>
        </table>
    </body>
</html>

courses/b6b39zwa/tutorials/solutions/07/start.txt · Last modified: 2018/10/01 15:02 (external edit)