2006/12/17

PHP mojavi + smarty + calendar

フレームワークの mojavi と、テンプレートの smarty、そして PEAR の Calendar を利用してカレンダーを書くことに挑戦。

ディレクトリは以下のようになります。

home_directory
+ mojavi
+ lib
+ opt
+ Calendar
+ Decorator
+ Engine
+ Month
+ Table
+ Util
+ smarty
+ internals
+ plugins
+ public_html
+ webapp
+ modules
+ MyCalender
+ actions
- YoteihyouAction.class.php
+ templates
- yoteihyou.tpl
+ views
- YoteihyouView_success.class.php

YoteihyouAction.class.php ファイル

<?php
require_once(OPT_DIR . 'Calendar/Month/Weekdays.php');
class YoteihyouAction extends Action
{
function execute (&$controller, &$request, &$user)
{
$year = 2006;
$month = 12;

$calendar = new Calendar_Month_Weekdays($year,$month,0);
$calendar->build();

$week = 0;
while ($day = $calendar->fetch()) {
$the_day = array();
$the_day['bgcolor'] = "#FFFFFF";
if ($day->isFirst()) {
$the_day['bgcolor'] = "#FFCCFF";
$week++;
} else if ($day->isLast()) {
$the_day['bgcolor'] = "#B3E7FF";
}

if ($day->isEmpty())
{
$the_day['day'] = " ";
} else {
$the_day['day'] = $day->thisDay();
}
$calendar_list[$week][$day->thisDay()] = $the_day;
}

$request->setAttribute('calendar_list', $calendar_list);
$request->setAttribute('year', $year);
$request->setAttribute('month', $month);

return VIEW_SUCCESS;
}
}
?>

YoteihyouView_success.class.php ファイル

<?php
require_once(LIB_DIR . 'CustomSmartyRenderer.class.php');
class YoteihyouView extends View
{
function & execute (&$controller, &$request, &$user)
{
$renderer =& new CustomSmartyRenderer ($controller, $request, $user);
$renderer->setTemplate('yoteihyou.tpl');
$renderer->setAttribute('year', $request->getAttribute('year'));
$renderer->setAttribute('month', $request->getAttribute('month'));
$renderer->setAttribute('calendar_list', $request->getAttribute('calendar_list'));

return $renderer;
}
}
?>

yoteihyou.tpl ファイル

<head>
<meta equiv="Content-Type" content="text/html; charset=euc-jp">
<meta equiv="Content-Language" content="ja">
<title>新しいページ</title>
</head>
<body>

<table border="0" cellpadding="2" cellspacing="0">
<caption>{$year} 年 {$month} 月</caption>
<tr>
<th align="center"><span style="color:#FF0000;">日</span></th>
<th align="center">月</th>
<th align="center">火</th>
<th align="center">水</th>
<th align="center">木</th>
<th align="center">金</th>
<th align="center"><span style="color:#0000FF;">土</span></th>
</tr>
{foreach from=$calendar_list key=week item=values}
<tr>
{foreach from=$values key=day item=the_day}
<td bgcolor="{$the_day.bgcolor}" align="right">
{$the_day.day}
</td>
{/foreach}
</tr>
{/foreach}
</table>

</body>
</html>

0 件のコメント: