135 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			135 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | ||
| defined('BASEPATH') OR exit('No direct script access allowed');
 | ||
| 
 | ||
| use Coduo\PHPHumanizer\DateTimeHumanizer;
 | ||
| 
 | ||
| class YoutubePlayerModel extends CI_Model
 | ||
| {
 | ||
| 
 | ||
|     public function __construct()
 | ||
|     {
 | ||
|         parent::__construct();
 | ||
|     }
 | ||
| 
 | ||
|     public function getRecommendVideos($id)
 | ||
|     {
 | ||
|         $data = $this->youtubePlayerModel->newestVids('15');
 | ||
| 
 | ||
|         $return = '';
 | ||
| 
 | ||
|         if (!empty($data)) {
 | ||
|             foreach ($data as $row) {
 | ||
|                 $videoId = $row['contentDetails'];
 | ||
|                 $videoId = $videoId['videoId'];
 | ||
|                 $title = $this->youtubePlayerModel->getInfos($videoId)['title'];
 | ||
| 
 | ||
|                 if ($id == $videoId) {
 | ||
|                     $return .= '<li>
 | ||
|                         <a onclick="switchVideo(\'' . $videoId . '\');" style="cursor: pointer;">
 | ||
|                             <div class="image-container active">
 | ||
|                                 <img src="http://img.youtube.com/vi/' . $videoId . '/maxresdefault.jpg" alt="' . $title . '" class="img-fluid">
 | ||
|                                 <div class="overlay"><span>' . $title . '</span></div>
 | ||
|                             </div>
 | ||
|                         </a>
 | ||
|                     </li>';
 | ||
|                 } else {
 | ||
|                     $return .= '<li>
 | ||
|                         <a onclick="switchVideo(\'' . $videoId . '\');" style="cursor: pointer;">
 | ||
|                             <div class="image-container">
 | ||
|                                 <img src="http://img.youtube.com/vi/' . $videoId . '/maxresdefault.jpg" alt="' . $title . '" class="img-fluid">
 | ||
|                                 <div class="overlay"><span>' . $title . '</span></div>
 | ||
|                             </div>
 | ||
|                         </a>
 | ||
|                     </li>';
 | ||
|                 }
 | ||
| 
 | ||
| 
 | ||
| //                if($id == $videoId) {
 | ||
| //                    $return .= '<a onClick="switchVideo(\'' . $videoId . '\');" class="btn btn-sm btn-default active raised">' .$title. '</a>';
 | ||
| //                } else {
 | ||
| //                    $return .= '<a onClick="switchVideo(\'' . $videoId . '\');" class="btn btn-sm btn-default raised">' .$title. '</a>';
 | ||
| //                }
 | ||
|             }
 | ||
|         } else {
 | ||
|             $return = null;
 | ||
|         }
 | ||
| 
 | ||
|         return $return;
 | ||
|     }
 | ||
| 
 | ||
|     public function newestVids($vidCount)
 | ||
|     {
 | ||
|         $data = $this->db->query('SELECT * FROM social_posts WHERE post_plattform = \'YouTube\' ORDER BY post_date DESC LIMIT ?', [$vidCount])->result_array();
 | ||
| 
 | ||
|         if (!empty($data)) {
 | ||
|             return $data;
 | ||
|         } else {
 | ||
|             return null;
 | ||
|         }
 | ||
|     }
 | ||
| 
 | ||
|     public function getInfos($id)
 | ||
|     {
 | ||
|         $data = json_decode(@file_get_contents('https://www.googleapis.com/youtube/v3/videos?part=snippet%2Cstatistics&hl=de&id=' . $id . '®ionCode=de&key=AIzaSyAZi6W9FODqd-bjlmV0sGJ8vjvSgFNTXVM'), true)['items'];
 | ||
|         $data = $data[0];
 | ||
|         $infos = $data['snippet'];
 | ||
|         $stats = $data['statistics'];
 | ||
| 
 | ||
|         $return = [];
 | ||
| 
 | ||
|         $return['title'] = $infos['title'];
 | ||
|         $description = htmlspecialchars($infos['description']);
 | ||
|         $descriptionList = explode(' ', $description);
 | ||
|         $description = '';
 | ||
|         foreach ($descriptionList as $item) {
 | ||
|             if (isset(parse_url($item)['scheme']) && isset(parse_url($item)['host'])) {
 | ||
|                 $description .= '<a href="' . $item . '" target="_blank">' . $item . '</a> ';
 | ||
|             } else {
 | ||
|                 if ($infos['channelTitle'] == 'KingOfDog') {
 | ||
|                     $items1 = ['✔️', '➕', '©'];
 | ||
|                     $items2 = ['</p><p>✔️', '<br>➕', '<br>©'];
 | ||
|                     $description .= str_replace($items1, $items2, $item) . ' ';
 | ||
|                 } else {
 | ||
|                     $description .= $item . ' ';
 | ||
|                 }
 | ||
|             }
 | ||
|         }
 | ||
|         $return['description'] = $description;
 | ||
|         $published = strtotime($infos['publishedAt']);
 | ||
|         $return['published'] = DateTimeHumanizer::difference(new \DateTime(), new \DateTime("@$published"), $_SESSION['site_lang']);
 | ||
|         $return['channelName'] = $infos['channelTitle'];
 | ||
|         $return['channelUrl'] = 'http://youtube.com/channel/' . $infos['channelId'];
 | ||
|         $return['views'] = number_format($stats['viewCount'], 0, ',', '.');
 | ||
|         $return['likes'] = $stats['likeCount'];
 | ||
|         $return['dislikes'] = $stats['dislikeCount'];
 | ||
|         $return['comments'] = number_format($stats['commentCount'], 0, ',', '.');
 | ||
|         $rateCount = intval($return['likes']) + intval($return['dislikes']);
 | ||
|         if (intval($return['likes']) > 0 && intval($return['dislikes']) > 0) {
 | ||
|             $return['likeWidth'] = intval($return['likes']) / $rateCount * 100;
 | ||
|             $return['dislikeWidth'] = intval($return['dislikes']) / $rateCount * 100;
 | ||
|             $return['grayWidth'] = 0;
 | ||
|         } elseif (intval($return['likes']) == 0 && intval($return['dislikes']) == 0) {
 | ||
|             $return['likeWidth'] = 0;
 | ||
|             $return['dislikeWidth'] = 0;
 | ||
|             $return['grayWidth'] = 100;
 | ||
|         } elseif (intval($return['likes']) == 0) {
 | ||
|             $return['likeWidth'] = 0;
 | ||
|             $return['dislikeWidth'] = 100;
 | ||
|             $return['grayWidth'] = 0;
 | ||
|         } else {
 | ||
|             $return['likeWidth'] = 100;
 | ||
|             $return['dislikeWidth'] = 0;
 | ||
|             $return['grayWidth'] = 0;
 | ||
|         }
 | ||
|         $return['thumbnails'] = $infos['thumbnails'];
 | ||
| 
 | ||
|         return $return;
 | ||
|     }
 | ||
| 
 | ||
|     public function checkVideo($videoID)
 | ||
|     {
 | ||
|         $data = json_decode(@file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=" . $videoID . "&key=AIzaSyAZi6W9FODqd-bjlmV0sGJ8vjvSgFNTXVM"));
 | ||
|         return $data->pageInfo->totalResults !== 0;
 | ||
|     }
 | ||
| }
 |