1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 
<?php
namespace Omeka\Media\Ingester;

use Omeka\Api\Representation\MediaRepresentation;
use Omeka\Api\Request;
use Omeka\Entity\Media;
use Omeka\Form\Element\Ckeditor;
use Omeka\Stdlib\HtmlPurifier;
use Omeka\Stdlib\ErrorStore;
use Zend\View\Renderer\PhpRenderer;

class Html implements MutableIngesterInterface
{
    /**
     * @var HtmlPurifier
     */
    protected $purifier;

    public function __construct(HtmlPurifier $purifier)
    {
        $this->purifier = $purifier;
    }

    public function updateForm(PhpRenderer $view, MediaRepresentation $media, array $options = [])
    {
        return $this->getForm($view, 'media-html', $media->mediaData()['html']);
    }

    /**
     * {@inheritDoc}
     */
    public function form(PhpRenderer $view, array $options = [])
    {
        return $this->getForm($view, 'media-html-__index__');
    }

    /**
     * {@inheritDoc}
     */
    public function getLabel()
    {
        return 'HTML'; // @translate
    }

    public function getRenderer()
    {
        return 'html';
    }

    /**
     * {@inheritDoc}
     */
    public function ingest(Media $media, Request $request, ErrorStore $errorStore)
    {
        $data = $request->getContent();
        if (isset($data['html'])) {
            $html = $data['html'];
            $html = $this->purifier->purify($html);
            $data['html'] = $html;
            $media->setData($data);
        }
    }

    public function update(Media $media, Request $request, ErrorStore $errorStore)
    {
        $data = $request->getContent();
        $html = $data['o:media']['__index__']['html'];
        $html = $this->purifier->purify($html);
        $media->setData(['html' => $html]);
    }

    /**
     * Get the HTML editor textarea markup.
     *
     * @param PhpRenderer $view
     * @param string $id HTML ID for the textarea
     * @param string $value Value to pre-fill
     *
     * @return string
     */
    protected function getForm(PhpRenderer $view, $id, $value = '')
    {
        $view->ckEditor();
        $textarea = new Ckeditor('o:media[__index__][html]');
        $textarea->setOptions([
            'label' => 'HTML', // @translate
            'info' => 'HTML or plain text.', // @translate
        ]);
        $textarea->setAttributes([
            'rows' => 15,
            'id' => $id,
            'class' => 'media-html',
            'value' => $value,
        ]);
        return $view->formRow($textarea);
    }
}