Salve a tutti,
sto affrontando la riscrittura di un plug-in sviluppato per mambo/joomla 1.0 che serve ad inserire dei diagrammi di posizione scacchistiche, sostituendo un certo testo con la tabella equivalente.
Il plugin originario si chiama mosFEn ed è stato sviluppato da Renè Torenstra nel 2007.
Il codice da me riscritto è il seguente:
<?php
/**********************************************************************************
* mosFEN
* @version 1.3
* @ ORIGINAL copyright (c) 2007 R. Torenstra
* @ Converted Joomla 1.5.x Version copyright (c) 2009 G.Maschietti
* @license GNU GPL (http://www.gnu.org/licenses/gpl.txt)
* @author http://www.alfieriinforma.it
*
* Version History:
*
* 1.3: adapted to be compliant with Joomla 1.5.x
* 1.2: Fixed:
* artf6342: XHTML compliancy
* artf6381: Strict defining of variables
* 1.1: Added external settings in the backend
* 1.0: Initial version for own use
**********************************************************************************/
// No direct access allowed to this file
defined( '_JEXEC' ) or die( 'Restricted access' );
// Import Joomla! Plugin library file
jimport('joomla.event.plugin');
//The Content plugin Loadmodule
class plgContentMosFEN extends JPlugin
{
/**
* Plugin that inserts a graphic of a chess position supplied in FEN-notation
*
* Usage:
* {mosFEN}Insert legal FEN-position here{/mosFEN}
*
*/
// onPrepareContent, meaning the plugin is rendered at the first stage in preparing content for output
public function onPrepareContent( &$row, &$params)
{
// A database connection is created
$db = JFactory::getDBO();
// simple performance check to determine whether bot should process further
if ( JString::strpos( $row->text, 'mosFEN' ) === false )
{
return true;
}
// expression to search for
$regex = "#{mosFEN}(.*?){/mosFEN}#s"; // Expression to search for
// check whether plugin has been unpublished
if ( !$this->params->get( 'enabled', 1 ) ) {
$row->text = preg_replace( $regex, '', $row->text );
return true;
}
// find all instances of plugin and put in $matches
preg_match_all( $regex, $row->text, $matches );
// Number of plugins
$count = count( $matches[0] );
// plugin only processes if there are any instances of the plugin in the text
if ( $count )
{
// Get plugin parameters
$size = $this->params->def( 'size');
$cdark = $this->params->def( 'cdark');
$clight = $this->params->def( 'clight');
$class = $this->params->def( 'class');
$this->_replacer( $row, $matches, $count, $regex, $size, $cdark, $clight, $class );
}
// No return value
}
// The proccessing function
protected function _replacer( &$row, &$matches, $count, $regex, $size, $cdark, $clight, $class )
{
// Set up tags
$img_path = '<img align="middle" width="' . $size . '" height="' . $size . '" alt="" src="'. JURI::base() . 'plugins/content/mosFEN/';
$clight = '<td bgcolor="' . $clight . '">';
$cdark = '<td bgcolor="' . $cdark . '">';
$td_open = $cdark; // First square is light, so start on dark
$td_close = '" /></td>';
// HTML output
$Output = '<div align="center"><table'. ( $class != '' ? ' class="'. $class . '"' : '' ) . '><tbody><tr>';
for ( $k=0; $k < $count; $k++ )
{
$FEN = $matches[1][$k];
// Replace digits with empty squares ("E")
$FEN_Explode = '';
for ( $i = 0; $i < strlen( $FEN ); $i++ ) {
$CurrentChar = $FEN{ $i };
if ( $CurrentChar == " " ) { // Stop at space. We don't need castling rights, etc.
break;
} elseif ( is_numeric( $CurrentChar ) ) { // Empty square
for ( $y = 1; $y <= $CurrentChar; $y++ ) {
$FEN_Explode .= "E";
}
} else {
$FEN_Explode .= $CurrentChar;
}
}
// Build the board table
for ( $i = 0; $i < strlen( $FEN_Explode ); $i++ ) {
$CurrentSquare = $FEN_Explode{ $i };
if ( $td_open == $clight ) { // Switch colour
$td_open = $cdark;
} else {
$td_open = $clight;
}
switch ( $CurrentSquare ) {
case "R":
$Piece = 'wr.gif';
break;
case "r":
$Piece = 'br.gif';
break;
case "N":
$Piece = 'wn.gif';
break;
case "n":
$Piece = 'bn.gif';
break;
case "B":
$Piece = 'wb.gif';
break;
case "b":
$Piece = 'bb.gif';
break;
case "Q":
$Piece = 'wq.gif';
break;
case "q":
$Piece = 'bq.gif';
break;
case "K":
$Piece = 'wk.gif';
break;
case "k":
$Piece = 'bk.gif';
break;
case "P":
$Piece = 'wp.gif';
break;
case "p":
$Piece = 'bp.gif';
break;
case "E":
$Piece = 'empty.gif';
break;
case "/":
break;
default: // Simple validity check to catch markup, etc.
return '<div align="center">[ Not a valid FEN position: (' . $CurrentSquare . ') ]</div>';
}
if ( $CurrentSquare == "/" ) { // New row
$Output = $Output . '</tr><tr>';
} else {
//echo $td_open . $img_path . $Piece . $td_close;
$Output = $Output . $td_open . $img_path . $Piece . $td_close;
}
}
// Wrap up
$Output .= '</tr></tbody></table></div>';
return $Output;
$row->text = preg_replace( $regex, $Output, $row->text );
return true;
}
}
}
?>
Purtroppo non mi funziona nel senso che non sostituisce, alla stringa [mosFEN}....{mosFEN} la tabella.
Inserendo poportune righe di debug, comunque, ho visto che la variabiel $Output contiene alla fine il giusto codice HTML. Purtroppo, come dicevo, non viene sostituito il codice originario con l'HTML corrispondente.
AIUTO!!!!!!!!!!!!ORA FUNZIONA!!!!