Source of file SilverStripeYouTubeVideo.php
Size: 5,877 Bytes - Last Modified: 2021-12-23T10:05:44+00:00
/var/www/docs.ssmods.com/process/src/code/dataobjects/SilverStripeYouTubeVideo.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 | <?php /** * Class SilverStripeSilverStripeYouTubeVideo * * @property string $Title * @property string $YouTubeURL * @property string $VideoID */ class SilverStripeYouTubeVideo extends YouTubeDataObject { /** * @var string */ private static $singular_name = 'YouTube Video'; /** * @var string */ private static $plural_name = 'YouTube Videos'; /** * @var string */ private static $description = 'A video from YouTube. The video can be a single video, or a video from a playlist.'; /** * @var array */ private static $db = [ 'VideoID' => 'Varchar(11)', ]; /** * @var array */ private static $belongs_many_many = [ 'Playlists' => 'YouTubeVideoPlaylist', 'VideoPages' => 'YouTubeIntegrationVideosPage', ]; /** * @var array */ private static $casting = [ 'Likes' => 'Int', ]; /** * @var int */ private $likes; /** * @var */ private $video_data; /** * @var int|bool */ private $views; /** * @var array */ private static $indexes = [ 'VideoID' => true, ]; /** * @var array */ public static $allowed_actions = [ 'VideoDataValue', ]; /** * @return FieldList */ public function getCMSFields() { $fields = parent::getCMSFields(); $value = $this->VideoID ? $this->VideoID : 'Video ID not available'; $fields->replaceField('VideoID', ReadonlyField::create('VideoID')->setValue($value)); $fields->removeByName([ 'Playlists', 'VideoPages', ]); if ($this->ID > 0) { $playlistConfig = GridFieldConfig_RecordViewer::create(); $playlists = GridField::create('Playlists', 'Custom Playlists', $this->Playlists(), $playlistConfig); $fields->addFieldToTab('Root.Playlists', $playlists); $pagesConfig = GridFieldConfig_RecordViewer::create(); $pages = GridField::create('VideoPages', 'Video Pages', $this->VideoPages(), $pagesConfig); $fields->addFieldtoTab('Root.VideoPages', $pages); } $this->extend('updateCMSFields', $fields); return $fields; } /** * @return ValidationResult */ public function validate() { $result = parent::validate(); if (!$this->YouTubeURL) { $result->error('A Video URL is required'); } if (!$id = $this->extractVideoID($this->YouTubeURL)) { $result->error('The Video URL supplied doesn\'t seem to match the YouTube video url pattern.'); } if ($video = SilverStripeYouTubeVideo::get()->filter('VideoID', $id)->exclude('ID', $this->ID)->first()) { $videoLink = "/admin/youtube-admin/SilverStripeYouTubeVideo/EditForm/field/SilverStripeYouTubeVideo/item/{$video->ID}/edit"; $result->error("A video with that YouTube ID already exists. <a href='{$videoLink}'>{$video->Title}</a>"); } if (!$this->getYouTubeClient()->getVideoInfo($id)) { $result->error('The video cannot be processed from YouTube'); } return $result; } /** * Before writing the record, save the video ID for easier access later. */ public function onBeforeWrite() { parent::onBeforeWrite(); $this->VideoID = $this->extractVideoID($this->YouTubeURL); } /** * Check provided url for known YouTube url pattern. {@link https://ctrlq.org/code/19797-regex-youtube-id} * * @param string $url * @return string|bool */ protected function extractVideoID($url = '') { $urlParts = parse_url($url); parse_str($urlParts['query'], $variables); return isset($variables['v']) ? $variables['v'] : false; } /** * Return total like count for this video from YouTube * * @return int */ public function getLikes() { if (!$this->likes) { $this->setLikes(); } return $this->likes; } /** * Set total like count for this video from YouTube * * @return $this */ public function setLikes() { $this->likes = $this->DataValue('statistics.likeCount'); return $this; } /** * Return Video Embed code via a SilverStripe include * * @return HTMLText */ public function getEmbedCode() { return $this->customise([ 'VideoID' => $this->VideoID, ])->renderWith('EmbedCode'); } /** * Video preview, the base embed provided by the YouTube API * * @return mixed */ public function getVideoCMSPreview() { return $this->DataValue('player.embedHtml'); } /** * Set YouTube video data * * @return $this */ public function setYouTubeData() { $data = parent::getYouTubeData(); $this->video_data = array_merge(static::data_to_array($this->getYouTubeClient()->getVideoInfo($this->VideoID)), $data); return $this; } /** * Return this video's YouTube data * * @return mixed */ public function getYouTubeData() { if (!$this->video_data) { $this->setYouTubeData(); } return $this->video_data; } /** * Return this video's View count * * @return bool|int */ public function getViews() { if (!$this->views) { $this->setViews(); } return $this->views; } /** * Set this video's View count * * @return $this */ public function setViews() { $this->views = $this->DataValue('statistics.viewCount'); return $this; } } |