<commit_before><?php
namespace Vivo\CMS\UI\Content\Editor;

use Vivo\CMS\UI\Content\Editor\ResourceEditorInterface;
use Vivo\CMS\Api;
use Vivo\CMS\Model;
use Vivo\UI\AbstractForm;
use Vivo\Form\Form;
use Vivo\Repository\Exception\PathNotSetException;
use Vivo\CMS\RefInt\SymRefConvertorInterface;
use Vivo\CMS\Model\ContentContainer;

use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;

class File extends AbstractForm implements EditorInterface, AdapterAwareInterface
{
    const ADAPTER_COMPONENT_NAME    = 'resourceAdapter';

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

    /**
     * @var \Vivo\CMS\Api\Content\File
     */
    private $fileApi;

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

    /**
     * Symbolic reference convertor
     * @var SymRefConvertorInterface
     */
    protected $symRefConvertor;

    /**
     * Constructor
     * @param \Vivo\CMS\Api\Content\File $fileApi
     * @param Api\Document $documentApi
     * @param SymRefConvertorInterface $symRefConvertor
     */
    public function __construct(Api\Content\File $fileApi, Api\Document $documentApi, SymRefConvertorInterface $symRefConvertor)
    {
        $this->fileApi          = $fileApi;
        $this->documentApi      = $documentApi;
        $this->symRefConvertor  = $symRefConvertor;
        $this->autoAddCsrf      = false;
    }

    /**
     * (non-PHPdoc)
     * @see Vivo\CMS\UI\Content\Editor.EditorInterface::setContent()
     */
    public function setContent(Model\Content $content)
    {
        $this->content = $content;
    }

    /**
    * Sets the editor adapter
    * @param AdapterInterface $adapter
    * @return void
    */
    public function setAdapter(AdapterInterface $adapter = null)
    {
        if($adapter) {
            $this->addComponent($adapter, self::ADAPTER_COMPONENT_NAME);
        }
    }

    /**
     * Returns instance of resource adapter
     * @return child of AbstractAdapter
     */
    public function getAdapter()
    {
        return $this->getComponent(self::ADAPTER_COMPONENT_NAME);
    }

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

        $adapter = $this->getComponent(self::ADAPTER_COMPONENT_NAME);
        if ($adapter) {
            $adapter->init();
        }
    }
    /**
     * (non-PHPdoc)
     * @see Vivo\CMS\UI\Content\Editor.EditorInterface::save()
     */
    public function save(Model\ContentContainer $contentContainer)
    {
        // TODO insert checkbox to form (really replace content with file?)
        $replaceContent = true;
        $form = $this->getForm();

        if($form->isValid()) {
            $data = $form->get('upload-file')->getValue();

            if ($replaceContent && $data["tmp_name"] != "") {
                $this->fileApi->saveFileWithUploadedFile($this->content, $data, $contentContainer);
            }
            else {
                $mimeType = $this->content->getMimeType();
                $ext = $this->fileApi->getExt($mimeType);

                $this->content->setFilename(null);
                $this->content->setMimeType($mimeType);
                $this->content->setExt($ext);

                $adapter = $this->getAdapter();
                if ($adapter instanceof ResourceEditorInterface && $adapter->dataChanged()) {
                    $data = $adapter->getData();

                    $this->content->setSize(mb_strlen($data, 'UTF-8'));

                    $this->saveContent($contentContainer);
                    $this->saveResource($data);
                }
                else {
                    $this->saveContent($contentContainer);
                }
            }
        }
    }

    /**
     * Saves content
     * @param ContentContainer $contentContainer
     */
    private function saveContent(ContentContainer $contentContainer)
    {<commit_msg>Fix deleting filename in save action<commit_after><?php
namespace Vivo\CMS\UI\Content\Editor;

use Vivo\CMS\UI\Content\Editor\ResourceEditorInterface;
use Vivo\CMS\Api;
use Vivo\CMS\Model;
use Vivo\UI\AbstractForm;
use Vivo\Form\Form;
use Vivo\Repository\Exception\PathNotSetException;
use Vivo\CMS\RefInt\SymRefConvertorInterface;
use Vivo\CMS\Model\ContentContainer;

use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;

class File extends AbstractForm implements EditorInterface, AdapterAwareInterface
{
    const ADAPTER_COMPONENT_NAME    = 'resourceAdapter';

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

    /**
     * @var \Vivo\CMS\Api\Content\File
     */
    private $fileApi;

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

    /**
     * Symbolic reference convertor
     * @var SymRefConvertorInterface
     */
    protected $symRefConvertor;

    /**
     * Constructor
     * @param \Vivo\CMS\Api\Content\File $fileApi
     * @param Api\Document $documentApi
     * @param SymRefConvertorInterface $symRefConvertor
     */
    public function __construct(Api\Content\File $fileApi, Api\Document $documentApi, SymRefConvertorInterface $symRefConvertor)
    {
        $this->fileApi          = $fileApi;
        $this->documentApi      = $documentApi;
        $this->symRefConvertor  = $symRefConvertor;
        $this->autoAddCsrf      = false;
    }

    /**
     * (non-PHPdoc)
     * @see Vivo\CMS\UI\Content\Editor.EditorInterface::setContent()
     */
    public function setContent(Model\Content $content)
    {
        $this->content = $content;
    }

    /**
    * Sets the editor adapter
    * @param AdapterInterface $adapter
    * @return void
    */
    public function setAdapter(AdapterInterface $adapter = null)
    {
        if($adapter) {
            $this->addComponent($adapter, self::ADAPTER_COMPONENT_NAME);
        }
    }

    /**
     * Returns instance of resource adapter
     * @return child of AbstractAdapter
     */
    public function getAdapter()
    {
        return $this->getComponent(self::ADAPTER_COMPONENT_NAME);
    }

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

        $adapter = $this->getComponent(self::ADAPTER_COMPONENT_NAME);
        if ($adapter) {
            $adapter->init();
        }
    }
    /**
     * (non-PHPdoc)
     * @see Vivo\CMS\UI\Content\Editor.EditorInterface::save()
     */
    public function save(Model\ContentContainer $contentContainer)
    {
        $form = $this->getForm();

        if($form->isValid()) {
            $data = $form->get('upload-file')->getValue();

            if ($data["tmp_name"] != "") {
                $this->fileApi->saveFileWithUploadedFile($this->content, $data, $contentContainer);
            }
            else {
                $mimeType = $this->content->getMimeType();
                $ext = $this->fileApi->getExt($mimeType);

                $this->content->setMimeType($mimeType);
                $this->content->setExt($ext);

                $adapter = $this->getAdapter();
                if ($adapter instanceof ResourceEditorInterface && $adapter->dataChanged()) {
                    $data = $adapter->getData();

                    $this->content->setSize(mb_strlen($data, 'UTF-8'));

                    $this->saveContent($contentContainer);
                    $this->saveResource($data);
                }
                else {
                    $this->saveContent($contentContainer);
                }
            }
        }
    }

    /**
     * Saves content
     * @param ContentContainer $contentContainer
     */
    private function saveContent(ContentContainer $contentContainer)
    {
{'fraction_non_alphanumeric': 0.1280094724378371, 'fraction_numerical': 0.0002631232732535193, 'mean_word_length': 2.1635455680399502, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 69, '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': '6257339', 'n_tokens_mistral': 2239, 'n_tokens_neox': 2150, 'n_words': 369}