LinkProvider/class_link.php

136 lines
3.7 KiB
PHP
Executable File

<?php
class WebLink {
private $id = null;
private $url = null;
private $title = null;
private $description = null;
const TABLE = "links";
public function getID() {
return $this->id;
}
public function getURL($flag = 0) {
switch ($flag) {
case HTMLSAFE:
return htmlspecialchars($this->url);
break;
case HTMLFORMSAFE:
return htmlspecialchars($this->url, ENT_QUOTES);
break;
default:
return $this->url;
break;
}
}
public function getTitle($flag = 0) {
switch ($flag) {
case HTMLSAFE:
return htmlspecialchars($this->title);
break;
case HTMLFORMSAFE:
return htmlspecialchars($this->title, ENT_QUOTES);
break;
default:
return $this->title;
break;
}
}
public function getDescription($flag = 0) {
switch ($flag) {
case HTMLSAFE:
return htmlspecialchars($this->description);
break;
case HTMLFORMSAFE:
return htmlspecialchars($this->description, ENT_QUOTES);
break;
default:
return $this->description;
break;
}
}
public function setURL($value) {
if ( is_null($value) || ($value == "") ) return false;
if ( !filter_var($value, FILTER_VALIDATE_URL) ) return false;
$this->url = $value;
return true;
}
public function setTitle($value) {
if ( is_null($value) || ($value == "") ) return false;
$this->title = $value;
return true;
}
public function setDescription($value) {
if ( is_null($value) || ($value == "") ) return false;
$this->description = $value;
return true;
}
public static function getList() {
global $globaldbh;
$query = "SELECT id FROM " . WebLink::TABLE . " ORDER BY title";
$sth = $globaldbh->prepare($query);
$sth->execute();
$thelist = array();
while ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
$thelist[] = new WebLink($row['id']);
}
return $thelist;
}
public function delete() {
global $globaldbh;
if ( is_null($this->id) ) return false;
$query = "DELETE from " . WebLink::TABLE . " WHERE id=:id";
$fields = array();
$fields[':id'] = $this->id;
$sth = $globaldbh->prepare($query);
$saved = $sth->execute($fields);
return $saved;
}
public function save() {
global $globaldbh;
$query = "INSERT INTO " . WebLink::TABLE . " " .
"(id, url, title, description) " .
"VALUES(:id, :url, :title, :description) " .
"ON DUPLICATE KEY UPDATE " .
"url=:url, title=:title, description=:description";
$fields = array();
$fields[':id'] = $this->getID();
$fields[':url'] = $this->getURL();
$fields[':title'] = $this->getTitle();
$fields[':description'] = $this->getDescription();
$sth = $globaldbh->prepare($query);
$saved = $sth->execute($fields);
return $saved;
}
public function __construct($id = null) {
global $globaldbh;
$query = "SELECT id, url, title, description FROM " . WebLink::TABLE . " WHERE id=:id";
$fields = array();
$fields[':id'] = $id;
$sth = $globaldbh->prepare($query);
$sth->execute($fields);
if ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
$this->id = $row['id'];
$this->setURL($row['url']);
$this->setTitle($row['title']);
$this->setDescription($row['description']);
}
return;
}
}