登録したスケジュールをカレンダーに表示(CakePHP修行 #9)

  • 2008-07-27 (日) 20:19

今回はカレンダーページ上に、登録したスケジュールのタイトルを表示する処理を実装します。

コントローラーのCalendarアクションを変更します。
・app/controllers/schedules_controller.php
[sourcecode language='php']
//calendar
function calendar($year = null, $month = null){
//システムメッセージをセット
$this->set(‘sys_msg’, $this->Session->read(‘sys_msg’));
$this->Session->delete(‘sys_msg’);

//指定された年月を取得/指定なしの場合はシステム日付を取得
if($year == null || $month == null){
$Month = new Calendar_Month_Weekdays( date(‘Y’), date(‘m’), 0 );
$year = date(‘Y’);
$month = date(‘m’);
} else {
$Month = new Calendar_Month_Weekdays( $year, $month, 0 );
}
//PEARよりカレンダー情報を読み込み
$Month->build();
$this->set(‘month’, $Month);
//取得した年月、次月、前月をYYYY/MM形式で変数にセット
$this->set(‘next_year_month’, date(“Y/m”, mktime(0, 0, 0, $month+1, 1, $year)));
$this->set(‘this_year_month’, date(“Y/m”, mktime(0, 0, 0, $month, 1, $year)));
$this->set(‘prev_year_month’, date(“Y/m”, mktime(0, 0, 0, $month-1, 1, $year)));

//取得した年月のスケジュール情報をScheduleから取得
$cond = array(
‘AND’ => array(
array(“Schedule.start” => “>=$year-$month-01″)
,array(“Schedule.start” => “< =$year-$month-31")
)
);
$this->set(‘schedules’, $this->Schedule->findAll($cond));
}
[/sourcecode]

ビューのCalendarページの日付セルにスケジュールタイトルを表示するように処理を付け加えます。
・app/views/schedules/calendar.thtml
[sourcecode language='php']

Calendar

< ?php
echo $html->link(‘< <', '/schedules/calendar/' . $prev_year_month);
echo $this_year_month;
echo $html->link(‘>>’, ‘/schedules/calendar/’ . $next_year_month);
?>


< ?php if(isset($sys_msg)){ ?>

< ?php echo($sys_msg); ?>


< ?php } ?>

< ?php
while ($Day = $month->fetch()) {
if ($Day->isFirst()) {
echo ”

\n”;
}
if ($Day->isEmpty()) {
echo ”

\n”;
} else {
echo ”

\n”;
}
if ($Day->isLast()) {
echo “

\n”;
}
}
?>

Sun Mon Tue Wed Thu Fri Sat
” . $Day->thisDay();
//add link
echo $html->link($html->image(“pencil_add.png” , array(“class”=>”alphafilter”)), ‘/schedules/add/’ . $month->year . “/” . $month->month . “/” . $Day->thisDay(),null,null,false);
echo “\n”;
//schedulesテーブルに登録されているタイトルを表示
echo ”

    \n”;
    foreach ($schedules as $schedule):
    if(date(“Ymd”, strtotime($schedule['Schedule']['start'])) == date(“Ymd”, mktime(0, 0, 0, $month->month, $Day->thisDay(), $month->year))){
    echo ”

  • “;
    echo $html->link($schedule['Schedule']['title'], ‘/schedules/edit/’.$schedule['Schedule']['id']);
    echo “
  • \n”;
    }
    endforeach;
    echo “

\n”;
echo “


[/sourcecode]

カレンダー上に表示するタイトルのスタイルを指定
・app/webroot/css/cake.generic.css
[sourcecode language='css']
#calendar ul{
margin: 0;
padding: 0;
list-style-image:none;
list-style-position:outside;
list-style-type:none;
}

#calendar li{
margin:0;
padding:3px 0px 1px 18px;
background-image:url(/img/arr.png);
background-repeat:no-repeat;
line-height:120%;
margin-bottom:0px;
}
[/sourcecode]

コメント:0

コメントフォーム
Remember personal info

トラックバック:0

このエントリーへのトラックバックURL
http://www.studio-kingdom.com/cakephp/cakephp_training/76/trackback
フィード

メタ

Return to page top