PHP 備忘録 | メシのタネ

このエントリーをはてなブックマークに追加

ソース公開 予定帳 PHP

 2013/04/23

カレンダーから予定帳をPHPで作ろうと思いました。
以外とやってみると大変でまだ編集機能と削除機能の実装ができてまてんが、
とりあえず、新規登録までできるようになったので、ソースを公開します。
誰得なんだって話ですよね。

使い方とか

日付をクリック→投稿画面が出てくる→内容入力→内容表示→
日付クリック→投稿したデータにタイムスタンプつけて全部表示


予定は月ごとに表示される。

コード:カレンダークラス

カレンダーの表示を行います。

かなり無理くりやってますが、もっとスマートにかけるはずです。
僕は頭が悪いので、この程度しかできません。

<?php
class MainBord
{

	protected $tougetu;		
	protected $tougetu_day;
	protected $prev;		
	protected $next;
	protected $youbi;
	private $flg;

	function __construct()
	{
   		ini_set("output_buffering", "On");
    	ini_set("output_handler", "mb_output_handler");
    	ini_set("default_charset", "UTF-8");
    	mb_language("Japanese");
    	mb_internal_encoding ("UTF-8");
    	mb_http_input("auto");
    	mb_http_output("UTF-8");
    	mb_detect_order("auto");
    	mb_substitute_character("none");
    	ob_start("mb_output_handler");
    	header("Content-Type: text/html; charset=UTF-8");
    	session_start();
	}

	public function main($flg="")
	{

		$this->f_check($flg);


		if(empty($flg))
		{
			$this->tougetu = date('Y-m');
			$this->get_Year(substr($this->tougetu,0,4));
			$this->calender($this->tougetu);
		} else if((isset($flg["next"])) && ($this->flg) && (empty($flg["edit"]))) {
			$this->tougetu = $flg["next"];
			$this->get_Year(substr($this->tougetu,0,4));
			$this->calender($this->tougetu);
		} else if((isset($flg["prev"])) && ($this->flg) && (empty($flg["edit"]))) {
			$this->tougetu = $flg["prev"];
			$this->get_Year(substr($this->tougetu,0,4));
			$this->calender($this->tougetu);
		} else if((isset($flg["tougetu"])) && ($this->flg) && !(empty($flg["edit"]))) {
			$this->tougetu = $flg["tougetu"];
			$this->get_Year(substr($this->tougetu,0,4));
			$this->calender($this->tougetu);
		} else if((isset($flg["tougetu"])) && ($this->flg)){
			$this->tougetu = $flg["tougetu"];
			$this->get_Year(substr($this->tougetu,0,4));
			$this->calender($this->tougetu);
		} 
		
	}

	private function f_check($flg)
	{
		if(isset($flg["next"]))
		{
			$f_flg = sprintf('%04d-%02d',substr($flg["next"],0,4),substr($flg["next"],5,7)) === '0000-00';
			(!$f_flg) ? $this->flg = true : $this->flg = false;
		} else if(isset($flg["prev"])) {
			$f_flg = sprintf('%04d-%02d',substr($flg["prev"],0,4),substr($flg["prev"],5,7)) === '0000-00';
			(!$f_flg) ? $this->flg = true : $this->flg = false;
		} else if(isset($flg["tougetu"])) {
			$f_flg = sprintf('%04d-%02d',substr($flg["prev"],0,4),substr($flg["tougetu"],5,7)) === '0000-00';
			(!$f_flg) ? $this->flg = true : $this->flg = false;
		} else {
		
		}
	}

