<?php

/**
 * @name:        YouTubeIntegration
 * @author:      Severin Vogt <http://www.severinvogt.ch>
 * @version:     2.0
 * @date:        2009-02-28
 *
 * This plugin integrates YouTube Videos
 *
 */

define('YOUTUBE_INTEGRATION_CFG_FILE', JLOG_BASEPATH.'personal'.DIRECTORY_SEPARATOR.'settings.YouTubeIntegration.inc.php');

// Load config file
if (@file_exists(YOUTUBE_INTEGRATION_CFG_FILE)) {
	include_once YOUTUBE_INTEGRATION_CFG_FILE;
}


// Apply BBCode
function do_bbcode_youtube($action, $attributes, $content, $params, $node_object) {
	if(!is_numeric(YOUTUBE_INTEGRATION_WIDTH)) {
		$width = '401';
	} else {
		$width = YOUTUBE_INTEGRATION_WIDTH;
	}
		
	if(!is_numeric(YOUTUBE_INTEGRATION_WIDTH)) {
		$height = '330';
	} else {
		$height = YOUTUBE_INTEGRATION_HEIGHT;
	}

	$str_htmlcode = '
		<object type="application/x-shockwave-flash" style="width: '.$width.'px; height: '.$height.'px;" data="http://www.youtube.com/v/'.$content.'"><param name="movie" value="http://www.youtube.com/v/'.$content.'"></object><br>
		';
			
	return $str_htmlcode;
}

class YouTubeIntegration extends JlogPlugin {

	// Define BB-Code parser
	function hook_bbCode(&$obj_bbcode) {
		$arr_youtube = array(
								"name" => "youtube",
								"callback_type" => "usecontent?",
								"callback_func" => "do_bbcode_youtube",
								"callback_params" => array("usecontent_param"=>"default"),
								"content_type" => "link",
								"allowed_within" => array("inline","block","listitem"),
								"not_allowed_within" => array(""),
								"flags" => array()
									
							);
   
		$obj_bbcode->_codes['youtube'] = $arr_youtube;
				
		return $obj_bbcode;
	}
	
	// Add button to form
	function hook_adminForm($form) {
		
		$additional_html = "
		
		<script type=\"text/javascript\">
		
		addLoadEvent(
			function () {
				if(jlog_bbcode_br || (typeof(jlog_admin) != 'undefined'))
				if (jlog_bbcode_br.insertBefore) {
					jlog_bbcode_do_button('YouTube Video', '[youtube]', '[/youtube]');
				}
			}
		);
		</script>
		
		";
		
		return $form.$additional_html;
	}
	
	// Display configuration and some informations
	function hook_adminContent($str_output) {		
		if ($_POST['YOUTUBE_INTEGRATION_SAVE'] == '1') {
			if (is_numeric($_POST['YOUTUBE_INTEGRATION_WIDTH']) && is_numeric($_POST['YOUTUBE_INTEGRATION_HEIGHT'])) {
				if (($f = @fopen(YOUTUBE_INTEGRATION_CFG_FILE, 'wb'))) {
					@fwrite($f, "<?php\n");
					@fwrite($f, "define('YOUTUBE_INTEGRATION_WIDTH', '".$_POST['YOUTUBE_INTEGRATION_WIDTH']."');\n");
					@fwrite($f, "define('YOUTUBE_INTEGRATION_HEIGHT', '".$_POST['YOUTUBE_INTEGRATION_HEIGHT']."');\n");
					@fwrite($f, '?>');
					@fclose($f);
				}

				$str_output = '
					<br />
					Die Werte wurden gespeichert.<br />
					<br />
					<a href="'.$_SERVER['REQUEST_URI'].'">&laquo;&nbsp;zur&uuml;ck</a>
				';
			} else {
				$str_output = '
					<br />
					Die Werte m&uuml;ssen numerisch sein!<br />
					<br />
					<a href="javascript:history.back();">&laquo;&nbsp;zur&uuml;ck</a>
				';
			}
		} else {
			$str_output = '
				
				<br />
				Mit diesem Plugin k&ouml;nnen YouTube-Videos in den Blog integriert werden.<br />
	
				<br />
				<hr />
				<br />
				<h2>Konfiguration</h2>
				<br />
				
				<form method="post" action="'.$_SERVER['REQUEST_URI'].'">
					<table cellpadding="0" cellspacing="0">
						<tr>
							<th>
								Bezeichnung
							</th>
							<th>
								Wert
							</th>
						</tr>
						<tr>
							<td>
								Breite:
							</td>
							<td>
								<input type="text" name="YOUTUBE_INTEGRATION_WIDTH" size="3" maxlength="4" value="'.$this->getWidth().'">&nbsp;px
							</td>
						</tr>
						<tr>
							<td>
								H&ouml;he:
							</td>
							<td>
								<input type="text" name="YOUTUBE_INTEGRATION_HEIGHT" size="3" maxlength="4" value="'.$this->getHeight().'">&nbsp;px
							</td>
						</tr>
						<tr>
							<td>
								&nbsp;
							</td>
							<td>
								<input type="hidden" name="YOUTUBE_INTEGRATION_SAVE" value="1">
								<input type="submit" value="speichern">
							</td>
						</tr>
					</table>
				</form>
				
				<br />
				<hr />
				<br />
				<h2>Beschreibung</h2>
				<br />
				
				Um ein YouTube-Video in den Blog einzuf$uuml;gen, muss die Video-ID von YouTube im entsprechenden BB-Code eingetragen werden.<br /><br />
				<b>Beispiel:</b><br />
				[youtube]<b>VIDEOID</b>*[/youtube]
				<br />
				<br />
				<i>*Die Video-ID sieht z.Bsp so aus: "LYhrYHmUPn0"</i>
			';
		}
				
		return $str_output;
	}
	
	// Gets width from defines
	function getWidth() {
		if(!is_numeric(YOUTUBE_INTEGRATION_WIDTH)) {
			$width = '401';
		} else {
			$width = YOUTUBE_INTEGRATION_WIDTH;
		}
		return $width;
	}
	
	// Gets height from defines
	function getHeight() {			
		if(!is_numeric(YOUTUBE_INTEGRATION_WIDTH)) {
			$height = '330';
		} else {
			$height = YOUTUBE_INTEGRATION_HEIGHT;
		}
		return $height;
	}
	
}
	
?>

