c96a699ef45f3156213f5638b3a41eb7fcf399cb
[lhc/web/wiklou.git] / includes / ImageGallery.php
1 <?php
2 if ( ! defined( 'MEDIAWIKI' ) )
3 die();
4
5 /**
6 * @package MediaWiki
7 */
8
9 /**
10 * Image gallery
11 *
12 * Add images to the gallery using add(), then render that list to HTML using toHTML().
13 *
14 * @package MediaWiki
15 */
16 class ImageGallery
17 {
18 var $mImages, $mShowBytes, $mShowFilename;
19
20 /**
21 * Create a new image gallery object.
22 */
23 function ImageGallery( ) {
24 $this->mImages = array();
25 $this->mShowBytes = true;
26 $this->mShowFilename = true;
27 }
28
29 /**
30 * Add an image to the gallery.
31 *
32 * @param Image $image Image object that is added to the gallery
33 * @param string $html Additional HTML text to be shown. The name and size of the image are always shown.
34 */
35 function add( $image, $html='' ) {
36 $this->mImages[] = array( &$image, $html );
37 }
38
39 /**
40 * Add an image at the beginning of the gallery.
41 *
42 * @param Image $image Image object that is added to the gallery
43 * @param string $html Additional HTML text to be shown. The name and size of the image are always shown.
44 */
45 function insert( $image, $html='' ) {
46 array_unshift( $this->mImages, array( &$image, $html ) );
47 }
48
49
50 /**
51 * isEmpty() returns true if the gallery contains no images
52 */
53 function isEmpty() {
54 return empty( $this->mImages );
55 }
56
57 /**
58 * Enable/Disable showing of the file size of an image in the gallery.
59 * Enabled by default.
60 *
61 * @param boolean $f set to false to disable
62 */
63 function setShowBytes( $f ) {
64 $this->mShowBytes = ( $f == true);
65 }
66
67 /**
68 * Enable/Disable showing of the filename of an image in the gallery.
69 * Enabled by default.
70 *
71 * @param boolean $f set to false to disable
72 */
73 function setShowFilename( $f ) {
74 $this->mShowFilename = ( $f == true);
75 }
76
77 /**
78 * Return a HTML representation of the image gallery
79 *
80 * For each image in the gallery, display
81 * - a thumbnail
82 * - the image name
83 * - the additional text provided when adding the image
84 * - the size of the image
85 *
86 */
87 function toHTML() {
88 global $wgLang, $wgUser;
89
90 $sk = $wgUser->getSkin();
91
92 $s = '<table class="gallery" cellspacing="0" cellpadding="0">';
93 $i = 0;
94 foreach ( $this->mImages as $pair ) {
95 $img =& $pair[0];
96 $text = $pair[1];
97
98 $name = $img->getName();
99 $nt = $img->getTitle();
100
101 // Not an image. Just print the name and skip.
102 if ( $nt->getNamespace() != NS_IMAGE ) {
103 $s .= '<td><div class="gallerybox" style="height: 152px;">' .
104 htmlspecialchars( $nt->getText() ) . '</div></td>' . (($i%4==3) ? "</tr>\n" : '');
105 $i++;
106
107 continue;
108 }
109
110 //TODO
111 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
112
113 if( $this->mShowBytes ) {
114 if( $img->exists() ) {
115 $nb = wfMsgHtml( 'nbytes', $wgLang->formatNum( $img->getSize() ) );
116 } else {
117 $nb = wfMsgHtml( 'filemissing' );
118 }
119 $nb = "$nb<br />\n";
120 } else {
121 $nb = '';
122 }
123
124 $textlink = $this->mShowFilename ?
125 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . "<br />\n" :
126 '' ;
127
128 $s .= ($i%4==0) ? '<tr>' : '';
129 $thumb = $img->getThumbnail( 120, 120 );
130 $vpad = floor( ( 150 - $thumb->height ) /2 ) - 2;
131 $s .= '<td><div class="gallerybox">' . '<div class="thumb" style="padding: ' . $vpad . 'px 0;">';
132
133 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
134 # in version 4.8.6 generated crackpot html in its absence, see:
135 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
136 $s .= $sk->makeKnownLinkObj( $nt, $thumb->toHtml() ) . '</div><div class="gallerytext">' . "\n" .
137 $textlink . $text . $nb .
138 '</div>';
139 $s .= "</div></td>\n";
140 $s .= ($i%4==3) ? '</tr>' : '';
141 $i++;
142 }
143 if( $i %4 != 0 ) {
144 $s .= "</tr>\n";
145 }
146 $s .= '</table>';
147
148 return $s;
149 }
150
151 } //class
152 ?>