<?php
namespace Claromentis\Design\UseCase;

$GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"] = (@$GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"] ?: array()) + array("_f73dab606630b388645c4f7d3fbb1" . "3bb" => base64_decode("YXJyYXlf" . "bWVyZ2U="), "_d98f77bc1" . "338b787366" . "4f68b11b20" . "a0b" => base64_decode("VVBEQVRFIHVzZXJzIFNFVCBza2luIGVxOnN0cjp0aGVtZV9uYW1lIFdIRVJFIGlkIElOIGlu" . "OmludDp1c2VyX2lkcw=="), "_6bc4e56" . "04f9cfb4" . "90b01cd9" . "4b12dd83" . "b" => base64_decode("YXJyYXlfY2h1" . "bms="), "_31767d7ad58e706dbf47b7a51" . "a0fe6c4" => base64_decode("Y291bnQ" . "="), "_49c62345a8f9" . "ce4222583cde4" . "2fb6525" => base64_decode("ZGVzaWduLnV0aWxpdHkudXNlcl90aGVtZXNfbm90X2V4a" . "XN0"), "_742c76e4ce" . "c99feecec42" . "23ee30c1cff" => base64_decode("YXJyYXlfaW" . "50ZXJzZWN0"), "_b66252d" . "7bbf3610" . "12a6531a" . "8e3a7323" . "0" => base64_decode("YXJyYXlfZGlm" . "Zg=="), "_0c5123aa744f69bbc0e8" . "b3ea2f6e847d" => base64_decode("bG1z" . "Zw=="));
use Claromentis\Core\DAL;
use Claromentis\Core\Services;
use Claromentis\Design\Repository\ThemeRepository;
use Claromentis\Design\ThemeAcl;
class ApplyTheme
{
    protected ThemeRepository $theme_repo;
    protected ThemeAcl $theme_acl;
    protected int $usersChanged = 0;
    protected array $errors = [];
    public function __construct(ThemeRepository $theme_repo, ThemeAcl $_11fd0c43f9c6d4cceb91b12c0a8bb755)
    {
        $this->theme_repo = $theme_repo;
        $this->theme_acl = $_11fd0c43f9c6d4cceb91b12c0a8bb755;
    }
    
    
    
    
    
    
    
    public function Apply(int $theme_id, array $_17247f55ce15f647f57c3ef8bb3a474a)
    {
        
        
        $this->usersChanged = 0;
        $this->errors = [];
        $theme = $this->theme_repo->GetThemeById($theme_id);
        if ($theme === null) {
            $this->errors[] = $GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_0c5123aa744f69b" . "bc0e8b3ea2f6e847" . "d"]($GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_49c6234" . "5a8f9ce4" . "222583cd" . "e42fb652" . "5"]);
            return;
        }
        
        
        $_e15d37184b051efbc0299c4bd871b26f = $this->theme_acl->GetUserIdsForTheme($theme_id);
        $_57a4b19ec23ef68cf6ac2195373f0b3c = $this->GetUserIdsWithForcedTheme($theme_id);
        $_f64e8034837ec2d36f8b446817679818 = $GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_742c76e4cec99f" . "eecec4223ee30c1" . "cff"]($GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_b66252d7bbf361012" . "a6531a8e3a73230"]($_17247f55ce15f647f57c3ef8bb3a474a, $_57a4b19ec23ef68cf6ac2195373f0b3c), $_e15d37184b051efbc0299c4bd871b26f);
        $db = Services::I()->GetDb();
        $db->DoTransaction(function (DAL\Db $db) use($_f64e8034837ec2d36f8b446817679818, $theme) {
            
            
            $db->DisableTokenCheck();
            
            
            $_feef993bf879f57b55aea4c1bec43e71 = $GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["_6bc4e56" . "04f9cfb4" . "90b01cd9" . "4b12dd83" . "b"]($_f64e8034837ec2d36f8b446817679818, 500);
            foreach ($_feef993bf879f57b55aea4c1bec43e71 as $_4ee71f0208bf9c6391c62bb8b31d315a) {
                $db->query($GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_d98f77bc1338b7873664f68b11" . "b20a0b"], $theme->GetCode(), $_4ee71f0208bf9c6391c62bb8b31d315a);
            }
            $db->EnableTokenCheck();
            $this->usersChanged = $GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["_31767d7ad58e706dbf47b7a51a" . "0fe6c4"]($_f64e8034837ec2d36f8b446817679818);
        });
    }
    
    
    
    
    
    
    public function GetSuccessful()
    {
        return $this->usersChanged;
    }
    
    
    
    
    
    
    public function GetErrors()
    {
        return $this->errors;
    }
    
    
    
    
    
    
    
    protected function GetUserIdsWithForcedTheme(int $theme_id) : array
    {
        $_3f2f5910ba908caa37b35da4ea6e7c44 = [];
        $themes = $this->theme_repo->GetThemes();
        foreach ($themes as $theme) {
            if ($theme->id === $theme_id) {
                continue;
            }
            $_3f2f5910ba908caa37b35da4ea6e7c44 = $GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_f73dab606" . "630b388645" . "c4f7d3fbb1" . "3bb"]($_3f2f5910ba908caa37b35da4ea6e7c44, $this->theme_acl->GetUserIdsWithForcedTheme($theme->id));
        }
        return $_3f2f5910ba908caa37b35da4ea6e7c44;
    }
}