<commit_before><?php
namespace Vivo\Backend\UI\Explorer\Editor;

use Vivo\UI\AbstractForm;
use Vivo\UI\TabContainerItemInterface;
use Vivo\Backend\UI\Form\ContentEditor as ContentEditorForm;
use Vivo\CMS\Model;
use Vivo\CMS\ComponentResolver;
use Vivo\CMS\Exception\InvalidArgumentException;

class Content extends AbstractForm
{
    /**
     * @var \Zend\ServiceManager\ServiceManager
     */
    private $sm;
    /**
     * @var \Vivo\CMS\Api\Document
     */
    private $documentApi;
    /**
     * @var \Vivo\Metadata\MetadataManager
     */
    private $metadataManager;
    /**
     * @var \Vivo\CMS\Model\ContentContainer
     */
    private $contentContainer;
    /**
     * @var \Vivo\CMS\Model\Content
     */
    private $content;

    /**
     * @param \Zend\ServiceManager\ServiceManager $sm
     * @param \Vivo\CMS\Api\Document $documentApi
     * @param \Vivo\Metadata\MetadataManager $metadataManager
     */
    public function __construct(
        \Zend\ServiceManager\ServiceManager $sm,
        \Vivo\CMS\Api\Document $documentApi,
        \Vivo\Metadata\MetadataManager $metadataManager)
    {
        $this->sm = $sm;
        $this->documentApi = $documentApi;
        $this->metadataManager = $metadataManager;
        $this->autoAddCsrf = false;
    }

    /**
     * @param \Vivo\CMS\Model\ContentContainer $contentContainer
     */
    public function setContentContainer(Model\ContentContainer $contentContainer)
    {
        $this->contentContainer = $contentContainer;
    }

    /**
     * @param \Vivo\CMS\Model\Content $content
     */
    public function setContent(Model\Content $content = null)
    {
        $this->content = $content;
    }

    public function init()
    {
        parent::init();

        if($this->content) {
            $form = $this->getForm();
            $form->bind($this->content);

            if(!$this->content->getUuid()) {
                $form->get('content')->get('state')->setValue('NEW');
            }

            try {
                $resolver = new ComponentResolver($this->sm->get('cms_config'));
                $editorClass = $resolver->resolve(get_class($this->content), ComponentResolver::EDITOR_COMPONENT);

                /* @var $editor \Vivo\CMS\UI\Content\Editor\EditorInterface */
                $editor = $this->sm->create($editorClass);
                $editor->setContent($this->content);

                $this->addComponent($editor, 'editorComponent');

                $editor->init();
            }
            catch(InvalidArgumentException $e)
            {
                // Could not determine editor_component class for model
            }
        }
    }

    protected function doGetForm()
    {
        if($this->content) {
            $id = $this->content->getUuid();
            $metadata = $this->metadataManager->getMetadata(get_class($this->content));
        }
        else {
            $id = 0;
            $metadata = array();
        }

        $form = new ContentEditorForm('content-'.$id, $metadata);

        return $form;
    }

    /**
     * @return boolean
     */
    public function save()
    {
        if ($this->getForm()->isValid()) {
            if(!$this->contentContainer->getUuid()) {
                $this->contentContainer = $this->documentApi->createContentContainer(
                    $this->getParent('Vivo\Backend\UI\Explorer\Explorer')->getEntity()
                );
            }

            $this->editorComponent->save($this->contentContainer);

            return true;
        }

        return false;
    }

}
<commit_msg>Content editor facelift; explorer is set as property<commit_after><?php
namespace Vivo\Backend\UI\Explorer\Editor;

use Vivo\UI\AbstractForm;
use Vivo\UI\TabContainerItemInterface;
use Vivo\Backend\UI\Form\ContentEditor as ContentEditorForm;
use Vivo\CMS\Model;
use Vivo\CMS\ComponentResolver;
use Vivo\CMS\Exception\InvalidArgumentException;

class Content extends AbstractForm
{
    /**
     * @var \Vivo\Backend\UI\Explorer\ExplorerInterface
     */
    private $explorer;

    /**
     * @var \Zend\ServiceManager\ServiceManager
     */
    private $sm;

    /**
     * @var \Vivo\CMS\Api\Document
     */
    private $documentApi;

    /**
     * @var \Vivo\Metadata\MetadataManager
     */
    private $metadataManager;

    /**
     * @var \Vivo\CMS\Model\ContentContainer
     */
    private $contentContainer;

    /**
     * @var \Vivo\CMS\Model\Content
     */
    private $content;

    /**
     * @param \Zend\ServiceManager\ServiceManager $sm
     * @param \Vivo\CMS\Api\Document $documentApi
     * @param \Vivo\Metadata\MetadataManager $metadataManager
     */
    public function __construct(
        \Zend\ServiceManager\ServiceManager $sm,
        \Vivo\CMS\Api\Document $documentApi,
        \Vivo\Metadata\MetadataManager $metadataManager)
    {
        $this->sm = $sm;
        $this->documentApi = $documentApi;
        $this->metadataManager = $metadataManager;
        $this->autoAddCsrf = false;
    }

    /**
     * @param \Vivo\CMS\Model\ContentContainer $contentContainer
     */
    public function setContentContainer(Model\ContentContainer $contentContainer)
    {
        $this->contentContainer = $contentContainer;
    }

    /**
     * @param \Vivo\CMS\Model\Content $content
     */
    public function setContent(Model\Content $content = null)
    {
        $this->content = $content;
    }

    public function init()
    {
        parent::init();

        $this->explorer = $this->getParent('Vivo\Backend\UI\Explorer\ExplorerInterface');

        if($this->content) {
            $form = $this->getForm();
            $form->bind($this->content);

            if(!$this->content->getUuid()) {
                $form->get('content')->get('state')->setValue('NEW');
            }

            try {
                $resolver = new ComponentResolver($this->sm->get('cms_config'));
                $editorClass = $resolver->resolve(get_class($this->content), ComponentResolver::EDITOR_COMPONENT);

                /* @var $editor \Vivo\CMS\UI\Content\Editor\EditorInterface */
                $editor = $this->sm->create($editorClass);
                $editor->setContent($this->content);

                $this->addComponent($editor, 'editorComponent');

                $editor->init();
            }
            catch(InvalidArgumentException $e)
            {
                // Could not determine editor_component class for model
            }
        }
    }

    protected function doGetForm()
    {
        if($this->content) {
            $id = $this->content->getUuid();
            $metadata = $this->metadataManager->getMetadata(get_class($this->content));
        }
        else {
            $id = 0;
            $metadata = array();
        }

        $form = new ContentEditorForm('content-'.$id, $metadata);

        return $form;
    }

    /**
     * @return boolean
     */
    public function save()
    {
        if ($this->getForm()->isValid()) {
            if(!$this->contentContainer->getUuid()) {
                $this->contentContainer = $this->documentApi->createContentContainer($this->explorer->getEntity());
            }

            $this->editorComponent->save($this->contentContainer);

            return true;
        }

        return false;
    }

}

{'fraction_non_alphanumeric': 0.13986301369863013, 'fraction_numerical': 0.000273972602739726, 'mean_word_length': 2.025694156651471, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6538891', 'n_tokens_mistral': 2121, 'n_tokens_neox': 2032, 'n_words': 324}