Issues (1963)

html/inc/more.inc (1 issue)

1
<?php
2
// This file is part of BOINC.
3
// https://boinc.berkeley.edu
4
// Copyright (C) 2024 University of California
5
//
6
// BOINC is free software; you can redistribute it and/or modify it
7
// under the terms of the GNU Lesser General Public License
8
// as published by the Free Software Foundation,
9
// either version 3 of the License, or (at your option) any later version.
10
//
11
// BOINC is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
// See the GNU Lesser General Public License for more details.
15
//
16
// You should have received a copy of the GNU Lesser General Public License
17
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
18
19
// show potentially long text with More/Less buttons
20
21
// output text; if significantly more than $nchars,
22
// show the first part and a More/Less button that toggles the remainder
23
//
24
// $text can't contain HTML tags (else would have to do lots of parsing)
25
//
26
function show_text_more($text, $nchars) {
27
    echo show_text_more_aux($test, $nchars);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $test seems to be never defined.
Loading history...
28
}
29
30
// same, but returns text rather than outputting it
31
//
32
function show_text_more_aux($text, $nchars) {
33
    static $count = 0;
34
    if (!$text) return '';
35
36
    $n = strlen($text);
37
    if ($n < $nchars) {
38
        return $text;
39
    }
40
    // find where to break
41
    $b = strpos($text, ' ', $nchars);
42
    if ($b === false) {
43
        return $text;
44
    }
45
46
    // don't break if tail is short
47
    if (($n - $b) < 40) {
48
        return $text;
49
    }
50
51
    $x = '';
52
    if ($count == 0) {
53
        $x .= more_text_script();
54
    }
55
    $x .= substr($text, 0, $b);
56
    $x .= sprintf('<span id="dots_%d">...</span>', $count);
57
    $x .= sprintf('<span id="more_%d">', $count);
58
    $x .= substr($text, $b);
59
    $x .= '</span>';
60
    $x .= sprintf(
61
        '<a onclick="toggle_more(\'dots_%d\', \'more_%d\', \'btn_%d\')" id="btn_%d"> (more)</a>',
62
        $count, $count, $count, $count
63
    );
64
    $x .= sprintf('
65
        <script>document.getElementById("more_%d").style.display = "none";</script>
66
        ',
67
        $count
68
    );
69
    $count++;
70
    return $x;
71
}
72
73
function more_text_script() {
74
    return '
75
<script>
76
function toggle_more(dots_id, more_id, btn_id) {
77
    var dots = document.getElementById(dots_id);
78
    var moreText = document.getElementById(more_id);
79
    var btnText = document.getElementById(btn_id);
80
81
    if (dots.style.display === "none") {
82
        dots.style.display = "inline";
83
        btnText.innerHTML = " (more)";
84
        moreText.style.display = "none";
85
    } else {
86
        dots.style.display = "none";
87
        btnText.innerHTML = " (less)";
88
        moreText.style.display = "inline";
89
    }
90
}
91
</script>
92
';
93
}
94
95
?>
96