Completed
Pull Request — master (#489)
by Richard
10:39
created

Buttonbox::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 3
rs 10
ccs 0
cts 2
cp 0
cc 1
eloc 1
nc 1
nop 0
crap 2
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
namespace Xmf\Template;
13
14
/**
15
 * Buttonbox
16
 *
17
 * @category  Xmf\Template\Buttonbox
18
 * @package   Xmf
19
 * @author    Grégory Mage (Aka Mage)
20
 * @author    trabis <[email protected]>
21
 * @copyright 2011-2016 The XOOPS Project http://sourceforge.net/projects/xoops/
22
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
23
 * @version   Release: 1.0
24
 * @link      http://xoops.org
25
 * @since     1.0
26
 */
27
class Buttonbox extends AbstractTemplate
28
{
29
    /**
30
     * @var array
31
     */
32
    private $items = array();
33
34
    /**
35
     * @var string
36
     */
37
    private $delimiter = "&nbsp;";
38
39
    /**
40
     * @var string
41
     */
42
    private $position = "right";
43
44
    /**
45
     * @var string
46
     */
47
    private $path = '';
48
49
    /**
50
     * init
51
     *
52
     * @return void
53
     */
54
    protected function init()
55
    {
56
    }
57
58
    /**
59
     * set position - alignment position
60
     *
61
     * @param string $position left, right, center
62
     *
63
     * @return Buttonbox
64
     */
65
    public function setPosition($position)
66
    {
67
        $this->position = $position;
68
        return $this;
69
    }
70
71
    /**
72
     * set path - path to image files. Do not set if icons are
73
     * specified with absolute URLs
74
     *
75
     * @param string $path URL path to image files
76
     *
77
     * @return Buttonbox
78
     */
79
    public function setImagePath($path)
80
    {
81
        $this->path = $path;
82
        return $this;
83
    }
84
85
    /**
86
     * setDelimiter
87
     *
88
     * @param string $delimiter delimiter put between buttons
89
     *
90
     * @return Buttonbox
91
     */
92
    public function setDelimiter($delimiter)
93
    {
94
        $this->delimiter = $delimiter;
95
        return $this;
96
    }
97
98
    /**
99
     * addItem to button box
100
     *
101
     * @param string $title title string for button
102
     * @param string $link  link for button
103
     * @param string $icon  icon for button
104
     * @param string $extra extra
105
     *
106
     * @return Buttonbox
107
     */
108 View Code Duplication
    public function addItem($title, $link, $icon, $extra = '')
109
    {
110
        $item['title'] = $title;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$item was never initialized. Although not strictly required by PHP, it is generally a good practice to add $item = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
111
        $item['link'] = $link;
112
        $item['icon'] = $icon;
113
        $item['extra'] = $extra;
114
        $this->items[] = $item;
115
        return $this;
116
    }
117
118
    /**
119
     * render the buttonbox
120
     *
121
     * @return void
122
     */
123
    protected function render()
124
    {
125
        $path = $this->path;
126
        switch ($this->position) {
127
            default:
128
            case "right":
129
                $ret = "<div class=\"floatright\">\n";
130
                break;
131
132
            case "left":
133
                $ret = "<div class=\"floatleft\">\n";
134
                break;
135
136
            case "center":
137
                $ret = "<div class=\"aligncenter\">\n";
138
        }
139
        $ret .= "<div class=\"xo-buttons\">\n";
140
        foreach ($this->items as $item) {
141
            $ret .= "<a class='ui-corner-all tooltip' href='" . $item['link'] . "' title='" . $item['title'] . "'>";
142
            $ret .= "<img src='" . $path . $item['icon'] . "' title='" . $item['title'] . "' />";
143
            $ret .= $item['title'] . ' ' . $item['extra'];
144
            $ret .= "</a>\n";
145
            $ret .= $this->delimiter;
146
        }
147
        $ret .= "</div>\n</div>\n";
148
        $this->tpl->assign('dummy_content', $ret);
149
    }
150
}
151