560 lines
14 KiB
PHP
560 lines
14 KiB
PHP
<?php
|
|
require_once(__DIR__ . "/dbOperations.inc.php");
|
|
|
|
function addStartRecord($user_id, $params, $project_id = null, $start_device_id = null)
|
|
{
|
|
$data = [
|
|
"user_id" => $user_id,
|
|
"start_time" => $params->get("start_time"),
|
|
"project_id" => $project_id,
|
|
"start_device_id" => $start_device_id
|
|
];
|
|
|
|
$db = new DbOperations();
|
|
$db->insert("time_records", $data);
|
|
$db->execute();
|
|
}
|
|
|
|
function addProject($user_id, $params)
|
|
{
|
|
$data = [
|
|
"user_id" => $user_id,
|
|
"name" => $params->get("name"),
|
|
"start_date" => $params->get("start_date")
|
|
];
|
|
|
|
$db = new DbOperations();
|
|
$db->insert("projects", $data);
|
|
$db->execute();
|
|
}
|
|
|
|
function addTimeRecord($user_id, $params, $project_id = null, $start_device_id = null)
|
|
{
|
|
$data = [
|
|
"user_id" => $user_id,
|
|
"start_time" => $params->get("start_time"),
|
|
"end_time" => $params->get("end_time"),
|
|
"duration" => $params->get("duration"),
|
|
"project_id" => $project_id,
|
|
"start_device_id" => $start_device_id
|
|
];
|
|
|
|
$db = new DbOperations();
|
|
$db->insert("time_records", $data);
|
|
$db->execute();
|
|
}
|
|
|
|
function getTimeRecord($user_id, $record_id)
|
|
{
|
|
$db = new DbOperations();
|
|
$db->select("time_records");
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
$db->where("record_id", Comparison::EQUAL, $record_id);
|
|
$result = $db->execute();
|
|
|
|
if (count($result) <= 0) {
|
|
return null;
|
|
}
|
|
$result = $result[0];
|
|
|
|
$result = getRecordExternalData($result);
|
|
|
|
return $result;
|
|
}
|
|
|
|
function getProjects($user_id)
|
|
{
|
|
$db = new DbOperations();
|
|
$db->select("projects");
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
$results = $db->execute();
|
|
|
|
foreach ($results as $key => $project) {
|
|
$meta = getProjectRecordDerivedData($user_id, $project["project_id"]);
|
|
|
|
foreach ($meta as $metaKey => $value) {
|
|
$results[$key][$metaKey] = $value;
|
|
}
|
|
}
|
|
|
|
return $results;
|
|
}
|
|
|
|
function getUser($user_id)
|
|
{
|
|
$db = new DbOperations();
|
|
$db->select("users", ["user_id", "name", "mail_address"]);
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
$result = $db->execute();
|
|
|
|
if (count($result) <= 0) {
|
|
return null;
|
|
}
|
|
$result = $result[0];
|
|
|
|
return $result;
|
|
}
|
|
|
|
function getProjectRecordDerivedData($user_id, $project_id)
|
|
{
|
|
$durationAttribute = "SUM(duration) AS total_duration";
|
|
$recordCountAttribute = "COUNT(*) AS record_count";
|
|
|
|
$db = new DbOperations();
|
|
$db->select("time_records", ["*", $durationAttribute, $recordCountAttribute], false);
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
$db->where("project_id", Comparison::EQUAL, $project_id);
|
|
$results = $db->execute();
|
|
|
|
if (count($results) <= 0) {
|
|
return ["duration" => 0, "record_count" => 0];
|
|
} else {
|
|
return [
|
|
"duration" => (int)$results[0]["total_duration"],
|
|
"record_count" => (int)$results[0]["record_count"]
|
|
];
|
|
}
|
|
}
|
|
|
|
function getProjectRecord($user_id, $project_id, $finished = null)
|
|
{
|
|
$db = new DbOperations();
|
|
$db->select("time_records");
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
$db->where("project_id", Comparison::EQUAL, $project_id);
|
|
|
|
if ($finished !== null) {
|
|
if ($finished) {
|
|
$db->addSql(" AND end_time IS NOT NULL");
|
|
} else {
|
|
$db->addSql(" AND end_time IS NULL");
|
|
}
|
|
}
|
|
|
|
$db->orderBy("start_time", Order::DESC);
|
|
$result = $db->execute();
|
|
|
|
if (count($result) <= 0) {
|
|
return null;
|
|
}
|
|
$result = $result[0];
|
|
|
|
$result = getRecordExternalData($result);
|
|
|
|
return $result;
|
|
}
|
|
|
|
function getRunningRecords($user_id)
|
|
{
|
|
$db = new DbOperations();
|
|
$db->select("time_records");
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
$db->addSql(" AND end_time IS NULL");
|
|
$results = $db->execute();
|
|
|
|
// Is still running?
|
|
foreach ($results as $key => $record) {
|
|
$results[$key] = getRecordExternalData($record);
|
|
}
|
|
|
|
return $results;
|
|
}
|
|
|
|
function getRecords($user_id, $limit = NULL, $finished = NULL)
|
|
{
|
|
$db = new DbOperations();
|
|
$db->select("time_records");
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
if ($finished != NULL) {
|
|
if ($finished) {
|
|
$db->addSql(" AND end_time IS NOT NULL");
|
|
} else {
|
|
$db->addSql(" AND end_time IS NULL");
|
|
}
|
|
}
|
|
if ($limit != NULL) {
|
|
$db->orderBy("record_id", Order::DESC);
|
|
$db->limit($limit);
|
|
}
|
|
$results = $db->execute();
|
|
|
|
// Is still running?
|
|
foreach ($results as $key => $record) {
|
|
$results[$key] = getRecordExternalData($record);
|
|
}
|
|
|
|
return $results;
|
|
}
|
|
|
|
function updateEndRecord($user_id, $params)
|
|
{
|
|
$record_id = $params->get("record_id");
|
|
|
|
// Get start instance to calculate duration
|
|
$start_time = getTimeRecord($user_id, $record_id)["start_time"];
|
|
|
|
$record = [
|
|
"record_id" => $record_id,
|
|
"end_time" => $params->get("end_time"),
|
|
"duration" => calcDuration($start_time, $params->get("end_time"))
|
|
];
|
|
|
|
updateRecord($user_id, $record);
|
|
}
|
|
|
|
function updateRecord($user_id, $record)
|
|
{
|
|
$record_id = $record["record_id"];
|
|
|
|
// Update given parameters
|
|
$data = [];
|
|
$props = ["end_time", "start_time", "duration", "project_id"];
|
|
foreach ($props as $p) {
|
|
if (array_key_exists ($p, $record)) {
|
|
$data[$p] = $record[$p];
|
|
}
|
|
}
|
|
|
|
$db = new DbOperations();
|
|
$db->update("time_records", $data);
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
$db->where("record_id", Comparison::EQUAL, $record_id);
|
|
$db->execute();
|
|
}
|
|
|
|
function updateProject($user_id, $project)
|
|
{
|
|
$project_id = $project["project_id"];
|
|
|
|
// Update given parameters
|
|
$data = [];
|
|
$props = ["name", "start_date", "color", "visible"];
|
|
foreach ($props as $p) {
|
|
if (array_key_exists ($p, $project)) {
|
|
$data[$p] = $project[$p];
|
|
}
|
|
}
|
|
|
|
$db = new DbOperations();
|
|
$db->update("projects", $data);
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
$db->where("project_id", Comparison::EQUAL, $project_id);
|
|
$db->execute();
|
|
}
|
|
|
|
function updateRecordTag($user_id, $tag)
|
|
{
|
|
$record_tag_id = $tag["record_tag_id"];
|
|
|
|
// Update given parameters
|
|
$data = [];
|
|
$props = ["name", "visible"];
|
|
foreach ($props as $p) {
|
|
if (array_key_exists ($p, $tag)) {
|
|
$data[$p] = $tag[$p];
|
|
}
|
|
}
|
|
|
|
$db = new DbOperations();
|
|
$db->update("record_tags", $data);
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
$db->where("record_tag_id", Comparison::EQUAL, $record_tag_id);
|
|
$db->execute();
|
|
}
|
|
|
|
function removeRecord($user_id, $params)
|
|
{
|
|
$record_id = $params->get("record_id");
|
|
|
|
$db = new DbOperations();
|
|
$db->delete("time_records");
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
$db->where("record_id", Comparison::EQUAL, $record_id);
|
|
$db->execute();
|
|
}
|
|
|
|
function removeProject($user_id, $params)
|
|
{
|
|
$project_id = $params->get("project_id");
|
|
|
|
$db = new DbOperations();
|
|
$db->delete("projects");
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
$db->where("project_id", Comparison::EQUAL, $project_id);
|
|
$db->execute();
|
|
|
|
removeRecordsOfProject($user_id, $project_id);
|
|
}
|
|
|
|
function secureRandomBytes ()
|
|
{
|
|
$pr_bits = '';
|
|
|
|
// Unix/Linux platform?
|
|
$fp = @fopen('/dev/urandom','rb');
|
|
if ($fp !== FALSE) {
|
|
$pr_bits .= @fread($fp,16);
|
|
@fclose($fp);
|
|
}
|
|
|
|
// MS-Windows platform?
|
|
if (@class_exists('COM')) {
|
|
// http://msdn.microsoft.com/en-us/library/aa388176(VS.85).aspx
|
|
try {
|
|
$CAPI_Util = new COM('CAPICOM.Utilities.1');
|
|
$pr_bits .= $CAPI_Util->GetRandom(16,0);
|
|
|
|
// if we ask for binary data PHP munges it, so we
|
|
// request base64 return value. We squeeze out the
|
|
// redundancy and useless ==CRLF by hashing...
|
|
if ($pr_bits) { $pr_bits = md5($pr_bits,TRUE); }
|
|
} catch (Exception $ex) {
|
|
// echo 'Exception: ' . $ex->getMessage();
|
|
}
|
|
}
|
|
|
|
return $pr_bits;
|
|
}
|
|
|
|
function createSseKey($user_id)
|
|
{
|
|
// Generate random key
|
|
$sse_key = secureRandomBytes();
|
|
|
|
$data = [
|
|
"user_id" => $user_id,
|
|
"sse_key" => $sse_key
|
|
];
|
|
|
|
$db = new DbOperations();
|
|
$db->insert("sse_keys", $data);
|
|
$db->execute();
|
|
|
|
return $sse_key;
|
|
}
|
|
|
|
function deleteSseKey($user_id, $params)
|
|
{
|
|
$sse_key_id = $params->get("sse_key_id");
|
|
$sse_key = $params->get("sse_key");
|
|
|
|
$db = new DbOperations();
|
|
$db->delete("sse_keys");
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
if ($sse_key_id != null) {
|
|
$db->where("sse_key_id", Comparison::EQUAL, $sse_key_id);
|
|
}
|
|
if ($sse_key != null) {
|
|
$db->where("sse_key", Comparison::EQUAL, $sse_key);
|
|
}
|
|
$db->execute();
|
|
}
|
|
|
|
function getSseKey($user_id, $params)
|
|
{
|
|
$sse_key_id = $params->get("sse_key_id");
|
|
$sse_key = $params->get("sse_key");
|
|
|
|
$db = new DbOperations();
|
|
$db->select("sse_keys");
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
if ($sse_key_id != null) {
|
|
$db->where("sse_key_id", Comparison::EQUAL, $sse_key_id);
|
|
}
|
|
if ($sse_key != null) {
|
|
$db->where("sse_key", Comparison::EQUAL, $sse_key);
|
|
}
|
|
$result = $db->execute();
|
|
|
|
if (count($result) <= 0) {
|
|
return null;
|
|
}
|
|
$result = $result[0];
|
|
|
|
return $result;
|
|
}
|
|
|
|
function getAllSseKeys($user_id)
|
|
{
|
|
$db = new DbOperations();
|
|
$db->select("sse_keys");
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
return $db->execute();
|
|
}
|
|
|
|
function validatedSseKey($user_id, $params)
|
|
{
|
|
$key_entry = getSseKey($user_id, $params);
|
|
return $key_entry != null;
|
|
}
|
|
|
|
function removeRecordsOfProject($user_id, $project_id)
|
|
{
|
|
$db = new DbOperations();
|
|
$db->delete("time_records");
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
$db->where("project_id", Comparison::EQUAL, $project_id);
|
|
$db->execute();
|
|
}
|
|
|
|
function removeRecordTag($user_id, $params)
|
|
{
|
|
$record_tag_id = $params->get("record_tag_id");
|
|
|
|
$db = new DbOperations();
|
|
$db->delete("record_tags");
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
$db->where("record_tag_id", Comparison::EQUAL, $record_tag_id);
|
|
$db->execute();
|
|
|
|
removeRecordTagFromAssociations($record_tag_id);
|
|
}
|
|
|
|
function removeRecordTagFromAssociations($record_tag_id)
|
|
{
|
|
$db = new DbOperations();
|
|
$db->delete("tags_on_records");
|
|
$db->where("record_tag_id", Comparison::EQUAL, $record_tag_id);
|
|
$db->execute();
|
|
}
|
|
|
|
function updateTimeRecord($user_id, $params)
|
|
{
|
|
$data = [];
|
|
|
|
$anythingUpdated = false;
|
|
if ($params->exists(["start_time"])) {
|
|
$data["start_time"] = $params->get("start_time");
|
|
$anythingUpdated = true;
|
|
}
|
|
if ($params->exists(["end_time"])) {
|
|
$data["end_time"] = $params->get("end_time");
|
|
$anythingUpdated = true;
|
|
}
|
|
if ($params->exists(["duration"])) {
|
|
$data["duration"] = $params->get("duration");
|
|
$anythingUpdated = true;
|
|
}
|
|
if ($params->exists(["project_id"])) {
|
|
$data["project_id"] = $params->get("project_id");
|
|
$anythingUpdated = true;
|
|
}
|
|
if ($params->exists(["start_device_id"])) {
|
|
$data["start_device_id"] = $params->get("start_device_id");
|
|
$anythingUpdated = true;
|
|
}
|
|
|
|
if ($anythingUpdated == false) {
|
|
return;
|
|
}
|
|
|
|
$db = new DbOperations();
|
|
$db->update("time_records", $data);
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
$db->where("record_id", Comparison::EQUAL, $params->get("record_id"));
|
|
$db->execute();
|
|
}
|
|
|
|
function getRecordExternalData($record)
|
|
{
|
|
if ($record == null) {
|
|
return null;
|
|
}
|
|
|
|
// Duration and running
|
|
if ($record["end_time"] == NULL) {
|
|
$record["duration"] = calcDuration($record["start_time"]);
|
|
$record["running"] = true;
|
|
} else {
|
|
$record["running"] = false;
|
|
}
|
|
|
|
// 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();
|
|
$db->select("projects");
|
|
$db->where("project_id", Comparison::EQUAL, $project_id);
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
|
|
return count($db->execute()) == 1;
|
|
}
|
|
|
|
function isDeviceValid($start_device_id, $user_id)
|
|
{
|
|
$db = new DbOperations();
|
|
$db->select("devices");
|
|
$db->where("start_device_id", Comparison::EQUAL, $start_device_id);
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
|
|
return count($db->execute()) == 1;
|
|
}
|
|
|
|
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();
|
|
}
|