<?php
$GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"] = (@$GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"] ?: array()) + array("_0c5123aa744f69b" . "bc0e8b3ea2f6e847" . "d" => base64_decode("bG1zZw" . "=="), "_88e50416ecd778ec00b6e4c15157" . "3c4e" => base64_decode("dm" . "lh" . "PQ" . "=="), "_6de69ae3ed4208c2" . "671709b73f9d8327" => base64_decode("L2ludHJhbmV0L2RvY3VtZW50cy9pbWFnZXMvZ" . "G9jX2xpbmsuZ2lm"), "_921d43f5f3ff2a2107c1f" . "ab0b6455bca" => base64_decode("ZG9jdW1lbnRzLmVycm9ycy51bmFibGVfbG9hZF9saW5rZWRfZG9jdW1lbn" . "Q="), "_06f13ee44879d6eeef32" . "0f026d28cd26" => base64_decode("Z2x5cGhpY2" . "9ucyBnbHlw" . "aGljb25zLW" . "xpbms="), "_51df50d2d6185f82" . "f66804b5bb6793bf" => base64_decode("ZG9jdW1lbnRzLmVycm9ycy5" . "saW5rZWRfZG9jdW1lbnRfbm" . "90X2V4aXN0"), "_a591754a8ddcdbd" . "a9c80f1db4d911e1" . "4" => base64_decode("aXNfb" . "nVsbA" . "=="), "_8ab6ddf42088608c501fcd" . "ed8995e76c" => base64_decode("IA=" . "="));
use Claromentis\Search\Indexing\IndexingQueue;




class ERMSDataLink extends ERMSDataObj
{
    const LINK_MD_NAME = 'doc_link_target_doc_id';
    
    
    
    
    protected $target_doc;
    public function __construct()
    {
        parent::__construct(DOC_TYPE_LINK);
    }
    public function Save()
    {
        if (!parent::Save()) {
            return false;
        }
        IndexingQueue::Reindex($this->GetSearchType(), $this->doc_id);
        return true;
    }
    
    
    
    
    public function SetTargetDocId($_55d3c75af03093e4e61a38a65c80ff45)
    {
        $this->SetValue(self::LINK_MD_NAME, $_55d3c75af03093e4e61a38a65c80ff45);
    }
    
    
    
    
    public function GetTargetDocId()
    {
        return (int) $this->GetValue(self::LINK_MD_NAME);
    }
    
    
    
    
    public function GetTargetDocument()
    {
        $db = Claromentis\Core\Services::I()->GetDb();
        if ($this->IsBroken()) {
            return $this->SetErrMsg($GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_0c5123aa744f6" . "9bbc0e8b3ea2f6" . "e847d"]($GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_51df50d2d6185f82f6" . "6804b5bb6793bf"]));
        }
        if ($GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_a591754a8ddcdbda9c80f1db" . "4d911e14"]($this->target_doc)) {
            $this->target_doc = ERMSBrowser::I()->GetDataByDocId($this->GetTargetDocId());
            if ($this->target_doc === false) {
                $db->DisableTokenCheck();
                $this->SetBroken(true);
                $this->Save();
                $db->EnableTokenCheck();
                return $this->SetErrMsg($GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_0c5123aa744f69bbc0e8b3ea2f6e8" . "47d"]($GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["_921d43f" . "5f3ff2a2" . "107c1fab" . "0b6455bc" . "a"]) . $GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_8ab6ddf4" . "2088608c5" . "01fcded89" . "95e76c"] . ERMSBrowser::I()->GetErrMsg());
            }
        }
        if ($this->title !== $this->target_doc->title) {
            
            
            $db->DisableTokenCheck();
            $this->title = $this->target_doc->title;
            $this->Save();
            $db->EnableTokenCheck();
        }
        return $this->target_doc;
    }
    public function GetSmallIconURL()
    {
        return $GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_6de69ae3ed4" . "208c2671709b" . "73f9d8327"];
    }
    public function GetSmallIconCSSClass()
    {
        return $GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_06f13ee44879d" . "6eeef320f026d2" . "8cd26"];
    }
    public function GetViewUrl($_cc83add86be707c15c48a842fbdd2614 = false, $_6817ec6d42314534466dd8262d02d837 = null)
    {
        $_52e055147b02267214fa729a49cbb53a = $this->GetTargetDocument();
        if (!$_52e055147b02267214fa729a49cbb53a) {
            return false;
        }
        
        
        $url = $_52e055147b02267214fa729a49cbb53a->GetViewUrl($_cc83add86be707c15c48a842fbdd2614, $_6817ec6d42314534466dd8262d02d837);
        $url = \Claromentis\Core\Util\UrlUtil::AppendQueryString($url, $GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_88e50416ec" . "d778ec00b6e" . "4c151573c4e"] . $this->GetDocID());
        return $url;
    }
    
    
    
    
    protected function _NewObject()
    {
        return new ERMSDataLink(DOC_TYPE_LINK);
    }
    
    
    
    
    
    
    
    
    
    public function UserHasPermission($rights)
    {
        if (($rights & ~(PERM_VIEW | PERM_DELETE | PERM_EDIT_PERMS)) != 0) {
            return false;
        }
        return parent::UserHasPermission($rights);
    }
    public function SetBroken($_2505e722eb65192a6f49cea4565e5ac4 = true)
    {
        $_55d3c75af03093e4e61a38a65c80ff45 = $this->GetTargetDocId();
        if ($_55d3c75af03093e4e61a38a65c80ff45 > 0 && $_2505e722eb65192a6f49cea4565e5ac4) {
            $this->SetTargetDocId(-$_55d3c75af03093e4e61a38a65c80ff45);
        }
        if ($_55d3c75af03093e4e61a38a65c80ff45 < 0 && !$_2505e722eb65192a6f49cea4565e5ac4) {
            $this->SetTargetDocId(-$_55d3c75af03093e4e61a38a65c80ff45);
        }
    }
    public function IsBroken()
    {
        return $this->GetTargetDocId() < 0;
    }
}