Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 39 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| VideosController | |
0.00% |
0 / 39 |
|
0.00% |
0 / 1 |
156 | |
0.00% |
0 / 1 |
| videoSelect | |
0.00% |
0 / 39 |
|
0.00% |
0 / 1 |
156 | |||
| 1 | <?php |
| 2 | // src/Controller/Admin/VideosController.php |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Controller\Admin; |
| 6 | |
| 7 | use App\Controller\AppController; |
| 8 | use App\Controller\Component\MediaPickerTrait; |
| 9 | use App\Utility\SettingsManager; |
| 10 | use Exception; |
| 11 | use Google_Client; |
| 12 | use Google_Service_YouTube; |
| 13 | |
| 14 | class VideosController extends AppController |
| 15 | { |
| 16 | use MediaPickerTrait; |
| 17 | |
| 18 | /** |
| 19 | * Video selection interface with YouTube API integration |
| 20 | * |
| 21 | * @return void |
| 22 | */ |
| 23 | public function videoSelect(): void |
| 24 | { |
| 25 | $this->viewBuilder()->setLayout('ajax'); |
| 26 | |
| 27 | $searchTerm = $this->request->getQuery('search'); |
| 28 | $filterByChannel = filter_var($this->request->getQuery('channel_filter'), FILTER_VALIDATE_BOOLEAN); |
| 29 | $videos = []; |
| 30 | |
| 31 | // If no explicit channel_filter parameter is provided, default to true (show channel videos) |
| 32 | $channelId = SettingsManager::read('Google.youtubeChannelId', env('YOUTUBE_CHANNEL_ID')); |
| 33 | if ($this->request->getQuery('channel_filter') === null && $channelId && $channelId !== 'your-api-key-here') { |
| 34 | $filterByChannel = true; |
| 35 | } |
| 36 | |
| 37 | if ($searchTerm || $filterByChannel) { |
| 38 | $client = new Google_Client(); |
| 39 | $apiKey = SettingsManager::read('Google.youtubeApiKey', env('YOUTUBE_API_KEY')); |
| 40 | $client->setDeveloperKey($apiKey); |
| 41 | |
| 42 | $youtube = new Google_Service_YouTube($client); |
| 43 | |
| 44 | try { |
| 45 | $searchParams = [ |
| 46 | 'maxResults' => 12, |
| 47 | 'type' => 'video', |
| 48 | 'order' => 'date', |
| 49 | ]; |
| 50 | |
| 51 | // Only add search term if it exists |
| 52 | if ($searchTerm) { |
| 53 | $searchParams['q'] = $searchTerm; |
| 54 | } |
| 55 | |
| 56 | // Always check for channel filter regardless of search term |
| 57 | if ($filterByChannel) { |
| 58 | $channelId = SettingsManager::read('Google.youtubeChannelId', env('YOUTUBE_CHANNEL_ID')); |
| 59 | if ($channelId) { |
| 60 | $searchParams['channelId'] = $channelId; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | $searchResponse = $youtube->search->listSearch('snippet', $searchParams); |
| 65 | |
| 66 | foreach ($searchResponse['items'] as $searchResult) { |
| 67 | $videos[] = [ |
| 68 | 'id' => $searchResult['id']['videoId'], |
| 69 | 'title' => $searchResult['snippet']['title'], |
| 70 | 'thumbnail' => $searchResult['snippet']['thumbnails']['medium']['url'], |
| 71 | 'description' => $searchResult['snippet']['description'], |
| 72 | ]; |
| 73 | } |
| 74 | } catch (Exception $e) { |
| 75 | $this->Flash->error(__('Error fetching videos: {0}', $e->getMessage())); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | $channelId = SettingsManager::read('Google.youtubeChannelId', env('YOUTUBE_CHANNEL_ID')); |
| 80 | $this->set(compact('videos', 'searchTerm', 'filterByChannel', 'channelId')); |
| 81 | |
| 82 | // Check if this is a search request that should only return results HTML |
| 83 | $galleryOnly = $this->request->getQuery('gallery_only'); |
| 84 | if ($galleryOnly) { |
| 85 | // For search requests, only return the results portion to avoid flicker |
| 86 | $this->viewBuilder()->setTemplate('video_select_results'); |
| 87 | } else { |
| 88 | // For initial load, return the full template with search form |
| 89 | $this->viewBuilder()->setTemplate('video_select'); |
| 90 | } |
| 91 | } |
| 92 | } |