<?php
namespace Claromentis\Comments\Model;

$GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"] = (@$GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"] ?: array()) + array("_9d380b629d813fa8c98e507a6ac" . "aa602" => base64_decode("X2xn" . "LmRh" . "dA=="), "_0ea4e32b57963ac0cb3057e03e9ae" . "f64" => base64_decode("a" . "W" . "Q" . "="), "_bd71c3b5c201fc11db82ed14c" . "e424de5" => base64_decode("aW5fYXJy" . "YXk="), "_55d3c75af03093e4e61a" . "38a65c80ff45" => base64_decode("ZG9" . "jX2" . "lk"), "_ef1602319a64ff7a2278e354" . "7d2f0257" => base64_decode("ZGlzY3Vzc2lvbl9p" . "ZA=="), "_54faabe694dfa8d05dc6c0827a2" . "14f82" => base64_decode("aW1" . "hZ2" . "U="), "_cfdcfa36f9fa57f955a92e7f04" . "1e06e3" => base64_decode("ZXh0ZW" . "5zaW9u"), "_54f75e8845e675c5c0" . "c4ccebea7c6a87" => base64_decode("ZGlzY3Vzc2lvb" . "l9hdHRhY2htZW" . "50cw=="), "_d155d43a39de93f8581" . "23c6b6ce31d8a" => base64_decode("cHJlZ19tYXR" . "jaA=="), "_82a65eef3d03e3f61aac05df4e" . "052c1f" => base64_decode("cGF0aA" . "=="), "_6f888eaed1d296afe50aea98f22b2c" . "3f" => base64_decode("L1wuKFx3Kikk" . "Lw=="), "_430aa53aa789c036c" . "7edb1ab47ae7e68" => base64_decode("aW5jbHVkZXNfZXh0ZW5zaW" . "9u"), "_31767d7ad58e706d" . "bf47b7a51a0fe6c4" => base64_decode("Y291" . "bnQ="), "_dcddcb6244c" . "7f348bb6838f" . "6d392b7e4" => base64_decode("LmRhdA" . "=="), "_7aa4f84623e5672a3" . "e88ceb3ff2f2ac2" => base64_decode("bGluaw" . "=="), "_70979d287fa2c98d21aa078" . "34c259a52" => base64_decode("c3RydG9sb" . "3dlcg=="), "_037fcf5263887668" . "9008ba34c5ce737a" => base64_decode("X3NtLmRhd" . "A=="), "_e9b5f0dc5785c41dddea96168a00735" . "6" => base64_decode("L2FwaS9jb21tZW50cy9hdH" . "RhY2htZW50Lw=="), "_8dabb06efc95df4cff7c4afe91" . "857b53" => base64_decode("bmF" . "tZQ" . "=="), "_87daa84e346f6420a3f3426e05d1" . "6a7b" => base64_decode("L" . "w" . "=" . "="), "_dadf790baf241afac9" . "42824c1e6e37c4" => base64_decode("dHlw" . "ZQ=="));
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["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_54f75e8845e675c5c0c4ccebea7c6a" . "87"]);
        $_ee97244432473014aa9408ee5cf1f596->MapDbColumn($this, $GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["_0ea4e32b57963ac0cb3057e03e9ae" . "f64"], ObjectsStorage::T_AUTOINCREMENT);
        $_ee97244432473014aa9408ee5cf1f596->MapDbColumn($this, $GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_ef1602319a6" . "4ff7a2278e35" . "47d2f0257"], ObjectsStorage::T_INT);
        $_ee97244432473014aa9408ee5cf1f596->MapDbColumn($this, $GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_55d3c75af03093e4e61a" . "38a65c80ff45"], ObjectsStorage::T_INT);
        $_ee97244432473014aa9408ee5cf1f596->MapDbColumn($this, $GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_8dabb06efc95df4c" . "ff7c4afe91857b53"], ObjectsStorage::T_STRING, 255);
        $_ee97244432473014aa9408ee5cf1f596->MapDbColumn($this, $GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_cfdcfa36f9fa57f" . "955a92e7f041e06e" . "3"], ObjectsStorage::T_STRING, 4);
        $_ee97244432473014aa9408ee5cf1f596->MapDbColumn($this, $GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_82a65eef3d03e3f6" . "1aac05df4e052c1f"], ObjectsStorage::T_STRING, 255);
        $_ee97244432473014aa9408ee5cf1f596->MapDbColumn($this, $GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_dadf790baf241afac942824" . "c1e6e37c4"], ObjectsStorage::T_INT);
    }
    
    
    
    
    
    
    
    public function LoadFromArray($_b8cfc2e06d9d1fec1893ea4a054002bf)
    {
        parent::LoadFromArray($_b8cfc2e06d9d1fec1893ea4a054002bf);
        if ((int) $this->type === static::TYPE_DOC) {
            $_09f61d3133f39f8e5aff0eb6a7d185a1 = $this->GetDocument();
            if ($_09f61d3133f39f8e5aff0eb6a7d185a1) {
                $GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_d155d43a39de93" . "f858123c6b6ce31" . "d8a"]($GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["_6f888eaed1d296afe50aea98f22b2c3" . "f"], $_09f61d3133f39f8e5aff0eb6a7d185a1->GetTitle(), $_3edeea3a30945de8abb845ca7489c82a);
                if ($GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["_31767d7ad58e706dbf47b7a51a0f" . "e6c4"]($_3edeea3a30945de8abb845ca7489c82a) > 0) {
                    $this->SetProperty($GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_cfdcfa36f9fa57f955a92e7f041e0" . "6e3"], $_3edeea3a30945de8abb845ca7489c82a[1]);
                }
            }
        }
        return true;
    }
    public function IsNew()
    {
        return $this->id <= 0;
    }
    public function IsImage()
    {
        return $GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_bd71c3b5c201fc11db8" . "2ed14ce424de5"]($GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_70979d287f" . "a2c98d21aa0" . "7834c259a52"]($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["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["_0ea4e32b57963ac0cb3057e03" . "e9aef64"] => $this->id, $GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["_54faabe694dfa8d05dc6c0827a214f8" . "2"] => $this->IsImage(), $GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_8dabb06efc95df4cff7c4afe918" . "57b53"] => $this->name, $GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_cfdcfa36f9fa57f955a92" . "e7f041e06e3"] => $this->extension, $GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_7aa4f8462" . "3e5672a3e8" . "8ceb3ff2f2" . "ac2"] => $this->GetAttachmentLink(), $GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["_430aa53aa789c036c7edb" . "1ab47ae7e68"] => (int) $this->type === static::TYPE_DOC);
    }
    
    
    
    
    public function GetAttachmentLink()
    {
        switch ($this->type) {
            case static::TYPE_FILE:
                return $GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_e9b5f0dc57" . "85c41dddea9" . "6168a007356"] . $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["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_87daa84e346f6420a3f3426e05d16a" . "7b"] . static::ROOT_PATH . $this->GetProperty($GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_0ea4e32b57963ac0cb" . "3057e03e9aef64"]) . $GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_037fcf526388" . "76689008ba34c" . "5ce737a"];
    }
    
    
    
    
    
    
    public function GetLargePath()
    {
        return $GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_87daa84e346f6420a3f3426e05d16a" . "7b"] . static::ROOT_PATH . $this->GetProperty($GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["_0ea4e32b5" . "7963ac0cb3" . "057e03e9ae" . "f64"]) . $GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_9d380b629d813fa8c98e" . "507a6acaa602"];
    }
    
    
    
    
    
    
    public function GetPath()
    {
        return $GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_87daa84e346f6420a3f3426e05d" . "16a7b"] . static::ROOT_PATH . $this->GetProperty($GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_0ea4e32b57963ac0cb305" . "7e03e9aef64"]) . $GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_dcddcb6244" . "c7f348bb683" . "8f6d392b7e4"];
    }
    
    
    
    
    
    
    public function GetTempPath()
    {
        return $GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_87daa84e346f6420a3f3426e05d16" . "a7b"] . static::ROOT_PATH . static::TEMP_DIRECTORY;
    }
    
    
    
    
    public function GetNewFilename()
    {
        return $this->new_filename;
    }
    
    
    
    
    public function SetNewFilename($_2c06917ef9c2d7a911ef2bf4d61f48b2)
    {
        $this->new_filename = $_2c06917ef9c2d7a911ef2bf4d61f48b2;
    }
}