User:Cm~mediawikiwiki/ImagePlusphp

From mediawiki.org
<?php
# Copyright (C) 2007 Florian Mayrhuber <f_mayrhuber@gmx.at>
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or 
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# http://www.gnu.org/copyleft/gpl.html


# Extended by Cynthia Mattingly @ Marketing Factory
# * Internationalization
# * Interface generalized for including files and opens image interface only when needed

/**
	
	 imageplus is an extension for mediawiki that allows user-friendly creation and insertion of
	 image markup as well as the adaption of already existing image markup in the wiki article.
	  
	 imageplus is able to deal with
	 - image file names
	 - caption 
	 - size (width + heigth limits)
	 - alignment (left/center/right) and
	 - misc. options (border, thumbnail, frame)
	 
	 all these parts of the wiki markup language are represented as ordinary html objects located
	 above the wiki textarea. all user inputs and changes are then  converted to valid wiki markup.

*/

if ( !defined( 'MEDIAWIKI' ) )
{
    die();
}

$wgExtensionFunctions[] = 'wfImagePlus';

// extension infos
$wgExtensionCredits['other'][] = array(
	'name'		=> 'ImagePlus',
	'author'	=> 'Florian Mayrhuber',
	'url'		=> 'http://www.mediawiki.org/wiki/Extension:ImagePlus',
	'description'	=> 'Offers a user-friendly way to insert new images and to edit existing image markup.'
);

$dir = dirname(__FILE__) . '/';
$wgExtensionMessagesFiles['ImagePlus'] = $dir . 'ImagePlus.i18n.php';

/**
	 * hook function
	 * asserts the main function to the hook
*/
function wfImagePlus()
{

    // set up hooks
    global $wgHooks;
    $wgHooks['EditPage::showEditForm:fields'][] = 'fnImagePlus';
	
}

