<?php
$query       = '';
$multi_query = '';
$limit       = 50;
$respond_error = 0;
$search_made = 0;
$page_id     = 1;
if (!empty($_GET['query']) || !empty($_GET['multi-query'])) {
    if (!empty($_GET['query'])) {
        $query = PT_Secure($_GET['query']);
    } else if ($_GET['multi-query']) {
        $query       = PT_Secure($_GET['multi-query']);
        $multi_query = $query;
        $explode     = explode(',', $query);
        if (!isset($_SESSION['keyword-id']) && empty($_GET['finished'])) {
            $query = $_SESSION['keyword-id'] = 0;
            $query = $explode[0];
        } else {
            $query = $explode[$_SESSION['keyword-id']];
        }
    }
    if (!empty($_GET['limit']) && $limit < 101) {
        $limit = (int) PT_Secure($_GET['limit']);
    }
    $search_made = 1;
    $page_id = 1;
    if (!empty($_GET['page-query'])) {
        if (is_numeric($_GET['page-query'])) {
            $page_id = PT_Secure($_GET['page-query']);
        }
    }
    $query = urlencode($query);
    $call_url   = "https://api.dailymotion.com/videos/?search=$query&page=$page_id&limit=$limit&fields=thumbnail_1080_url,thumbnail_large_url,title,duration,description,tags,id";
    $get_videos = connect_to_url($call_url);
    $get_videos = json_decode($get_videos);
    if (!empty($get_videos)) {
        if (!empty($get_videos->total)) {
            $ids = array();
            foreach ($get_videos->list as $key => $video) {
                $check_if_exists = $db->where('daily', $video->id)->getValue(T_VIDEOS, 'count(*)');
                if ($check_if_exists == 0) {
                    $ids[] = $video->id;
                }
            }
        }
    } else {
        $respond_error = 1;
        $error_text = 'Error connecting to Dailmotion API, you might reached your daily limit, please try again later.';
    }
}
if (empty($ids) && empty($respond_error)) {
    $keyword_id = (isset($_SESSION['keyword-id'])) ? $_SESSION['keyword-id'] : null;
    if (!empty($explode[$keyword_id + 1])) {
        $_SESSION['keyword-id'] = $_SESSION['keyword-id'] + 1;
    } else {
        unset($_SESSION['keyword-id']);
        if (empty($_GET['finished']) && $search_made == 1) {
            header("Location: " . PT_LoadAdminLinkSettings('import-from-dailymotion?finished=1'));
        }
    }
}
$category_filter = 0;
if (!empty($_GET['category_id'])) {
    if (in_array($_GET['category_id'], array_keys(ToArray($pt->categories)))) {
        $category_filter = $_GET['category_id'];
    }
}
$get_ini     = ini_get('max_input_vars');
$auto_import = 0;
if (!empty($_GET['auto_import'])) {
    $auto_import = 1;
}
?>
<div class="container-fluid">
	<?php if ($get_ini <= 1000) { ?>
	<div class="alert alert-warning">
		If you want to import more than 180 videos by one click, you need to increase <b>max_input_vars</b> in your php.ini file.
	</div>
	<?php } ?>
    <div class="block-header">
        <h2>Videos > Import Videos > Import From Dailymotion</h2>
    </div>
    <!-- Vertical Layout -->
    <div class="row">
        <div class="col-lg-12 col-md-12">
            <div class="card">
                <div class="header">
                    <h2>Import Videos From Dailymotion</h2>
                </div>
                <div class="body">
                   <div class="row">
                       <div class="col-md-12" style="margin-bottom:0;">
                            <form method="get" action="<?php echo PT_LoadAdminLinkSettings('import-from-dailymotion'); ?>">
                              <div class="row">
                              	<div class="col-md-2" id="normal-query-form">
	                              	<div class="form-group form-float">
	                                    <div class="form-line">
	                                        <input type="text" name="query" id="query" class="form-control" value="<?php echo (empty($multi_query)) ? urldecode($query) : ''; ?>">
	                                        <label class="form-label search-form">Keyword To Import</label>
	                                    </div>
	                                </div>
	                              </div>
	                              <div class="col-lg-2 col-md-2">
                                    <select class="form-control show-tick" id="limit" name="limit">
                                        <option value="all" selected disabled>Limit Per Page</option>
                                        <option value="5" <?php echo ($limit == 5) ? 'selected' : '' ?>>5</option>
                                        <option value="10" <?php echo ($limit == 10) ? 'selected' : '' ?>>10</option>
                                        <option value="25" <?php echo ($limit == 25) ? 'selected' : '' ?>>25</option>
                                        <option value="50" <?php echo ($limit == 50) ? 'selected' : '' ?>>50 (Default)</option>
                                        <option value="100" <?php echo ($limit == 100) ? 'selected' : '' ?>>100 (Default)</option>
                                    </select>
                                </div>
                                <div class="col-lg-2 col-md-2">
                                    <select class="form-control show-tick" id="page-query" name="page-query">
                                        <option value="1" selected disabled>Page</option>
                                        <?php for ($i=1; $i < 100; $i++) { ?>
                                        	<option value="<?php echo $i ?>" <?php echo ($page_id == $i) ? 'selected' : '' ?>><?php echo $i ?></option>
                                        <?php } ?>
                                    </select>
                                </div>
                                <div class="col-lg-2 col-md-2">
                                    <select class="form-control show-tick" id="category_id" name="category_id">
                                        <?php 
                                        foreach($pt->categories as $key => $category) { 
                                            $selected = ($key == $category_filter) ? 'selected' : '';
                                        ?>
                                        <option value="<?php echo $key?>" <?php echo $selected?>><?php echo $category?></option>
                                        <?php } ?>
                                    </select>
                                </div>
                                <div class="col-lg-4 col-md-4">
                                    <select class="form-control show-tick" id="auto_import" name="auto_import">
                                        <option value="0" <?php echo ($auto_import == 0) ? 'selected' : '';?>>Manual Import</option>
                                        <option value="1" <?php echo ($auto_import == 1) ? 'selected' : '';?>>Auto Import (Auto Import After Loading)</option>
                                    </select>
                                </div>
                                <div class="clearfix"></div>
                                <div class="col-md-12 <?php echo (!empty($multi_query)) ? '' : 'hidden' ?>" id="multi-query-form">
                                  <div class="form-group form-float">
                                      <div class="form-line">
                                          <textarea name="multi-query" id="multi-query" class="form-control" rows="5"><?php echo $multi_query ?></textarea>
                                          <label class="form-label search-form">Keywords separated by comma (This will import the next word automatically after the previous one is finished, 100 pages will be imported for each keyword)</label>
                                      </div>
                                  </div>
                                </div>
                                <div class="clearfix"></div>
	                            <div class="col-md-2">
	                              	<button class="btn btn-info">Search</button>
	                            </div>
                              </div>
                              <div class="clearfix"></div>
                           </form>
                       </div>
                   </div>
                   <div class="clearfix"></div>
                </div>
            </div>
            <?php if ((isset($_GET['query']) || isset($_GET['multi-query'])) && $respond_error == 0) { ?>
            <div class="card">
                <div class="header">
                    <h2>Videos</h2>
                </div>
                <div class="body">
                   <div class="row" id="videos-layout">
                        <?php 
                        if (!empty($ids)) {
                        	foreach ($get_videos->list as $key => $video) {
                        		$thumb = PT_GetMedia('upload/photos/thumbnail.jpg');
                                if (!empty($video->thumbnail_1080_url)) {
                                    $thumb = $video->thumbnail_1080_url;
                                } else if (!empty($video->thumbnail_large_url)) {
                                    $thumb = $video->thumbnail_large_url;
                                }
                                $thumb = str_replace('http://', 'https://', $thumb);
                        		$tags = '';
                        		if (is_array($video->tags)) {
                                    $tags_array = array();
                                    $tag_count = 0;
				    				foreach ($video->tags as $key => $tag) {
				    					if ($tag_count < 11) {
                                            $tags_array[] = $tag;
                                        }
                                        $tag_count++;
				    				}
				    				$tags = implode(',', $tags_array);
				    			}
					            $duration = '00:00';
					            if (!empty($video->duration)) {
					                $duration = gmdate("i:s", $video->duration);
					            }
	                        	$array_data = array(
	                        		'ID' => $video->id,
	                        		'TITLE' => $video->title,
	                        		'DESC' => $video->description,
	                        		'THUMB' => $thumb,
	                        		'TAGS' => $tags,
	                        		'DURATION' => $duration
	                        	);
	                        	echo PT_LoadAdminPage('import-from-dailymotion/list', $array_data);
	                        }
                        } else {
                        	echo '<div class="no-data-found">No videos found, try another page.</div>';
                        }
                        ?>

                   </div>
                   <div class="clearfix"></div>
                   <?php if (!empty($ids)) { ?>
                    <div class="text-center">
                		<button class="btn btn-info waves-effect waves-light btn-lg btn-load-more-videos" data-token="<?php echo $page_id?>">LOAD <?php echo $limit ?> MORE VIDEOS</button>
                		<button class="btn btn-success waves-effect waves-light btn-lg btn-import">IMPORT SELECTED <span></span></button>
                	</div>
                    <?php } ?>
                </div>
            </div>
            <?php } ?>
            <?php if (isset($_GET['finished']) && !empty($_SESSION['imported-videos'])) { ?>
            <div class="card">
                <div class="header bg-light-green">
                    <h2><i class="fa fa-check"></i> Auto Import Successfully Completed!</h2>
                </div>
                <div class="body">
                   <?php echo (!empty($_SESSION['imported-videos'])) ? number_format($_SESSION['imported-videos']) : ''; ?> videos successfully imported.
                </div>
            </div>
            <?php unset($_SESSION['imported-videos']); } ?>
            <?php if ($respond_error == 1 && !empty($error_text)) { ?>
            <div class="card">
                <div class="header bg-red">
                    <h2><i class="fa fa-times"></i> Error found while fetching the videos!</h2>
                </div>
                <div class="body">
                   <?php echo $error_text?>
                </div>
            </div>
            <?php } ?>
        </div>
        <div class="clearfix"></div>
    </div>
    <!-- #END# Vertical Layout -->