	private function get_Year($year)
	{
		if($year == date('Y'))
		{
			$this->next = date("Y-m", mktime(0, 0, 0, substr($this->tougetu,5,7)+1, 1, date("Y")));
			$this->prev = date("Y-m", mktime(0, 0, 0, substr($this->tougetu,5,7)-1, 1, date("Y")));
			$this->tougetu_day = date("t", mktime(0, 0, 0, substr($this->tougetu,5,7), 1, date("Y")));
			$this->get_calheader();
		} else if($year > date('Y')) {
			$i = $year - date('Y');
			$this->next = date("Y-m", mktime(0, 0, 0, substr($this->tougetu,5,7)+1, 1, date("Y")+$i));
			$this->prev = date("Y-m", mktime(0, 0, 0, substr($this->tougetu,5,7)-1, 1, date("Y")+$i));
			$this->tougetu_day = date("t", mktime(0, 0, 0, substr($this->tougetu,5,7), 1, date("Y")+$i));
			$this->get_calheader();
		} else if($year < date('Y')) {
			$i = date('Y') - $year;
			$this->next = date("Y-m", mktime(0, 0, 0, substr($this->tougetu,5,7)+1, 1, date("Y")-$i));
			$this->prev = date("Y-m", mktime(0, 0, 0, substr($this->tougetu,5,7)-1, 1, date("Y")-$i));
			$this->tougetu_day = date("t", mktime(0, 0, 0, substr($this->tougetu,5,7), 1, date("Y")-$i));
			$this->get_calheader();
		}
	}

	private function get_calheader()
	{		
			echo '<ul id="calhead">'.PHP_EOL;
			echo '<li>'.PHP_EOL.'<a href="/hoge/index.php?next='.$this->next.'">翌月</a>'.PHP_EOL.'</li>'.PHP_EOL;
			echo '<li>'.$this->tougetu.'</li>'.PHP_EOL;
			echo '<li>'.PHP_EOL.'<a href="/hoge/index.php?prev='.$this->prev.'">先月</a>'.PHP_EOL.'</li>'.PHP_EOL;
			echo '</ul>'.PHP_EOL;
	}

	private function calender($str)
	{

		if($str == date("Y-m"))
		{
			$days = array(date("t",time()),date("Y-m"));
			$this->caloutres($days);
		} else if (($str == $this->tougetu) && ($str > date("Y-m"))) {
			$days = array($this->tougetu_day,$this->tougetu);
			$this->caloutres($days);
			return $this->tougetu = $this->next;
		} else if (($str == $this->tougetu) && ($str < date("Y-m"))) {
			$days = array($this->tougetu_day,$this->tougetu);
			$this->caloutres($days);
			return $this->tougetu = $this->prev;
		} else {
			$days = array($this->tougetu_day,$this->tougetu);
			$this->caloutres($days);
		}

	}

	private function caloutres($days)
	{

		if(is_array($days))
		{
			$this->youbi = array('日','月','火','水','木','金','土');

			if($days[1] === date('Y-m'))
			{
				$nen = date('Y');
				$tuki = date('m');
				$start_w = date("w", mktime(0, 0, 0,$tuki,1, $nen));
				$end_res = date("w", mktime(0, 0, 0,$tuki,$days[0], $nen));
			} else if($days[1] === $this->tougetu) {
				$nen = substr($this->tougetu,0,4);
				$tuki = substr($this->tougetu,5,7);
				$start_w = date("w", mktime(0, 0, 0,$tuki,1, $nen));
				$end_res = date("w", mktime(0, 0, 0,$tuki,$days[0], $nen));
			}

			$end_w = $end_res+1;
			$end_ul = 7 - $end_w;

			echo '<ul id="calcon_youbi">'.PHP_EOL;
			for($c = 0; $c < count($this->youbi); $c++)
			{
				($c >= 40) ? exit : true;
				echo '<li>'.$this->youbi[$c].'</li>'.PHP_EOL;
			}
			echo '</ul>'.PHP_EOL;

			echo '<ul id="calcon_date">'.PHP_EOL;
			for($i = 1; $i <= $start_w; $i++)
			{
				($i >= 40) ? exit : true;
				echo '<li>&nbsp;</li>'.PHP_EOL;
			}
			
			for($n = 1; $n <= $days[0]; $n++)
			{
				($n >= 40) ? exit : true;
				echo '<li><a class="dateedit" href="/hoge/index.php?tougetu='.$this->tougetu.'&edit='.$nen.'-'.$tuki.'-'.$n.'">'.$n.'</a></li>'.PHP_EOL;
			}
			if(!empty($end_ul))
			{
				for($j = 0; $j < $end_ul; $j++)
				{
					($n >= 40) ? exit : true;
					echo '<li>&nbsp;</li>'.PHP_EOL;
				}
			}
			echo '</ul>';
		}



	}

}

