Added new record tag API
This commit is contained in:
parent
4e241f0ed2
commit
68f8ebf719
6 changed files with 198 additions and 11 deletions
24
juggl-server/api/addRecordTag.php
Normal file
24
juggl-server/api/addRecordTag.php
Normal file
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
session_start();
|
||||
require_once(__DIR__ . "/services/apiBranch.inc.php");
|
||||
require_once(__DIR__ . "/services/responses.inc.php");
|
||||
require_once(__DIR__ . "/services/jugglDbApi.inc.php");
|
||||
|
||||
class AddRecordTagBranch extends ApiBranch
|
||||
{
|
||||
function get(ParamCleaner $params)
|
||||
{
|
||||
respondStatus(405);
|
||||
}
|
||||
|
||||
function post(ParamCleaner $params)
|
||||
{
|
||||
$user_id = $params->get("user_id");
|
||||
$tag_name = $params->get("tag_name");
|
||||
|
||||
addRecordTag($user_id, $tag_name);
|
||||
}
|
||||
}
|
||||
|
||||
$branch = new AddRecordTagBranch();
|
||||
$branch->execute();
|
24
juggl-server/api/addTagToRecord.php
Normal file
24
juggl-server/api/addTagToRecord.php
Normal file
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
session_start();
|
||||
require_once(__DIR__ . "/services/apiBranch.inc.php");
|
||||
require_once(__DIR__ . "/services/responses.inc.php");
|
||||
require_once(__DIR__ . "/services/jugglDbApi.inc.php");
|
||||
|
||||
class AddTagToRecordBranch extends ApiBranch
|
||||
{
|
||||
function get(ParamCleaner $params)
|
||||
{
|
||||
respondStatus(405);
|
||||
}
|
||||
|
||||
function post(ParamCleaner $params)
|
||||
{
|
||||
$record_id = $params->get("record_id");
|
||||
$tag_id = $params->get("tag_id");
|
||||
|
||||
addTagToRecord($tag_id, $record_id);
|
||||
}
|
||||
}
|
||||
|
||||
$branch = new AddTagToRecordBranch();
|
||||
$branch->execute();
|
29
juggl-server/api/getRecordTags.php
Normal file
29
juggl-server/api/getRecordTags.php
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
session_start();
|
||||
require_once(__DIR__ . "/services/apiBranch.inc.php");
|
||||
require_once(__DIR__ . "/services/jsonBuilder.inc.php");
|
||||
require_once(__DIR__ . "/services/responses.inc.php");
|
||||
require_once(__DIR__ . "/services/jugglDbApi.inc.php");
|
||||
|
||||
class GetRecordTagsBranch extends ApiBranch
|
||||
{
|
||||
function get(ParamCleaner $params)
|
||||
{
|
||||
respondStatus(405);
|
||||
}
|
||||
|
||||
function post(ParamCleaner $params)
|
||||
{
|
||||
$user_id = $params->get("user_id");
|
||||
|
||||
$tags = getRecordTags($user_id);
|
||||
|
||||
$json = new JsonBuilder();
|
||||
$json->addRecordTags($tags);
|
||||
|
||||
respondJson($json);
|
||||
}
|
||||
}
|
||||
|
||||
$branch = new GetRecordTagsBranch();
|
||||
$branch->execute();
|
24
juggl-server/api/removeTagFromRecord.php
Normal file
24
juggl-server/api/removeTagFromRecord.php
Normal file
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
session_start();
|
||||
require_once(__DIR__ . "/services/apiBranch.inc.php");
|
||||
require_once(__DIR__ . "/services/responses.inc.php");
|
||||
require_once(__DIR__ . "/services/jugglDbApi.inc.php");
|
||||
|
||||
class RemoveTagFromRecordBranch extends ApiBranch
|
||||
{
|
||||
function get(ParamCleaner $params)
|
||||
{
|
||||
respondStatus(405);
|
||||
}
|
||||
|
||||
function post(ParamCleaner $params)
|
||||
{
|
||||
$record_id = $params->get("record_id");
|
||||
$tag_id = $params->get("tag_id");
|
||||
|
||||
removeTagFromRecord($tag_id, $record_id);
|
||||
}
|
||||
}
|
||||
|
||||
$branch = new RemoveTagFromRecordBranch();
|
||||
$branch->execute();
|
|
@ -57,6 +57,23 @@ class JsonBuilder
|
|||
return $this;
|
||||
}
|
||||
|
||||
function addRecordTags(array $record_tags)
|
||||
{
|
||||
if ($record_tags === null) return;
|
||||
|
||||
$columns = array(
|
||||
"record_tag_id" => "",
|
||||
"name" => "",
|
||||
"user_id" => ""
|
||||
);
|
||||
|
||||
$this->jsonData['record_tags'] = array();
|
||||
foreach ($record_tags as $tag) {
|
||||
$this->jsonData['record_tags'][] = $this->createJsonArray($tag, $columns);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function createJsonArray(array $data, array $columns)
|
||||
{
|
||||
$jsonArray = array();
|
||||
|
|
|
@ -44,10 +44,7 @@ function getTimeRecord($user_id, $record_id)
|
|||
}
|
||||
$result = $result[0];
|
||||
|
||||
// Is still running?
|
||||
if ($result["end_time"] == null) {
|
||||
$result["duration"] = calcDuration($result["start_time"]);
|
||||
}
|
||||
$result = getRecordExternalData($result);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
@ -114,10 +111,7 @@ function getProjectRecord($user_id, $project_id, $finished = null)
|
|||
}
|
||||
$result = $result[0];
|
||||
|
||||
// Is still running?
|
||||
if ($result["end_time"] == null) {
|
||||
$result["duration"] = calcDuration($result["start_time"]);
|
||||
}
|
||||
$result = getRecordExternalData($result);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
@ -131,8 +125,8 @@ function getRunningRecords($user_id)
|
|||
$results = $db->execute();
|
||||
|
||||
// Is still running?
|
||||
foreach ($results as $key => $value) {
|
||||
$results[$key]["duration"] = calcDuration($results[$key]["start_time"]);
|
||||
foreach ($results as $key => $record) {
|
||||
$results[$key] = getRecordExternalData($record);
|
||||
}
|
||||
|
||||
return $results;
|
||||
|
@ -163,7 +157,6 @@ function updateTimeRecord($user_id, $params)
|
|||
{
|
||||
$data = [];
|
||||
|
||||
|
||||
$anythingUpdated = false;
|
||||
if ($params->exists(["start_time"])) {
|
||||
$data["start_time"] = $params->get("start_time");
|
||||
|
@ -197,6 +190,32 @@ function updateTimeRecord($user_id, $params)
|
|||
$db->execute();
|
||||
}
|
||||
|
||||
function getRecordExternalData($record)
|
||||
{
|
||||
if ($record == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Duration
|
||||
if ($record["end_time"] == NULL) {
|
||||
$record["duration"] = calcDuration($record["start_time"]);
|
||||
}
|
||||
|
||||
// Tags
|
||||
$tags = array();
|
||||
foreach (getTagsOnRecord($record["record_id"]) as $key => $tag) {
|
||||
$data = [
|
||||
"record_tag_id" => $tag["record_tag_id"],
|
||||
"name" => $tag["name"],
|
||||
"user_id" => $tag["user_id"]
|
||||
];
|
||||
$tags[] = $data;
|
||||
}
|
||||
$record["tags"] = $tags;
|
||||
|
||||
return $record;
|
||||
}
|
||||
|
||||
function isProjectValid($project_id, $user_id)
|
||||
{
|
||||
$db = new DbOperations();
|
||||
|
@ -221,3 +240,53 @@ function calcDuration($start_time, $end_time = "NOW")
|
|||
{
|
||||
return (int)(strtotime($end_time) - strtotime($start_time));
|
||||
}
|
||||
|
||||
function getRecordTags($user_id)
|
||||
{
|
||||
$db = new DbOperations();
|
||||
$db->select("record_tags");
|
||||
$db->where("user_id", Comparison::EQUAL, $user_id);
|
||||
return $db->execute();
|
||||
}
|
||||
|
||||
function getTagsOnRecord($record_id)
|
||||
{
|
||||
$db = new DbOperations();
|
||||
$db->select("tags_on_records");
|
||||
$db->innerJoin("record_tags", "record_tag_id");
|
||||
$db->where("record_id", Comparison::EQUAL, $record_id);
|
||||
return $db->execute();
|
||||
}
|
||||
|
||||
function addRecordTag($user_id, $tag_name)
|
||||
{
|
||||
$data = [
|
||||
"user_id" => $user_id,
|
||||
"name" => $tag_name
|
||||
];
|
||||
|
||||
$db = new DbOperations();
|
||||
$db->insert("record_tags", $data);
|
||||
$db->execute();
|
||||
}
|
||||
|
||||
function addTagToRecord($tag_id, $record_id)
|
||||
{
|
||||
$data = [
|
||||
"record_tag_id" => $tag_id,
|
||||
"record_id" => $record_id
|
||||
];
|
||||
|
||||
$db = new DbOperations();
|
||||
$db->insert("tags_on_records", $data);
|
||||
$db->execute();
|
||||
}
|
||||
|
||||
function removeTagFromRecord($tag_id, $record_id)
|
||||
{
|
||||
$db = new DbOperations();
|
||||
$db->delete("tags_on_records");
|
||||
$db->where("record_tag_id", Comparison::EQUAL, $tag_id);
|
||||
$db->where("record_id", Comparison::EQUAL, $record_id);
|
||||
$db->execute();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue