Webservices
This commit is contained in:
128
services/www/api/objects/gallery_item.php
Normal file
128
services/www/api/objects/gallery_item.php
Normal file
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
class GalleryItem {
|
||||
|
||||
private const table_name = "gallery";
|
||||
private const select = "
|
||||
SELECT
|
||||
g.*,
|
||||
(select count(id) from gallery_like where gallery_id = g.id) as likes,
|
||||
|
||||
lu.id as lu_id,
|
||||
lu.name as lu_name,
|
||||
lu.surname as lu_surname,
|
||||
lu.code as lu_code,
|
||||
lu.picture as lu_picture,
|
||||
lu.admin as lu_admin,
|
||||
lu.table as lu_table,
|
||||
lu.witness as lu_witness,
|
||||
lu.invited_by as lu_invited_by,
|
||||
|
||||
au.id as au_id,
|
||||
au.name as au_name,
|
||||
au.surname as au_surname,
|
||||
au.code as au_code,
|
||||
au.picture as au_picture,
|
||||
au.admin as au_admin,
|
||||
au.table as au_table,
|
||||
au.witness as au_witness,
|
||||
au.invited_by as au_invited_by,
|
||||
|
||||
glcu.id as glcu_id,
|
||||
glcu.gallery_id as glcu_gallery_id,
|
||||
glcu.user_id as glcu_user_id,
|
||||
glcu.created as glcu_created
|
||||
|
||||
FROM " . self::table_name . " g
|
||||
-- Author
|
||||
LEFT JOIN `user` au
|
||||
ON au.id = g.author
|
||||
-- First liked user
|
||||
LEFT JOIN gallery_like gl
|
||||
ON gl.gallery_id = g.id
|
||||
LEFT JOIN `user` lu
|
||||
ON lu.id = gl.user_id
|
||||
-- Like from current user
|
||||
LEFT JOIN gallery_like glcu
|
||||
ON glcu.gallery_id = g.id
|
||||
AND glcu.user_id = :uid
|
||||
";
|
||||
|
||||
static function read($db, $uid, $page, $elemPerPage){
|
||||
$start = $page * $elemPerPage;
|
||||
$limit = $elemPerPage + 1; // Retrieve one more item to know if there are elements and compute "more" value
|
||||
$query = self::select . "
|
||||
GROUP BY g.id
|
||||
ORDER BY created DESC
|
||||
LIMIT :start,:limit";
|
||||
|
||||
// prepare query statement
|
||||
$stmt = $db->prepare($query);
|
||||
$stmt->bindParam(":uid", $uid);
|
||||
$stmt->bindParam(":start", $start, PDO::PARAM_INT);
|
||||
$stmt->bindParam(":limit", $limit, PDO::PARAM_INT);
|
||||
|
||||
// execute query
|
||||
$stmt->execute();
|
||||
|
||||
return $stmt;
|
||||
}
|
||||
|
||||
static function readById($db, $uid, $id){
|
||||
$query = self::select . "
|
||||
WHERE g.id = :id
|
||||
GROUP BY g.id
|
||||
ORDER BY created DESC
|
||||
";
|
||||
// prepare query statement
|
||||
$stmt = $db->prepare($query);
|
||||
$stmt->bindParam(":id", $id);
|
||||
$stmt->bindParam(":uid", $uid);
|
||||
|
||||
// execute query
|
||||
$stmt->execute();
|
||||
|
||||
return $stmt;
|
||||
}
|
||||
|
||||
static function create($db, $item){
|
||||
$query = "INSERT INTO " . self::table_name . "
|
||||
SET image_url=:imageUrl, image_thumb_url=:imageThumbUrl, description=:description, author=:author";
|
||||
|
||||
// prepare query
|
||||
$stmt = $db->prepare($query);
|
||||
|
||||
// sanitize
|
||||
$imageUrl=htmlspecialchars(strip_tags($item['imageUrl']));
|
||||
$imageThumbUrl=htmlspecialchars(strip_tags($item['imageThumbUrl']));
|
||||
$description=htmlspecialchars(strip_tags($item['description']));
|
||||
$author=htmlspecialchars(strip_tags($item['author']));
|
||||
|
||||
// bind values
|
||||
$stmt->bindParam(":imageUrl", $imageUrl);
|
||||
$stmt->bindParam(":imageThumbUrl", $imageThumbUrl);
|
||||
$stmt->bindParam(":description", $description);
|
||||
$stmt->bindParam(":author", $author);
|
||||
|
||||
// execute query
|
||||
if($stmt->execute()){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static function fromRow($row) {
|
||||
return array(
|
||||
"id" => $row['id'],
|
||||
"imageUrl" => $row['image_url'],
|
||||
"imageThumbUrl" => $row['image_thumb_url'],
|
||||
"likes" => $row['likes'],
|
||||
"firstUserLiked" => User::createFromRow($row, 'lu'),
|
||||
"description" => $row['description'],
|
||||
"author" => User::createFromRow($row, 'au'),
|
||||
"created" => $row['created'],
|
||||
"currentUserLike" => Like::createFromRow($row, 'glcu'),
|
||||
);
|
||||
}
|
||||
}
|
||||
?>
|
||||
101
services/www/api/objects/like.php
Normal file
101
services/www/api/objects/like.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
class Like {
|
||||
private $conn;
|
||||
private const table_name = "gallery_like";
|
||||
|
||||
public $id;
|
||||
public $gallery_id;
|
||||
public $user_id;
|
||||
public $created;
|
||||
|
||||
public function __construct($db){
|
||||
$this->conn = $db;
|
||||
}
|
||||
|
||||
function create() {
|
||||
$query = "INSERT INTO " . self::table_name . "
|
||||
SET gallery_id=:galleryId, user_id=:userId";
|
||||
|
||||
// prepare query
|
||||
$stmt = $this->conn->prepare($query);
|
||||
|
||||
$this->created=(new \DateTime())->format('Y-m-d H:i:s');
|
||||
|
||||
// bind values
|
||||
$stmt->bindParam(":userId", $this->user_id);
|
||||
$stmt->bindParam(":galleryId", $this->gallery_id);
|
||||
|
||||
// execute query
|
||||
if($stmt->execute()){
|
||||
return $this->conn->lastInsertId();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static function delete($db, $id) {
|
||||
$query = "DELETE FROM " . self::table_name . " WHERE id = :id";
|
||||
$stmt = $db->prepare($query);
|
||||
$stmt->bindParam(":id", $id);
|
||||
if($stmt->execute()){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function createFromRow($row, $prefix = '') {
|
||||
if ($prefix)
|
||||
$prefix = $prefix . '_';
|
||||
else
|
||||
$prefix = '';
|
||||
|
||||
if (!$row[$prefix . 'id'])
|
||||
return null;
|
||||
|
||||
$instance = new self(null);
|
||||
$instance->id = $row[$prefix . 'id'];
|
||||
$instance->gallery_id = $row[$prefix . 'gallery_id'];
|
||||
$instance->user_id = $row[$prefix . 'user_id'];
|
||||
$instance->created = $row[$prefix . 'created'];
|
||||
return $instance;
|
||||
}
|
||||
|
||||
public static function get($db, $uid, $gid) {
|
||||
$q = "
|
||||
SELECT *
|
||||
FROM " . self::table_name . "
|
||||
WHERE
|
||||
user_id = :uid AND
|
||||
gallery_id = :gid
|
||||
";
|
||||
$stmt = $db->prepare($q);
|
||||
$stmt->bindParam(":uid", $uid);
|
||||
$stmt->bindParam(":gid", $gid);
|
||||
$stmt->execute();
|
||||
|
||||
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
if ($row)
|
||||
return self::createFromRow($row);
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function byUserAndId($db, $uid, $id) {
|
||||
$q = "
|
||||
SELECT *
|
||||
FROM " . self::table_name . "
|
||||
WHERE
|
||||
user_id = :uid AND
|
||||
id = :id
|
||||
";
|
||||
$stmt = $db->prepare($q);
|
||||
$stmt->bindParam(":uid", $uid);
|
||||
$stmt->bindParam(":id", $id);
|
||||
$stmt->execute();
|
||||
|
||||
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
if ($row)
|
||||
return self::createFromRow($row);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
65
services/www/api/objects/presence.php
Normal file
65
services/www/api/objects/presence.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
class Presence {
|
||||
|
||||
// database connection and table name
|
||||
private $conn;
|
||||
private $table_name = "presence";
|
||||
|
||||
// object properties
|
||||
public $id;
|
||||
public $userId;
|
||||
public $willBePresent = false;
|
||||
public $notes = null;
|
||||
public $created;
|
||||
|
||||
// constructor with $db as database connection
|
||||
public function __construct($db){
|
||||
$this->conn = $db;
|
||||
}
|
||||
|
||||
static function read($db, $uid){
|
||||
$query = "
|
||||
SELECT will_be_present, notes
|
||||
FROM presence
|
||||
WHERE user_id = :uid
|
||||
ORDER BY created DESC
|
||||
LIMIT 1";
|
||||
|
||||
// prepare query statement
|
||||
$stmt = $db->prepare($query);
|
||||
$stmt->bindParam(":uid", $uid);
|
||||
|
||||
// execute query
|
||||
$stmt->execute();
|
||||
|
||||
return $stmt;
|
||||
}
|
||||
|
||||
function create(){
|
||||
|
||||
$query = "INSERT INTO " . $this->table_name . "
|
||||
SET user_id=:userId, will_be_present=:willBePresent, notes=:notes";
|
||||
|
||||
// prepare query
|
||||
$stmt = $this->conn->prepare($query);
|
||||
|
||||
// sanitize
|
||||
$this->userId=$this->userId;
|
||||
$this->created=(new \DateTime())->format('Y-m-d H:i:s');
|
||||
|
||||
|
||||
// bind values
|
||||
$stmt->bindParam(":userId", $this->userId);
|
||||
$wbp = $this->willBePresent ? 1 : 0;
|
||||
$stmt->bindParam(":willBePresent", $wbp);
|
||||
$stmt->bindParam(":notes", $this->notes);
|
||||
|
||||
// execute query
|
||||
if($stmt->execute()){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
}
|
||||
?>
|
||||
43
services/www/api/objects/token.php
Normal file
43
services/www/api/objects/token.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
class Token {
|
||||
private $conn;
|
||||
private $table_name = "token";
|
||||
|
||||
public $userId;
|
||||
public $user;
|
||||
public $token;
|
||||
public $created;
|
||||
public $expires;
|
||||
|
||||
// constructor with $db as database connection
|
||||
public function __construct($db){
|
||||
$this->conn = $db;
|
||||
}
|
||||
|
||||
function create(){
|
||||
$params = "user_id=:userId, token=:token, created=:created, expires=:expires";
|
||||
$query = "INSERT INTO " . $this->table_name . "
|
||||
SET " . $params;
|
||||
|
||||
// prepare query
|
||||
$stmt = $this->conn->prepare($query);
|
||||
|
||||
$this->created=(new \DateTime())->format('Y-m-d H:i:s');
|
||||
$this->expires=(new \DateTime())->add(new DateInterval('P1Y3M'))->format('Y-m-d H:i:s');
|
||||
$this->token=md5(sprintf('%d-%s', $this->userId, $this->expires));
|
||||
|
||||
// bind values
|
||||
$stmt->bindParam(":userId", $this->userId);
|
||||
$stmt->bindParam(":token", $this->token);
|
||||
$stmt->bindParam(":created", $this->created);
|
||||
$stmt->bindParam(":expires", $this->expires);
|
||||
|
||||
// execute query
|
||||
if($stmt->execute()){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
62
services/www/api/objects/user.php
Normal file
62
services/www/api/objects/user.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
class User {
|
||||
private $conn;
|
||||
private $table_name = "user";
|
||||
|
||||
public $id;
|
||||
public $name;
|
||||
public $surname;
|
||||
public $code;
|
||||
public $picture;
|
||||
public $admin;
|
||||
public $table;
|
||||
public $witness;
|
||||
public $invited_by;
|
||||
|
||||
// constructor with $db as database connection
|
||||
public function __construct($db){
|
||||
$this->conn = $db;
|
||||
}
|
||||
|
||||
static function get($db, $uid){
|
||||
$query = "
|
||||
SELECT *
|
||||
FROM user AS u
|
||||
WHERE u.id = :uid;
|
||||
";
|
||||
|
||||
// prepare query statement
|
||||
$stmt = $db->prepare($query);
|
||||
$stmt->bindParam(":uid", $uid);
|
||||
|
||||
// execute query
|
||||
$stmt->execute();
|
||||
|
||||
return $stmt;
|
||||
}
|
||||
|
||||
public static function createFromRow($row, $prefix = '') {
|
||||
if ($prefix)
|
||||
$prefix = $prefix . '_';
|
||||
else
|
||||
$prefix = '';
|
||||
|
||||
// If user is null
|
||||
if (!$row[$prefix . 'id'])
|
||||
return null;
|
||||
|
||||
$instance = new self(null);
|
||||
$instance->id = $row[$prefix . 'id'];
|
||||
$instance->name = $row[$prefix . 'name'];
|
||||
$instance->surname = $row[$prefix . 'surname'];
|
||||
$instance->code = $row[$prefix . 'code'];
|
||||
$instance->picture = $row[$prefix . 'picture'];
|
||||
$instance->admin = $row[$prefix . 'admin'] ? true : false;
|
||||
$instance->table = $row[$prefix . 'table'];
|
||||
$instance->witness = $row[$prefix . 'witness'] ? true : false;
|
||||
$instance->invited_by = $row[$prefix . 'invited_by'];
|
||||
return $instance;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user