xref: /dokuwiki/vendor/openpsa/universalfeedcreator/lib/Creator/HTMLCreator.php (revision a087da71ea972d05ca659a1e9406c87aa7ae258b)
1<?php
2
3/**
4 * HTMLCreator is a FeedCreator that writes an HTML feed file to a specific
5 * location, overriding the createFeed method of the parent FeedCreator.
6 * The HTML produced can be included over http by scripting languages, or serve
7 * as the source for an IFrame.
8 * All output by this class is embedded in <div></div> tags to enable formatting
9 * using CSS.
10 *
11 * @author  Pascal Van Hecke
12 * @since   1.7
13 * @package de.bitfolge.feedcreator
14 */
15class HTMLCreator extends FeedCreator
16{
17
18    protected $contentType = "text/html";
19
20    /**
21     * Contains HTML to be output at the start of the feed's html representation.
22     */
23    public $header;
24
25    /**
26     * Contains HTML to be output at the end of the feed's html representation.
27     */
28    public $footer;
29
30    /**
31     * Contains HTML to be output between entries. A separator is only used in
32     * case of multiple entries.
33     */
34    public $separator;
35
36    /**
37     * Used to prefix the stylenames to make sure they are unique
38     * and do not clash with stylenames on the user's page.
39     */
40    public $stylePrefix;
41
42    /** @var bool Determines whether the links open in a new window or not. */
43    public $openInNewWindow = true;
44
45    /** @var string image alignments in output */
46    public $imageAlign = "right";
47
48    /**
49     * In case of very simple output you may want to get rid of the style tags,
50     * hence this variable.  There's no equivalent on item level, but of course you can
51     * add strings to it while iterating over the items ($this->stylelessOutput .= ...)
52     * and when it is non-empty, ONLY the styleless output is printed, the rest is ignored
53     * in the function createFeed().
54     */
55    public $stylelessOutput = "";
56
57    /**
58     * Writes the HTML.
59     *
60     * @inheritdoc
61     */
62    public function createFeed()
63    {
64        // if there is styleless output, use the content of this variable and ignore the rest
65        if ($this->stylelessOutput != "") {
66            return $this->stylelessOutput;
67        }
68
69        //if no stylePrefix is set, generate it yourself depending on the script name
70        if ($this->stylePrefix == "") {
71            $this->stylePrefix = str_replace(".", "_", $this->_generateFilename())."_";
72        }
73
74        //set an openInNewWindow_token_to be inserted or not
75        if ($this->openInNewWindow) {
76            $targetInsert = " class='target_blank'";
77        } else {
78            $targetInsert = '';
79        }
80
81        // use this array to put the lines in and implode later with "document.write" javascript
82        $feedArray = array();
83        if ($this->image != null) {
84            $imageStr = "<a href='".$this->image->link."'".$targetInsert.">".
85                "<img src='".$this->image->url."' border='0' alt='".
86                FeedCreator::iTrunc(htmlspecialchars($this->image->title), 100).
87                "' align='".$this->imageAlign."' ";
88            if ($this->image->width) {
89                $imageStr .= " width='".$this->image->width."' ";
90            }
91            if ($this->image->height) {
92                $imageStr .= " height='".$this->image->height."' ";
93            }
94            $imageStr .= "/></a>";
95            $feedArray[] = $imageStr;
96        }
97
98        if ($this->title) {
99            $feedArray[] = "<div class='".$this->stylePrefix."title'><a href='".$this->link."' ".$targetInsert." class='".$this->stylePrefix."title'>".
100                FeedCreator::iTrunc(htmlspecialchars($this->title), 100)."</a></div>";
101        }
102        if ($this->getDescription()) {
103            $feedArray[] = "<div class='".$this->stylePrefix."description'>".
104                str_replace("]]>", "", str_replace("<![CDATA[", "", $this->getDescription())).
105                "</div>";
106        }
107
108        if ($this->header) {
109            $feedArray[] = "<div class='".$this->stylePrefix."header'>".$this->header."</div>";
110        }
111
112        for ($i = 0; $i < count($this->items); $i++) {
113            if ($this->separator and $i > 0) {
114                $feedArray[] = "<div class='".$this->stylePrefix."separator'>".$this->separator."</div>";
115            }
116
117            if ($this->items[$i]->title) {
118                if ($this->items[$i]->link) {
119                    $feedArray[] =
120                        "<div class='".$this->stylePrefix."item_title'><a href='".$this->items[$i]->link."' class='".$this->stylePrefix.
121                        "item_title'".$targetInsert.">".FeedCreator::iTrunc(
122                            htmlspecialchars(strip_tags($this->items[$i]->title)),
123                            100
124                        ).
125                        "</a></div>";
126                } else {
127                    $feedArray[] =
128                        "<div class='".$this->stylePrefix."item_title'>".
129                        FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)), 100).
130                        "</div>";
131                }
132            }
133            if ($this->items[$i]->getDescription()) {
134                $feedArray[] =
135                    "<div class='".$this->stylePrefix."item_description'>".
136                    str_replace("]]>", "", str_replace("<![CDATA[", "", $this->items[$i]->getDescription())).
137                    "</div>";
138            }
139        }
140        if ($this->footer) {
141            $feedArray[] = "<div class='".$this->stylePrefix."footer'>".$this->footer."</div>";
142        }
143
144        $feed = "".join($feedArray, "\r\n");
145
146        return $feed;
147    }
148
149    /**
150     * Overrides parent to produce .html extensions
151     *
152     * @return string the feed cache filename
153     * @since  1.4
154     * @access private
155     */
156    protected function _generateFilename()
157    {
158        $fileInfo = pathinfo($_SERVER["PHP_SELF"]);
159
160        return substr($fileInfo["basename"], 0, -(strlen($fileInfo["extension"]) + 1)).".html";
161    }
162}
163