Split link generation, table of contents, and image functions from the
[mediawiki.git] / includes / ImageGallery.php
blob81af0cbae836bf9c325b993f49baf55f0d539724
1 <?php
2 /**
3 * @package MediaWiki
4 */
6 /**
7 * This is not a valid entry point, perform no further processing unless MEDIAWIKI is defined
8 */
9 if( defined( 'MEDIAWIKI' ) ) {
12 /**
13 * Image gallery
15 * Add images to the gallery using add(), then render that list to HTML using toHTML().
17 * @package MediaWiki
19 class ImageGallery
21 var $mImages, $mShowBytes, $mShowFilename;
23 /**
24 * Create a new image gallery object.
26 function ImageGallery( ) {
27 $this->mImages = array();
28 $this->mShowBytes = true;
29 $this->mShowFilename = true;
32 /**
33 * Add an image to the gallery.
35 * @param Image $image Image object that is added to the gallery
36 * @param string $html Additional HTML text to be shown. The name and size of the image are always shown.
38 function add( $image, $html='' ) {
39 $this->mImages[] = array( &$image, $html );
42 /**
43 * isEmpty() returns false iff the gallery doesn't contain any images
45 function isEmpty() {
46 return empty( $this->mImages );
49 /**
50 * Enable/Disable showing of the file size of an image in the gallery.
51 * Enabled by default.
53 * @param boolean $f set to false to disable
55 function setShowBytes( $f ) {
56 $this->mShowBytes = ( $f == true);
59 /**
60 * Enable/Disable showing of the filename of an image in the gallery.
61 * Enabled by default.
63 * @param boolean $f set to false to disable
65 function setShowFilename( $f ) {
66 $this->mShowFilename = ( $f == true);
69 /**
70 * Return a HTML representation of the image gallery
72 * For each image in the gallery, display
73 * - a thumbnail
74 * - the image name
75 * - the additional text provided when adding the image
76 * - the size of the image
79 function toHTML() {
80 global $wgLang, $wgContLang, $wgUser;
82 $sk = $wgUser->getSkin();
84 $s = '<table style="border:solid 1px #DDDDDD; cellspacing:0; cellpadding:0; margin:1em;">';
85 $i = 0;
86 foreach ( $this->mImages as $pair ) {
87 $img =& $pair[0];
88 $text = $pair[1];
90 $name = $img->getName();
91 $nt = $img->getTitle();
93 // Not an image. Just print the name and skip.
94 if ( $nt->getNamespace() != NS_IMAGE ) {
95 $s .= '<td valign="top" width="150px" style="background-color:#F0F0F0;">' .
96 htmlspecialchars( $nt->getText() ) . '</td>' . (($i%4==3) ? "</tr>\n" : '');
97 $i++;
99 continue;
102 //TODO
103 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
105 if( $this->mShowBytes ) {
106 if( $img->exists() ) {
107 $nb = wfMsg( 'nbytes', $wgLang->formatNum( $img->getSize() ) );
108 } else {
109 $nb = wfMsg( 'filemissing' );
111 $nb = htmlspecialchars( $nb ) . '<br />';
112 } else {
113 $nb = '';
116 '' ;
117 $textlink = $this->mShowFilename ?
118 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . '<br />' :
119 '' ;
121 $s .= ($i%4==0) ? '<tr>' : '';
122 $thumb = $img->getThumbnail(120,120);
123 $s .= '<td valign="top" width="150px" style="background-color:#F0F0F0;">' .
124 '<table width="100%" height="150px">'.
125 '<tr><td align="center" valign="center" style="background-color:#F8F8F8;border:solid 1px #888888;">' .
126 $sk->makeKnownLinkObj( $nt, $thumb->toHtml() ) . '</td></tr></table> ' .
127 $textlink . $text . $nb;
129 $s .= "</td>\n" . (($i%4==3) ? "</tr>\n" : '');
131 $i++;
133 if( $i %4 != 0 ) {
134 $s .= "</tr>\n";
136 $s .= '</table>';
138 return $s;
141 } //class
146 } // defined( 'MEDIAWIKI' )