Passed
Pull Request — master (#5606)
by David
08:45
created

show_text_more_aux()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 38
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 24
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 38
rs 9.2248
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
35
    $n = strlen($text);
36
    if ($n < $nchars) {
37
        return $text;
38
    }
39
    // find where to break
40
    $b = strpos($text, ' ', $nchars);
41
    if ($b === false) {
42
        return $text;
43
    }
44
45
    // don't break if tail is short
46
    if (($n - $b) < 40) {
47
        return $text;
48
    }
49
50
    $x = '';
51
    if ($count == 0) {
52
        $x .= more_text_script();
53
    }
54
    $x .= substr($text, 0, $b);
55
    $x .= sprintf('<span id="dots_%d">...</span>', $count);
56
    $x .= sprintf('<span id="more_%d">', $count);
57
    $x .= substr($text, $b);
58
    $x .= '</span>';
59
    $x .= sprintf(
60
        '<a onclick="toggle_more(\'dots_%d\', \'more_%d\', \'btn_%d\')" id="btn_%d"> (more)</a>',
61
        $count, $count, $count, $count
62
    );
63
    $x .= sprintf('
64
        <script>document.getElementById("more_%d").style.display = "none";</script>
65
        ',
66
        $count
67
    );
68
    $count++;
69
    return $x;
70
}
71
72
function more_text_script() {
73
    return '
74
<script>
75
function toggle_more(dots_id, more_id, btn_id) {
76
    var dots = document.getElementById(dots_id);
77
    var moreText = document.getElementById(more_id);
78
    var btnText = document.getElementById(btn_id);
79
80
    if (dots.style.display === "none") {
81
        dots.style.display = "inline";
82
        btnText.innerHTML = " (more)";
83
        moreText.style.display = "none";
84
    } else {
85
        dots.style.display = "none";
86
        btnText.innerHTML = " (less)";
87
        moreText.style.display = "inline";
88
    }
89
}
90
</script>
91
';
92
}
93
94
?>
95