<?php

namespace App\System\Controller;
use App\System\Basic\beContoller;
use App\System\Model\SettingModel;
use App\System\Vendor\beLanguage;
use App\System\Vendor\beUpdate;

class UpdateController extends beContoller {
    //@ioncube.dk beEncrypt("be_diyocp","check") -> "6bb3584839c10cfff5f626b2b9b715c8" RANDOM
    public function check(){
        beLanguage::beReadLang("update");
        $update = beUpdate::beCheckUpdate();
        if(!$update["status"]){
            beError(beGetLang("update"),beGetLang("update_connection_error"));
        }
        $mainfile = BE_APPPATH."resources/cache/version.json";
        if(!file_exists($mainfile)){
            beError(beGetLang("update"),beGetLang("update_file_not_found"));
        }
        $system = file_get_contents($mainfile);
        $system = json_decode($system,true);
        $build = $system["build"];
        $version = $system["version"];
        return beGetView("admin.update",["update" => $update,"build" => $build,"version" => $version]); 
    }
    //@ioncube.dk beEncrypt("be_diyocp","update_post") -> "1ba245651a3d9c4b7250002c58b9f0c2" RANDOM
    public function update_post(){
        global $be;
        $data = beUpdate::beDownloadUpdate();
        if(!$data["status"]){
            return beAjaxError(["message" => $data["message"]]);
        }
        $be->settings["system_data"]["update"] = [];
        SettingModel::beUpdate(["svalue" => json_encode($be->settings["system_data"],JSON_UNESCAPED_UNICODE)])->beWhere("scode","system_data")->beExecute();
        return beAjaxSuccess(["message" => $data["message"]]);
    }
}

?>