学習日記49日目

スタートアップ研修記はこちらです。

どうも、enomotoです。
今日はカレーを食べにチリチリまで行きました。
やっぱりカレーは最高だなぁと思いました。そんなカレーの話はおいといて
きょうも行った作業を書いていきたいと思います。

今日、やったこと。

  • スケジュール表示ができるようになった。
  • スケジュール登録ができるようになった。

現在のファイル構成

/
├ /web/index.php (update)
|   └/css/style.css (update)
├ /modules/calendar/view/month_view.php
|                └/logic/month_view.php (update)
├ /modules/calendar/view/day_view.php (new)
|                └/logic/day_view.php (new)
└ /lib/controler.php (update)
     ├/DB.php (new)
     └/library.php (new)

次回以降への課題

  • DBのデータを編集できるようにする
  • ログイン画面を付けてマルチユーザー化する

全部、コードを書くとキリがないので新しく追加した部分だけ公開したいと思います。

/modules/calendar/logic/day_view.php

<?php

class DayView
{

  public $year;
  public $month;
  public $days;
  public $cal_title;
  public $calendar;
  public $post_year;
  public $post_month;
  public $post_day;
  public $host;
  public $post_title;
  public $create_at;
  public $link;
  public $sql;
  public $result;
  public $list;

  public function render($get) {
    // テンプレート読み込み
    require_once($get);
  }
  
  public function execute() {
    // パラメーターの取得と確認
    $lib = new Library();
    $lib->checkGetParameter();
    $this->year = $lib->year;
    $this->month = $lib->month;
    $this->days = $lib->days;
    // この日記のタイトル
    $this->cal_title = sprintf('%d年%d月%d日の予定表',$this->year,$this->month,$this->days); //タイトル
    // スケジュールの投稿
    if (isset($_POST['editmode'])) {
      if ($_POST['hh'] >= 00 && $_POST['hh'] <= 24 || $_POST['mm'] >= 00 && $_POST['mm'] <= 59) {
        $this->postSchedule();
      }
    }
    // スケジュールの表示
    $this->lib = new Library();
    $this->link = $lib->connectDB();
    DB::selectdb();
    $this->sql = sprintf('SELECT * FROM schedules
                          WHERE year=%d AND month=%d AND day=%d AND is_deleted=0 GROUP BY time;'
                         ,$this->year,$this->month,$this->days);
    $this->result = mysql_query($this->sql);
    DB::close();
    // PEAR::Calendar::Dayの開始
    $this->calendar = new Calendar_Day($this->year,$this->month,$this->days);
  }
  public function postSchedule() {
    $this->lib = new Library();
    $this->post_year = $this->lib->changeSpecialParameter($_GET['year']);
    $this->post_month = $this->lib->changeSpecialParameter($_GET['month']);
    $this->post_day = $this->lib->changeSpecialParameter($_GET['day']);
    $this->post_time = $this->lib->changeSpecialParameter($_POST['hh']).":".$this->lib->changeSpecialParameter($_POST['mm']);
    $this->host = gethostbyaddr($_SERVER["REMOTE_ADDR"]);
    $this->post_title = $this->lib->changeSpecialParameter($_POST['title']);
    $this->create_at = date("Y-n-j G:i:s");
    $this->link = $this->lib->connectDB();
    DB::selectdb();
    $this->sql = sprintf('INSERT INTO
                          schedules(year,month,day,time,user_id,create_at,host,title,is_deleted)
                          VALUES(%d,%d,%d,\'%s\',1,\'%s\',\'%s\',\'%s\',0);'
                         ,$this->post_year,$this->post_month,$this->post_day,$this->post_time,
                         $this->create_at,$this->host,$this->post_title);
    $this->lib->commitDB($this->sql);
    DB::close();
    header("Location: ?year=$this->year&month=$this->month&day=$this->days");
  }
}

/modules/calendar/view/day_view.php

<!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" lang="ja" xml:lang="ja">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="content-style-type" content="text/css" />
<link href="./css/style.css" rel="stylesheet" type="text/css" />
<title><?php echo __TITLE__."".$this->cal_title; ?></title>
</head>
<body>
<div id="wrapper">
   <h1><?php echo $this->cal_title; ?></h1>
<p class="center"><a href="<?php echo "?year=".$this->calendar->thisYear()."&amp;month=".$this->calendar->thisMonth(); ?>">カレンダーに戻る</a></p>
<table>
<thead>
<tr>
<th>日付</th>
<th>予定</th>
</tr>
</thead>
<?php
  if($this->result==true) :
    while($this->list = mysql_fetch_array($this->result)):
 ?>
<tr>
<td><?php echo $this->list['time']; ?></td>
<td><?php echo $this->list['title']; ?></td>
</tr>
<?php endwhile; ?>
<?php else: ?>
<tr>
<td colspan="2">データベースが読み込みできません。</td>
</tr>
<?php endif; ?>
</table>
<hr />
<h2>予定追加</h2>
<form method="post" action="">
<input type="hidden" name="editmode" value="1">
<table>
<thead>
<tr>
<th>予定の時刻</th>
<th colspan="2">予定の内容</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<select name="hh">
<?php
      for ($i=0;$i<=23;$i++) {
        printf('<option value="%02d">%02d</option>',$i,$i);
      }
?>
</select>
 : 
<select name="mm">
<?php
      for ($i=0;$i<=59;$i++) {
        printf('<option value="%02d">%02d</option>',$i,$i);
      }
?>
</select>
</td>
<td><input type="text" name="title" size="70" /></td>
<td><input type="submit" name="登録" /><input type="reset" name="リセット" /></td>
</tr>
</tbody>
</table>
</form>
</div>
</body>
</html>

/lib/library.php

<?php
class Library
{
  public $link;
  public $error;
  public $year;
  public $month;
  public $days;

