<?php
namespace Claromentis\Comments\Model;

$GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"] = (@$GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"] ?: array()) + array("_70979d287fa2c98d21" . "aa07834c259a52" => base64_decode("c3RydG9sb3dl" . "cg=="), "_82a65eef3d03e3f61aac05df4e0" . "52c1f" => base64_decode("cGF0a" . "A=="), "_54f75e8845e675c5c0c4ccebea7c6a" . "87" => base64_decode("ZGlzY3Vzc2lvbl9hdHRhY2htZW50c" . "w=="), "_dadf790baf241afac942824c1e6e" . "37c4" => base64_decode("dHlw" . "ZQ=="), "_6f888eaed1d296afe50ae" . "a98f22b2c3f" => base64_decode("L1wuK" . "Fx3Ki" . "kkLw=" . "="), "_8dabb06efc95df4cff7c4" . "afe91857b53" => base64_decode("bm" . "Ft" . "ZQ" . "=="), "_dcddcb6244c7f3" . "48bb6838f6d392b" . "7e4" => base64_decode("LmRh" . "dA=="), "_87daa84e346" . "f6420a3f3426" . "e05d16a7b" => base64_decode("Lw=" . "="), "_037fcf52638876689008ba" . "34c5ce737a" => base64_decode("X3NtLm" . "RhdA=="), "_430aa53aa789c036c7edb1ab" . "47ae7e68" => base64_decode("aW5jbHVkZXNfZXh0ZW5z" . "aW9u"), "_31767d7ad58e706dbf" . "47b7a51a0fe6c4" => base64_decode("Y291" . "bnQ="), "_9d380b629d" . "813fa8c98e5" . "07a6acaa602" => base64_decode("X2xnLm" . "RhdA=="), "_bd71c3b5c201fc11db82ed1" . "4ce424de5" => base64_decode("aW5fY" . "XJyYX" . "k="), "_0ea4e32b57963ac0cb3057e03e9ae" . "f64" => base64_decode("aWQ" . "="), "_55d3c75af03093e4e61a3" . "8a65c80ff45" => base64_decode("ZG9" . "jX2" . "lk"), "_d155d43a39de93f858123c6b6ce31" . "d8a" => base64_decode("cHJlZ" . "19tYX" . "RjaA=" . "="), "_ef1602319a64ff7a227" . "8e3547d2f0257" => base64_decode("ZGlzY3Vzc2lvbl9pZA" . "=="), "_e9b5f0dc5785c4" . "1dddea96168a007" . "356" => base64_decode("L2FwaS9jb21t" . "ZW50cy9hdHRh" . "Y2htZW50Lw=="), "_cfdcfa36f9fa57f955a92e7f041e0" . "6e3" => base64_decode("ZXh0ZW" . "5zaW9u"), "_54faabe694dfa8d05dc6c0827a214f8" . "2" => base64_decode("aW1h" . "Z2U="), "_7aa4f84623e5672a3e88ceb3ff2f" . "2ac2" => base64_decode("bGluaw=" . "="));
use Claromentis\Core\Repository\RepositoryItemAbstract;
use ObjectsStorage;