/**
	* main function integrates html for imageplus into output
*/
function fnImagePlus( &$p, &$out )
{
	wfLoadExtensionMessages( 'ImagePlus' );
	
	// get complete filelist from DB
    $dbr = &wfGetDB( DB_SLAVE );
	$fileList = fnGetAllFilenames( $dbr );
    
    // transform results into javascript array for autocomplete textfield
	$jsFiles = "<script type=\"text/javascript\">";
	$jsFiles .= "var aImages = new Array(";
   	
	   while ( $row = $dbr->fetchObject($fileList) )
   	{
		$jsFiles .= "'" . $row->img_name . " (" . $row->img_width . "x" . $row->img_height . ")', ";
	}
	
    $jsFiles = trim($jsFiles, ",");
    $jsFiles = substr($jsFiles,0,-2);
	$jsFiles .= ");</script>";
    $out->addScript($jsFiles);
    
    // get last 10 files from DB
    $dbr = &wfGetDB( DB_SLAVE );
	$latestFiles = fnGetLastFilenames( $dbr );
    
    // import image plus javascript functions
    $out->addScript("<script type=\"text/javascript\" src=\"extensions/imageplus/imageplus.js\"></script>\n");
    
    // import files for autocomplete text field
	$out->addScript("<script type=\"text/javascript\" src=\"extensions/imageplus/moacdropdown/js/modomt.js\"></script>\n");
	$out->addScript("<script type=\"text/javascript\" src=\"extensions/imageplus/moacdropdown/js/getobject2.js\"></script>\n");
	$out->addScript("<script type=\"text/javascript\" src=\"extensions/imageplus/moacdropdown/js/acdropdown.js\"></script>\n");
    $out->addScript("<style>@import url( extensions/imageplus/moacdropdown/css/dropdown.css );</style>");
    
	// html opening tags for image+
    $out->addHTML( "<div><a href=\"javascript:showHide()\"><img src=\"extensions/imageplus/addimage.png\" alt=\"".wfMsg( 'extension_name' )."\" title=\"".wfMsg( 'extension_name' )."\"/></a></div>" );
    $out->addHTML( "<div id=\"imageplus\" name=\"imageplus\" style=\"display:none\">" );
    $out->addHTML( "<h2>".wfMsg( 'add_file' )."</h2>" );
	$out->addHTML( "<table width=\"900\" cellpadding=\"0\" border=\"0\"><tr>" );
    	
	// select box for latest files
	$out->addHTML( "<td width=\"130\" align=\"right\">".wfMsg( 'last_uploaded' ).": </td>" );
	$out->addHTML( "<td width=\"20\">&nbsp;</td><td width=\"300\" align=\"left\"> " );
	// put all files into select field
	$out->addHTML( "<select onchange=\"createMarkup(this)\" id=\"latestFiles\" name=\"latestFiles\" 
					style=\"width:280px;border: 1px solid\">" );    
	$out->addHTML( "<option value=\"".wfMsg( 'last_uploaded' )."\" style=\"color:#000000;\">none</option>" );
    while ( $row = $dbr->fetchObject($latestFiles) )
    {
		$option = $row->img_name . " ( " . $row->img_width . "x" . $row->img_height . " ) ";
		$out->addHTML( "<option value=\"$option\" style=\"color:#000000;\">$option</option>" );
    }
    $out->addHTML( "</select></td>" );
    
    // autocomplete text field
	$out->addHTML( "<td width=\"130\" align=\"right\">".wfMsg( 'autocomplete' ).": </td>" );
	$out->addHTML( "<td width=\"20\">&nbsp;</td><td width=\"300\" align=\"left\"> " );
	$out->addHTML( "<input id=\"files\" style=\"width:280px;\" onblur=\"createMarkup(this)\" value=\"\" class=\"dropdown\" autocomplete=\"off\" acdropdown=\"true\" autocomplete_list=\"array:aImages\" 
					autocomplete_format=\"formatFiles\" autocomplete_matchbegin=\"true\"></td></tr><tr>");
    
	$out->addHTML( "</table>");
	$out->addHTML( "<div id=\"imageedit\" name=\"imageedit\" style=\"display:none\">" );
    $out->addHTML( "<h2>".wfMsg( 'edit_image_options' )."</h2>" );
	$out->addHTML( "<table width=\"900\" cellpadding=\"0\" border=\"0\"><tr>" );
    
	$out->addHTML( "<td width=\"130\" align=\"right\">".wfMsg( 'position' ).": </td>" );
	$out->addHTML( "<td width=\"20\">&nbsp;</td>" );	
	// radio buttons for left/center/right
	$out->addHTML( "<td align=\"left\">" );
	$out->addHTML( "<input type=\"radio\" id=\"left\" name=\"position\" value=\"left\" onchange=\"createMarkup(this)\"/>".wfMsg( 'left' )."" );
	#$out->addHTML( "</td><td width=\"75\" align=\"left\">" );
	$out->addHTML( "<input type=\"radio\" id=\"center\" name=\"position\" value=\"center\" onchange=\"createMarkup(this)\"/>".wfMsg( 'center' )."" );
	#$out->addHTML( "</td><td width=\"75\" align=\"left\">" );
	$out->addHTML( "<input type=\"radio\" id=\"right\" name=\"position\" value=\"right\" onchange=\"createMarkup(this)\"/>".wfMsg( 'right' )."" );
	#$out->addHTML( "</td><td width=\"75\" align=\"left\">" );
	$out->addHTML( "<input type=\"radio\" id=\"nonea\" name=\"position\" value=\"none\" onchange=\"createMarkup(this)\"/>".wfMsg( 'none' )."" );
	$out->addHTML( "</td>" );
	
	// radio buttons for border/thumb/frame
	$out->addHTML( "<td width=\"130\" align=\"right\">".wfMsg( 'type' ).": </td>" );
	$out->addHTML( "<td width=\"20\">&nbsp;</td>" );	
	$out->addHTML( "</td><td align=\"left\">" );
	$out->addHTML( "<input type=\"radio\"  name=\"format\" id=\"border\" value=\"border\" onchange=\"createMarkup(this)\"/>".wfMsg( 'border' )."" );
	#$out->addHTML( "</td><td width=\"75\" align=\"left\">" );
	$out->addHTML( "<input type=\"radio\" name=\"format\" id=\"thumb\" value=\"thumb\" onchange=\"createMarkup(this)\"/>".wfMsg( 'thumb' )."" );
	#$out->addHTML( "</td><td width=\"75\" align=\"left\">");
	$out->addHTML( "<input type=\"radio\" name=\"format\" id=\"frame\" value=\"frame\" onchange=\"createMarkup(this)\"/>".wfMsg( 'frame' )."" );
	#$out->addHTML( "</td><td width=\"75\" align=\"left\">" );
	$out->addHTML( "<input type=\"radio\" name=\"format\" id=\"nonef\" value=\"none\" onchange=\"createMarkup(this)\"/>".wfMsg( 'none' )."" );
	$out->addHTML( "</td></tr><tr>" );
	$out->addHTML( "<td width=\"130\" align=\"right\">".wfMsg( 'caption' ).": </td>" );
	$out->addHTML( "<td width=\"20\">&nbsp;</td><td width=\"300\" align=\"left\"> " );
	$out->addHTML ( "<input type=\"text\" value=\"\" id=\"caption\" style=\"width:278px;border: 1px solid\" 
		maxlength=\"40\"/ onkeyup=\"createMarkup(this)\" onmouseout=\"createMarkup(this)\"></td>");
	$out->addHTML( "<td width=\"130\" align=\"right\">".wfMsg( 'size' ).": </td>" );
	$out->addHTML( "<td width=\"20\">&nbsp;</td>" );	
	$out->addHTML( "<td colspan=\"4\" width=\"300\" align=\"left\">&nbsp;<input type=\"text\" value=\"0\" id=\"size\" 
	size=\"23\" maxlength=\"12\" onkeyup=\"createMarkup(this)\" onmouseout=\"createMarkup(this)\"/>  px (".wfMsg( 'dimension_clue' ).")</td>" );
	$out->addHTML( "</tr></table>" );
	$out->addHTML( "</div>" );
	
    $out->addHTML( "<h3>".wfMsg( 'wiki_code' )."</h3>" );
	// preview markup and insert button
	$out->addHTML( "<table width=\"900\" cellpadding=\"0\" border=\"0\"><tr>");
	#$out->addHTML( "<td width=\"130\" height=\"50\" align=\"right\">".wfMsg( 'wiki_code' ).":</td>" );
	#$out->addHTML( "<td width=\"20\">&nbsp;</td>" );
	$out->addHTML( "<td width=\"430\" align=\"left\">" );
	$out->addHTML( "<input type=\"text\" value=\"\" id=\"markup\" size=\"67\" maxlength=\"100\" readonly/></td>" );
	$out->addHTML( "<td width=\"20\" align=\"center\">&nbsp;</td>" );
	$out->addHTML( "<td width=\"300\" align=\"left\"><input type=\"button\" style=\"width:164px\" id=\"insert\" 
	value=\"".wfMsg( 'insert_code_close_editor' )."\" onclick=\"insertMarkup()\"/>" );
	$out->addHTML( "</td></tr></table><br/><br/></div>" );

    return true;
}

/**
	 * retrieves uploaded files from database
	 *
	 * param: wiki db object
	 * return: results (filename, filesize,description,width,heigth)
*/
function fnGetAllFilenames( $dbr )
{

     $res = $dbr->select('image', // FROM
       array('img_name','img_size','img_description','img_width','img_height')); // SELECT
       //array(), $fname, array('ORDER BY' => 'img_name'));// GROUP BY

    return $res;

}

/**
	 * retrieves last 10 uploaded files from database
	 *
	 * param: wiki db object
	 * return:results (filename, filesize,description,width,heigth)
*/
function fnGetLastFilenames( $dbr )
{
	$fname = '';
     $res = $dbr->select('image', // FROM
       array('img_name','img_size','img_description','img_width','img_height','img_timestamp'), // SELECT
       array(), $fname, array('ORDER BY' => 'img_timestamp desc', 'LIMIT' => '10'));// add. SQL options

    return $res;

}

?>