246 lines
7.4 KiB
PHP
Raw Normal View History

<?php
/* μlogger
*
* Copyright(C) 2017 Bartek Fabiszewski (www.fabiszewski.net)
*
* This is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
2017-04-11 17:00:40 +02:00
require_once(ROOT_DIR . "/helpers/db.php");
2017-04-14 17:24:09 +02:00
require_once(ROOT_DIR . "/helpers/position.php");
2017-04-09 23:35:55 +02:00
/**
* Track handling
*/
class uTrack {
public $id;
public $userId;
public $name;
public $comment;
public $isValid = false;
2017-04-09 23:35:55 +02:00
/**
* Constructor
*
* @param int $trackId Track id
*/
public function __construct($trackId = NULL) {
if (!empty($trackId)) {
2019-01-23 12:23:25 +01:00
try {
$query = "SELECT id, user_id, name, comment FROM " . self::db()->table('tracks') . " WHERE id = ? LIMIT 1";
$stmt = self::db()->prepare($query);
$stmt->execute([$trackId]);
2019-01-24 19:07:41 +01:00
$stmt->bindColumn('id', $this->id, PDO::PARAM_INT);
$stmt->bindColumn('user_id', $this->userId, PDO::PARAM_INT);
2019-01-23 12:23:25 +01:00
$stmt->bindColumn('name', $this->name);
$stmt->bindColumn('comment', $this->comment);
2019-01-24 19:07:41 +01:00
if ($stmt->fetch(PDO::FETCH_BOUND)) {
$this->isValid = true;
}
2019-01-23 12:23:25 +01:00
} catch (PDOException $e) {
// TODO: handle exception
2019-01-24 19:07:41 +01:00
syslog(LOG_ERR, $e->getMessage());
}
}
}
/**
* Get db instance
*
* @return uDb instance
*/
private static function db() {
2019-05-15 11:48:24 +02:00
return uDb::getInstance();
}
2017-04-09 23:35:55 +02:00
/**
* Add new track
*
* @param string $userId User id
* @param string $name Name
* @param string $comment Optional comment
* @return int|bool New track id, false on error
*/
public static function add($userId, $name, $comment = NULL) {
$trackId = false;
if (!empty($userId) && !empty($name)) {
2019-01-23 12:23:25 +01:00
try {
$table = self::db()->table('tracks');
$query = "INSERT INTO $table (user_id, name, comment) VALUES (?, ?, ?)";
$stmt = self::db()->prepare($query);
$params = [ $userId, $name, $comment ];
$stmt->execute($params);
$trackId = (int) self::db()->lastInsertId("${table}_id_seq");
2019-01-23 12:23:25 +01:00
} catch (PDOException $e) {
// TODO: handle exception
2019-01-24 19:07:41 +01:00
syslog(LOG_ERR, $e->getMessage());
}
}
return $trackId;
}
/**
* Add new position to track
*
* @param int $userId
2019-05-15 11:48:24 +02:00
* @param int $timestamp Unix time stamp
* @param double $lat
* @param double $lon
* @param double $altitude Optional
* @param double $speed Optional
* @param double $bearing Optional
* @param int $accuracy Optional
* @param string $provider Optional
* @param string $comment Optional
* @param int $imageId Optional
* @return int|bool New position id in database, false on error
*/
public function addPosition($userId, $timestamp, $lat, $lon,
$altitude = NULL, $speed = NULL, $bearing = NULL, $accuracy = NULL,
$provider = NULL, $comment = NULL, $imageId = NULL) {
2018-11-09 12:42:35 +01:00
return uPosition::add($userId, $this->id, $timestamp, $lat, $lon,
$altitude, $speed, $bearing, $accuracy, $provider, $comment, $imageId);
}
2017-04-14 17:24:09 +02:00
/**
* Delete track with all positions
*
* @return bool True if success, false otherwise
*/
public function delete() {
$ret = false;
if ($this->isValid) {
// delete positions
if (uPosition::deleteAll($this->userId, $this->id) === false) {
2017-04-14 17:24:09 +02:00
return false;
}
// delete track metadata
2019-01-23 12:23:25 +01:00
try {
$query = "DELETE FROM " . self::db()->table('tracks') . " WHERE id = ?";
$stmt = self::db()->prepare($query);
$stmt->execute([ $this->id ]);
2017-04-14 17:24:09 +02:00
$ret = true;
$this->id = NULL;
$this->userId = NULL;
$this->name = NULL;
$this->comment = NULL;
$this->isValid = false;
2019-01-23 12:23:25 +01:00
} catch (PDOException $e) {
// TODO: handle exception
2019-01-24 19:07:41 +01:00
syslog(LOG_ERR, $e->getMessage());
2017-04-14 17:24:09 +02:00
}
}
return $ret;
}
/**
* Update track
*
* @param string|null $name New name (not empty string) or NULL if not changed
* @param string|null $comment New comment or NULL if not changed (to remove content use empty string: "")
* @return bool True if success, false otherwise
*/
public function update($name = NULL, $comment = NULL) {
$ret = false;
if (empty($name)) { $name = $this->name; }
if (is_null($comment)) { $comment = $this->comment; }
if ($comment === "") { $comment = NULL; }
2017-04-14 17:24:09 +02:00
if ($this->isValid) {
2019-01-23 12:23:25 +01:00
try {
$query = "UPDATE " . self::db()->table('tracks') . " SET name = ?, comment = ? WHERE id = ?";
$stmt = self::db()->prepare($query);
$params = [ $name, $comment, $this->id ];
$stmt->execute($params);
2017-04-14 17:24:09 +02:00
$ret = true;
$this->name = $name;
$this->comment = $comment;
2019-01-23 12:23:25 +01:00
} catch (PDOException $e) {
// TODO: handle exception
2019-01-24 19:07:41 +01:00
syslog(LOG_ERR, $e->getMessage());
2017-04-14 17:24:09 +02:00
}
}
return $ret;
}
2017-04-12 20:16:39 +02:00
/**
* Delete all user's tracks
*
* @param string $userId User id
* @return bool True if success, false otherwise
*/
public static function deleteAll($userId) {
2017-04-12 20:16:39 +02:00
$ret = false;
if (!empty($userId) && uPosition::deleteAll($userId) === true) {
// remove all tracks
try {
$query = "DELETE FROM " . self::db()->table('tracks') . " WHERE user_id = ?";
$stmt = self::db()->prepare($query);
$stmt->execute([ $userId ]);
$ret = true;
} catch (PDOException $e) {
// TODO: handle exception
syslog(LOG_ERR, $e->getMessage());
2017-04-12 20:16:39 +02:00
}
}
return $ret;
}
2017-04-09 23:35:55 +02:00
/**
* Get all tracks
*
* @param int $userId Optional limit to user id
* @return array|bool Array of uTrack tracks, false on error
*/
public static function getAll($userId = NULL) {
if (!empty($userId)) {
2019-01-23 12:23:25 +01:00
$where = "WHERE user_id=" . self::db()->quote($userId);
} else {
$where = "";
}
2019-01-23 12:23:25 +01:00
$query = "SELECT id, user_id, name, comment FROM " . self::db()->table('tracks') . " $where ORDER BY id DESC";
try {
$result = self::db()->query($query);
$trackArr = [];
while ($row = $result->fetch()) {
$trackArr[] = self::rowToObject($row);
}
} catch (PDOException $e) {
// TODO: handle exception
2019-01-24 19:07:41 +01:00
syslog(LOG_ERR, $e->getMessage());
2019-01-23 12:23:25 +01:00
$trackArr = false;
}
return $trackArr;
}
2017-04-09 23:35:55 +02:00
/**
* Convert database row to uTrack
*
* @param array $row Row
* @return uTrack Track
*/
private static function rowToObject($row) {
$track = new uTrack();
$track->id = $row['id'];
$track->userId = $row['user_id'];
$track->name = $row['name'];
$track->comment = $row['comment'];
$track->isValid = true;
return $track;
}
2017-04-09 23:35:55 +02:00
}
?>