<div id="import-modal" class="modal fade" role="dialog" data-backdrop="static" data-keyboard="false">
  <div class="modal-dialog">
    <div class="modal-content modal-col-teal">
      <div class="modal-body text-center">
        <h4><p class="loading">Importing <?php if (isset($_SESSION['keyword-id'])) { echo (!empty($explode[$_SESSION['keyword-id']])) ? '"' . $explode[$_SESSION['keyword-id']] . '"' : '';} ?></p></h4>
      </div>
    </div>
  </div>
</div>
<script>
$('#auto_import').change(function(event) {
  if ($(this).val() == 1) {
    $('#multi-query-form').removeClass('hidden');
    $('#normal-query-form').addClass('hidden');
    $('#query').val('');
  } else {
    $('#multi-query-form').addClass('hidden');
    $('#multi-query').val('');
    $('#normal-query-form').removeClass('hidden');
  }
});
$('.btn-load-more-videos').on('click' , function(event) {
	event.preventDefault();
	var token = $(this).attr('data-token');
	$('.btn-load-more-videos').text('Loading..');
	$.post('{{LINK aj/ap/load-more-daily}}', {pageToken: token, query: $('#query').val(), limit: $('#limit').val() }, function(data, textStatus, xhr) {
		if (data.status == 200) {
			$('#videos-layout').append(data.html);
			$('.btn-load-more-videos').attr('data-token', data.token);
			$('.btn-load-more-videos').text('LOAD <?php echo $limit ?> MORE VIDEOS');
			$(".form-line").addClass("focused");
			$('.btn-import').find('span').text(' (' + $('.import-checkbox-input:checked').size() + ')');
		} else {
			$('.btn-load-more-videos').text('No more videos found');
		}
	});
});
$('.import-checkbox-input, .check-all').change(function(event) {
    $('.btn-import').find('span').text(' (' + $('.import-checkbox-input:checked').size() + ')');
});
$(function () {
	$('.btn-import').find('span').text(' (' + $('.import-checkbox-input:checked').size() + ')');
    <?php if ($auto_import == 1 && !empty($ids)) {?>
    setTimeout(function () {
        $('.btn-import').trigger('click');
    }, 3000);
    <?php } ?>
    <?php if (empty($ids) && !empty($_SESSION['keyword-id'])) {?>
      $('#page-query').val(1);
      $('form').submit();
    <?php } ?>
});
$('.btn-import').on('click', function(event) {
    data = new Array();
    $('.btn-import').attr('disabled', true);
    $('.btn-import').text('Please wait..');
    $('#import-modal').modal('show');
    $('.youtube-video').each(function(index, el) {
    	var video_id = $(this).attr('data-id');
    	var is_checked = $(this).find('.import-checkbox-input').is(":checked");
    	if (is_checked) {
    		object_video = {
    			video_id: video_id, 
    			title: $(this).find('#title').val(), 
    			description: $(this).find('#description').val(),
    			duration: $(this).find('#duration').val(),
    			tags: $(this).find('#tags').val(),
    			thumb: $(this).find('#thumb').val(),
    		}
    		data.push(object_video);
    	}
    });
    $.post('{{LINK aj/ap/import-daily-videos}}', {videos: data, category_id: $('#category_id').val()}, function () {
        $('#import-modal').find('.modal-content').removeClass('modal-col-teal').addClass('modal-col-light-green');
        $('#import-modal').find('.loading').html('<i class="fa fa-check"></i> Videos successfully imported, redirecting to next page');
        var val = Number($('#page-query').val());
        if (val < 100) {
        	$('#page-query').val(val + 1);
        	$('form').submit();
        } else {
        	$('#page-query').val(1);
        	$('form').submit();
        }
    });
});
</script>

<style>

p {
  color: white;
  font: 30px;
  text-align: center;}


/* loading dots */

.loading:after {
  content: ' .';
  animation: dots 1s steps(5, end) infinite;}

@keyframes dots {
  0%, 20% {
    color: rgba(0,0,0,0);
    text-shadow:
      .25em 0 0 rgba(0,0,0,0),
      .5em 0 0 rgba(0,0,0,0);}
  40% {
    color: white;
    text-shadow:
      .25em 0 0 rgba(0,0,0,0),
      .5em 0 0 rgba(0,0,0,0);}
  60% {
    text-shadow:
      .25em 0 0 white,
      .5em 0 0 rgba(0,0,0,0);}
  80%, 100% {
    text-shadow:
      .25em 0 0 white,
      .5em 0 0 white;}}

</style>