write_classについて

このクラスでは、書き込みを担当してます。

サニタイズとタイムスタンプを生成して書き込んでます。

write_classのコード

<?php

class FileWrite
{
	private $write;
	private $con;
	private $plan_day;
	private $tougetu;
	private $edit_day;
	public $error_mes;
	private $date;
	private $flg;
	const path = "hoge/hoge.csv"; //パスです

	public function Writemain($date,$str)
	{
		$this->d_check($date);
		$this->c_check($str);

		if($this->flg)
		{
			$this->con["plan"] = $this->plan_day;
			$this->con["entry"] = date('Y-m-d H:i:s');
			$this->write = implode(",",$this->con);
			$this->write_file($this->write);
		}
	}

	private function d_check($date)
	{
		$this->tougetu = $date['tougetu'];
		$this->plan_day = sprintf('%04d-%02d%03d',substr($date['edit'],0,4),substr($date['edit'],5,6),substr($date['edit'],7,8));
		$this->edit_day = $date['edit'];
	}

	private function c_check($str)
	{
		foreach($str as $key => $val)
		{
			if(empty($val))
			{
				$this->con[$key] = false;
			} else {
				$val = str_replace(",","",$val);
				$this->con[$key] = htmlspecialchars($val);	
			}
		}

		if(!empty($this->con))
		{
			if(in_array(false,$this->con))
			{
				$this->flg = false;
				$this->r_check();
			} else {
				$this->flg = true;
			}
		}
	}

	private function r_check()
	{
		$this->error_mes = '入力してない項目があります。';
		header('location: /hoge/index.php?tougetu='.$this->tougetu.'&edit='.$this->edit_day.'');
	}

	private function write_file($str)
	{
		if(file_exists(self::path))
		{
			$str = str_replace(array("rn","r","n"), '', $str);
			file_put_contents(self::path, $str."n", FILE_APPEND | LOCK_EX);
		}
	}

}

編集というなの編集できないフォームを取得するだけのedit_class

いつか改良されて、編集が簡単にできる日が来ます。

class Edit extends MainBord
{

	protected $edit_ym;
	public $edit_day;

	public function Editmain($str)
	{
		$this->edit_day = sprintf('%04d-%02d%03d',substr($str,0,4),substr($str,5,6),substr($str,7,8));
		$this->edit_ym = substr($this->edit_day,0,7);
	}

	public function get_form()
	{

		echo '<h3>'.$this->edit_day.'予定の入力</h3><a href="index.php">やっぱやめる</a><div class="form_wrap_m">';
		echo '<form action="/hoge/index.php?tougetu='.$this->edit_ym.'" method="post">';
		echo '<label>タイトル入力</label><br /><input type="text" name="title"><br />';
		echo '<label>投稿者</label><br /><input type="text" name="toukou"><br />';
		echo '<label>内容入力</label><br /><textarea name="contents"></textarea><br />';
		echo '<input type="submit" value="書き込み" /></div>';
		echo '</form></div>';
	}

}

読み込みクラス

csvデータの整理と、出力を担当してます。
こんなことになるなら、DB使えば良かったと思います。

<?php

class FileRead
{
	protected $read;
	protected $readout;
	protected $tougetu;
	protected $flg;
	protected $date;
	protected $editday = array();
	protected $d_flg;
	protected $ed_flg = array();
	protected $f_flg = array();
	protected $outdata = array();

	const path = "datta/dat.csv";
	
