From 7088d326aba88b1b20f099fa240b34c41f45d418 Mon Sep 17 00:00:00 2001 From: Bartek Fabiszewski Date: Fri, 14 Apr 2017 17:24:09 +0200 Subject: [PATCH] Add track edit/delete feature --- css/main.css | 4 ++ helpers/position.php | 22 +++++++--- helpers/track.php | 59 +++++++++++++++++++++++++ index.php | 17 ++++--- js/admin.js | 2 +- js/main.js | 7 ++- js/track.js | 100 ++++++++++++++++++++++++++++++++++++++++++ lang.php | 2 +- lang/en.php | 14 ++++-- lang/pl.php | 12 ++++- utils/handletrack.php | 80 +++++++++++++++++++++++++++++++++ 11 files changed, 301 insertions(+), 18 deletions(-) create mode 100644 js/track.js create mode 100644 utils/handletrack.php diff --git a/css/main.css b/css/main.css index 4039d0e..762fc42 100755 --- a/css/main.css +++ b/css/main.css @@ -53,6 +53,10 @@ select { #menu input[type = "checkbox"] { width: auto; } +.menulink { + display: block; + margin-top: .2em; +} #main { height: 100%; margin-right: 165px; diff --git a/helpers/position.php b/helpers/position.php index 88b9f7f..be4f6a5 100644 --- a/helpers/position.php +++ b/helpers/position.php @@ -102,17 +102,27 @@ } /** - * Delete all user's positions + * Delete all user's positions, optionally limit to given track * - * @param string $userId User id + * @param int $userId User id + * @param int $trackId Optional track id * @return bool True if success, false otherwise */ - public function deleteAll($userId) { + public function deleteAll($userId, $trackId = NULL) { $ret = false; if (!empty($userId)) { - $query = "DELETE FROM positions WHERE user_id = ?"; + $args = []; + $where = "WHERE user_id = ?"; + $args[0] = "i"; + $args[1] = &$userId; + if (!empty($trackId)) { + $where .= " AND track_id = ?"; + $args[0] .= "i"; + $args[2] = &$trackId; + } + $query = "DELETE FROM positions $where"; $stmt = self::$db->prepare($query); - $stmt->bind_param('i', $userId); + call_user_func_array([ $stmt, 'bind_param' ], $args); $stmt->execute(); if (!self::$db->error && !$stmt->errno) { $ret = true; @@ -127,6 +137,7 @@ * (for given user if specified) * * @param int $userId Optional user id + * @return uPosition Self */ public function getLast($userId = NULL) { if (!empty($userId)) { @@ -145,6 +156,7 @@ $where ORDER BY p.time DESC LIMIT 1"; $this->loadWithQuery($query, $params); + return $this; } /** diff --git a/helpers/track.php b/helpers/track.php index 39115e3..c4a4e21 100644 --- a/helpers/track.php +++ b/helpers/track.php @@ -18,6 +18,7 @@ */ require_once(ROOT_DIR . "/helpers/db.php"); + require_once(ROOT_DIR . "/helpers/position.php"); /** * Track handling @@ -77,6 +78,64 @@ return $trackId; } + /** + * Delete track with all positions + * + * @return bool True if success, false otherwise + */ + public function delete() { + $ret = false; + if ($this->isValid) { + // delete positions + $position = new uPosition(); + if ($position->deleteAll($this->userId, $this->id) === false) { + return false; + } + // delete track metadata + $query = "DELETE FROM tracks WHERE id = ?"; + $stmt = self::$db->prepare($query); + $stmt->bind_param('i', $this->id); + $stmt->execute(); + if (!self::$db->error && !$stmt->errno) { + $ret = true; + $this->id = NULL; + $this->userId = NULL; + $this->name = NULL; + $this->comment = NULL; + $this->isValid = false; + } + $stmt->close(); + } + 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; } + if ($this->isValid) { + $query = "UPDATE tracks SET name = ?, comment = ? WHERE id = ?"; + $stmt = self::$db->prepare($query); + $stmt->bind_param('ssi', $name, $comment, $this->id); + $stmt->execute(); + if (!self::$db->error && !$stmt->errno) { + $ret = true; + $this->name = $name; + $this->comment = $comment; + } + $stmt->close(); + } + return $ret; + } + /** * Delete all user's tracks * diff --git a/index.php b/index.php index 5372e20..f81cf8c 100755 --- a/index.php +++ b/index.php @@ -80,6 +80,7 @@ var init_latitude = ''; var init_longitude = ''; var lang = ; + var admin = isAdmin) ?>; var auth = 'isValid) ? $user->login : "null" ?>'; var pass_regex = passRegex() ?>; @@ -95,6 +96,9 @@ isAdmin): ?> + isValid): ?> + +