<?php
/**
* Jamroom jrYouTube Module Form Controller
* @copyright 2009 by Talldude Networks LLC
* @author Brian Johnson -
bigguy@talldude.net
*/
require('include/jamroom-include.inc.php');
// Our script
$GLOBALS['JR_SCRIPT_NAME'] = 'jrYouTube.php';
$_post = getPostVars();
// Login required - get $_user
$_user = sessionVerify();
// get the correct language pack included..
$language = getLanguage($_user['user_language']);
// Show correct form based on "mode" received
switch ($_post['mode']) {
// Create
case 'create':
// retrieve saved data from session
$_rep = getForm('jrYouTube');
jmHtmlBegin($language['jrYouTube'][1]);
jmBodyBegin();
jmSpanCell($language['jrYouTube'][1],'',30,'html_modify.png');
jrGetFormNotice();
jmBeginForm('jrYouTube.php?mode=save');
jmInput($language['jrYouTube'][4],'youtube_video_id','text',$_rep['youtube_video_id']);
jmInput($language['jrYouTube'][5],'youtube_video_category','text',$_rep['youtube_video_category']);
jrFormSubmit($language['jrYouTube'][2],false,false,'admin.php?s=section_jrYouTubeMenu');
jmEndForm();
jmBodyEnd();
jmHtmlEnd();
exit;
break;
// Save
case 'save':
// Save posted values to session
saveForm('jrYouTube');
// Error Checking - YouTube ID is required
if (strlen($_post['youtube_video_id']) === 0) {
setFormHighlight('youtube_video_id');
jrSetFormNotice('error',$language['jrYouTube'][6]);
jrLocation('jrYouTube.php?mode=create');
}
// We need to grab the YouTube ID from the URL they entered
// URL will be like: http://www.youtube.com/watch?v=84sZMS95yYM
// We need "v"
if (isset($_post['youtube_video_id']) && strstr($_post['youtube_video_id'],'http')) {
// They entered the FULL UL
$_tmp = parse_url($_post['youtube_video_id']);
parse_str($_tmp['query'],$_tmp);
// This will get us "v=84sZMS95yYM" - run through
if (isset($_tmp['v']) && strlen($_tmp['v']) > 0) {
$_post['youtube_video_id'] = $_tmp['v'];
}
else {
setFormHighlight('youtube_video_id');
jrSetFormNotice('error',$language['jrYouTube'][6]);
jrLocation('jrYouTube.php?mode=create');
}
}
else {
// See if they gave us just the YouTube ID
// We safeBaseName the value to prevent someone from entering a full path
$_post['youtube_video_id'] = safeBaseName(trim($_post['youtube_video_id']));
}
// No error - prep database values
$_post['youtube_video_id'] = dbEscapeString($_post['youtube_video_id']);
$_post['youtube_video_category'] = dbEscapeString($_post['youtube_video_category']);
// see if we are doing a new video or an
// existing video (we'll receive a youtube_id)
if (checkType($_post['youtube_id'],'number_nz')) {
$req = "UPDATE {$jamroom_db['jrYouTubeVideos']} SET
youtube_video_id = '{$_post['youtube_video_id']}',
youtube_video_time = '". time() ."',
youtube_video_category = '{$_post['youtube_video_category']}'
WHERE youtube_id = '{$_post['youtube_id']}'
AND youtube_band_id = '{$_user['user_band_id']}'
LIMIT 1";
$cnt = dbQuery($req,'COUNT');
if (!checkType($cnt,'number_nz')) {
jrSetFormNotice('error','Error updating entry - check connection');
jrLocation("jrYouTube.php?mode=update&youtube_id={$_post['youtube_id']}");
}
}
else {
$req = "INSERT INTO {$jamroom_db['jrYouTubeVideos']} (
youtube_band_id,
youtube_video_id,
youtube_video_time,
youtube_video_category
) VALUES (
'{$_user['user_band_id']}',
'{$_post['youtube_video_id']}',
'". time() ."',
'{$_post['youtube_video_category']}'
)";
$cnt = dbQuery($req,'INSERT_ID');
$_post['youtube_id'] = (int) $cnt;
if (!checkType($cnt,'number_nz')) {
jrSetFormNotice('error','Error saving entry - check connection');
jrLocation('jrYouTube.php?mode=create');
}
}
// Looks good - reset, rebuild and return
resetForm('jrYouTube');
setLock($_user['user_band_id'],'on');
jrSetFormNotice('success',$language['jrYouTube'][7]);
jrLocation("jrYouTube.php?mode=update&youtube_id={$_post['youtube_id']}");
break;
// Modify
case 'modify':
// Get existing videos for this profile
$req = "SELECT youtube_id, youtube_video_id, youtube_video_category
FROM {$jamroom_db['jrYouTubeVideos']}
WHERE youtube_band_id = '{$_user['user_band_id']}'
ORDER BY youtube_video_id ASC";
$_rt = dbQuery($req,'NUMERIC');
if (isset($_rt[0]) && is_array($_rt[0])) {
foreach ($_rt as $_video) {
$_sel["{$_video['youtube_id']}"] = "{$_video['youtube_video_id']} ({$_video['youtube_video_category']})";
}
}
jmHtmlBegin($language['jrYouTube'][11]);
jmBodyBegin();
jmSpanCell($language['jrYouTube'][11],'',30,'html_modify.png');
jrGetFormNotice();
jmBeginForm('jrYouTube.php?mode=update');
jmSelect($language['jrYouTube'][11],'youtube_id',$_sel);
jrFormSubmit($language['jrYouTube'][11],false,false,'admin.php?s=section_jrYouTubeMenu');
jmEndForm();
jmBodyEnd();
jmHtmlEnd();
exit;
break;
// Update
case 'update':
if (!checkType($_post['youtube_id'],'number_nz')) {
setFormHighlight('youtube_id');
jrSetFormNotice('error',$language['jrYouTube'][6]);
jrLocation('jrYouTube.php?mode=modify');
}
// Get the request youtube video info from the DB
$req = "SELECT *
FROM {$jamroom_db['jrYouTubeVideos']}
WHERE youtube_id = '{$_post['youtube_id']}'
LIMIT 1";
$_rt = dbQuery($req,'SINGLE');
if (!isset($_rt) || !is_array($_rt)) {
// Cannot find entry in database
setFormHighlight('youtube_id');
jrSetFormNotice('error',$language['jrYouTube'][6]);
jrLocation('jrYouTube.php?mode=modify');
}
jmHtmlBegin($language['jrYouTube'][11]);
jmBodyBegin();
jmSpanCell($language['jrYouTube'][11],'',30,'html_modify.png');
jrGetFormNotice();
jmBeginForm("jrYouTube.php?mode=save&youtube_id={$_post['youtube_id']}");
jmShowLine('<object width="480" height="295"><param name="movie" value="http://www.youtube.com/v/'. $_rt['youtube_video_id'] .'&hl=en&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'. $_rt['youtube_video_id'] .'&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="295"></embed></object>');
jmInput($language['jrYouTube'][4],'youtube_video_id','text',$_rt['youtube_video_id']);
jmInput($language['jrYouTube'][5],'youtube_video_category','text',$_rt['youtube_video_category']);
jrFormSubmit($language['jrYouTube'][2],false,false,'admin.php?s=section_jrYouTubeMenu');
jmEndForm();
jmBodyEnd();
jmHtmlEnd();
exit;
break;
// Delete
case 'delete':
jmHtmlBegin($language['jrYouTube'][1]);
jmBodyBegin();
jmSpanCell($language['jrYouTube'][1],'',30,'html_modify.png');
jmBeginForm('jrYouTube.php?mode=remove');
jrGetFormNotice();
// Get our current videos
$req = "SELECT *
FROM {$jamroom_db['jrYouTubeVideos']}
WHERE youtube_band_id = '{$_user['user_band_id']}'";
$_rt = dbQuery($req,'youtube_video_id',null,false,'youtube_video_id');
if (is_array($_rt)) {
jmSelect($language['jrYouTube'][3],'youtube_video_id',$_rt);
jrFormSubmit($language['jrYouTube'][3],false,false,false,$language['jrYouTube'][9]);
}
else {
jmShowLine($language['jrYouTube'][8]);
jrCancel('admin.php?s=section_jrYouTubeMenu');
}
jmEndForm();
jmBodyEnd();
jmHtmlEnd();
exit;
break;
// Remove
case 'remove':
// Error Checking - YouTube ID is required
if (strlen($_post['youtube_video_id']) > 0) {
$req = "DELETE FROM {$jamroom_db['jrYouTubeVideos']}
WHERE youtube_video_id = '". dbEscapeString($_post['youtube_video_id']) ."'
AND youtube_band_id = '{$_user['user_band_id']}'
LIMIT 1";
$cnt = dbQuery($req,'COUNT');
if (isset($cnt) && $cnt == '1') {
setLock($_user['user_band_id'],'on');
jrSetFormNotice('success',$language['jrYouTube'][10]);
jrLocation('jrYouTube.php?mode=delete');
}
}
jrSetFormNotice('error',$language['jrYouTube'][6]);
jrLocation('jrYouTube.php?mode=delete');
break;
// Video Browse - admin only
case 'video_browse':
jrAdminOnly();
jmHtmlBegin('Browse YouTube Videos');
jmBodyBegin();
jmSpanCell('Browse YouTube Videos','',30,'html_select.png');
// Start our header
$dat[1]['title'] = 'YouTube URL';
$dat[1]['style'] = 'width:75%;';
$dat[2]['title'] = 'Category';
$dat[2]['style'] = 'width:25%;';
htmlPageSelect('header',$dat);
unset($dat);
// Get all of the YouTube Videos entered
$req = "SELECT *
FROM {$jamroom_db['jrYouTubeVideos']}
ORDER BY youtube_id DESC";
$_rt = dbQuery($req,'NUMERIC');
if (isset($_rt) && is_array($_rt)) {
foreach ($_rt as $_video) {
$dat[1]['title'] = "<a href=\"{$config['jryoutube_base_url']}{$_video['youtube_video_id']}\" target=\"_blank\">{$_video['youtube_video_id']}</a>";
$dat[2]['title'] = $_video['youtube_video_category'];
htmlPageSelect('row',$dat);
}
}
htmlPageSelect('footer');
jmBodyEnd();
jmHtmlEnd();
exit;
break;
// Default - invalid mode
default:
jrInvalidOption();
break;
}
?>