  // DBコネクト
  public function connectDB() {
    $link = DB::connect();
    $error = mysql_error($link);
    if(strlen($error)!=0) {
      return 'ERROR:'.$error;
      exit();
    }
    return $link;
  }

  // DBコミット
  public function commitDB($nanka) {
    $result = mysql_query('begin;');
    $error = mysql_error(DB::connect());
    if (strlen($error)!=0) {
      return 'ERROR:'.$error;
      exit();
    }
    $result = mysql_query($nanka);
    $error = mysql_error(DB::connect());
    if (strlen($error)!=0) {
      return 'ERROR:'.$error;
      $result = mysql_query('rollback;');
      exit();
    }
    $result = mysql_query('commit;');
    $error = mysql_error(DB::connect());
    if (strlen($error)!=0) {
      return 'ERROR:'.$error;
      exit();
    }
  }

  //パラメータの取得・チェック
  public function checkGetParameter() {
    if (isset($_GET['year']) && isset($_GET['month'])) {
      // GETにYearとMonthが入力されていたら
      if ($_GET['year'] >= 1970 && $_GET['year'] <= 2037) {
        // Yearが1970から2037までだったらGETの中身を代入
        $this->year = $_GET['year'];
      } else {
        // それ以外は現在のYearを代入
        $this->year = date('Y');
      }
      if ($_GET['month'] >= 1 && $_GET['month'] <= 12) {
        // Monthが1から12までだったらGETの中身を代入
        $this->month = $_GET['month'];
      } else {
        // それ以外は現在のMonthを代入
        $this->month = date('n');
      }
    } elseif(isset($_GET['year']) && !isset($_GET['month'])) {
      // GETにYearとMonthが入力されていたら
      if ($_GET['year'] >= 1970 && $_GET['year'] <= 2100) {
        // Yearが1970から2100までだったらGETの中身を代入
        $this->year = $_GET['year'];
      } else {
        // それ以外は現在のYearを代入
        $this->year = date('Y');
      }
      // Monthは無いのでとりあえず1を代入
      $this->month = 1;
    } else {
      // 何もない場合は現在のYearとMonthを代入
      $this->year = date('Y');
      $this->month = date('n');
    }
    if (isset($_GET['day'])) {
      // GETにDayが入力されていたら
      $check = new Calendar_Day($this->year,$this->month,$_GET['day']);
      if ($check->isValid()) {
        $this->days = $_GET['day'];
      } else {
        header("Location: ?");
        exit();
      }
    }
  }
  public function changeSpecialParameter($data) {
    $get = htmlspecialchars($data);
    $put = mysql_real_escape_string($get);
    return $put;
  }
}

/lib/DB.php

<?php
class DB
{
  // 初期設定
  public static $hostname = 'localhost'; // DBのホスト設定
  public static $user = 'root'; // UserID
  public static $pass = ''; // Password
  public static $dbname = 'steps_cal'; // DBの選択

  public static function connect() {
    return mysql_connect(self::$hostname,self::$user,self::$pass);
  }
  
  public static function selectdb() {
    mysql_select_db(self::$dbname,DB::connect());
  }

  public static function close() {
    mysql_close(DB::connect());
  }
}