<?php

# This script is to hide some clientarea access for VMware services. It will run only from your WHMCS root diretory where WHMCS is installed.

use WHMCS\Database\Capsule;

require __DIR__ . '/init.php';

if (!defined("WHMCS"))
    die("This file cannot be accessed directly");

$setting = '';

/* Remove comment like as given below "//" if you want to hide any option from clientarea. In given below code currently three tabs are hidden. Remove slashes with specific option to hide that. */

$tabaccess = [
    // "power",
    // "pause", 
    // "softreboot", 
    // "hardreboot", 
    // "reinstall",
    "mount",
    "upgradevmtool", 
    // "snapshot", 
    // "removesnapshot",
    // "revertsnapshot",
    "migrate"
];

if (!empty($tabaccess)) {

    
    $setting = implode(", ", $tabaccess);

    $type = "product"; /*Enter text either product or either service*/

    $productIdsArr = [1, 2, 3]; /*if $type is product then enter VMware products ID with comma seperated to update client area access for specific VMware products. It will globally auto hide options for all client product service for specific product.*/

    $serviceIdsArr = [123, 345, 678]; /* If $type is service then enter client product service id with comma seperated to update client area access for specific client product service*/

    if("product" == $type){
        $getServices = Capsule::table("tblhosting")->where("domainstatus", "Active")->whereIn("packageid", $productIdsArr)->get();
    }else{
        $getServices = Capsule::table("tblhosting")->where("domainstatus", "Active")->whereIn("id", $serviceIdsArr)->get();        
    }

    if(0 != count($getServices)){
        foreach($getServices as $service){
            $ifVMwareProduct = Capsule::table('tblproducts')->select("servertype")->where('id', $service->packageid)->where("servertype", "vmware")->count();
            if($ifVMwareProduct > 0){
                $serviceId = $service->id;
                $userId = $service->userid;
                vmwareUpdateAccessSetting($setting, $userId, $serviceId);
            }
        }
    }
}

function vmwareUpdateAccessSetting($setting = [], int $userId, int $serviceId){
    if("" != $setting){
        $values = [
            'setting' => $setting,
            'uid' => $userId,
            'sid' => $serviceId,
        ];
        $row = Capsule::table('mod_vmware_settings')->where('uid', $userId)->where('sid', $serviceId)->count();
        if ($row == 0) {
            try {        
                Capsule::table('mod_vmware_settings')->insert($values);
            } catch (Exception $ex) {        
                logActivity("could't insert into table mod_vmware_settings error: {$ex->getMessage()}");
            }
        } else {        
            try {        
                Capsule::table('mod_vmware_settings')->where('uid', $userId)->where('sid', $serviceId)->update($values);
            } catch (Exception $ex) {        
                logActivity("could't update into table mod_vmware_settings error: {$ex->getMessage()}");
            }
        }
    }
}

die("Done");
?>