<?php
namespace Claromentis\Infocapture\Repository;

$GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"] = (@$GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"] ?: array()) + array("projec" . "t_id" => base64_decode("cHJva" . "mVjdF" . "9pZA=" . "="), "_c2792bf964e36497f526072d3948" . "bfbb" => base64_decode("VVBEQVRFIGhkX2ZpZWxkc19ncm91cAoJCQkJCQlTRVQgcHJvamVjdF9pZD1pbnQ6cGlkLCBuYW1lPXN0cjpuYW1lCgkJCQkJCVdIRVJFIGl" . "kPWludDppZA=="), "_e4c8216" . "6b4feb2b" . "b948c018" . "fa5639bd" . "4" => base64_decode("REVMRVRFIEZST00gaGRfZmllbGRzX2dyb3V" . "wX2NvbnRlbnQKCQkJCQlXSEVSRSBncm91cF" . "9pZD1pbnQ6aWQ="), "_f0ce5625f7610eb80be1df49fb91bd" . "80" => base64_decode("SU5TRVJUIElOVE8gaGRfZmllbGRzX2dyb3VwX2Nvb" . "nRlbnQoZ3JvdXBfaWQsIGZpZWxkX3N5bV9uYW1lKQ" . "oJCQkJCQkJVkFMVUVTIChpbnQ6aWQsIHN0cjpuYW1" . "lKQ=="), "_bf685946305bf310f66" . "6b87a68c31f2d" => base64_decode("U0VMRUNUIGZpZWxkX3N5bV9uYW1lIEZST00gaGRfZmllbGRzX2dyb3VwX2NvbnRlbnQgV0hFUkUgZ" . "3JvdXBfaWQ9aW50OmdpZA=="), "_eb0addf69657" . "8634c74648c34" . "5dabaf0" => base64_decode("U0VMRUNUICogRlJPTSBoZF9maWVsZHNfZ3" . "JvdXAgV0hFUkUgaWQ9aW50Omlk"), "_6cd946afa118e34ca19fc01" . "caacdda2a" => base64_decode("REVMRVRFIEZST00gaGRfZm" . "llbGRzX2dyb3VwIFdIRVJF" . "IGlkPWludDppZA=="), "i" . "d" => base64_decode("a" . "W" . "Q" . "="), "_b130615fe91d" . "db1db1cdfcbca" . "f344796" => base64_decode("REVMRVRFIEZST00gaGRfZmllbGRzX2dyb3VwX2Nv" . "bnRlbnQKCQkJCQkJV0hFUkUgZ3JvdXBfaWQ9aW50" . "Omlk"), "_eac43f3" . "6ccaefab" . "544fb3a2" . "bbf02bf8" . "8" => base64_decode("SU5TRVJUIElOVE8gaGRfZmllbGRzX2dyb3VwKHBy" . "b2plY3RfaWQsIG5hbWUpCgkJCQkJCVZBTFVFUyAo" . "aW50OnBpZCwgc3RyOm5hbWUp"), "n" . "a" . "m" . "e" => base64_decode("bmFtZQ" . "=="));
use Claromentis\Core\DAL\Interfaces\DbInterface;
use Claromentis\Core\Event\EventDispatcher;
use Claromentis\Infocapture\Event\FieldGroupModifiedEvent;
use Claromentis\Infocapture\InfocaptureApplication;
use Claromentis\Infocapture\InfocaptureEvents;
use Claromentis\Infocapture\Model\ICFieldGroup;
class FieldGroupRepository
{
    
    
    protected $db;
    protected $dispatcher;
    
    
    protected $cache;
    public function __construct(DbInterface $db, EventDispatcher $_b497ce6f2af10f67516aed0ecf34d8cf)
    {
        $this->db = $db;
        $this->dispatcher = $_b497ce6f2af10f67516aed0ecf34d8cf;
    }
    
    
    
    
    public function Load(int $id) : ?ICFieldGroup
    {
        if ($id <= 0) {
            return null;
        }
        $_228a045965dce8051bc32fa56cedd325 = null;
        if (isset($this->cache[$id])) {
            $_228a045965dce8051bc32fa56cedd325 = clone $this->cache[$id];
            $_228a045965dce8051bc32fa56cedd325->fields = null;
        } else {
            $info = $this->db->query($GLOBALS["__65da1b1ec2d6653e8" . "a0ab7848e536215"]["_eb0addf696578634c74648c345dab" . "af0"], $id)->fetchArray();
            if (isset($info)) {
                $_228a045965dce8051bc32fa56cedd325 = new ICFieldGroup();
                $_228a045965dce8051bc32fa56cedd325->setInfo($info);
                $this->cache[$id] = clone $_228a045965dce8051bc32fa56cedd325;
            }
        }
        return $_228a045965dce8051bc32fa56cedd325;
    }
    
    
    
    
    
