<?php
// PukiWiki - Yet another WikiWikiWeb clone.
// $Id: tile.inc.php,v 1.0 2014/12/19 00:00:00 acompass.net Exp $
//
// Showing items in tile pattern function for Pukiwiki plugin.
// License: GPL v2 or (at your option) any later version
// Usage : #tile( option1, option2, option3 ){{ lines }}
//     option1-5 : none    : No option
//               : number  : number of column. Default is 5.
//               : shuffle : shuffle statements
//               : border:width : show border of tile by width.
//                                  Default is no border. Width is 1.
//               : background:color : table background color. Default is white.
//               : bodercolor:color : boder color. Default is black.
// Ex. #tile( 10, shuffle, border:2, background:#FFFF00, bodercolor:red ){{
//         &size(40){&color(red){AAA};};
//         BBB
//            :
//     }}
//
//     #tile(){{
//         &size(40){AAA};
//         &size(50){BBB};
//            :
//     }}
// 

function plugin_tile_convert() {
	$option = array();
	$arg_num = func_num_args();
	$args = func_get_args();
	// Default設定
	$option1 = 5;
	$option5 = '#000000'; // Black
	$html = '';
	$opflg2 = FALSE;
	$opflg3 = FALSE;
	$opflg4 = FALSE;
	$opflg5 = FALSE;

	// Option Check
	for ( $i = 0; $i < $arg_num - 1; $i++ ) {

		$option = tile_check_option( $args[$i] );

		switch ( $option['type'] ) {
		case 'option1' :
			$option1 = $option['value'];
			break;
		case 'option2' :
			$opflg2 = TRUE;
			break;
		case 'option3' :
			$option3 = $option['value'];
			$opflg3 = TRUE;
			break;
		case 'option4' :
			$option4 = $option['value'];
			$opflg4 = TRUE;
			break;
		case 'option5' :
			$option5 = $option['value'];
			break;
		case 'error' :
			$html = $option['value'];
			return $html;
			break;
		default :
			$html = '<p class="error">tile.inc.php System Error. </p>';
			return $html;
		}
	} // End of for


	$text   = $args[$arg_num - 1];
	$lines = mbsplit("\r" ,$text);
	if ( $opflg2 == TRUE ) {
		shuffle( $lines );
	}

	foreach ( $lines as $line ) {
		if ( $line != '' ) {
			$items[] = convert_html($line);
		}
	}

	if ( $opflg3 == TRUE ) {
		$html = '<div><table border="1" cellspacing="' . $option3. '" ';
		$html .= 'bgcolor="' . $option5. '" ';
		$html .= 'bordercolor="' . $option5. '" ';
		$html .= 'cellpadding="0"><tbody><tr>';
	} else {
		$html = '<div><table border="0" cellspacing="0" cellpadding="0"><tbody><tr>';
	}

	$col = 0;
	foreach ( $items as $item ) {
		if ( $option1 < ++$col ) {
			$html .= '</tr><tr>';
			$col = 1;
		}
		if ( $opflg4 == TRUE ) {
			$html .= '<td style="background-color:' . $option4 . '">' . $item . '</td>';
		} else {
			$html .= '<td>' . $item . '</td>';
		}
	}
	for ( $i = $col; $i < $option1 ; $i++ ) {
		if ( $opflg4 == TRUE ) {
			$html .= '<td style="background-color:' . $option4 . '"></td>';
		} else {
			$html .= '<td></td>';
		}
	}
	$html .= '</tr></tbody></table></div>';

	return $html;
}

function tile_check_option( $parm ) {

	$option = array();
	$parms = explode(':', $parm);

	switch ( $parms[0] ) {
	case 'shuffle' :
		$option['type'] = 'option2';
		$option['value'] = 'shuffle';
		break;

	case 'border' :
		$option['type'] = 'option3';
		if (is_numeric ($parms[1])) {
			$option['value'] = $parms[1];
		} else if ( $parms[1] == '' ) {
			$option['value'] = 1; // Default value
		} else {
			$option['type'] = 'error';
			$option['value'] = '<p class="error">#tile():Invalid border value : ['. $array[1] .'] </p>';
		}		break;

	case 'background' :
		$option['type'] = 'option4';
		if ( $parms[1] == '' ) {
			$option['value'] = '#FFFFFF'; // Default value. White
		} else {
			$option['value'] = $parms[1];
		}
		break;

	case 'bordercolor' :
		$option['type'] = 'option5';
		if ( $parms[1] == '' ) {
			$option['value'] = '#000000'; // Default value. Black
		} else {
			$option['value'] = $parms[1];
		}
		break;

	default :
		if (is_numeric ($parm) ) {
			$option['type'] = 'option1';
			$option['value'] = $parm;
		} else {
			$option['type'] = 'error';
			$option['value'] = '<p class="error">#tile():Invalid option : ['. $parm .'] </p>';
		}
		break;
	}

	return $option;

}

?>
