Pimcore\Model\Document\Tag\Pdf::frontend PHP Method

frontend() public method

public frontend ( )
    public function frontend()
    {
        $asset = Asset::getById($this->id);
        $options = $this->getOptions();
        if ($asset instanceof Asset\Document && $asset->getPageCount()) {
            $pageCount = $asset->getPageCount();
            $hotspots = $this->getHotspots();
            $rewritePath = function ($data) use($options) {
                if (!is_array($data)) {
                    return [];
                }
                foreach ($data as &$element) {
                    if (isset($options["hotspotCallback"]) && is_callable($options["hotspotCallback"])) {
                        $element = $options["hotspotCallback"]($element);
                        if (!is_array($element)) {
                            throw new \Exception("Return value must be the the array passed as parameter (can be modified)");
                        }
                        if (isset($element["attributes"]) && is_array($element["attributes"])) {
                            $attributes = $element["attributes"];
                            $element["attributes"] = [];
                            foreach ($attributes as $name => $value) {
                                $element["attributes"][] = ["name" => $name, "value" => $value];
                            }
                        }
                    }
                    if (array_key_exists("data", $element) && is_array($element["data"]) && count($element["data"]) > 0) {
                        foreach ($element["data"] as &$metaData) {
                            if ($metaData["value"] instanceof Element\ElementInterface) {
                                $metaData["value"] = $metaData["value"]->getFullPath();
                            }
                        }
                    }
                }
                return $data;
            };
            for ($i = 1; $i <= $pageCount; $i++) {
                $pageData = ["thumbnail" => (string) $asset->getImageThumbnail(["width" => 200, "height" => 200, "contain" => true, "format" => "pjpeg"], $i, true), "detail" => (string) $asset->getImageThumbnail(["width" => 1500, "height" => 1500, "contain" => true, "quality" => "85", "format" => "pjpeg"], $i, true)];
                if (is_array($hotspots) && isset($hotspots[$i]) && $hotspots[$i]) {
                    $pageData["hotspots"] = $rewritePath($hotspots[$i]);
                }
                $data["pages"][] = $pageData;
            }
            $data["pdf"] = $asset->getFullPath();
            $data["fullscreen"] = true;
            if (isset($options["fullscreen"])) {
                $data["fullscreen"] = (bool) $options["fullscreen"];
            }
            $jsVarName = $this->getName();
            $divId = "pimcore-pdf-" . uniqid();
            $jsonData = \Zend_Json::encode($data);
            $code = <<<HTML

            <div id="{$divId}" class="pimcore-pdfViewer">
                <div class="pimcore-pdfPages"></div>
                <div class="pimcore-pdfZoom">+</div>
                <div class="pimcore-pdfDownload">&#x21e9;</div>
                <div class="pimcore-pdfFullscreenClose">x</div>
                <div class="pimcore-pdfButtonLeft pimcore-pdfButton "><div class="pimcore-pdfArrowLeft"></div></div>
                <div class="pimcore-pdfButtonRight pimcore-pdfButton "><div class="pimcore-pdfArrowRight"></div></div>
            </div>

            <link rel="stylesheet" type="text/css" href="/pimcore/static/js/frontend/pdfViewer/styles.css" />
            <script type="text/javascript" src="/pimcore/static/js/frontend/pdfViewer/viewer.js"></script>
            <script type="text/javascript">
                var pimcore_pdf = pimcore_pdf || {};
                pimcore_pdf["{$jsVarName}"] = new pimcore.pdf({
                    id: "{$divId}",
                    data: {$jsonData}
                });
            </script>
HTML;
            return $code;
        } else {
            return $this->getErrorCode("Asset is not a valid PDF");
        }
    }