pridana API metoda getallgrouped,
implementovane nacitanie dat do Dashboard z backend
This commit is contained in:
194
lib/functions.inc.php
Normal file
194
lib/functions.inc.php
Normal file
@ -0,0 +1,194 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Check database
|
||||
*/
|
||||
function dbCheck() {
|
||||
global $db;
|
||||
$db_version = option('version');
|
||||
if ($db_version === null) {
|
||||
$db->create('options', [
|
||||
'key' => [
|
||||
'VARCHAR(64)',
|
||||
'NOT NULL',
|
||||
'UNIQUE'
|
||||
],
|
||||
'value' => [
|
||||
'TEXT',
|
||||
'NOT NULL'
|
||||
],
|
||||
'created_at' => [
|
||||
'DATETIME',
|
||||
'DEFAULT CURRENT_TIMESTAMP'
|
||||
]
|
||||
]);
|
||||
option('version', '0');
|
||||
$db_version = '0';
|
||||
}
|
||||
if ($db_version === '0') {
|
||||
$db->create('reports', [
|
||||
'report_id' => [
|
||||
'INTEGER',
|
||||
'PRIMARY KEY',
|
||||
'AUTOINCREMENT'
|
||||
],
|
||||
'report_title' => [
|
||||
'VARCHAR(255)',
|
||||
'DEFAULT NULL'
|
||||
],
|
||||
'report_description' => [
|
||||
'TEXT',
|
||||
'DEFAULT NULL'
|
||||
],
|
||||
'report_status' => [
|
||||
'INTEGER',
|
||||
'DEFAULT 0'
|
||||
],
|
||||
'report_group' => [
|
||||
'VARCHAR(255)',
|
||||
'DEFAULT NULL'
|
||||
],
|
||||
'report_priority' => [
|
||||
'INTEGER',
|
||||
'DEFAULT 0'
|
||||
],
|
||||
'created_dt' => [
|
||||
'DATETIME',
|
||||
'DEFAULT NULL'
|
||||
],
|
||||
]);
|
||||
option('version', '1');
|
||||
$db_version = '1';
|
||||
}
|
||||
if ($db_version === '1') {
|
||||
$db->create('attachments', [
|
||||
'attachment_id' => [
|
||||
'INTEGER',
|
||||
'PRIMARY KEY',
|
||||
'AUTOINCREMENT'
|
||||
],
|
||||
'report_id' => [
|
||||
'INTEGER',
|
||||
'NOT NULL'
|
||||
],
|
||||
'attachment_type' => [
|
||||
'VARCHAR(255)',
|
||||
'DEFAULT NULL'
|
||||
],
|
||||
'attachment_content' => [
|
||||
'TEXT',
|
||||
'DEFAULT NULL'
|
||||
],
|
||||
'created_dt' => [
|
||||
'DATETIME',
|
||||
'DEFAULT NULL'
|
||||
],
|
||||
'updated_dt' => [
|
||||
'DATETIME',
|
||||
'DEFAULT NULL'
|
||||
],
|
||||
]);
|
||||
option('version', '2');
|
||||
$db_version = '2';
|
||||
}
|
||||
}
|
||||
|
||||
function option($key, $value = null)
|
||||
{
|
||||
global $db;
|
||||
if (tableExits('options') === null) {
|
||||
return null;
|
||||
}
|
||||
if ($value === null) {
|
||||
return $db->get('options', 'value', [
|
||||
'key' => $key
|
||||
]);
|
||||
}
|
||||
$exits = $db->get('options', 'value', [
|
||||
'key' => $key
|
||||
]);
|
||||
if ($exits !== null) {
|
||||
return $db->update('options', [
|
||||
'value' => $value
|
||||
], [
|
||||
'key' => $key
|
||||
]);
|
||||
}
|
||||
return $db->insert('options', [
|
||||
'key' => $key,
|
||||
'value' => $value
|
||||
]);
|
||||
}
|
||||
|
||||
function tableExits($table) {
|
||||
global $db;
|
||||
return $db->get('sqlite_master', 'name', [
|
||||
'type' => 'table',
|
||||
'name' => $table
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports
|
||||
*/
|
||||
function reportAdd($title, $description, $status = 0, $group = null, $priority = 0) {
|
||||
global $db;
|
||||
$status = intval($status);
|
||||
$priority = intval($priority);
|
||||
$db->insert('reports', [
|
||||
'report_title' => $title,
|
||||
'report_description' => $description,
|
||||
'report_status' => $status,
|
||||
'report_group' => $group,
|
||||
'report_priority' => $priority,
|
||||
'created_dt' => date('Y-m-d H:i:s')
|
||||
]);
|
||||
return $db->id();
|
||||
}
|
||||
|
||||
function reportUpdate($report_id, $title, $description, $status = 0, $group = null, $priority = 0) {
|
||||
global $db;
|
||||
return $db->update('reports', [
|
||||
'report_title' => $title,
|
||||
'report_description' => $description,
|
||||
'report_status' => $status,
|
||||
'report_group' => $group,
|
||||
'report_priority' => $priority,
|
||||
], [
|
||||
'report_id' => $report_id
|
||||
]);
|
||||
}
|
||||
|
||||
function reportDelete($report_id) {
|
||||
global $db;
|
||||
return $db->delete('reports', [
|
||||
'report_id' => $report_id
|
||||
]);
|
||||
}
|
||||
|
||||
function reportGet($report_id) {
|
||||
global $db;
|
||||
return $db->get('reports', '*', [
|
||||
'report_id' => $report_id
|
||||
]);
|
||||
}
|
||||
|
||||
function reportGetAll($status = null) {
|
||||
global $db;
|
||||
return $db->select('reports', '*', [
|
||||
'ORDER' => 'report_priority',
|
||||
'report_status' => $status
|
||||
]);
|
||||
}
|
||||
|
||||
function reportGetAllGrouped($status = null) {
|
||||
if ($status === null) $status = array(0, 1, 2, 3);
|
||||
$all = reportGetAll($status);
|
||||
$groups = [];
|
||||
foreach ($all as $report) {
|
||||
$groups[$report['report_status']][] = $report;
|
||||
}
|
||||
return $groups;
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user