Passed
Push — master ( b2f73c...0708c5 )
by Michael
59s queued 16s
created

PrintPage()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 194
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 61
c 0
b 0
f 0
dl 0
loc 194
rs 8.8509
cc 4
nc 8
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright      {@link https://xoops.org/ XOOPS Project}
14
 * @license        {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
15
 * @package
16
 * @since
17
 * @author         XOOPS Development Team
18
 */
19
20
/**
21
 * Print an article
22
 *
23
 * This page is used to print an article. The advantage of this script is that you
24
 * only see the article and nothing else.
25
 *
26
 * @package               News
27
 * @author                Xoops Modules Dev Team
28
 * @copyright    (c)      XOOPS Project (https://xoops.org)
29
 *
30
 * Parameters received by this page :
31
 * @page_param            int        storyid                    Id of news to print
32
 *
33
 * @page_title            Story's title - Printer Friendly Page - Topic's title - Site's name
34
 *
35
 * @template_name         This page does not use any template
36
 */
37
38
use Xmf\Request;
39
use XoopsModules\News;
40
use XoopsModules\News\NewsStory;
41
42
require_once dirname(__DIR__, 2) . '/mainfile.php';
43
// require_once XOOPS_ROOT_PATH . '/modules/news/class/class.newsstory.php';
44
$storyid = Request::getInt('storyid', 0, 'GET');
45
if (empty($storyid)) {
46
    redirect_header(XOOPS_URL . '/modules/news/index.php', 2, _NW_NOSTORY);
47
}
48
49
// Verify that the article is published
50
$story = new NewsStory($storyid);
51
// Not yet published
52
if (0 == $story->published() || $story->published() > time()) {
53
    redirect_header(XOOPS_URL . '/modules/news/index.php', 2, _NW_NOSTORY);
54
}
55
56
// Expired
57
if (0 != $story->expired() && $story->expired() < time()) {
58
    redirect_header(XOOPS_URL . '/modules/news/index.php', 2, _NW_NOSTORY);
59
}
60
61
// Verify permissions
62
/** @var \XoopsGroupPermHandler $grouppermHandler */
63
$grouppermHandler = xoops_getHandler('groupperm');
64
if (is_object($xoopsUser)) {
65
    $groups = $xoopsUser->getGroups();
66
} else {
67
    $groups = XOOPS_GROUP_ANONYMOUS;
68
}
69
if (!$grouppermHandler->checkRight('news_view', $story->topicid(), $groups, $xoopsModule->getVar('mid'))) {
70
    redirect_header(XOOPS_URL . '/modules/news/index.php', 3, _NOPERM);
71
}
72
73
$xoops_meta_keywords    = '';
74
$xoops_meta_description = '';
75
76
if ('' !== trim($story->keywords())) {
77
    $xoops_meta_keywords = $story->keywords();
78
} else {
79
    $xoops_meta_keywords = News\Utility::createMetaKeywords($story->hometext() . ' ' . $story->bodytext());
80
}
81
82
if ('' !== trim($story->description())) {
83
    $xoops_meta_description = $story->description();
84
} else {
85
    $xoops_meta_description = strip_tags($story->title());
86
}
87
88
function PrintPage()
89
{
90
    global $xoopsConfig, $xoopsModule, $story, $xoops_meta_keywords, $xoops_meta_description;
91
    $myts     = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
92
    $datetime = formatTimestamp($story->published(), News\Utility::getModuleOption('dateformat')); ?>
93
    <!DOCTYPE html>
94
<html xml:lang="<?php echo _LANGCODE; ?>" lang="<?php echo _LANGCODE; ?>">
95
    <?php
96
    echo "<head>\n";
97
    echo '<title>' . htmlspecialchars($story->title(), ENT_QUOTES | ENT_HTML5) . ' - ' . _NW_PRINTER . ' - ' . htmlspecialchars($story->topic_title(), ENT_QUOTES | ENT_HTML5) . ' - ' . $xoopsConfig['sitename'] . '</title>';
98
    echo '<meta http-equiv="Content-Type" content="text/html; charset=' . _CHARSET . '">';
99
    echo '<meta name="author" content="XOOPS">';
100
    echo '<meta name="keywords" content="' . $xoops_meta_keywords . '">';
101
    echo '<meta name="COPYRIGHT" content="Copyright (c) 2014 by ' . $xoopsConfig['sitename'] . '">';
102
    echo '<meta name="DESCRIPTION" content="' . $xoops_meta_description . '">';
103
    echo '<meta name="generator" content="XOOPS">';
104
    echo '<meta name="robots" content="noindex,nofollow">';
105
    if (file_exists(XOOPS_ROOT_PATH . '/language/' . $GLOBALS['xoopsConfig']['language'] . '/style.css')) {
106
        echo '<link rel="stylesheet" type="text/css" media="all" title="Style sheet" href="' . XOOPS_URL . '/language/' . $GLOBALS['xoopsConfig']['language'] . '/style.css">';
107
    } else {
108
        echo '<link rel="stylesheet" type="text/css" media="all" title="Style sheet" href="' . XOOPS_URL . '/language/english/style.css">';
109
    }
110
    echo '<link rel="stylesheet" type="text/css" media="all" title="Style sheet" href="' . XOOPS_URL . '/modules/news/assets/css/print.css">';
111
    $supplemental = '';
112
    if (News\Utility::getModuleOption('footNoteLinks')) {
113
        $supplemental = "footnoteLinks('content','content'); "; ?>
114
        <script type="text/javascript">
115
            // <![CDATA[
116
            /*------------------------------------------------------------------------------
117
             Function:       footnoteLinks()
118
             Author:         Aaron Gustafson (aaron at easy-designs dot net)
119
             Creation Date:  8 May 2005
120
             Version:        1.3
121
             Homepage:       http://www.easy-designs.net/code/footnoteLinks/
122
             License:        Creative Commons Attribution-ShareAlike 2.0 License
123
             http://creativecommons.org/licenses/by-sa/2.0/
124
             Note:           This version has reduced functionality as it is a demo of
125
             the script's development
126
             ------------------------------------------------------------------------------*/
127
            function footnoteLinks(containerID, targetID) {
128
                if (!document.getElementById || !document.getElementsByTagName || !document.createElement) return false;
129
                if (!document.getElementById(containerID) || !document.getElementById(targetID)) return false;
130
                var container = document.getElementById(containerID);
131
                var target = document.getElementById(targetID);
132
                var h2 = document.createElement('h2');
133
                addClass.apply(h2, ['printOnly']);
134
                var h2_txt = document.createTextNode('<?php echo _NW_LINKS; ?>');
135
                h2.appendChild(h2_txt);
136
                var coll = container.getElementsByTagName('*');
137
                var ol = document.createElement('ol');
138
                addClass.apply(ol, ['printOnly']);
139
                var myArr = [];
140
                var thisLink;
141
                var num = 1;
142
                for (var i = 0; i < coll.length; i++) {
143
                    if (coll[i].getAttribute('href') ||
144
                        coll[i].getAttribute('cite')) {
145
                        thisLink = coll[i].getAttribute('href') ? coll[i].href : coll[i].cite;
146
                        var note = document.createElement('sup');
147
                        addClass.apply(note, ['printOnly']);
148
                        var note_txt;
149
                        var j = inArray.apply(myArr, [thisLink]);
150
                        if (j || j === 0) { // if a duplicate
151
                            // get the corresponding number from
152
                            // the array of used links
153
                            note_txt = document.createTextNode(j + 1);
154
                        } else { // if not a duplicate
155
                            var li = document.createElement('li');
156
                            var li_txt = document.createTextNode(thisLink);
157
                            li.appendChild(li_txt);
158
                            ol.appendChild(li);
159
                            myArr.push(thisLink);
160
                            note_txt = document.createTextNode(num);
161
                            num++;
162
                        }
163
                        note.appendChild(note_txt);
164
                        if (coll[i].tagName.toLowerCase() === 'blockquote') {
165
                            var lastChild = lastChildContainingText.apply(coll[i]);
166
                            lastChild.appendChild(note);
167
                        } else {
168
                            coll[i].parentNode.insertBefore(note, coll[i].nextSibling);
169
                        }
170
                    }
171
                }
172
                target.appendChild(h2);
173
                target.appendChild(ol);
174
175
                return true;
176
            }
177
178
            // ]]>
179
        </script>
180
        <script type="text/javascript">
181
            // <![CDATA[
182
            /*------------------------------------------------------------------------------
183
             Excerpts from the jsUtilities Library
184
             Version:        2.1
185
             Homepage:       http://www.easy-designs.net/code/jsUtilities/
186
             License:        Creative Commons Attribution-ShareAlike 2.0 License
187
             http://creativecommons.org/licenses/by-sa/2.0/
188
             Note:           If you change or improve on this script, please let us know.
189
             ------------------------------------------------------------------------------*/
190
            if (Array.prototype.push === null) {
191
                Array.prototype.push = function (item) {
192
                    this[this.length] = item;
193
194
                    return this.length;
195
                };
196
            }
197
            // ---------------------------------------------------------------------
198
            //                  function.apply (if unsupported)
199
            //           Courtesy of Aaron Boodman - http://youngpup.net
200
            // ---------------------------------------------------------------------
201
            if (!Function.prototype.apply) {
202
                Function.prototype.apply = function (oScope, args) {
203
                    var sarg = [];
204
                    var rtrn, call;
205
                    if (!oScope) oScope = window;
206
                    if (!args) args = [];
207
                    for (var i = 0; i < args.length; i++) {
208
                        sarg[i] = "args[" + i + "]";
209
                    }
210
                    call = "oScope.__applyTemp__(" + sarg.join(",") + ");";
211
                    oScope.__applyTemp__ = this;
212
                    rtrn = eval(call);
213
                    oScope.__applyTemp__ = null;
214
215
                    return rtrn;
216
                };
217
            }
218
219
            function inArray(needle) {
220
                for (var i = 0; i < this.length; i++) {
221
                    if (this[i] === needle) {
222
                        return i;
223
                    }
224
                }
225
226
                return false;
227
            }
228
229
            function addClass(theClass) {
230
                if (this.className !== '') {
231
                    this.className += ' ' + theClass;
232
                } else {
233
                    this.className = theClass;
234
                }
235
            }
236
237
            function lastChildContainingText() {
238
                var testChild = this.lastChild;
239
                var contentCntnr = ['p', 'li', 'dd'];
240
                while (testChild.nodeType != 1) {
241
                    testChild = testChild.previousSibling;
242
                }
243
                var tag = testChild.tagName.toLowerCase();
244
                var tagInArr = inArray.apply(contentCntnr, [tag]);
245
                if (!tagInArr && tagInArr !== 0) {
246
                    testChild = lastChildContainingText.apply(testChild);
247
                }
248
249
                return testChild;
250
            }
251
252
            // ]]>
253
        </script>
254
        <style type="text/css" media="screen">
255
            .printOnly {
256
                display: none;
257
            }
258
        </style>
259
        <?php
260
    }
261
    echo '</head>';
262
    echo '<body bgcolor="#ffffff" text="#000000" onload="' . $supplemental . ' window.print()">
263
        <div id="content">
264
        <table border="0"><tr><td align="center">
265
        <table border="0" width="100%" cellpadding="0" cellspacing="1" bgcolor="#000000"><tr><td>
266
        <table border="0" width="100%" cellpadding="20" cellspacing="1" bgcolor="#ffffff"><tr><td align="center">
267
        <img src="' . XOOPS_URL . '/images/logo.png" border="0" alt=""><br><br>
268
        <h3>' . $story->title() . '</h3>
269
        <small><b>' . _NW_DATE . '</b>&nbsp;' . $datetime . ' | <b>' . _NW_TOPICC . '</b>&nbsp;' . htmlspecialchars($story->topic_title(), ENT_QUOTES | ENT_HTML5) . '</small><br><br></td></tr>';
270
    echo '<tr valign="top" style="font:12px;"><td>' . $story->hometext() . '<br>';
271
    $bodytext = $story->bodytext();
272
    $bodytext = str_replace('[pagebreak]', '<br style="page-break-after:always;">', $bodytext);
273
    if ('' !== $bodytext) {
274
        echo $bodytext . '<br><br>';
275
    }
276
    echo '</td></tr></table></td></tr></table>
277
    <br><br>';
278
    printf(_NW_THISCOMESFROM, htmlspecialchars($xoopsConfig['sitename'], ENT_QUOTES));
279
    echo '<br><a href="' . XOOPS_URL . '/">' . XOOPS_URL . '</a><br><br>
280
        ' . _NW_URLFORSTORY . ' <!-- Tag below can be used to display Permalink image --><!--img src="' . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . '/assets/images/x.gif" /--><br>
281
        <a class="ignore" href="' . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . '/article.php?storyid=' . $story->storyid() . '">' . XOOPS_URL . '/modules/news/article.php?storyid=' . $story->storyid() . '</a>
282
        </td></tr></table></div>
283
        </body>
284
        </html>
285
        ';
286
}
287
288
PrintPage();
289