class Attachment extends RepositoryItemAbstract
{
    
    
    
    
    public $repository;
    
    
    
    
    protected $db_values;
    public static $image_ext = array('jpeg', 'jpg', 'png', 'gif');
    const THUMB_SIZE_W = 370;
    const THUMB_SIZE_H = 370;
    const LARGE_SIZE_W = 800;
    const LARGE_SIZE_H = 800;
    const TEMP_DIRECTORY = 'tmp/';
    const ROOT_PATH = 'discussion/';
    
    
    const TYPE_LINK = 1;
    const TYPE_DOC = 2;
    const TYPE_FILE = 3;
    
    
    
    
    protected $new_filename = '';
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    public function InitDbMapping(ObjectsStorage $_ee97244432473014aa9408ee5cf1f596)
    {
        $_ee97244432473014aa9408ee5cf1f596->MapDbTable($this, $GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["_54f75e8845e675c5c0c4ccebea7" . "c6a87"]);
        $_ee97244432473014aa9408ee5cf1f596->MapDbColumn($this, $GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_0ea4e32b57963ac0cb" . "3057e03e9aef64"], ObjectsStorage::T_AUTOINCREMENT);
        $_ee97244432473014aa9408ee5cf1f596->MapDbColumn($this, $GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_ef1602319a64ff7a2278e3547d2f0" . "257"], ObjectsStorage::T_INT);
        $_ee97244432473014aa9408ee5cf1f596->MapDbColumn($this, $GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_55d3c75af03093e4e61a38a6" . "5c80ff45"], ObjectsStorage::T_INT);
        $_ee97244432473014aa9408ee5cf1f596->MapDbColumn($this, $GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_8dabb06efc95df4cff7c4" . "afe91857b53"], ObjectsStorage::T_STRING, 255);
        $_ee97244432473014aa9408ee5cf1f596->MapDbColumn($this, $GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_cfdcfa3" . "6f9fa57f" . "955a92e7" . "f041e06e" . "3"], ObjectsStorage::T_STRING, 4);
        $_ee97244432473014aa9408ee5cf1f596->MapDbColumn($this, $GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_82a65eef3d03e3f61aac05df4e0" . "52c1f"], ObjectsStorage::T_STRING, 255);
        $_ee97244432473014aa9408ee5cf1f596->MapDbColumn($this, $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["_dadf790baf241afac942824c1e" . "6e37c4"], ObjectsStorage::T_INT);
    }
    
    
    
    
    
    
    
    public function LoadFromArray($_b8cfc2e06d9d1fec1893ea4a054002bf)
    {
        parent::LoadFromArray($_b8cfc2e06d9d1fec1893ea4a054002bf);
        if ((int) $this->type === static::TYPE_DOC) {
            $_09f61d3133f39f8e5aff0eb6a7d185a1 = $this->GetDocument();
            if ($_09f61d3133f39f8e5aff0eb6a7d185a1) {
                $GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_d155d43a39de93f858123c6b6ce" . "31d8a"]($GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_6f888eaed1d296afe5" . "0aea98f22b2c3f"], $_09f61d3133f39f8e5aff0eb6a7d185a1->GetTitle(), $_3edeea3a30945de8abb845ca7489c82a);
                if ($GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_31767d7ad58e706dbf47b7a" . "51a0fe6c4"]($_3edeea3a30945de8abb845ca7489c82a) > 0) {
                    $this->SetProperty($GLOBALS["__65da1b1ec2d6653e8" . "a0ab7848e536215"]["_cfdcfa36f9fa5" . "7f955a92e7f041" . "e06e3"], $_3edeea3a30945de8abb845ca7489c82a[1]);
                }
            }
        }
        return true;
    }
    public function IsNew()
    {
        return $this->id <= 0;
    }
    public function IsImage()
    {
        return $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5" . "36215"]["_bd71c3b5c201fc11db82ed14ce424de" . "5"]($GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_70979d28" . "7fa2c98d2" . "1aa07834c" . "259a52"]($this->extension), static::$image_ext) && (int) $this->type !== static::TYPE_DOC;
    }
    
    
    
    
    
    
    
    
    public function ResizeToThumb($root)
    {
        if ($this->IsImage()) {
            return \ClaImage::Resize($root . $this->GetPath(), $root . $this->GetThumbPath(), static::THUMB_SIZE_W, null);
        }
        return false;
    }
    
    
    
    
    
    
    
    
    public function ResizeToLarge($root)
    {
        if ($this->IsImage()) {
            return \ClaImage::Resize($root . $this->GetPath(), $root . $this->GetLargePath(), static::LARGE_SIZE_W, static::LARGE_SIZE_H);
        }
        return false;
    }
    
    
    
    
    public function ToArray()
    {
        return array($GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_0ea4e32b57963ac0cb3057e03e9aef6" . "4"] => $this->id, $GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_54faabe694dfa8d05dc6c0827a214f" . "82"] => $this->IsImage(), $GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_8dabb06" . "efc95df4" . "cff7c4af" . "e91857b5" . "3"] => $this->name, $GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_cfdcfa36f9fa57f955a92" . "e7f041e06e3"] => $this->extension, $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5" . "36215"]["_7aa4f84623e5672a3e88ceb3f" . "f2f2ac2"] => $this->GetAttachmentLink(), $GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["_430aa53a" . "a789c036c" . "7edb1ab47" . "ae7e68"] => (int) $this->type === static::TYPE_DOC);
    }
    
    
    
    
    public function GetAttachmentLink()
    {
        switch ($this->type) {
            case static::TYPE_FILE:
                return $GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_e9b5f0dc5785c41dddea961" . "68a007356"] . $this->id;
                break;
            case static::TYPE_DOC:
                $_09f61d3133f39f8e5aff0eb6a7d185a1 = $this->GetDocument();
                if ($_09f61d3133f39f8e5aff0eb6a7d185a1) {
                    return $_09f61d3133f39f8e5aff0eb6a7d185a1->GetViewUrl();
                }
                break;
            case static::TYPE_LINK:
                return $this->path;
                break;
        }
        return null;
    }
    
    
    
    
    
    
    protected function GetDocument()
    {
        if ($this->doc_id) {
            return \ERMSBrowser::I()->GetDataByDocId($this->doc_id);
        }
        return false;
    }
    
    
    
    
    
    
    public function HasViewPermission()
    {
        
        
        if (!$this->doc_id) {
            return true;
        }
        
        
        $_09f61d3133f39f8e5aff0eb6a7d185a1 = \ERMSBrowser::I()->GetDataByDocId($this->doc_id);
        if (!$_09f61d3133f39f8e5aff0eb6a7d185a1) {
            return false;
        }
        if ($_09f61d3133f39f8e5aff0eb6a7d185a1->UserHasPermission(PERM_VIEW)) {
            return true;
        }
        return false;
    }
    
    
    
    
    
    
    public function GetThumbPath()
    {
        return $GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_87daa84e346f6420a3f3426e05d16a" . "7b"] . static::ROOT_PATH . $this->GetProperty($GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_0ea4e32b57963ac0cb3057e03e" . "9aef64"]) . $GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_037fcf5" . "26388766" . "89008ba3" . "4c5ce737" . "a"];
    }
    
    
    
    
    
    
    public function GetLargePath()
    {
        return $GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["_87daa84e346f6420a3" . "f3426e05d16a7b"] . static::ROOT_PATH . $this->GetProperty($GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_0ea4e32b57963ac0cb3057" . "e03e9aef64"]) . $GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["_9d380b629d81" . "3fa8c98e507a6" . "acaa602"];
    }
    
    
    
    
    
    
    public function GetPath()
    {
        return $GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_87daa84e346f6420a3f34" . "26e05d16a7b"] . static::ROOT_PATH . $this->GetProperty($GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_0ea4e32b57963ac0cb" . "3057e03e9aef64"]) . $GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_dcddcb6244c7f348bb6838f" . "6d392b7e4"];
    }
    
    
    
    
    
    
    public function GetTempPath()
    {
        return $GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["_87daa84e346f6420a" . "3f3426e05d16a7b"] . static::ROOT_PATH . static::TEMP_DIRECTORY;
    }
    
    
    
    
    public function GetNewFilename()
    {
        return $this->new_filename;
    }
    
    
    
    
    public function SetNewFilename($_2c06917ef9c2d7a911ef2bf4d61f48b2)
    {
        $this->new_filename = $_2c06917ef9c2d7a911ef2bf4d61f48b2;
    }
}