<?php
namespace Claromentis\Pages\Facade;

$GLOBALS["__65da1b1ec2d6653e8" . "a0ab7848e536215"] = (@$GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"] ?: array()) + array("_ade28a4deb083faaf8abef00c4" . "ad8f0d" => base64_decode("UHJvdmlkZWQgcGFnZSB0byBwdW" . "JsaXNoIGlzIG5vdCBhIGNoZWNr" . "ZWQgb3V0IGNvcHk="), "_e93333593279e8c7" . "b1ba473232e5d9fa" => base64_decode("UGFnZSBpcyBub3QgYSB3b3Jr" . "aW5nIGNvcHk="));
use Claromentis\Core\Security\SecurityContext;
use Claromentis\Pages\Exception\InvalidObjectStateException;
use Claromentis\Pages\Exception\PageNotFoundException;
use Claromentis\Pages\Exception\WorkingCopyExistsException;
use Claromentis\Pages\Model\Page;
use Claromentis\Pages\Model\PageUrl;
use Claromentis\Pages\PagesAcl;
use Claromentis\Pages\Repository\PagesRepository;
use Claromentis\Pages\Repository\TileProvider;
use Claromentis\Pages\Repository\UrlRepository;
use Claromentis\Pages\Repository\UserOptionRepository;
use Date;




class Drafts
{
    protected $url_repo;
    protected $pages_repo;
    protected $tile_provider;
    public function __construct(UrlRepository $_4fd544a0039d23957d8ced73ba74acb9, PagesRepository $pages_repo, TileProvider $tile_provider, UserOptionRepository $_5ae8237d36039808f402d90087da833f, PagesAcl $acl)
    {
        $this->url_repo = $_4fd544a0039d23957d8ced73ba74acb9;
        $this->pages_repo = $pages_repo;
        $this->tile_provider = $tile_provider;
        $this->user_options_repo = $_5ae8237d36039808f402d90087da833f;
        $this->acl = $acl;
    }
    
    
    
    
    
    
    
    public function GetWorkingCopy(PageUrl $page_url, $_3877cdf57a61e49b56aeab7af930ee82)
    {
        return $this->pages_repo->GetWorkingCopy($page_url->id, $_3877cdf57a61e49b56aeab7af930ee82);
    }
    
    
    
    
    
    
    
    
    
    public function CreateWorkingCopy(PageUrl $page_url, $_3877cdf57a61e49b56aeab7af930ee82)
    {
        $_8c02736ffb2efc1d69d2c9a2a34e7294 = $this->pages_repo->GetWorkingCopy($page_url->id, $_3877cdf57a61e49b56aeab7af930ee82);
        if ($_8c02736ffb2efc1d69d2c9a2a34e7294 !== null) {
            throw new WorkingCopyExistsException();
        }
        $_9c0ad0a93f6d3bc07a2251e182103ba5 = $this->pages_repo->GetById($page_url->latest_page_id);
        $this->tile_provider->LoadPageTiles($_9c0ad0a93f6d3bc07a2251e182103ba5);
        $_868a4603880e5dcf105b618a147debbd = $this->tile_provider->GetTilesIds($page_url->latest_page_id);
        $_8c02736ffb2efc1d69d2c9a2a34e7294 = $_9c0ad0a93f6d3bc07a2251e182103ba5->CreateCopy(-1, true, $_3877cdf57a61e49b56aeab7af930ee82);
        $this->pages_repo->Save($_8c02736ffb2efc1d69d2c9a2a34e7294);
        $this->tile_provider->SaveTiles($_8c02736ffb2efc1d69d2c9a2a34e7294->GetTiles(), $_8c02736ffb2efc1d69d2c9a2a34e7294->id);
        $_c45717d7591a44a29c1cb3d9b448e736 = $this->tile_provider->GetTilesIds($_8c02736ffb2efc1d69d2c9a2a34e7294->id);
        
        
        $this->user_options_repo->CopyUserOptions($_c45717d7591a44a29c1cb3d9b448e736, $_868a4603880e5dcf105b618a147debbd, $_3877cdf57a61e49b56aeab7af930ee82);
        return $_8c02736ffb2efc1d69d2c9a2a34e7294;
    }
    
    
    
    
    
    
    
    
    
    public function DeleteWorkingCopiesExcept(PageUrl $page_url, Page $_221da17e3055fae8d3ff516b832a5f3b)
    {
        $_3f05c7e033d5fa5883b27e9248e3ab53 = $this->pages_repo->GetAllWorkingCopies($page_url->id, [$_221da17e3055fae8d3ff516b832a5f3b->id]);
        foreach ($_3f05c7e033d5fa5883b27e9248e3ab53 as $_8b9b0f907ebaff8e8d57c82073116b57) {
            $this->DeleteWorkingCopy($page_url, $_8b9b0f907ebaff8e8d57c82073116b57);
        }
    }
    
    
    
    
    
    
    
    public function DeleteWorkingCopy(PageUrl $page_url, Page $_221da17e3055fae8d3ff516b832a5f3b)
    {
        if ($_221da17e3055fae8d3ff516b832a5f3b->version != -1) {
            throw new InvalidObjectStateException($GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_e933335" . "93279e8c" . "7b1ba473" . "232e5d9f" . "a"]);
        }
        $this->DeletePage($_221da17e3055fae8d3ff516b832a5f3b);
    }
    
    
    
    
    
    
    
    
    
    
    
    public function Publish(PageUrl $page_url, Page $_3f69fe7a8adad21a4f0989666cfdce5f, SecurityContext $security)
    {
        if (!$_3f69fe7a8adad21a4f0989666cfdce5f->is_draft) {
            throw new InvalidObjectStateException($GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_ade28a4deb083faaf8abef0" . "0c4ad8f0d"]);
        }
        $_b120cf24cff39f8829375179dca8b021 = $this->pages_repo->GetById($page_url->active_page_id);
        $_a98c96e86677550f6792bf7ad6c3569f = $this->acl->GetPageAcl($_b120cf24cff39f8829375179dca8b021->id);
        $_3f69fe7a8adad21a4f0989666cfdce5f->SetModifiedBy($security->GetUserId());
        $_d230962d9a57dc6ba2b774d9ba3bb81e = new Date();
        $_3f69fe7a8adad21a4f0989666cfdce5f->SetModifiedByDate($_d230962d9a57dc6ba2b774d9ba3bb81e->getDate());
        $_3f69fe7a8adad21a4f0989666cfdce5f->SetVersion($_b120cf24cff39f8829375179dca8b021->version + 1);
        $this->pages_repo->Save($_3f69fe7a8adad21a4f0989666cfdce5f);
        $_54d246948f01819daf7e1c1b070a2182 = clone $_a98c96e86677550f6792bf7ad6c3569f;
        $_54d246948f01819daf7e1c1b070a2182->SetObjectId($_3f69fe7a8adad21a4f0989666cfdce5f->id);
        $this->acl->SetPageAcl($_3f69fe7a8adad21a4f0989666cfdce5f->id, $_54d246948f01819daf7e1c1b070a2182);
        $page_url->SetActivePageId($_3f69fe7a8adad21a4f0989666cfdce5f->id);
        $page_url->SetLatestPageId($_3f69fe7a8adad21a4f0989666cfdce5f->id);
        $this->url_repo->Save($page_url);
        $this->tile_provider->PurgeDeleted($_3f69fe7a8adad21a4f0989666cfdce5f->id);
        $_a98c96e86677550f6792bf7ad6c3569f->Clear();
        $this->acl->SetPageAcl($_b120cf24cff39f8829375179dca8b021->id, $_a98c96e86677550f6792bf7ad6c3569f);
        $this->DeletePage($_b120cf24cff39f8829375179dca8b021);
    }
    
    
    
    
    protected function DeletePage(Page $_221da17e3055fae8d3ff516b832a5f3b)
    {
        $this->tile_provider->DeletePageTiles($_221da17e3055fae8d3ff516b832a5f3b->id, true);
        $this->pages_repo->Delete($_221da17e3055fae8d3ff516b832a5f3b);
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}