<?php
trait common_function2
{

    private $uploadFiles = array(
        /*Image Type*/
        "jpg",
        "jpeg",
        "bmp",
        "gif",
        "png",
        "img",
        /*Files Type*/
        "txt",
        "pdf",
        "psd",
        "docx",
        "doc",
        "pptx",
        "ppt",
        "xlsx",
        "xlr",
        "xls",
        "csv",
        "pps",
        "zip",
        "gzip",
        "rar",
        "gz",
        "tar",
        "tar.gz",
        "ios",
        "max",
        "dwg",
        "eps",
        "ai",
        "torrent",
        "html",
        "css",
        "js",
        "xml",
        "xhtml",
        "rss",
        /*Media Type*/
        "mp4",
        "m4a",
        "mp3",
        "mpg3",
        "3gp",
        "flv",
        "wmv",
        "wav",
        "mqv",
        "mpeg4",
        "swf",
        "mov",
        "mpg",
        "avi",
        "raw",
        "wmv",
        "rm",
        "obj"
    ); // only these type file allow to upload, write in lower case

    /**
     * @param $image
     */
    public function deleteOldSingleImage($image)
    {
        if ($image == '') {
            return false;
        }
        @unlink(__DIR__ . "/../../images/$image");
    }

    public function check_file_allow($filename)
    {
        //check file extension, is allow to upload or not,
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        if (!empty($ext) && in_array(strtolower($ext), $this->uploadFiles)) {
            return true;
        }
        return false;
    }

    public function check_document_file_ext($filename)
    {
        //check file extension, is allow to upload or not,
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        if (!empty($ext) && in_array(strtolower($ext), $this->document_type_extensions)) {
            return true;
        }
        return false;
    }

    /**
     * @param $data $_FILES['name']
     * @param string $folder
     * @param string $imgName
     * @return bool|string ImgName
     *
     * e.g:   $image = $this->functions->uploadSingleImage($_FILES['image'],'deal');
     */
    public function uploadSingleImage($data, $folder = 'mixed', $imgName = '', $fileType = '', $path = '')
    {

        if ($this->check_file_allow($data["name"]) == false)
            return false;

        # if path is empty string then use default path inside images folder
        if ($path == '') {
            $path = __DIR__ . "/../../images/";
        } else {
            $path = __DIR__ . $path;
        }

        if (substr($folder, -1, 1) == '/') {
        } else {
            $folder = $folder . '/';
        }

        $path = $path . $folder;

        $year = date('Y') . '/';
        $month = date('m') . '/';
        $rand = rand(101, 999);

        //create folder pages
        if (!file_exists($path)) {
            mkdir($path, 0777);
        }

        //create folder year
        if (!file_exists($path . $year)) {
            mkdir($path . $year, 0777);
        }

        //create folder month
        if (!file_exists($path . $year . $month)) {
            mkdir($path . $year . $month, 0777);
        }

        if (($data["size"]) > 0) {

            #### Allowing document file type checking
            $document_file = false;
            if ($fileType == 'document') {
                // $document_file = $this->check_document_file_ext($data["name"]);
                $document_file = true;
            }

            if (
                ($data["type"] == "image/png") || ($data["type"] == "image/jpeg") ||
                ($data["type"] == "image/gif") || ($data["type"] == "image/jpg") || ($data["type"] == $fileType) || ('all' == $fileType) || $document_file == TRUE
            ) {


                if ($data["error"] > 0) {
                    return false;
                } else {
                    $realName = $data["name"];
                    if ($imgName != '') {
                        $imgName = $rand . '-' . $imgName . '-' . $realName;
                    } else {
                        $imgName = $rand . '-' . $realName;
                    }
                    $imgName = sanitize_file_name($imgName);

                    $imgName = $year . $month . $imgName;
                    $return = $folder . $imgName;

                    if (move_uploaded_file($data["tmp_name"], $path . $imgName)) {
                        return $return;
                    }
                }
            } else {
                return false;
            }
        }
        return false;
    }

    public function uploadSingleFile($data, $folder = 'files', $fileName = '', $fileType = 'document', $path = '')
    {
        if ($this->check_file_allow($data["name"]) == false)
            return false;
        return $this->uploadSingleImage($data, $folder, $fileName, $fileType, $path);
    }




    /**
     * @param $fileName
     * @return mixed
     *
     * e.g:   $globalVar = $this->functions->require_once_custom('globalVar');
     */
    public function require_once_custom($fileName)
    {
        $adminFile = __DIR__ . '/../../' . ADMIN_FOLDER . '/';
        $webFile = __DIR__ . '/../../';
        $model = $webFile . "_models/";

        switch ($fileName) {
            case 'webProduct_functions':
                return require_once($webFile . '_models/functions/webProduct_functions.php');
                break;
            case 'twilio_whatsapp':
                return require_once($webFile . '_models/integrations/twilio_whatsapp.php');
                break;

            case 'webFooter':
                return file_get_contents($webFile . 'footer.php');
                break;
            default:
                return false;
                break;
        }
    }


    public function isImageExists($image, $HasWebUrl = false)
    {
        if ($HasWebUrl == true) {
            $image = str_replace(WEB_URL, "", $image);
        }
        if (file_exists(__DIR__ . "/../../images/" . $image) && $image != '' && $image !== false) {
            return true;
        } else {
            return false;
        }
    }
}
