Ist leider doch noch nicht fertig geworden, Schwesterchen war über Pfingsten zu Besuch und ich war die meiste Zeit unterwegs.
Wird aber im Laufe dieser Woche auf jedenfall was.
Ich poste grad mal den aktuellen Zwischenstand, vielleicht fallen jemandem beim rüberschauen Anregungen ein.
Die Klasse eventCalendar ist noch nicht komplett durchgetestet, wenn die vorerst endgültige Version kommt werde ich das hier auch wieder rauseditieren. Beschreibung der Methoden kommt auch dann.
class calendar {
private $month;
private $year;
private $we = 2;
private $dates = array();
public function __construct($month=false,$year=false) {
if($month===false || !$this->setMonth($month)) {
$this->month = date("m");
}
if($year===false || !$this->setYear($year)) {
$this->year = date("Y");
}
}
public function setMonth($month) {
if($this->checkMonth($month)) {
$this->month = $month<10 ? "0".(int)$month : (int)$month;
return true;
}
else return false;
}
public function getMonth() {
return $this->month;
}
public function setYear($y) {
if($this->checkYear($y)) {
$this->year = $y>=1970 ? $y : (100==$y ? 2000 : ($y<10 ? "200".$y : ($y<=38 ? "20".$y : "19".$y)));
return true;
}
else return false;
}
public function getYear() {
return $this->year;
}
public function setWe($we) {
if(1==$we || 2==$we) {
$this->we = $we;
return true;
}
else return false;
}
public function getWe() {
return $this->we;
}
public function addDate($time,$descr) {
if(date("Y",$time)==$this->year && ""!=trim($descr)) {
$this->dates[$time][] = $descr;
return true;
}
else return false;
}
public function resetDates() {
$this->dates = array();
}
public function checkMonth($m) {
$m = (int)$m;
return $m>=1 && $m<=12 ? true : false;
}
public function checkYear($y) {
$y = (int)$y;
return ($y>=1970 && $y<=2038) || ($y>=0 && $y<=38) || ($y>=70 && $y<=100) ? true : false;
}
public function getCalendar($thumb=true) {
$class = $thumb ? "thumb" : "";
$time = mktime(0,0,0,$this->month,1,$this->year);
$count = date("t",$time);
$wday = date("w",$time);
$w = (int)date("W",$time);
$week[$w] = "<td id='{$class}CalKw'>{$w}</td>";
$diff = $wday ? $wday-1 : 6;
if($diff) {
$week[$w].= "<td colspan='{$diff}'></td>\n";
}
for($i=1; $i<=$count; $i++) {
$week[$w].= ""==$week[$w] ? "<td id='{$class}CalKw'>{$w}</td>" : "";
$id = "";
$title = "";
$event = "";
$t = mktime(0,0,0,$this->month,$i,$this->year);
$wd = date("w",$t);
if(array_key_exists($t,$this->dates)) {
$id = " id='{$class}CalEvent'";
$title = " title='".implode(" || ",$this->dates[$t])."'";
$event = $thumb ? "" : "<div id='event'>".implode("<br>",$this->dates[$t])."</div>";
}
elseif(!$wd || (2==$this->we && 6==$wd)) {
$id = " id='{$class}CalWe'";
}
$week[$w].= "<td{$id}{$title}>{$i}{$event}</td>\n";
if(!$wd) {
$w++;
$week[$w] = "";
}
}
if($wd) {
$diff = 7-$wd;
$week[$w].= "<td colspan='{$diff}'></td>\n";
}
$calendar = "<tr>".implode("</tr>\n<tr>\n",$week)."</tr>";
$return = "
<table border='0' cellpadding='0' cellspacing='0' id='{$class}Calendar'>
<tr>
<th>KW</th><th>Mo</th><th>Di</th><th>Mi</th><th>Do</th><th>Fr</th><th>Sa</th><th>So</th>
</tr>
{$calendar}
</table>";
return $return;
}
}
Alles anzeigen
class eventCalendar extends calendar {
private $dbKeys = array("table","pk","dateField","dateFieldType","descrField");
protected $dfTypes = array("date","datetime","timestamp");
protected $dbValues = array();
protected $mysqli = false;
protected $db;
// *************************************************************
// ****************** public functions *******************
// *************************************************************
public function __construct(array $dbValues=array(),$month=false,$year=false) {
if(!$this->setDbValues($dbValues)) {
return false;
}
parent::__construct($month,$year);
}
public function setDbValues(array $dbValues=array()) {
if(!count($dbValues)) {
$this->dbValues = array(
"table" =>"termine",
"pk" =>"id",
"dateField" =>"datum",
"dateFieldType"=>"timestamp",
"descrField" =>"termin"
);
return true;
}
elseif($this->checkDbValues($dbValues)) {
$this->dbValues = $dbValues;
return true;
}
else return false;
}
public function setMysqli(mysqli $db) {
$this->db = $db;
$this->mysqli = true;
}
public function setTk() {
switch($this->dbValues['dateFieldType']) {
case "date":
$start = $this->getYear()."-".$this->getMonth()."-01";
$stop = $this->getYear()."-".$this->getMonth()."-31";
break;
case "datetime":
$start = $this->getYear()."-".$this->getMonth()."-01 00:00:00";
$stop = $this->getYear()."-".$this->getMonth()."-31 23:59:59";
break;
case "timestamp":
$start = $this->getYear()."-".$this->getMonth()."01000000";
$stop = $this->getYear()."-".$this->getMonth()."31235959";
break;
default: return false;
}
return $this->mysqli ? $this->setCalendarMysqli($start,$stop) : $this->setCalendarMysql($start,$stop);
}
public function setTkYear() {
switch($this->dbValues['dateFieldType']) {
case "date":
$start = $this->getYear()."-01-01";
$stop = $this->getYear()."-12-31";
break;
case "datetime":
$start = $this->getYear()."-01-01 00:00:00";
$stop = $this->getYear()."-12-31 23:59:59";
break;
case "timestamp":
$start = $this->getYear()."0101000000";
$stop = $this->getYear()."1231235959";
break;
default: return false;
}
return $this->mysqli ? $this->setCalendarMysqli($start,$stop) : $this->setCalendarMysql($start,$stop);
}
public function getTk($thumb=true) {
return $this->getCalendar($thumb);
}
public function getTkYear($cols=3) {
switch((int)$cols) {
case 1:
case 2:
case 3:
case 4:
case 6:
case 12:
break;
case 5:
$cols = 4;
break;
default:
$cols = $cols<12 ? 6 : 12;
break;
}
$col = 1;
$months = array();
for($i=1;$i<=12;$i++) {
$this->setMonth($i);
$months[$col][] = $this->getCalendar();
$col += 0==$i%$cols ? 1 : 0;
}
$rows = array();
foreach($months as $v) {
$rows[] = "<tr>\n<td id='calMonth'>\n".implode("</td>\n<td id='calMonth'>\n",$v)."\n</td>\n</tr>";
}
$yk = "<table cellpadding='0' cellspacing='0' border='0' id='calYear'><tr><th colspan='{$cols}'>{$this->getYear()}</th></tr>\n".implode("\n",$rows)."</table>";
return $yk;
}
public function insertDate($datum,$descr) {
return $this->mysqli ? insertMysqli($datum,$descr) : insertMysql($datum,$descr);
}
public function updateDate($pk,$datum,$descr) {
return $this->mysqli ? updateMysqli($pk,$datum,$descr) : updateMysql($pk,$datum,$descr);
}
public function deleteDate($pk) {
return $this->mysqli ? deleteMysqli($pk,$datum,$descr) : deleteMysql($pk,$datum,$descr);
}
// *************************************************************
// ***************** private functions *******************
// *************************************************************
private function setCalendarMysql($start,$stop) {
$sql = "
SELECT
{$this->dbValues['dateField']} AS datum,
{$this->dbValues['descrField']} AS descr
FROM
{$this->dbValues['table']}
WHERE
{$this->dbValues['dateField']} BETWEEN '{$start}' AND '{$stop}'
ORDER BY
{$this->dbValues['dateField']} ASC";
if(!$res = mysql_query($sql)) { return false; }
while($row = mysql_fetch_array($res)) {
switch($this->dbValues['dateFieldType']) {
case "date":
$d = explode("-",$row['datum']);
$time = mktime(0,0,0,$d[1],$d[2],$d[0]);
break;
case "datetime":
$dt = explode(" ",$row['datum']);
$d = explode("-",$dt[0]);
$time = mktime(0,0,0,$d[1],$d[2],$d[0]);
break;
case "timestamp":
$y = substr($row['datum'],0,4);
$m = substr($row['datum'],4,2);
$d = substr($row['datum'],6,2);
$time = mktime(0,0,0,$m,$d,$y);
break;
default: return false;
}
$descr = stripslashes(trim($row['descr']));
$this->addDate($time,$descr);
}
return true;
}
private function setCalendarMysqli($start,$stop) {
$sql = "
SELECT
{$this->dbValues['dateField']},
{$this->dbValues['descrField']}
FROM
{$this->dbValues['table']}
WHERE
{$this->dbValues['dateField']} BETWEEN ? AND ?
ORDER BY
{$this->dbValues['dateField']} ASC";
if(!$res = $this->db->prepare($sql)) { return false; }
$res->bind_param("ss",$start,$stop);
$res->execute();
$res->bind_result($datum,$descr);
while($res->fetch()) {
switch($this->dbValues['dateFieldType']) {
case "date":
$d = explode("-",$datum);
$time = mktime(0,0,0,$d[1],$d[2],$d[0]);
break;
case "datetime":
$dt = explode(" ",$datum);
$d = explode("-",$dt[0]);
$time = mktime(0,0,0,$d[1],$d[2],$d[0]);
break;
case "timestamp":
$y = substr($datum,0,4);
$m = substr($datum,4,2);
$d = substr($datum,6,2);
$time = mktime(0,0,0,$m,$d,$y);
break;
default: return false;
}
$descr = stripslashes(trim($descr));
$this->addDate($time,$descr);
}
return true;
}
private function insertMysql($datum,$descr) {
$datum = mysql_real_escape_string($datum);
$descr = mysql_real_escape_string($descr);
$sql = "
INSERT INTO
{$dbValues['table']}
SET
{$dbValues['dateField']}='{$datum}',
{$dbValues['descrField']}='{$descr}'";
return mysql_query($sql);
}
private function insertMysqli($datum,$descr) {
$sql = "
INSERT INTO
{$dbValues['table']}
SET
{$dbValues['dateField']}=?,
{$dbValues['descrField']}=?";
$res = $this->db->prepare($sql);
$res->bind_param("ss",$datum,$descr);
return $res->execute();
}
private function updateMysql($pk,$datum,$descr) {
$pk = (int)$pk;
$datum = mysql_real_escape_string($datum);
$descr = mysql_real_escape_string($descr);
$sql = "
UPDATE
{$dbValues['table']}
SET
{$dbValues['dateField']}='{$datum}',
{$dbValues['descrField']}='{$descr}'
WHERE
{$dbValues['pk']}={$pk}";
return mysql_query($sql);
}
private function updateMysqli($pk,$datum,$descr) {
$sql = "
UPDATE INTO
{$dbValues['table']}
SET
{$dbValues['dateField']}=?,
{$dbValues['descrField']}=?
WHERE
{$dbValues['pk']}=?";
$res = $this->db->prepare($sql);
$res->bind_param("ssi",$datum,$descr,$pk);
return $res->execute();
}
private function deleteMysql($pk) {
$pk = (int)$pk;
$sql = "
DELETE FROM
{$dbValues['table']}
WHERE
{$dbValues['pk']}={$pk}";
return mysql_query($sql);
}
private function deleteMysqli($pk) {
$sql = "
DELETE FROM
{$dbValues['table']}
WHERE
{$dbValues['pk']}=?";
$res = $this->db->prepare($sql);
$res->bind_param("i",$pk);
return $res->execute();
}
private function checkDbValues(array $dbValues) {
if(count($this->dbKeys)!=count($dbValues)) {
return false;
}
foreach($dbValues as $k=>$v) {
if(!in_array($k,$this->dbKeys) || !is_string($v) || ("dateFieldType"==$k && !in_array($v,$this->dfTypes))) {
return false;
}
}
return true;
}
}
Alles anzeigen