Vimeo-omeka-module/src/Site/BlockLayout/Vimeo.php

107 lines
3.3 KiB
PHP
Raw Normal View History

2021-07-18 20:58:50 +02:00
<?php
namespace Vimeo\Site\BlockLayout;
use Omeka\Api\Representation\SiteRepresentation;
use Omeka\Api\Representation\SitePageRepresentation;
use Omeka\Api\Representation\SitePageBlockRepresentation;
use Omeka\Site\BlockLayout\AbstractBlockLayout;
use Laminas\View\Renderer\PhpRenderer;
use Laminas\Form\FormElementManager;
use Vimeo\Form\VimeoBlockForm;
use DOMDocument;
2021-07-18 20:58:50 +02:00
class Vimeo extends AbstractBlockLayout
{
/**
* @var FormElementManager
*/
protected $formElementManager;
/**
* @var array
*/
protected $defaultSettings = [];
/**
* @param FormElementManager $formElementManager
* @param array $defaultSettings
*/
public function __construct(FormElementManager $formElementManager, array $defaultSettings)
{
$this->formElementManager = $formElementManager;
$this->defaultSettings = $defaultSettings;
}
public function getLabel() {
2021-07-30 13:38:28 +02:00
return 'Vimeo video';
2021-07-18 20:58:50 +02:00
}
public function form(PhpRenderer $view,
SiteRepresentation $site,
SitePageRepresentation $page = null,
SitePageBlockRepresentation $block = null
) {
$form = $this->formElementManager->get(VimeoBlockForm::class);
$data = $block
? $block->data() + $this->defaultSettings
: $this->defaultSettings;
$form->setData([
'o:block[__blockIndex__][o:data][title]' => $data['title'],
'o:block[__blockIndex__][o:data][width]' => $data['width'],
'o:block[__blockIndex__][o:data][renderSourceLink]' => $data['renderSourceLink'],
]);
$form->prepare();
$html = '';
$html .= $view->blockAttachmentsForm($block);
$html .= '<a href="#" class="collapse" aria-label="collapse"><h4>';
$html .= $view->translate('Options'). '</h4></a>';
$html .= '<div class="collapsible" style="padding-top:6px;">';
$html .= $view->formCollection($form);
$html .= '</div>';
return $html;
}
public function render(PhpRenderer $view, SitePageBlockRepresentation $block)
{
$attachments = $block->attachments();
if (!$attachments) {
return '';
}
$media = $attachments[0]->item()->media()[0];
2021-08-30 18:14:25 +02:00
$ratio = $media->mediaData()['width'] / $media->mediaData()['height'];
2021-07-18 20:58:50 +02:00
2021-08-30 18:14:25 +02:00
// creates an iframe injecting our width and calculated ratio
$attribs= 'ratio="'.$ratio.'"'.PHP_EOL;
$doc = new DOMDocument();
$doc->loadHTML($media->mediaData()['html']);
$html_element= $doc->getElementsByTagName('iframe')->item(0);
if ($html_element->hasAttributes()) {
foreach ($html_element->attributes as $attr) {
$name = $attr->nodeName;
$value = $attr->nodeValue;
if ($name == 'height') {
2021-08-30 18:14:25 +02:00
// we will calculate height with jQuery
continue;
}
if ($name == 'width') {
2021-08-30 18:14:25 +02:00
$attrib = 'width="'.$block->dataValue('width').'%"';
$attribs .= $attrib.PHP_EOL;
continue;
}
$attrib = $name.'="'.$value.'"';
$attribs .= $attrib.PHP_EOL;
}
}
$iframe = "<iframe ".$attribs." >".PHP_EOL."</iframe>";
return $view->partial('common/block-layout/vimeo', [
'title' => $block->dataValue('title'),
'renderSourceLink' => $block->dataValue('renderSourceLink'),
'item_url' => $attachments[0]->item()->url(),
'vimeo_iframe' => $iframe
]);
}
}