	public function read_main($date,$flg="")
	{
		(!empty($date['edit'])) ? $this->edit_flg($date['edit']) : false;
		$this->sort_flg($flg,$date);
		$this->flg = $flg;
		$this->read = file(self::path);
		foreach($this->read as $line => $val)
		{
			$this->readout[$line] = explode(",",$val);
		}
		if($flg !== false)
		{
			$this->seikei($this->readout,$this->d_flg);
		}

	}

	private function edit_flg($flg)
	{
		if(!empty($date['edit']))
		{
			$this->date = $date['edit'];
		} else if(!(empty($date)) && $flg == false) {
			$this->date = $date;
		}
	}

	private function sort_flg($flg,$date)
	{
		if(!empty($flg['next']))
		{
			$this->d_flg = $flg['next'];
		} else if(!empty($flg['prev'])) {
			$this->d_flg = $flg['prev'];
		} else if(!empty($date['tougetu'])){
			$this->d_flg = $date['tougetu'];
		}

	}

	private function seikei($ar,$flg)
	{
		asort($ar);
		$this->seikei_flg($ar,$flg);
		$this->seikei_date_flg($this->ed_flg,$ar);
		$i = 0;
			foreach($this->outdata as $key => $val)
			{
				if($i === 10){break;}
				echo '<ul class="read_file">';
				foreach($val as $keys => $vals)
				{
					if($keys === 2) 
					{

					} else if(($keys === 4) && (!empty($this->flg['edit']))) {
						
					} else if($keys === 3) {
						$this->editday[] = $vals;

						if(strlen($vals) > 40)
						{
							echo '<li><a class="datelink" href="index.php?view='.$this->editday[$i].'">'.substr($vals,0,40).'</a></li>';
						} else {
							echo '<li><a class="datelink" href="index.php?view='.$this->editday[$i].'">'.$vals.'</a></li>';
						}
					} else {
						if(strlen($vals) > 40)
						{
							echo '<li>'.substr($vals,0,40).'</li>';
						} else {
							echo '<li>'.$vals.'</li>';
						}
					}
				}
				echo '</ul>';
			$i++;
			}

	}

	private function seikei_flg($data,$flg)
	{
		for($i = 0; $i < count($data); $i++)
		{
			$this->f_flg[] = $data[$i][3];

			if(substr($this->f_flg[$i],0,7) == $flg)
			{
				$this->ed_flg[$i] = $this->f_flg[$i];
			}
		}

	}


	private function seikei_date_flg($date,$data)
	{
		
		for($i = 0; $i < count($data); $i++)
		{
			if(strpos($data[$i][3],$date[$i]) !== false)
			{
				$this->outdata[$i] = $data[$i];
			}
		}

	}

}

詳細表示のクラス

詳細表示のviewクラスです。

<?php

class View extends FileRead
{

	protected $serchdate;
	protected $exflg;
	protected $result;
	protected $voutput = array();


	public function viewmain($date)
	{

		$this->exflg = false;
		parent::read_main($date,$this->exflg);		
		$this->serchdate = $date;
		$this->viewserch(parent::$this->read);
	}

	public function viewserch($data)
	{

		foreach($data as $key => $val)
		{

			$pos = strpos($val,$this->serchdate);
			if ($pos !== false) 
			{
	    	 	$this->voutput[] = explode(",",$this->result[$key] = $val);
			}
		}

		$this->viewrifine($this->voutput);
	}

	private function viewrifine($data)
	{
		echo '<div id="view">';
		foreach($data as $key => $val)
		{

			echo '<ul class="v_list">';
			foreach($val as $keys => $vals)
			{
				($keys === 0) ? $keys ='タイトル': $keys ;
				($keys === 1) ? $keys ='投稿者': $keys ;
				($keys === 2) ? $keys ='内容': $keys ;
				($keys === 3) ? $keys ='予定日': $keys ;
				($keys === 4) ? $keys ='記入日': $keys ;
				echo '<li><strong>'.$keys.'</strong></li><li>'.$vals.'</li>';
			}
			echo '</ul>';
		}
		echo '</div>';
		echo '<div class="list_con">';
		echo '<a href="index.php">前のページへ</a>';
		echo '</div>';
	}

}