    public function LoadWithFieldNames(int $id) : ?ICFieldGroup
    {
        if (isset($this->cache[$id])) {
            $_228a045965dce8051bc32fa56cedd325 = clone $this->cache[$id];
        } else {
            $_228a045965dce8051bc32fa56cedd325 = $this->Load($id);
        }
        if (!isset($_228a045965dce8051bc32fa56cedd325)) {
            return null;
        }
        if (!isset($_228a045965dce8051bc32fa56cedd325->fields)) {
            $fields = $this->db->query($GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_bf68594630" . "5bf310f666b" . "87a68c31f2d"], $id)->fetchAllValues();
            $_228a045965dce8051bc32fa56cedd325->fields = $fields;
            $this->cache[$id]->fields = $fields;
        }
        return $_228a045965dce8051bc32fa56cedd325;
    }
    
    
    
    
    
    public function Save(?ICFieldGroup ...$_3326d8e3c0b65a90b24f968cc4225c7d)
    {
        $this->db->DoTransaction(function (DbInterface $db) use($_3326d8e3c0b65a90b24f968cc4225c7d) {
            foreach ($_3326d8e3c0b65a90b24f968cc4225c7d as $_228a045965dce8051bc32fa56cedd325) {
                $this->SaveOne($_228a045965dce8051bc32fa56cedd325);
            }
        });
    }
    
    
    
    
    
    public function SaveOne(?ICFieldGroup $_228a045965dce8051bc32fa56cedd325)
    {
        if (!isset($_228a045965dce8051bc32fa56cedd325)) {
            return;
        }
        $this->db->DoTransaction(function (DbInterface $db) use($_228a045965dce8051bc32fa56cedd325) {
            if ($_228a045965dce8051bc32fa56cedd325->id) {
                $db->query($GLOBALS["__65da1b1ec2d6653e8" . "a0ab7848e536215"]["_c2792bf964e36497f526072d3948" . "bfbb"], $_228a045965dce8051bc32fa56cedd325->project_id, $_228a045965dce8051bc32fa56cedd325->name, $_228a045965dce8051bc32fa56cedd325->id);
            } else {
                $db->query($GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_eac43f36ccaefab544" . "fb3a2bbf02bf88"], $_228a045965dce8051bc32fa56cedd325->project_id, $_228a045965dce8051bc32fa56cedd325->name);
                $_228a045965dce8051bc32fa56cedd325->setInfo([$GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["i" . "d"] => $db->insertId(), $GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["project_" . "id"] => $_228a045965dce8051bc32fa56cedd325->project_id, $GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["n" . "a" . "m" . "e"] => $_228a045965dce8051bc32fa56cedd325->name]);
            }
            if (isset($_228a045965dce8051bc32fa56cedd325->fields)) {
                $db->query($GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_b130615fe91ddb1" . "db1cdfcbcaf34479" . "6"], $_228a045965dce8051bc32fa56cedd325->id);
                foreach ($_228a045965dce8051bc32fa56cedd325->fields as $_66dc8b21e839a9a4739443a621ecc45a) {
                    $db->query($GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_f0ce5625f" . "7610eb80be" . "1df49fb91b" . "d80"], $_228a045965dce8051bc32fa56cedd325->id, $_66dc8b21e839a9a4739443a621ecc45a);
                }
            }
        });
        $event = new FieldGroupModifiedEvent($this->cache[$_228a045965dce8051bc32fa56cedd325->id] ?? null, $_228a045965dce8051bc32fa56cedd325);
        $this->dispatcher->dispatch(InfocaptureEvents::IC_FIELD_GROUP_MODIFIED, $event);
        $cache = clone $_228a045965dce8051bc32fa56cedd325;
        $_356c60c6f74ec31fe7a14d980e088ec1 = null;
        if (isset($this->cache[$cache->id])) {
            $_356c60c6f74ec31fe7a14d980e088ec1 = $this->cache[$cache->id]->fields;
        }
        $cache->fields = $cache->fields ?? $_356c60c6f74ec31fe7a14d980e088ec1;
        $this->cache[$cache->id] = $cache;
    }
    
    
    
    
    
    public function Delete(ICFieldGroup $_228a045965dce8051bc32fa56cedd325)
    {
        $_7e350312331e8d97526a308cebea7956 = $_228a045965dce8051bc32fa56cedd325->id;
        $this->db->DoTransaction(function (DbInterface $db) use($_228a045965dce8051bc32fa56cedd325) {
            $db->query($GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_6cd946afa118e" . "34ca19fc01caac" . "dda2a"], $_228a045965dce8051bc32fa56cedd325->id);
            $db->query($GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_e4c82166b4f" . "eb2bb948c018" . "fa5639bd4"], $_228a045965dce8051bc32fa56cedd325->id);
            $_228a045965dce8051bc32fa56cedd325->setInfo([$GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["i" . "d"] => 0, $GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["project" . "_id"] => $_228a045965dce8051bc32fa56cedd325->project_id, $GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["n" . "a" . "m" . "e"] => $_228a045965dce8051bc32fa56cedd325->name]);
        });
        $event = new FieldGroupModifiedEvent($this->cache[$_7e350312331e8d97526a308cebea7956] ?? null, null);
        $this->dispatcher->dispatch(InfocaptureEvents::IC_FIELD_GROUP_MODIFIED, $event);
        unset($this->cache[$_7e350312331e8d97526a308cebea7956]);
    }
}