xref: /dokuwiki/vendor/openpsa/universalfeedcreator/lib/Creator/HTMLCreator.php (revision d3233986baa7dfe44490b805ae2e4296fad59401)
1572dd708SAndreas Gohr<?php
2572dd708SAndreas Gohr
3572dd708SAndreas Gohr/**
4572dd708SAndreas Gohr * HTMLCreator is a FeedCreator that writes an HTML feed file to a specific
5572dd708SAndreas Gohr * location, overriding the createFeed method of the parent FeedCreator.
6572dd708SAndreas Gohr * The HTML produced can be included over http by scripting languages, or serve
7572dd708SAndreas Gohr * as the source for an IFrame.
8572dd708SAndreas Gohr * All output by this class is embedded in <div></div> tags to enable formatting
9572dd708SAndreas Gohr * using CSS.
10572dd708SAndreas Gohr *
11572dd708SAndreas Gohr * @author  Pascal Van Hecke
12572dd708SAndreas Gohr * @since   1.7
13572dd708SAndreas Gohr */
14572dd708SAndreas Gohrclass HTMLCreator extends FeedCreator
15572dd708SAndreas Gohr{
16572dd708SAndreas Gohr
17572dd708SAndreas Gohr    protected $contentType = "text/html";
18572dd708SAndreas Gohr
19572dd708SAndreas Gohr    /**
20572dd708SAndreas Gohr     * Contains HTML to be output at the start of the feed's html representation.
21572dd708SAndreas Gohr     */
22572dd708SAndreas Gohr    public $header;
23572dd708SAndreas Gohr
24572dd708SAndreas Gohr    /**
25572dd708SAndreas Gohr     * Contains HTML to be output at the end of the feed's html representation.
26572dd708SAndreas Gohr     */
27572dd708SAndreas Gohr    public $footer;
28572dd708SAndreas Gohr
29572dd708SAndreas Gohr    /**
30572dd708SAndreas Gohr     * Contains HTML to be output between entries. A separator is only used in
31572dd708SAndreas Gohr     * case of multiple entries.
32572dd708SAndreas Gohr     */
33572dd708SAndreas Gohr    public $separator;
34572dd708SAndreas Gohr
35572dd708SAndreas Gohr    /**
36572dd708SAndreas Gohr     * Used to prefix the stylenames to make sure they are unique
37572dd708SAndreas Gohr     * and do not clash with stylenames on the user's page.
38572dd708SAndreas Gohr     */
39572dd708SAndreas Gohr    public $stylePrefix;
40572dd708SAndreas Gohr
41572dd708SAndreas Gohr    /** @var bool Determines whether the links open in a new window or not. */
42572dd708SAndreas Gohr    public $openInNewWindow = true;
43572dd708SAndreas Gohr
44572dd708SAndreas Gohr    /** @var string image alignments in output */
45572dd708SAndreas Gohr    public $imageAlign = "right";
46572dd708SAndreas Gohr
47572dd708SAndreas Gohr    /**
48572dd708SAndreas Gohr     * In case of very simple output you may want to get rid of the style tags,
49572dd708SAndreas Gohr     * hence this variable.  There's no equivalent on item level, but of course you can
50572dd708SAndreas Gohr     * add strings to it while iterating over the items ($this->stylelessOutput .= ...)
51572dd708SAndreas Gohr     * and when it is non-empty, ONLY the styleless output is printed, the rest is ignored
52572dd708SAndreas Gohr     * in the function createFeed().
53572dd708SAndreas Gohr     */
54572dd708SAndreas Gohr    public $stylelessOutput = "";
55572dd708SAndreas Gohr
56572dd708SAndreas Gohr    /**
57572dd708SAndreas Gohr     * Writes the HTML.
58572dd708SAndreas Gohr     *
59572dd708SAndreas Gohr     * @inheritdoc
60572dd708SAndreas Gohr     */
61572dd708SAndreas Gohr    public function createFeed()
62572dd708SAndreas Gohr    {
63572dd708SAndreas Gohr        // if there is styleless output, use the content of this variable and ignore the rest
64572dd708SAndreas Gohr        if ($this->stylelessOutput != "") {
65572dd708SAndreas Gohr            return $this->stylelessOutput;
66572dd708SAndreas Gohr        }
67572dd708SAndreas Gohr
68572dd708SAndreas Gohr        //if no stylePrefix is set, generate it yourself depending on the script name
69572dd708SAndreas Gohr        if ($this->stylePrefix == "") {
70572dd708SAndreas Gohr            $this->stylePrefix = str_replace(".", "_", $this->_generateFilename())."_";
71572dd708SAndreas Gohr        }
72572dd708SAndreas Gohr
73572dd708SAndreas Gohr        //set an openInNewWindow_token_to be inserted or not
74572dd708SAndreas Gohr        if ($this->openInNewWindow) {
75572dd708SAndreas Gohr            $targetInsert = " class='target_blank'";
76572dd708SAndreas Gohr        } else {
77572dd708SAndreas Gohr            $targetInsert = '';
78572dd708SAndreas Gohr        }
79572dd708SAndreas Gohr
80572dd708SAndreas Gohr        // use this array to put the lines in and implode later with "document.write" javascript
81572dd708SAndreas Gohr        $feedArray = array();
82572dd708SAndreas Gohr        if ($this->image != null) {
83572dd708SAndreas Gohr            $imageStr = "<a href='".$this->image->link."'".$targetInsert.">".
84572dd708SAndreas Gohr                "<img src='".$this->image->url."' border='0' alt='".
85572dd708SAndreas Gohr                FeedCreator::iTrunc(htmlspecialchars($this->image->title), 100).
86572dd708SAndreas Gohr                "' align='".$this->imageAlign."' ";
87572dd708SAndreas Gohr            if ($this->image->width) {
88572dd708SAndreas Gohr                $imageStr .= " width='".$this->image->width."' ";
89572dd708SAndreas Gohr            }
90572dd708SAndreas Gohr            if ($this->image->height) {
91572dd708SAndreas Gohr                $imageStr .= " height='".$this->image->height."' ";
92572dd708SAndreas Gohr            }
93572dd708SAndreas Gohr            $imageStr .= "/></a>";
94572dd708SAndreas Gohr            $feedArray[] = $imageStr;
95572dd708SAndreas Gohr        }
96572dd708SAndreas Gohr
97572dd708SAndreas Gohr        if ($this->title) {
98572dd708SAndreas Gohr            $feedArray[] = "<div class='".$this->stylePrefix."title'><a href='".$this->link."' ".$targetInsert." class='".$this->stylePrefix."title'>".
99572dd708SAndreas Gohr                FeedCreator::iTrunc(htmlspecialchars($this->title), 100)."</a></div>";
100572dd708SAndreas Gohr        }
101572dd708SAndreas Gohr        if ($this->getDescription()) {
102572dd708SAndreas Gohr            $feedArray[] = "<div class='".$this->stylePrefix."description'>".
103572dd708SAndreas Gohr                str_replace("]]>", "", str_replace("<![CDATA[", "", $this->getDescription())).
104572dd708SAndreas Gohr                "</div>";
105572dd708SAndreas Gohr        }
106572dd708SAndreas Gohr
107572dd708SAndreas Gohr        if ($this->header) {
108572dd708SAndreas Gohr            $feedArray[] = "<div class='".$this->stylePrefix."header'>".$this->header."</div>";
109572dd708SAndreas Gohr        }
110572dd708SAndreas Gohr
111572dd708SAndreas Gohr        for ($i = 0; $i < count($this->items); $i++) {
112572dd708SAndreas Gohr            if ($this->separator and $i > 0) {
113572dd708SAndreas Gohr                $feedArray[] = "<div class='".$this->stylePrefix."separator'>".$this->separator."</div>";
114572dd708SAndreas Gohr            }
115572dd708SAndreas Gohr
116572dd708SAndreas Gohr            if ($this->items[$i]->title) {
117572dd708SAndreas Gohr                if ($this->items[$i]->link) {
118572dd708SAndreas Gohr                    $feedArray[] =
119572dd708SAndreas Gohr                        "<div class='".$this->stylePrefix."item_title'><a href='".$this->items[$i]->link."' class='".$this->stylePrefix.
120572dd708SAndreas Gohr                        "item_title'".$targetInsert.">".FeedCreator::iTrunc(
121572dd708SAndreas Gohr                            htmlspecialchars(strip_tags($this->items[$i]->title)),
122572dd708SAndreas Gohr                            100
123572dd708SAndreas Gohr                        ).
124572dd708SAndreas Gohr                        "</a></div>";
125572dd708SAndreas Gohr                } else {
126572dd708SAndreas Gohr                    $feedArray[] =
127572dd708SAndreas Gohr                        "<div class='".$this->stylePrefix."item_title'>".
128572dd708SAndreas Gohr                        FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)), 100).
129572dd708SAndreas Gohr                        "</div>";
130572dd708SAndreas Gohr                }
131572dd708SAndreas Gohr            }
132572dd708SAndreas Gohr            if ($this->items[$i]->getDescription()) {
133572dd708SAndreas Gohr                $feedArray[] =
134572dd708SAndreas Gohr                    "<div class='".$this->stylePrefix."item_description'>".
135572dd708SAndreas Gohr                    str_replace("]]>", "", str_replace("<![CDATA[", "", $this->items[$i]->getDescription())).
136572dd708SAndreas Gohr                    "</div>";
137572dd708SAndreas Gohr            }
138572dd708SAndreas Gohr        }
139572dd708SAndreas Gohr        if ($this->footer) {
140572dd708SAndreas Gohr            $feedArray[] = "<div class='".$this->stylePrefix."footer'>".$this->footer."</div>";
141572dd708SAndreas Gohr        }
142572dd708SAndreas Gohr
1436cb05674SAndreas Gohr        $feed = "".implode("\r\n", $feedArray);
144572dd708SAndreas Gohr
145572dd708SAndreas Gohr        return $feed;
146572dd708SAndreas Gohr    }
147572dd708SAndreas Gohr
148572dd708SAndreas Gohr    /**
149572dd708SAndreas Gohr     * Overrides parent to produce .html extensions
150572dd708SAndreas Gohr     *
151572dd708SAndreas Gohr     * @return string the feed cache filename
152572dd708SAndreas Gohr     * @since  1.4
153572dd708SAndreas Gohr     * @access private
154572dd708SAndreas Gohr     */
155572dd708SAndreas Gohr    protected function _generateFilename()
156572dd708SAndreas Gohr    {
157*d3233986SAndreas Gohr        $fileInfo = pathinfo($_SERVER["SCRIPT_NAME"]);
158572dd708SAndreas Gohr
159572dd708SAndreas Gohr        return substr($fileInfo["basename"], 0, -(strlen($fileInfo["extension"]) + 1)).".html";
160572dd708SAndreas Gohr    }
161572dd708SAndreas Gohr}
162