|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://dukt.net/videos/ |
|
4
|
|
|
* @copyright Copyright (c) Dukt |
|
5
|
|
|
* @license https://github.com/dukt/videos/blob/v2/LICENSE.md |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace dukt\videos\controllers; |
|
9
|
|
|
|
|
10
|
|
|
use Craft; |
|
11
|
|
|
use craft\helpers\Json; |
|
12
|
|
|
use craft\web\Controller; |
|
13
|
|
|
use dukt\videos\errors\GatewayNotFoundException; |
|
14
|
|
|
use dukt\videos\helpers\VideosHelper; |
|
15
|
|
|
use dukt\videos\Plugin as Videos; |
|
16
|
|
|
use dukt\videos\Plugin; |
|
17
|
|
|
use yii\base\InvalidConfigException; |
|
18
|
|
|
use yii\web\Response; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Vue controller |
|
22
|
|
|
*/ |
|
23
|
|
|
class ExplorerController extends Controller |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @return Response |
|
27
|
|
|
* @throws InvalidConfigException |
|
28
|
|
|
*/ |
|
29
|
|
|
public function actionGetGateways(): Response |
|
30
|
|
|
{ |
|
31
|
|
|
$gateways = Videos::$plugin->getGateways()->getGateways(); |
|
32
|
|
|
|
|
33
|
|
|
$gatewaysArray = []; |
|
34
|
|
|
|
|
35
|
|
|
foreach ($gateways as $gateway) { |
|
36
|
|
|
$gatewaysArray[] = [ |
|
37
|
|
|
'name' => $gateway->getName(), |
|
38
|
|
|
'handle' => $gateway->getHandle(), |
|
39
|
|
|
'sections' => $gateway->getExplorerSections() |
|
40
|
|
|
]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $this->asJson($gatewaysArray); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return Response |
|
48
|
|
|
* @throws GatewayNotFoundException |
|
49
|
|
|
* @throws InvalidConfigException |
|
50
|
|
|
* @throws \dukt\videos\errors\GatewayMethodNotFoundException |
|
51
|
|
|
* @throws \yii\web\BadRequestHttpException |
|
52
|
|
|
*/ |
|
53
|
|
|
public function actionGetVideos(): Response |
|
54
|
|
|
{ |
|
55
|
|
|
$this->requireAcceptsJson(); |
|
56
|
|
|
|
|
57
|
|
|
$rawBody = Craft::$app->getRequest()->getRawBody(); |
|
58
|
|
|
$payload = Json::decodeIfJson($rawBody); |
|
59
|
|
|
|
|
60
|
|
|
$gatewayHandle = strtolower($payload['gateway']); |
|
61
|
|
|
$method = $payload['method']; |
|
62
|
|
|
$options = $payload['options'] ?? []; |
|
63
|
|
|
|
|
64
|
|
|
$gateway = Videos::$plugin->getGateways()->getGateway($gatewayHandle); |
|
65
|
|
|
|
|
66
|
|
|
if (!$gateway instanceof \dukt\videos\base\Gateway) { |
|
67
|
|
|
throw new GatewayNotFoundException('Gateway not found.'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$videosResponse = $gateway->getVideos($method, $options); |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
// Todo: Make this happen in the Video model toArray() |
|
74
|
|
|
|
|
75
|
|
|
$videos = []; |
|
76
|
|
|
|
|
77
|
|
|
foreach ($videosResponse['videos'] as $video) { |
|
78
|
|
|
$videos[] = VideosHelper::videoToArray($video); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $this->asJson([ |
|
82
|
|
|
'videos' => $videos, |
|
83
|
|
|
'more' => $videosResponse['more'], |
|
84
|
|
|
'moreToken' => $videosResponse['moreToken'] |
|
85
|
|
|
]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function actionGetVideo(): \yii\web\Response |
|
89
|
|
|
{ |
|
90
|
|
|
$this->requireAcceptsJson(); |
|
91
|
|
|
|
|
92
|
|
|
$rawBody = Craft::$app->getRequest()->getRawBody(); |
|
93
|
|
|
$payload = Json::decodeIfJson($rawBody); |
|
94
|
|
|
$url = $payload['url']; |
|
95
|
|
|
|
|
96
|
|
|
$video = Plugin::getInstance()->getVideos()->getVideoByUrl($url); |
|
97
|
|
|
|
|
98
|
|
|
if ($video === null) { |
|
99
|
|
|
return $this->asErrorJson(Craft::t('videos', 'Unable to find the video.')); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$videoArray = VideosHelper::videoToArray($video); |
|
103
|
|
|
|
|
104
|
|
|
return $this->asJson($videoArray); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function actionGetVideoEmbedHtml(): Response |
|
108
|
|
|
{ |
|
109
|
|
|
$this->requireAcceptsJson(); |
|
110
|
|
|
|
|
111
|
|
|
$rawBody = Craft::$app->getRequest()->getRawBody(); |
|
112
|
|
|
$payload = Json::decodeIfJson($rawBody); |
|
113
|
|
|
|
|
114
|
|
|
$gatewayHandle = strtolower($payload['gateway']); |
|
115
|
|
|
$videoId = $payload['videoId']; |
|
116
|
|
|
|
|
117
|
|
|
$video = Videos::$plugin->getVideos()->getVideoById($gatewayHandle, $videoId); |
|
118
|
|
|
|
|
119
|
|
|
$html = Craft::$app->getView()->renderTemplate('videos/_elements/embedHtml', [ |
|
120
|
|
|
'video' => $video |
|
121
|
|
|
]); |
|
122
|
|
|
|
|
123
|
|
|
return $this->asJson([ |
|
124
|
|
|
'html' => $html |
|
125
|
|
|
]); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|