インスタンスとか

結局呼び出し部分が散らかってます。精進します。

<html>
	<head>
		<meta name="viewport" content="width=device-width, user-scalable=yes, maximum-scale=1.0, minimum-scale=1.0">
	<script type="text/javascript" src="https://www.google.com/jsapi"></script>
	<script type="text/javascript">google.load("jquery", "1");</script>
	<script>
	$(function()
	{

		var linkdate = new Array;
		$('.datelink').each(function(ele)
		{
			linkdate[ele] = $(this).text();
		});

		var dateedit = new Array;
		$('.dateedit').each(function(ele)
		{
			dateedit[ele] = $(this).text();
		});

		var j_getdate =  changeformat(linkdate);

		function changeformat(needle)
		{
	
			var res = new Array;
			var result = new Array;
			var dat = new Array;

			for(i = 0; i < needle.length; i++)
			{
				res[i] = needle[i].replace(/-/g,"/");
				dat[i] = new Date(res[i]);
				result[i] = dat[i].getDate();
			}
			return result;
		}

		var flg = new Array;
		var date_key = new Array;

		$.each(dateedit,function(ele)
		{
			flg[ele] = $.inArray(ele,j_getdate);	
		});

		$.each(flg,function(ele)
		{
			if(flg[ele] !== -1)
			{
				date_key[ele] = flg[ele];
				$('.dateedit').eq(ele-1).css("color","red");
			}
		});




	});
	</script>
	<link rel="stylesheet" type="text/css" href="/tekitou.css" />
		<!--[if gte IE 9]>
		  <style type="text/css">
		    .gradient {
		       filter: none;
		    }
		  </style>
		<![endif]-->
	</head>
	<body>

<?php

require_once("calender_class.php");
require_once("edit_class.php");
require_once("read_class.php");
require_once("write_class.php");
require_once("view_class.php");


function param($p){return $p;}

param(function($flg)
{
	$bord = new MainBord;
	if(empty($flg))
	{
		$bord->main();
	} else if($flg) {
		$bord->main($flg);
	}
})->__invoke($_GET);

param(function($flg)
{
	if($flg['edit'])
	{
		echo '<div id="get_form">';
		$edit = new Edit;
		$edit->Editmain($flg['edit']);
		$edit->get_form();
		echo '</div>';
	}
})->__invoke($_GET);

param(function($flg,$date)
{
	if(!empty($date['edit'])) 
	{
		$_SESSION["date"] = $date;
	}

	if(!(empty($flg['title']) && !(empty($flg['contents']))))
	{
		$write = new FileWrite;
		$write->Writemain($_SESSION["date"],$flg);
	}
})->__invoke($_POST,$_GET);

param(function($flg)
{

	$bord = new MainBord;
	$read = new FileRead;

	if(!empty($flg['edit']))
	{		
		echo '<div id="read_area">';
		$read->read_main($_SESSION["date"],$flg);
		echo '</div>';
	} else if(empty($flg['view'])) {
		echo '<div id="read_area_d">';
		$read->read_main($_SESSION["date"],$flg);
		echo '</div>';
	} else if(empty($flg)) {
		echo '<div id="read_area_d">';
		$date_ar = array();
		$date_ar[0] = date("Y-m");
		$date_ar[1] = date("Y-m-d");
		$read->read_main($date_ar,$flg);
		echo '</div>';

	}


})->__invoke($_GET);

param(function($flg)
{

	if(!empty($flg))
	{
		$view = new view;
		$view = $view->viewmain($flg);
	}

})->__invoke($_GET["view"]);



?>
	</body>
</html>

そのうちオートロードします。
以上です。


このエントリーをはてなブックマークに追加

コメント

"ソース公開 予定帳 PHP"
でメシのタネのおすすめを検索したよ!

プログラミング備忘録 | メシのタネ