<?php 
/*    
 *    Copyright (c) 2010 VidiScript
 *
 *    This file is part of VidiScript.
 *
 *    VidiScript 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 3 of the License, or
 *    (at your option) any later version.
 *
 *    VidiScript 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 VidiScript.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    File Name: videoads.inc
 *    Description: Admin options for managing video ads
 *    $Date: 2010-02-21 23:16:57 +0000 (Sun, 21 Feb 2010) $
 *    $Revision: 12 $
 */
global $db,$sitepath ;
$videoForm = '' ;
if (isset($_POST['deleteprepost']) && ($_POST['prepostdelid'] != '')) {
	$sql = "DELETE FROM prepostrollads WHERE id=".$_POST['prepostdelid'] ;
	$db->query($sql) ;
}
if (isset($_POST['deletebanner']) && ($_POST['bannerdelid'] != '')) {
	$sql = "DELETE FROM bannerads WHERE id=".$_POST['bannerdelid'] ;
	$db->query($sql) ;
}
if (isset($_POST['editprepostsubmit']) && ($_POST['editppid'] != '')) {
	$editppid = $_POST['editppid'] ;
	$epppath = $_POST['epppath'] ;
	$epplink = $_POST['epplink'] ;
	$epptext = $_POST['epptext'] ;
	$eppwhen = $_POST['eppwhen'] ;
	if (! empty($_FILES['eppupload']['tmp_name'])) {
		$filename = str_replace(" ", "_", $_FILES['eppupload']['name']) ;
		if (move_uploaded_file($_FILES['eppupload']['tmp_name'], 'uploads/ads/'.$filename)) {
			$epppath = $sitepath.'uploads/ads/'.$filename ;
		}
	}
	if (($epppath != '') && ($epplink != '') && ($epptext != '') && ($eppwhen != '')) {
		$sql = "UPDATE prepostrollads SET path='".$epppath."',link='".$epplink."',text='".$epptext."',preorpost='".$eppwhen."' WHERE id=".$editppid ;
		$db->query($sql) ;
	}
}
if (isset($_POST['editbannersubmit']) && ($_POST['editbanid'] != '')) {
	$editbanid = $_POST['editbanid'] ;
	$ebantitle = $_POST['ebantitle'] ;
	$ebandescription = $_POST['ebandescription'] ;
	$ebanlink = $_POST['ebanlink'] ;
	$ebanthumb = $_POST['ebanthumb'] ;
	if (! empty($_FILES['bannerupload']['tmp_name'])) {
		$filename = str_replace(" ", "_", $_FILES['bannerupload']['name']) ;
		if (move_uploaded_file($_FILES['bannerupload']['tmp_name'], 'uploads/ads/'.$filename)) {
			$ebanthumb = $sitepath.'uploads/ads/'.$filename ;
		}
	}
	if (($ebantitle != '') && ($ebandescription != '') && ($ebanlink != '') && ($ebanthumb != '')) {
		$sql = "UPDATE bannerads SET title='".$ebantitle."',description='".$ebandescription."',link='".$ebanlink."',thumbnail='".$ebanthumb."' WHERE id=".$editbanid ;
		$db->query($sql) ;
	}
}
if (isset($_POST['preposteditid'])) {
	$preposteditid = $_POST['preposteditid'] ;
}
else {
	$preposteditid = '' ;
}
if (isset($_POST['bannereditid'])) {
	$bannereditid = $_POST['bannereditid'] ;
}
else {
	$bannereditid = '' ;
}
$pre = getSetting("showpreroll", $db) ;
$post = getSetting("showpostroll", $db) ;
$banner = getSetting("showbanner", $db) ;
$predelay = getSetting("prerollimagedelay", $db) ;
$postdelay = getSetting("postrollimagedelay", $db) ;
$bannerstart = getSetting("bannerstart", $db) ;
$bannerend = getSetting("bannerend", $db) ;
if (isset($_POST['settingssubmit'])) {
	$pre = ($_POST['showpre'] == "on") ? 1 : 0 ;
	$post = ($_POST['showpost'] == "on") ? 1 : 0 ;
	$banner = ($_POST['showbanner'] == "on") ? 1 : 0 ;
	$predelay = $_POST['predelay'] ;
	$postdelay = $_POST['postdelay'] ;
	$bannerstart = $_POST['bannerstart'] ;
	$bannerend = $_POST['bannerend'] ;
	$db->query("UPDATE setting SET value='".$bannerstart."' WHERE setting='bannerstart'") ;
	$db->query("UPDATE setting SET value='".$bannerend."' WHERE setting='bannerend'") ;
	$db->query("UPDATE setting SET value='".$predelay."' WHERE setting='prerollimagedelay'") ;
	$db->query("UPDATE setting SET value='".$postdelay."' WHERE setting='postrollimagedelay'") ;
	$db->query("UPDATE setting SET value='".$pre."' WHERE setting='showpreroll'") ;
	$db->query("UPDATE setting SET value='".$post."' WHERE setting='showpostroll'") ;
	$db->query("UPDATE setting SET value='".$banner."' WHERE setting='showbanner'") ;
}
$showpre = ($pre == 1) ? 'CHECKED' : '' ;
$showpost = ($post == 1) ? 'CHECKED' : '' ;
$showbanner = ($banner == 1) ? 'CHECKED' : '' ;
if (isset($_POST['addprepostsubmit'])) {
	$prepostpath = $_POST['prepostpath'] ;
	$prepostlink = $_POST['prepostlink'] ;
	$preposttext = $_POST['preposttext'] ;
	$prepostwhen = $_POST['prepostwhen'] ;
	if (! empty($_FILES['prepostupload']['tmp_name'])) {
		$filename = str_replace(" ", "_", $_FILES['prepostupload']['name']) ;
		if (move_uploaded_file($_FILES['prepostupload']['tmp_name'], 'uploads/ads/'.$filename)) {
			$prepostpath = $sitepath.'uploads/ads/'.$filename ;
		}
	}
	if (($prepostpath != '') && ($prepostlink != '') && ($preposttext != '')) {
		$db->query("INSERT INTO prepostrollads (path,text,link,preorpost) VALUES('".$prepostpath."','".$preposttext."','".$prepostlink."','".$prepostwhen."')") ;
	}
}
if (isset($_POST['addbannersubmit'])) {
	$bannertitle = $_POST['bannertitle'] ;
	$bannerdescription = $_POST['bannerdescription'] ;
	$bannerlink = $_POST['bannerlink'] ;
	$bannerthumb = $_POST['bannerthumb'] ;
	if (! empty($_FILES['bannerupload']['tmp_name'])) {
		$filename = str_replace(" ", "_", $_FILES['bannerupload']['name']) ;
		if (move_uploaded_file($_FILES['bannerupload']['tmp_name'], 'uploads/ads/'.$filename)) {
			$bannerthumb = $sitepath.'uploads/ads/'.$filename ;
		}
	}
	if (($bannertitle != '') && ($bannerdescription != '') && ($bannerlink != '') && ($bannerthumb != '')) {
		$db->query("INSERT INTO bannerads (title,description,link,thumbnail) VALUES('".$bannertitle."','".$bannerdescription."','".$bannerlink."','".$bannerthumb."')") ;
	}
}
if (isset($_POST['editsettings'])) {
	$videoForm .= "<h2>Video ad settings</h2><br>" ;
	$videoForm .= "<div align=right><h2><a href='".$sitepath."?id=117'>>>back to ad management</a></h2></div>" ;
	$videoForm .= "<form action='' method='post'>" ;
	$videoForm .= "<fieldset><ol>" ;
	$videoForm .= "<li><label for='showpre'>Show pre-roll ads</label><input type='checkbox' name='showpre' ".$showpre."></li>" ;
	$videoForm .= "<li><label for='predelay'>Pre-roll image/HTML ad delay</label><input size=5 type='text' name='predelay' value='".$predelay."'></li>" ;
	$videoForm .= "<li><label for='showpost'>Show post-roll ads</label><input type='checkbox' name='showpost' ".$showpost."></li>" ;
	$videoForm .= "<li><label for='postdelay'>Post-roll image/HTML ad delay</label><input size=5 type='text' name='postdelay' value='".$postdelay."'></li>" ;
	$videoForm .= "<li><label for='showbanner'>Show banner ads</label><input type='checkbox' name='showbanner' ".$showbanner."></li>" ;
	$videoForm .= "<li><label for='bannerstart'>Banner ad start seconds</label><input size=5 type='text' name='bannerstart' value='".$bannerstart."'></li>" ;
	$videoForm .= "<li><label for='bannerend'>Banner ad end seconds</label><input size=5 type='text' name='bannerend' value='".$bannerend."'></li>" ;
	$videoForm .= "<li><label for='settingssubmit'>&nbsp;</label><input class='biggerbutton' type='submit' name='settingssubmit' value='save'></li>" ;
	$videoForm .= "</ol></fieldset>" ;
	$videoForm .= "</form>" ;
}
else if (isset($_POST['addppsubmit'])) {
	$videoForm .= "<div align=right><h2><a href='".$sitepath."?id=117'>>>back to ad management</a></h2></div>" ;
	$videoForm .= "<form method='post' action='' enctype='multipart/form-data'>" ;
	$videoForm .= "<fieldset><ol>" ;
	$videoForm .= "<b><h2>add pre/post-roll ad</h2></b><br>" ;
	$videoForm .= "<li><label for='prepostpath'>URL of ad (video, image or html)</label><input type='text' size=90 name='prepostpath' value='".$prepostpath."'></li>" ;
	$videoForm .= "<li><label for='prepostupload'>OR upload ad media (video or image)</label><input size='65' name='prepostupload' class='filebox' type='file' /></li>" ;
	$videoForm .= "<li><label for='prepostlink'>URL the ad links to</label><input type='text' size=90 name='prepostlink' value=''></li>" ;
	$videoForm .= "<li><label for='preposttext'>text to display in video player</label><input size=90 type='text' name='preposttext' value=''></li>" ;
	$videoForm .= "<li><label for='prepostwhen'>display pre/post roll (before or after video)</label><input type='radio' name='prepostwhen' value='pre' CHECKED>pre-roll&nbsp;&nbsp;&nbsp;&nbsp;<input type='radio' name='prepostwhen' value='post'>post-roll</li>" ;
	$videoForm .= "<li><label for='addprepostsubmit'>&nbsp;</label><input class='biggerbutton' type='submit' name='addprepostsubmit' value='add'></li>" ;
	$videoForm .= "</ol></fieldset>" ;
	$videoForm .= "</form><br><br><br>" ;
}
else if (isset($_POST['editprepost'])) {
	$db->query("SELECT * FROM prepostrollads WHERE id=".$preposteditid) ;
	if ($db->numRows() > 0) {
		$ppres = $db->fetch() ;
		$prepostpath = $ppres['path'] ;
		$prepostlink = $ppres['link'] ;
		$preposttext = $ppres['text'] ;
		if ($ppres['preorpost'] == 'pre') {
			$prechecked = ' CHECKED' ;
			$postchecked = '' ;
		}
		else {
			$prechecked = '' ;
			$postchecked = ' CHECKED' ;
		}
		$videoForm .= "<div align=right><h2><a href='".$sitepath."?id=117'>>>back to ad management</a></h2></div>" ;
		$videoForm .= "<form method='post' action='' enctype='multipart/form-data'>" ;
		$videoForm .= "<fieldset><ol>" ;
		$videoForm .= "<b><h2>edit pre/post-roll ad</h2></b><br>" ;
		$videoForm .= "<li><label for='epppath'>URL of ad (video, image or html)</label><input type='text' size=90 name='epppath' value='".$prepostpath."'></li>" ;
		$videoForm .= "<li><label for='eppupload'>OR upload ad media (video or image)</label>
				<input size='45' name='eppupload' class='filebox' type='file' /></li>" ;
		$videoForm .= "<li><label for='epplink'>URL the ad links to</label><input type='text' size=90 name='epplink' value='".$prepostlink."'></li>" ;
		$videoForm .= "<li><label for='epptext'>text to display in video player</label><input size=90 type='text' name='epptext' value='".$preposttext."'></li>" ;
		$videoForm .= "<li><label for='eppwhen'>display pre/post roll (before or after video)</label><input type='radio' name='eppwhen' value='pre'".$prechecked.">pre-roll&nbsp;&nbsp;&nbsp;&nbsp;<input type='radio' name='eppwhen' value='post'".$postchecked.">post-roll</li>" ;
		$videoForm .= "<input type='hidden' name='editppid' value='".$preposteditid."'>" ;
		$videoForm .= "<li><label for='editprepostsubmit'>&nbsp;</label><input class='biggerbutton' type='submit' name='editprepostsubmit' value='save'></li>" ;
		$videoForm .= "</ol></fieldset>" ;
		$videoForm .= "</form><br><br><br>" ;
	}
}
else if (isset($_POST['addbansubmit'])) {
	$videoForm .= "<div align=right><h2><a href='".$sitepath."?id=117'>>>back to ad management</a></h2></div>" ;
	$videoForm .= "<form method='post' action='' enctype='multipart/form-data'>" ;
	$videoForm .= "<fieldset><ol>" ;
	$videoForm .= "<b><h2>add banner ad</h2></b><br>" ;
	$videoForm .= "<li><label for='bannertitle'>title</label><input type='text' size=90 name='bannertitle' value=''></li>" ;
	$videoForm .= "<li><label for='bannerdescription'>description</label><input type='text' size=90 name='bannerdescription' value=''></li>" ;
	$videoForm .= "<li><label for='bannerlink'>URL the ad links to</label><input size=90 type='text' name='bannerlink' value=''></li>" ;
	$videoForm .= "<li><label for='bannerthumb'>optional thumbnail URL</label><input size=90 type='text' name='bannerthumb' value=''></li>" ;
	$videoForm .= "<li><label for='bannerupload'>OR upload thumbnail image</label><input size='65' name='bannerupload' class='filebox' type='file' /></li>" ;
	$videoForm .= "<li><label for='addbannersubmit'>&nbsp;</label><input class='biggerbutton' type='submit' name='addbannersubmit' value='add'></li>" ;
	$videoForm .= "</ol></fieldset>" ;
	$videoForm .= "</form><br><br><br>" ;
}
else if (isset($_POST['editbanner'])) {
	$db->query("SELECT * FROM bannerads WHERE id=".$bannereditid) ;
	if ($db->numRows() > 0) {
		$banres = $db->fetch() ;
		$bannertitle = $banres['title'] ;
		$bannerdescription = $banres['description'] ;
		$bannerlink = $banres['link'] ;
		$bannerthumb = $banres['thumbnail'] ;
		$videoForm .= "<div align=right><h2><a href='".$sitepath."?id=117'>>>back to ad management</a></h2></div>" ;
		$videoForm .= "<form method='post' action='' enctype='multipart/form-data'>" ;
		$videoForm .= "<fieldset><ol>" ;
		$videoForm .= "<b><h2>edit banner ad</h2></b><br>" ;
		$videoForm .= "<li><label for='ebantitle'>title</label><input type='text' size=90 name='ebantitle' value='".$bannertitle."'></li>" ;
		$videoForm .= "<li><label for='ebandescription'>description</label><input type='text' size=90 name='ebandescription' value='".$bannerdescription."'></li>" ;
		$videoForm .= "<li><label for='ebanlink'>URL the ad links to</label><input size=90 type='text' name='ebanlink' value='".$bannerlink."'></li>" ;
		$videoForm .= "<li><label for='ebanthumb'>optional thumbnail URL</label><input size=90 type='text' name='ebanthumb' value='".$bannerthumb."'></li>" ;
		$videoForm .= "<li><label for='bannerupload'>OR upload thumbnail image</label><input size='65' name='bannerupload' class='filebox' type='file' /></li>" ;
		$videoForm .= "<input type='hidden' name='editbanid' value='".$bannereditid."'>" ;
		$videoForm .= "<li><label for='editbannersubmit'>&nbsp;</label><input class='biggerbutton' type='submit' name='editbannersubmit' value='save'></li>" ;
		$videoForm .= "</ol></fieldset>" ;
		$videoForm .= "</form><br><br><br>" ;
	}
}
else {
	$videoForm .= "<div align=right><h2><form method='post' action=''><input class='biggerbutton' type='submit' value='edit video ad settings' name='editsettings'></form></h2></div>" ;
	$videoForm .= "<h2>Pre/post-roll ads</h2>" ;
	$db->query("SELECT * FROM prepostrollads") ;
	$count = 0 ;
	if ($db->numRows() > 0) {
		$videoForm .= "<table width=100% style='border: 1px solid #404040'><tr><td colspan=2 align=center><h2>current pre/post-roll ads (selected randomly)</h2></td></tr><tr><td>" ;
		$adres = $db->fetchAll() ;
		foreach ($adres as $key=>$value) {
			$count++ ;
			$preorpost = ($value['preorpost'] == 'pre') ? 'pre-roll' : 'post-roll' ;
			$videoForm .= "<div align=center><font color=#000000><table width=100% style='border: 1px solid #404040'><tr><td width=50px align=center>$count</td><td width=50px><h4>".$preorpost."</h4></td><td align=left>".$value['path']."</td><td align=right width=50px><form method='post' action=''><input type='hidden' name='preposteditid' value='".$value['id']."'><input class='biggerbutton' type='submit' name='editprepost' value='edit'></form></td><td align=right width=50px><form method='post' action=''><input type='hidden' name='prepostdelid' value='".$value['id']."'><input class='biggerbutton' type='submit' name='deleteprepost' value='delete'></form></td></tr></table></font></div>" ;
		}
		$videoForm .= "<div align=center><font color=#000000><table width=100% style='border: 1px solid #404040'><tr><td colspan=5><div align=right><form method='post' action=''><input class='biggerbutton' type='submit' value='add pre/post-roll ad' name='addppsubmit'></form></div> </td></tr></table></font></div></td></tr></table><br><br>" ;
	}
	else {
		$videoForm .= "<div align=center><font color=#000000><table width=100% style='border: 1px solid #404040'><tr><td colspan=5><div align=right><form method='post' action=''><input class='biggerbutton' type='submit' value='add pre/post-roll ad' name='addppsubmit'></form></div> </td></tr></table></font></div></td></tr></table><br><br>" ;
		$videoForm .= "<h3>You have not set up any pre/post-roll ads yet</h3>" ;
	}
	$videoForm .= "<h2>Banner ads</h2>" ;
	$db->query("SELECT * FROM bannerads") ;
	$count = 0 ;
	if ($db->numRows() > 0) {
		$videoForm .= "<table width=100% style='border: 1px solid #404040'><tr><td colspan=2 align=center><h2>Current banner ads (selected randomly)</h2></td></tr><tr><td>" ;
		$adres = $db->fetchAll() ;
		foreach ($adres as $key=>$value) {
			$count++ ;
			$videoForm .= "<div align=center><font color=#000000><table width=100% style='border: 1px solid #404040'><tr><td width=50px align=center>$count</td><td align=left>".$value['title']."</td><td align=right width=50px><form method='post' action=''><input type='hidden' name='bannereditid' value='".$value['id']."'><input class='biggerbutton' type='submit' value='edit' name='editbanner'></form></td><td align=right width=50px><form method='post' action=''><input type='hidden' name='bannerdelid' value='".$value['id']."'><input class='biggerbutton' type='submit' value='delete' name='deletebanner'></form></td></tr></table></font></div>" ;
		}
		$videoForm .= "<div align=center><font color=#000000><table width=100% style='border: 1px solid #404040'><tr><td colspan=4><div align=right><form method='post' action=''><input class='biggerbutton' type='submit' value='add banner ad' name='addbansubmit'></form></div> </td></tr></table></font></div></td></tr></table>" ;
	}
	else {
		$videoForm .= "<div align=center><font color=#000000><table width=100% style='border: 1px solid #404040'><tr><td colspan=4><div align=right><form method='post' action=''><input class='biggerbutton' type='submit' value='add banner ad' name='addbansubmit'></form></div> </td></tr></table></font></div></td></tr></table>" ;
		$videoForm .= "<h2>You have not set up any banner ads yet</h2>" ;
	}
}
?>
