]> _ Git - fluidbook_tools.git/commitdiff
wip #5929 @0.75
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 29 Aug 2023 08:59:09 +0000 (10:59 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 29 Aug 2023 08:59:09 +0000 (10:59 +0200)
src/Links/Link.php
src/Links/LottieLink.php [new file with mode: 0644]

index 007f86e68222ece2d12a7f9889d301f56941d089..ef9b44e04ef8230d26e015c165a94bb2d631c17d 100644 (file)
@@ -16,7 +16,8 @@ use Fluidbook\Tools\Compiler\CompilerInterface;
 use SodiumException;
 use stdClass;
 
-class Link {
+class Link
+{
 
     const WEB_INFOS = 1;
     const PRODUCT = 1;
@@ -65,6 +66,7 @@ class Link {
     const FLIPCARD = 41;
     const PDF = 42;
     const TEXT_POPUP = 43;
+    const LOTTIE = 44;
 
 
     protected static string|false|null $_linksKey = null;
@@ -144,7 +146,8 @@ class Link {
      * @param CompilerInterface $compiler
      * @return Link
      */
-    public static function getInstance($id, $init, &$compiler) {
+    public static function getInstance($id, $init, &$compiler)
+    {
         $init = ArrayUtil::asArray($init);
 
         static::normalizeAttributes($init);
@@ -211,6 +214,8 @@ class Link {
             case static::HTML5MULTIMEDIA:
             case static::MULTIMEDIA:
                 return self::getMultimediaInstance($id, $init, $compiler);
+            case static::LOTTIE:
+                return new LottieLink($id, $init, $compiler);
             case static::STATSTAG:
                 return new StatsTagLink($id, $init, $compiler);
             case static::PHONE:
@@ -288,7 +293,8 @@ class Link {
 
     }
 
-    public static function normalizeAttributes(&$a) {
+    public static function normalizeAttributes(&$a)
+    {
         $numbers = ['left', 'top', 'rot', 'width', 'height'];
         foreach ($a as $k => $v) {
             if (in_array($k, $numbers)) {
@@ -305,11 +311,13 @@ class Link {
         }
     }
 
-    public function getSurface() {
+    public function getSurface()
+    {
         return $this->width * $this->height;
     }
 
-    public static function normalizeInlineIntegration($inline) {
+    public static function normalizeInlineIntegration($inline)
+    {
         if ($inline == '1' || $inline === 'true' || $inline === true) {
             return 'inline';
         } else if (!$inline || $inline === 'false') {
@@ -318,7 +326,8 @@ class Link {
         return $inline;
     }
 
-    public static function parseExtras($extras, $normalizeKey = false) {
+    public static function parseExtras($extras, $normalizeKey = false)
+    {
         $extras = trim($extras);
         if ($extras === '') {
             return [];
@@ -345,7 +354,8 @@ class Link {
         return $res;
     }
 
-    public static function parseAnimations($animations) {
+    public static function parseAnimations($animations)
+    {
         $anims = explode('---', $animations);
         $res = [];
 
@@ -371,7 +381,8 @@ class Link {
         return $res;
     }
 
-    public static function replaceCustomURL($url) {
+    public static function replaceCustomURL($url)
+    {
         $url = trim($url);
         if (strpos($url, 'custom:') === 0) {
             $e = explode(':', $url, 2);
@@ -381,7 +392,8 @@ class Link {
         return $url;
     }
 
-    public static function getMultimediaInstance($id, $init, &$compiler) {
+    public static function getMultimediaInstance($id, $init, &$compiler)
+    {
         if ($init['to'] == '') {
             return null;
         }
@@ -405,11 +417,13 @@ class Link {
         return null;
     }
 
-    public static function isScorm($linkData) {
+    public static function isScorm($linkData)
+    {
         return (isset($linkData['scorm']) && $linkData['scorm']) || (self::_isScormLink($linkData['to']));
     }
 
-    protected static function _isScormLink($url) {
+    protected static function _isScormLink($url)
+    {
         if (strpos($url, 'http://') >= 0 || strpos($url, 'https://') > 0) {
             return false;
         }
@@ -421,7 +435,8 @@ class Link {
      * @param $init array
      * @param $compiler CompilerInterface
      */
-    public function __construct($id, $init, &$compiler) {
+    public function __construct($id, $init, &$compiler)
+    {
         $this->_init = $init;
 
         foreach ($init as $k => $v) {
@@ -456,18 +471,21 @@ class Link {
     /**
      * @param int $initialOrder
      */
-    public function setInitialOrder(int $initialOrder): void {
+    public function setInitialOrder(int $initialOrder): void
+    {
         $this->initialOrder = $initialOrder;
     }
 
     /**
      * @return int
      */
-    public function getInitialOrder(): int {
+    public function getInitialOrder(): int
+    {
         return $this->initialOrder;
     }
 
-    public function getDepth() {
+    public function getDepth()
+    {
         if ($this->zindex == -1 || null === $this->zindex || !$this->zindex) {
             return $this->defaultZIndex;
         }
@@ -477,7 +495,8 @@ class Link {
         return $this->zindex;
     }
 
-    public function getTooltipAttribute($t = null) {
+    public function getTooltipAttribute($t = null)
+    {
         if (null === $t) {
             $t = $this->getTooltip();
         }
@@ -491,19 +510,22 @@ class Link {
         }
     }
 
-    public function isOnNormalPage() {
+    public function isOnNormalPage()
+    {
         // $this->page is normally an integer but it can also be a string (eg. background / aftersearch)
         return is_int($this->page) || preg_match('/^\d+$/', $this->page, $matches);
     }
 
-    public function overlapDoublePage() {
+    public function overlapDoublePage()
+    {
         if (!$this->isOnNormalPage()) {
             return false;
         }
         return ($this->page % 2 == 0 && ($this->left + $this->width) > $this->compiler->getWidthForLinks());
     }
 
-    public function getRightClone() {
+    public function getRightClone()
+    {
         $res = clone $this;
         $res->page++;
         $res->left -= $this->compiler->getWidthForLinks();
@@ -513,15 +535,18 @@ class Link {
         return $res;
     }
 
-    public function init() {
+    public function init()
+    {
 
     }
 
-    public function getDefaultTooltip() {
+    public function getDefaultTooltip()
+    {
         return false;
     }
 
-    public function getTooltip() {
+    public function getTooltip()
+    {
         if (!$this->tooltip) {
             if (!$this->getDefaultTooltip()) {
                 return '';
@@ -532,21 +557,24 @@ class Link {
     }
 
 
-    public function getHTMLContainer() {
+    public function getHTMLContainer()
+    {
         if (null === $this->_htmlContainer) {
             $this->_htmlContainer = $this->_getHTMLContainer();
         }
         return $this->_htmlContainer;
     }
 
-    protected function _getHTMLContainer() {
+    protected function _getHTMLContainer()
+    {
         $addContent = $this->getAdditionnalContent();
 
         $attached = $this->attached ? ' data-attached="' . $this->attached . '" ' : '';
         return '<div class="' . $this->getHTMLContainerClass() . '" style="mix-blend-mode:' . $this->blendmode . ';" data-blendmode="' . $this->blendmode . '" ' . $attached . ' data-hidden="' . $this->hidden . '" data-scorm="' . $this->scorm . '" data-id="' . $this->uid . '" id="l_' . $this->id . '"' . $addContent . '>' . $this->getHTMLContent() . '</div>';
     }
 
-    public function getHTMLContainerClass() {
+    public function getHTMLContainerClass()
+    {
         $res = trim('link ' . $this->class);
         if ((int)$this->page % 2 == 1) {
             $res .= ' odd';
@@ -558,11 +586,13 @@ class Link {
         return $res;
     }
 
-    public function getHTMLContent() {
+    public function getHTMLContent()
+    {
         return '';
     }
 
-    public function getAdditionnalContent() {
+    public function getAdditionnalContent()
+    {
         $res = '';
         if ($this->role !== '') {
             $res .= ' role="' . $this->role . '"';
@@ -585,7 +615,8 @@ class Link {
 
     }
 
-    public function getClasses() {
+    public function getClasses()
+    {
 
         $res = array();
         if (isset($this->image_rollover) && $this->image_rollover != 'none') {
@@ -594,19 +625,23 @@ class Link {
         return $res;
     }
 
-    public function copyExternalFile($file, $video = false) {
+    public function copyExternalFile($file, $video = false)
+    {
         $this->compiler->copyLinkFile($file, 'data/links/', $video);
     }
 
-    public function copyExternalDir($dir, $dest = 'data/links') {
+    public function copyExternalDir($dir, $dest = 'data/links')
+    {
         $this->compiler->copyLinkDir($dir, $dest);
     }
 
-    public function unzipFile($file, $junkPaths = false) {
+    public function unzipFile($file, $junkPaths = false)
+    {
         return $this->compiler->unzipFile($file, false, null, $junkPaths);
     }
 
-    public function getCssScale() {
+    public function getCssScale()
+    {
         if (is_numeric($this->page) || is_int($this->page)) {
             return $this->compiler->getLinkScale();
         } else {
@@ -614,27 +649,32 @@ class Link {
         }
     }
 
-    public function getCSSZIndex() {
+    public function getCSSZIndex()
+    {
         $zindex = $this->getAddZIndex() + (($this->getDepth() + 1) * 10000) - min(9999, max(1, round(9999 * (($this->width * $this->height) / $this->compiler->getBookSurface()))));
         return 'z-index:' . $zindex . ';';
     }
 
-    public function getAddZIndex() {
+    public function getAddZIndex()
+    {
         return $this->addzindex;
     }
 
-    public function moveOnEvenPage() {
+    public function moveOnEvenPage()
+    {
         return false;
     }
 
-    public function getCSSContainer() {
+    public function getCSSContainer()
+    {
         if (null === $this->_cssContainer) {
             $this->_cssContainer = $this->_getCSSContainer();
         }
         return $this->_cssContainer;
     }
 
-    protected function _getCSSContainer() {
+    protected function _getCSSContainer()
+    {
         if ($this->moveOnEvenPage()) {
             $this->page--;
             $this->left += $this->compiler->getWidth();
@@ -679,15 +719,18 @@ class Link {
         return $css;
     }
 
-    public function getCSS() {
+    public function getCSS()
+    {
         return '';
     }
 
-    public function keep() {
+    public function keep()
+    {
         return false;
     }
 
-    public static function getUniversalLocation($loc, $css = false) {
+    public static function getUniversalLocation($loc, $css = false)
+    {
         $datas = parse_url($loc);
 
         if ((isset($datas['scheme']) && !is_null($datas['scheme'])) || strpos($loc, '#') === 0) {
@@ -701,19 +744,23 @@ class Link {
         }
     }
 
-    public function isOutsidePage() {
+    public function isOutsidePage()
+    {
         return $this->top > $this->compiler->getHeightForLinks($this->page) || $this->left > $this->compiler->getWidthForLinks($this->page);
     }
 
-    public function ignore() {
+    public function ignore()
+    {
         return false;
     }
 
-    public function getConfigZIP($d) {
+    public function getConfigZIP($d)
+    {
         return $this->compiler->getConfigZIP($d);
     }
 
-    public function getConfigHTML($d, $html) {
+    public function getConfigHTML($d, $html)
+    {
         $c = file_get_contents($this->compiler->working_path($html));
         $res = array('width' => $this->video_width, 'height' => $this->video_height);
         if (stristr($c, '<div id="lottie"></div>')) {
@@ -728,7 +775,8 @@ class Link {
         return array_merge($res, $r);
     }
 
-    public function getConfigOAM($d) {
+    public function getConfigOAM($d)
+    {
         $x = simplexml_load_string(file_get_contents($d . '/config.xml'));
         $config = (string)$x->oamfile['src'];
         $config = str_replace('/Assets', '', $d . '/' . $config);
@@ -753,7 +801,8 @@ class Link {
         return $res;
     }
 
-    public static function _getLinkKey() {
+    public static function _getLinkKey()
+    {
         if (self::$_linksKey === null) {
             self::$_linksKey = base64_decode('o2p2yYGI8yMEHf+Y5/e6NdTINbbXg3NIo8BODgdhPxI=');
         }
@@ -763,7 +812,8 @@ class Link {
     /**
      * @throws \SodiumException
      */
-    public static function decryptLink($link) {
+    public static function decryptLink($link)
+    {
         if (!in_array($link['type'], self::$_encryptedTypes)) {
             return $link;
         }
@@ -782,7 +832,8 @@ class Link {
         return $link;
     }
 
-    public static function decryptLinks($links) {
+    public static function decryptLinks($links)
+    {
         $res = [];
         foreach ($links as $key => $link) {
 
@@ -792,7 +843,8 @@ class Link {
     }
 
 
-    public static function encryptLinks($links) {
+    public static function encryptLinks($links)
+    {
         $res = [];
         foreach ($links as $key => $link) {
             $res[$key] = self::encryptLinkAttrs($link);
@@ -803,7 +855,8 @@ class Link {
     /**
      * @throws SodiumException
      */
-    public static function encryptLinkAttrs($link) {
+    public static function encryptLinkAttrs($link)
+    {
         if (!isset($link['type']) || !in_array($link['type'], self::$_encryptedTypes)) {
             return $link;
         }
diff --git a/src/Links/LottieLink.php b/src/Links/LottieLink.php
new file mode 100644 (file)
index 0000000..9e0903d
--- /dev/null
@@ -0,0 +1,67 @@
+<?php
+
+namespace Fluidbook\Tools\Links;
+
+use Cubist\Util\CSS;
+use Cubist\Util\Files\Files;
+use Cubist\Util\Json;
+use SplFileInfo;
+
+class LottieLink extends HTMLMultimediaLink
+{
+    protected $role = '';
+    protected $_config = null;
+    protected $_content = '';
+    protected $_url;
+    protected $_externalIframe = false;
+    public $defaultZIndex = 50;
+
+    public function getHTMLContent()
+    {
+        if ($this->_content == '') {
+            $ext = files::getExtension($this->to);
+
+            if ($ext === 'zip') {
+                $d = $this->unzipFile($this->to, false);
+                $this->_config = $this->getConfigZIP($d['dir']);
+                $this->copyExternalDir($d['dir'], $d['fdir']);
+                if (file_exists($d['dir'] . '/index.html')) {
+                    $html = file_get_contents($d['dir'] . '/index.html');
+                    $html = str_replace('var pRatio = window.devicePixelRatio || 1,', 'var pRatio = 0.5,', $html);
+                    $this->_config['lowDef'] = 'index_ld.html';
+                    $this->compiler->getVirtualDirectory()->file_put_contents($d['fdir'] . '/' . $this->_config['lowDef'], $html);
+                }
+            } elseif ($ext === 'html') {
+                $fdir = 'data/links';
+                $dir = $fdir;
+
+                $d = array('fdir' => $fdir, 'dir' => $dir);
+                $this->compiler->getVirtualDirectory()->copy($this->compiler->working_path($this->to), $d['dir'] . '/' . $this->to);
+                $this->_config = $this->getConfigHTML($d['dir'], $this->to);
+                $this->copyExternalFile($d['dir'] . '/' . $this->to);
+            }
+            if ($this->_config['width'] == 0) {
+                $this->_config['width'] = $this->width;
+            }
+            if ($this->_config['height'] == 0) {
+                $this->_config['height'] = $this->height;
+            }
+
+            $res = '';
+            $s = $this->in_popup ? 1 : $this->getCssScale();
+            if ($this->_config['html']) {
+                $this->_url = $d['fdir'] . '/' . $this->_config['html'];
+                if ($this->extra) {
+                    $this->_url .= '?' . $this->extra;
+                }
+
+                $iw = $this->_config['width'];
+                $ih = $this->_config['height'];
+                $res = '<div class="mask"></div>';
+                $res .= '<iframe data-scale="' . $s . '" data-width="' . $iw . '" data-height="' . $ih . '" width="' . $iw . '" height="' . $ih . '" src="' . $this->_url . '" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" allowfullscreen mozallowfullscreen="true" webkitallowfullscreen="true" onmousewheel="" style="visibility:hidden;" tabindex="-1" onload="this.style.visibility=\'visible\';"></iframe>';
+            }
+            $this->_content = $res;
+        }
+        return $this->_content;
+    }
+}