<?php
//File Content

class DB_FILE_CONTENT
{

    public $DB = false;
    public $tablename = 'file_content';

    use BASICDB;
    public function __construct($DB = false)
    {
        if (!$DB) {
            $DB = DB();
        }
        if (!$DB) {
            return false;
        }
        if ($DB) {
            $this->DB = $DB;
        }
        if (!$this->tablename) {
            return false;
        }

    }
    function NewFileContent($file, $filename = '', $toDB = true)
    {
        if (!file_exists($file)) {
            return false;
        }
        if (!$filename) {
            $filename = basename($file);
        }
        $table = 'file_content';
        $filecontent = file_get_contents($file);
        $filehash = hash_file('sha256', $file);
        $size = filesize($file);
        $ifexists = DBFunctions($this->tablename, $this->DB)->CheckifUIDorHashKeyExist($filehash, $fieldstoselectarray = ['uid', 'hashkey'])['uid'] ?? false;
        if ($ifexists) {
            return $ifexists;
        }
        global $DB;
        if (!$DB) {
            $DB = DB();
        }

        $data = ['titlename' => $filename, 'hashkey' => $filehash, 'size_in_bytes' => $size];
        if ($toDB === true) {
            $data['content'] = $filecontent;
        } elseif (!$toDB) {
            $toDB = 'files/';

        } else {
            if (!is_dir($toDB)) {
                return false;
            }
        }
        if (!endsWithSlash($toDB)) {
            $toDB .= "/";
        }
        if ($toDB !== true) {
            file_put_contents($toDB . $filehash, $filecontent);
            if (!file_exists($toDB . $filehash) or filesize($toDB . $filehash) !== $size) {
                return false;
            }
            $data['filelocation'] = $toDB;
            $data['content'] = '';
        }
        $newuid = insertintodb($DB, $table, $data);

        return $newuid;
    }

    function DeleteFileContent($uidorhashkey)
    {
        $where = [];
        if (!$uidorhashkey) {
            return false;
        }
        if (is_numeric($uidorhashkey)) {
            $where['uid'] = $uidorhashkey;
        } else {
            $where['hashkey'] = $uidorhashkey;
        }
        return deletefromdb('file_content', $where);
    }

    function getFileContentDetails($uidorhashkey, $fieldstoselect = '')
    {
        return getDetailsbyUIDorHashkey('file_content', $uidorhashkey, $fieldstoselect);
    }

    function getFileContentUIDbyHashkey($hashkey)
    {
        if (!$hashkey or is_numeric($hashkey)) {
            return false;
        }
        return $this->getFileContentDetails($hashkey, ['uid', 'hashkey'])['uid'] ?? false;
    }
    function getFileContentHashkeybyUID($UID)
    {
        if (!$UID or !is_numeric($UID)) {
            return false;
        }
        return $this->getFileContentDetails($UID, ['uid', 'hashkey'])['hashkey'] ?? false;
    }

}

function DB_FILE_CONTENT($DB = false)
{
    return new DB_FILE_CONTENT($DB);
}


class FILE_CONTENT_QUICKMULTIPLESEARCH
{

    use DBClassSearch;
    public $data;

    public $tablename = 'file_content';

    private $parentidresults = [];
    public $DB;

    public function __construct($data = [], $likefields = [], $fieldstoselectarray = '', $orderby = '', $noindex = 0, $whereappend = ' and ', $dateonlyarray = '', $newdata = false, $DB = false)
    {
        return $this->initialize($data, $likefields, $fieldstoselectarray, $orderby, $noindex, $whereappend, $dateonlyarray, $newdata, $DB);
    }

    function FindName($uid)
    {
        return $this->Find($fieldname = 'uid', $contenttosearch = $uid, $exact = true)[0]['name'];
    }

}

function FILE_CONTENT_QUICKMULTIPLESEARCH($DB = false)
{
    return new FILE_CONTENT_QUICKMULTIPLESEARCH($DB);
}


class DB_FILE_LIST
{

