<?php
include('header.php');
if (!isset($options['api_facebook_videos_enabled']) OR $options['api_facebook_videos_enabled'] != 1) {
echo notification('warning','You didn\'t Enabled Facebook Videos.');	
} else {
if (!empty($_GET['case'])) {
$case = make_safe($_GET['case']);	
} else {
$case = '';	
}
switch ($case) {
case 'fetch';
if (isset($_GET['facebook_user_link'])) {
$link = make_safe($_GET['facebook_user_link']);	
$parse = parse_url($link);	
$username = str_replace('/','',$parse['path']);
$token = $options['api_facebook_extended_token'];
if (!isset($_GET['page'])) {
$graph = 'https://graph.facebook.com/v2.0/'.$username.'/videos?fields=tags,length,picture,description,name,source,id,created_time,from,icon,thumbnails,updated_time&access_token='.$token.'&limit=24';
} else {
	if ($_GET['cursor'] == 'after') {
		$page = 'after='.$_GET['page'];
	} else {
		$page = 'before='.$_GET['page'];
	}
$graph = 'https://graph.facebook.com/v2.0/'.$username.'/videos?fields=tags,length,picture,description,name,source,id,created_time,from,icon,thumbnails,updated_time&access_token='.$token.'&limit=24&'.$page;
}
$url = json_decode(file_get_contents($graph),true);	
if (count($url['data']) > 0) {
$result = true;	
} else {
$result = false;	
}
}
?>
<div class="page-header page-heading">
	<h1 class="row"><div class="col-md-8"><i class="fa fa-facebook-square"></i> Fetch Facebook Videos From User or Page</div>
	<div class="col-md-4">
	<a href="facebook.php" class="btn btn-success pull-right" data-toggle="tooltip" data-placement="left" title="Add Single Facebook Video"><span class="fa fa-plus"></span></a>
	</div>
	</h1>
</div>
<div class="row">
<div class="col-xs-12">
<form method="GET" action="facebook.php">
<input type="hidden" name="case" value="fetch" />
<div class="form-group">
<label for="facebook_user_link">Facebook User or Page Link</label>
<div class="input-group">
<input type="text" name="facebook_user_link" id="facebook_user_link" class="form-control" placeholder="https://www.facebook.com/BuzzFeedVideo" <?php if (isset($_GET['facebook_user_link'])) {echo 'value="'.$_GET['facebook_user_link'].'"';} ?> />
<span class="input-group-addon"><button type="submit" class="btn-link"><span class="fa fa-refresh"></span></button></span>
</div>
</div>
<?php 
$categories = $general->categories('category_order ASC');
if ($categories == 0) {
echo notification('warning','Please add at least one category to import the videos in.');	
} else {
?>
<div class="form-group">
		<label>Category</label>
		<select name="category" id="category" class="form-control">
		<?php 
		foreach ($categories AS $category) {
		?>
		<option value="<?php echo $category['id']; ?>"><?php echo $category['category']; ?></option>
		<?php	
		}
		?>
		</select>
		<p class="help">This is the category where the video will be saved when click <b><span class="text-danger"><i class="fa fa-refresh"></i> Import</span></b>.</p>
		</div>
<?php 
}
?>

</form>
<?php 
if ($result == true) {
foreach ($url['data'] AS $video) {
if (isset($video['name'])) {
	$video_title = $video['name'];
} else {
	if (isset($video['description'])) {
		$video_title = mb_substr($video['description'],0,100,'UTF-8');
	} else {
		$video_title = $video['id'];	
	}
}
?>
<div class="col-md-3 col-sm-6 col-xs-12">
<div class="video">
<div class="video-thumbnail"><a href="javascript:void();" data-toggle="ajax-modal" data-url="ajax.php?case=media_details&id=<?php echo $video['id']; ?>&site=facebook"><img src="<?php echo $video['picture']; ?>" class="img-responsive" /></a></div>
<div class="video-title"><a href="javascript:void();" data-toggle="ajax-modal" data-url="ajax.php?case=media_details&id=<?php echo $video['id']; ?>&site=facebook"><?php echo mb_substr($video_title,0,60,'UTF-8'); ?></a></div>
<div class="video-tools">
<div class="col-xs-6 first-action-div">
<a href="javascript:void();" data-toggle="ajax-modal" data-url="ajax.php?case=media_details&id=<?php echo $video['id']; ?>&site=facebook" class="text-info"><i class="fa fa-reorder"></i> Details</a>
</div>
<div class="col-xs-6 action-div" id="f<?php echo $video['id']; ?>">
<?php if (check_video_id($video['id'],'facebook') == 0) { ?>
<a href="javascript:void();" class="import-single-facebook-video text-danger" id="<?php echo $video['id']; ?>"><i class="fa fa-refresh"></i> Import</a>
<?php } else { ?>	
<span class="text-success"><i class="fa fa-check"></i> Imported</span>
<?php	
}
?>
</div>
</div>
</div>
</div>
<?php	
}	
?>
<div class="clear"></div>
<div class="col-xs-12 pages-line">
<div class="row">
<div class="col-xs-6">
<?php
if (isset($url['paging']['previous'])) {
echo '<a class="btn btn-default" href="facebook.php?case=fetch&facebook_user_link='.$_GET['facebook_user_link'].'&page='.$url['paging']['cursors']['before'].'&cursor=before">Previous</a>';	
}
?>
</div>
<div class="col-xs-6 text-right">
<?php
if (isset($url['paging']['next'])) {
echo '<a class="btn btn-default" href="facebook.php?case=fetch&facebook_user_link='.$_GET['facebook_user_link'].'&page='.$url['paging']['cursors']['after'].'&cursor=after">Next</a>';	
}
?>
</div>
</div>
</div>
<?php
} 
?>
</div>
</div>
<?php
break;	
default;
?>
<div class="page-header page-heading">
	<h1 class="row"><div class="col-md-6"><i class="fa fa-facebook-square"></i> Facebook Videos</div>
	<div class="col-md-6">
	<a href="facebook.php?case=fetch" class="btn btn-success pull-right" data-toggle="tooltip" data-placement="left" title="Fetch Videos From User or Page"><span class="fa fa-refresh"></span></a>
	</div>
	</h1>
</div>
<div class="row">
<div class="col-xs-12">
<div class="form-group">
<label for="facebook_video_link">Facebook Video Link</label>
<div class="input-group">
<input type="text" name="facebook_video_link" id="facebook_video_link" class="form-control" placeholder="https://www.facebook.com/BuzzFeedVideo/videos/1845294242278116/" />
<span class="input-group-addon"><button id="fetch_facebook_video" class="btn-link"><span class="fa fa-plus"></span></button></span>
</div>
<p class="help">if the video set as <b>Private</b> By user it won't be imported.</p>
</div>
<?php 
$categories = $general->categories('category_order ASC');
if ($categories == 0) {
echo notification('warning','Please add at least one category to import the videos in.');	
} else {
?>
<div class="form-group">
		<label>Category</label>
		<select name="category" id="category" class="form-control">
		<?php 
		foreach ($categories AS $category) {
		?>
		<option value="<?php echo $category['id']; ?>"><?php echo $category['category']; ?></option>
		<?php	
		}
		?>
		</select>
		<p class="help">This is the category where the video will be saved when click <b><span class="text-danger"><i class="fa fa-plus"></i></span></b>.</p>
		</div>
<?php 
}
?>
<div id="facebook-video-details"></div>
</div>
</div>
<?php
}
}
include('footer.php');
?>