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'; } if ($db_version === '2') { $db->query("ALTER TABLE reports ADD COLUMN ordnum INTEGER DEFAULT 0"); option('version', '3'); $db_version = '3'; } } 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, $report_data) { global $db; $stm = $db->update('reports', $report_data, [ 'report_id' => $report_id ]); return ($stm->rowCount() > 0); } function reportUpdateStatus($report_id, $status) { global $db; $stm = $db->update('reports', [ 'report_status' => $status ], [ 'report_id' => $report_id ]); return ($stm->rowCount() > 0); } function reportUpdateOrdnum($ordnums) { global $db; $ordnums = json_decode($ordnums, true); $suc = true; foreach ($ordnums as $report_id => $ordnum) { $stm = $db->update('reports', [ 'ordnum' => $ordnum ], [ 'report_id' => $report_id ]); $suc &= ($stm->rowCount() > 0); } return $suc; } function reportDelete($report_id) { global $db; $stm = $db->delete('reports', [ 'report_id' => $report_id ]); return ($stm->rowCount() > 0); } function reportGet($report_id) { global $db; return $db->get('reports', '*', [ 'report_id' => $report_id ]); } function reportGetAll($status = null) { global $db; if ($status === null) $status = array(0, 1, 2, 3); return $db->select('reports', '*', [ 'ORDER' => ['report_priority' => 'DESC', 'ordnum' => 'ASC'], 'report_status' => $status ]); } function reportGetAllGrouped($status = null) { $all = reportGetAll($status); $groups = []; foreach ($all as $report) { $groups[$report['report_status']][] = $report; } return $groups; } /** * Attachments */ function attachmentAdd($report_id, $attachment_type, $attachment_content) { global $db; $stm = $db->insert('attachments', [ 'report_id' => $report_id, 'attachment_type' => $attachment_type, 'attachment_content' => $attachment_content, 'created_dt' => date('Y-m-d H:i:s') ]); return ($stm->rowCount() > 0); } function attachmentUpdate($attachment_id, $attachment_content) { global $db; if (strlen(trim($attachment_content)) <= 0) return attachmentDelete($attachment_id); $stm = $db->update('attachments', [ 'attachment_content' => $attachment_content ], [ 'attachment_id' => $attachment_id ]); return ($stm->rowCount() > 0); } function attachmentDelete($attachment_id) { global $db; $stm = $db->delete('attachments', [ 'attachment_id' => $attachment_id ]); return ($stm->rowCount() > 0); } function attachmentGetAll($report_id) { global $db; return $db->select('attachments', '*', [ 'ORDER' => ['created_dt' => 'ASC'], 'report_id' => $report_id ]); } ?>