SimpleModelSite/class_link.php

147 lines
4.3 KiB
PHP

<?php
class Link implements JsonSerializable {
private $id = 0;
private $label = "";
private $url = "";
private $createtime = "";
public function getID() {
return intval($this->id);
}
public function getLabel($flag = 0) {
switch ($flag) {
case HTMLSAFE:
return htmlspecialchars($this->label);
break;
case HTMLFORMSAFE:
return htmlspecialchars($this->label, ENT_QUOTES);
break;
case CSVSAFE:
return str_replace('"', '""', $this->label);
break;
default:
return $this->label;
break;
}
}
public function getURL($flag = 0) {
switch ($flag) {
case HTMLSAFE:
return htmlspecialchars($this->url);
break;
case HTMLFORMSAFE:
return htmlspecialchars($this->url, ENT_QUOTES);
break;
case CSVSAFE:
return str_replace('"', '""', $this->url);
break;
default:
return $this->url;
break;
}
}
public function getCreateTime($flag = 0) {
switch ($flag) {
case TIMESTAMP:
return strtotime($this->createtime);
break;
case PRETTY:
return date("F j Y H:i:s", strtotime($this->createtime));
break;
default:
return $this->createtime;
break;
}
}
public function setID($id = null) {
if (is_null($id)) return false;
$id = abs(intval($id));
if ($id == 0) return false;
$this->id = $id;
return true;
}
public function setFolderName($foldername = null) {
if (is_null($foldername) || ($foldername == "")) return false;
settype($foldername, "string");
$this->foldername = $foldername;
return true;
}
public function setLabel($label = null) {
if (is_null($label) || ($label == "")) return false;
settype($label, "string");
$this->label = $label;
return true;
}
public function setURL($url = null) {
if (is_null($url) || ($url == "")) return false;
settype($url, "string");
$this->url = $url;
return true;
}
public static function getLinks() {
global $globaldbh;
$query = "SELECT id FROM " . AppDB::TABLE_LINKS . " ORDER BY id";
$sth = $globaldbh->prepare($query);
$sth->execute();
$thelist = array();
while ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
$thelist[] = new Link($row['id']);
}
return $thelist;
}
public function jsonSerialize() {
return [
'id' => $this->getID(),
'label' => $this->getLabel(),
'url' => $this->getURL(),
'createtime' => $this->getCreateTime()
];
}
public function save() {
global $globaldbh;
$query = "INSERT INTO " . AppDB::TABLE_LINKS . " ";
$query .= "(id, label, url, createtime) ";
$query .= "VALUES(:id, :label, :url, NOW()) ";
$query .= "ON DUPLICATE KEY UPDATE ";
$query .= "label=:label, url=:url";
$sth = $globaldbh->prepare($query);
$sth->bindValue(":id", (int) $this->getID(), PDO::PARAM_INT);
$sth->bindValue(":label", $this->getLabel(), PDO::PARAM_STR);
$sth->bindValue(":url", $this->getURL(), PDO::PARAM_STR);
$sth->execute();
if ( $this->getID() == 0 ) $this->setID($globaldbh->lastInsertId());
}
function __construct($reqid = 0) {
global $globaldbh;
$reqid = intval($reqid);
$query = "SELECT id, label, url, createtime " .
"FROM " . AppDB::TABLE_LINKS . " WHERE id=:id";
$sth = $globaldbh->prepare($query);
$sth->bindValue(":id", $reqid, PDO::PARAM_INT);
$sth->execute();
if ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
$this->setID($row['id']);
$this->setLabel($row['label']);
$this->setURL($row['url']);
$this->createtime = $row['createtime'];
}
}
}
// vim: set ts=4:sw=4