    public $DB = false;
    public $tablename = 'file_list';

    use BASICDB;
    public function __construct($DB = false)
    {
        if (!$DB) {
            $DB = DB();
        }
        if (!$DB) {
            return false;
        }
        if ($DB) {
            $this->DB = $DB;
        }
        if (!$this->tablename) {
            return false;
        }

    }

    function insertFileListintoDB($contentuid, $filename, $description = '', $tags = '', $categories = '', $added = '', $addedby = '', $hidden = 0)
    {


        if (!$contentuid or !$filename) {
            return false;
        }


        if (!$addedby) {
            global $CurrentUserUID;
            $addedby = $CurrentUserUID;
            if (!$addedby) {
                $addedby = CurrentUserUID();
            }
        }
        if (!$added) {
            $added = serverdatetimesql();
        }
        $modified = serverdatetimesql();

        if (!$description) {
            $description = '';
        }
        if (!$tags) {
            $tags = '';
        }
        if (!$categories) {
            $categories = '';
        }

        $hash = generatenewhash($this->tablename);
        $categories = tryjsonencode($categories);

        $datenow = serverdatetimesql();
        $tags = tryjsonencode($tags);


        $data = [
            'hashkey' => $hash,
            'contentuid' => $contentuid,
            'useruid_access_list' => tryjsonencode(''),
            'filename' => $filename,
            'description' => $description,
            'tags' => $tags,
            'categories' => $categories,
            'created' => $added,
            'modified' => $modified,
            'hidden' => $hidden,
            'addedby' => $addedby
        ];

        global $DB;
        if (!$DB) {
            $DB = DB();
        }
        $key = insertintodb($DB, 'file_list', $data);

        return $key;
    }


    function getFileListDetails($uidorhashkey, $fieldstoselect = '')
    {
        return getDetailsbyUIDorHashkey($this->tablename, $uidorhashkey, $fieldstoselect);
    }


    function getFileListUIDbyHashkey($hashkey)
    {
        if (!$hashkey or is_numeric($hashkey)) {
            return false;
        }
        return $this->getFileListDetails($hashkey, ['uid', 'hashkey'])['uid'] ?? false;
    }

    function getFileListHashkeybyUID($UID)
    {
        if (!$UID or !is_numeric($UID)) {
            return false;
        }
        return $this->getFileListDetails($UID, ['uid', 'hashkey'])['hashkey'] ?? false;
    }


    function InsertFileListandFileContentFromFile($filelocation, $filename, $toDBtrueifFalseSateLocationtoSave = false, $description = '', $tags = '', $categories = '', $hidden = 0)
    {
        if (!$filelocation and !$filename) {
            return false;
        }
        $contentuid = DB_FILE_CONTENT()->NewFileContent($filelocation, $filename, $toDBtrueifFalseSateLocationtoSave);


        $filelistuid = $this->insertFileListintoDB($contentuid, $filename, $description, $tags, $categories, $added = '', $addedby = '', $hidden);

        return $filelistuid;

    }


}

function DB_FILE_LIST($DB = false)
{
    return new DB_FILE_LIST($DB);
}


class FILE_LIST_QUICKMULTIPLESEARCH
{

    use DBClassSearch;
    public $data;

    public $tablename = 'file_list';

    private $parentidresults = [];
    public $DB;

    public function __construct($data = [], $likefields = [], $fieldstoselectarray = '', $orderby = '', $noindex = 0, $whereappend = ' and ', $dateonlyarray = '', $newdata = false, $DB = false)
    {
        return $this->initialize($data, $likefields, $fieldstoselectarray, $orderby, $noindex, $whereappend, $dateonlyarray, $newdata, $DB);
    }

    function FindName($uid)
    {
        return $this->Find($fieldname = 'uid', $contenttosearch = $uid, $exact = true)[0]['name'];
    }

}

function FILE_LIST_QUICKMULTIPLESEARCH($DB = false)
{
    return new FILE_CONTENT_QUICKMULTIPLESEARCH($DB);
}