Skip to content

Commit

Permalink
Add Video Widget
Browse files Browse the repository at this point in the history
  • Loading branch information
sonvnn committed Jul 24, 2024
1 parent 5e6a280 commit b0df665
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 2 deletions.
134 changes: 134 additions & 0 deletions framework/elements/video/video.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

/**
* @package Astroid Framework
* @author Astroid Framework Team https://astroidframe.work
* @copyright Copyright (C) 2023 AstroidFrame.work.
* @license https://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or Later
* DO NOT MODIFY THIS FILE DIRECTLY AS IT WILL BE OVERWRITTEN IN THE NEXT UPDATE
* You can easily override all files under /astroid/ folder.
* Just copy the file to JROOT/templates/YOUR_ASTROID_TEMPLATE/astroid/elements/module_position/module_position.php folder to create and override
*/

// No direct access.
defined('_JEXEC') or die;
extract($displayData);
use Joomla\CMS\Uri\Uri;
use Astroid\Helper\Media;

$video_type = $params->get('video_type', 'url');
$url = $params->get('url', '');
$mp4_video = $params->get('mp4_video', '');
$ogv_video = $params->get('ogv_video', '');

$no_cookie = $params->get('no_cookie', 0);
$show_rel_video = $params->get('show_rel_video', 0);
$youtube_shorts = $params->get('youtube_shorts', 0);
$aspect_ratio = $params->get('aspect_ratio', '16x9');

$vimeo_show_author = 'byline=' . $params->get('vimeo_show_author', 0);
$vimeo_mute_video = 'muted=' . $params->get('vimeo_mute_video', 1);
$vimeo_show_author_profile = 'portrait=' . $params->get('vimeo_show_author_profile', 0);
$vimeo_show_video_title = 'title=' . $params->get('vimeo_show_video_title', 0);

$show_control = $params->get('show_control', 1);
$video_loop = $params->get('video_loop', 1);
$video_mute = $params->get('video_mute', 1);
$autoplay_video = $params->get('autoplay_video', 1);
$video_poster = $params->get('video_poster', '');
if ($video_poster) {
$video_poster = Uri::base(true) . '/' . Media::getPath() . '/' . $video_poster;
}
$video_title = "";

if (!(!empty($mp4_video) && (strpos($mp4_video, "http://") !== false || strpos($mp4_video, "https://") !== false)))
{
$mp4_video = Uri::base(true) . '/' . Media::getPath() . '/' . $mp4_video;
}

if (!(!empty($ogv_video) && (strpos($ogv_video, "http://") !== false || strpos($ogv_video, "https://") !== false)))
{
$ogv_video = Uri::base(true) . '/' . Media::getPath() . '/' . $ogv_video;
}

if ($url)
{
$video = parse_url($url);

$youtube_no_cookie = $no_cookie ? '-nocookie' : '';

if(array_key_exists('host', $video)) {
switch ($video['host'])
{
case 'youtu.be':
$id = trim($video['path'], '/');
$src = '//www.youtube' . $youtube_no_cookie . '.com/embed/' . $id . '?iv_load_policy=3' . $show_rel_video;
$video_title = $params->get('video_title', 'Youtube Video');
break;

case 'www.youtube.com':
case 'youtube.com':
$query = [];
$playlist_id = null;
if(array_key_exists('query', $video)) {
parse_str($video['query'], $query);
}

if($video['path'] === '/playlist') {
if (preg_match('/\blist=([^&]+)/', $video['query'], $matches)) {
$playlist_id = $matches[1];
}
}

$src = '';

if($playlist_id) {
$src = '//www.youtube.com/embed/?listType=playlist&list=' . $playlist_id;
} else {
$id = ($youtube_shorts) ? str_replace('/shorts/', "", $video['path']) : $query['v'];
$src = '//www.youtube' . $youtube_no_cookie . '.com/embed/' . $id . '?iv_load_policy=3' . $show_rel_video;
}

$video_title = $params->get('video_title', 'Youtube Video');
break;

case 'vimeo.com':
case 'www.vimeo.com':
case 'player.vimeo.com':
$initialSrc = $url;

if($video['host'] !== 'player.vimeo.com') {
$id = trim($video['path'], '/');
$initialSrc = "//player.vimeo.com/video/{$id}";
}

$src = $initialSrc . '?' . implode('&', array($vimeo_mute_video, $vimeo_show_author, $vimeo_show_author_profile, $vimeo_show_video_title, 'autoplay='. $autoplay_video, 'loop='. $video_loop, 'controls='. $show_control));
break;
}
}
}

if ($video_type == 'url')
{
echo '<div class="as-video-url-block ratio ratio-'.$aspect_ratio.'">';
echo '<iframe class="as-video-item" src="' . $src . '" ' . ($video_title ? 'title="'.$video_title. '"' : '') . ' allow="accelerometer" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
echo '</div>';
}
else
{
if ($mp4_video || $ogv_video)
{
echo '<div class="as-video-local-wrap">';
echo '<video class="as-video-local-source ratio ratio-'.$aspect_ratio.'"' . ($video_loop != 0 ? ' loop' : '') . ($autoplay_video != 0 ? ' autoplay' : '') . ($show_control != 0 ? ' controls' : '') . ($video_mute != 0 ? ' muted' : '') . ' poster="' . $video_poster . '" controlsList="nodownload" playsinline>';
if (!empty($mp4_video))
{
echo '<source src="' . $mp4_video . '" type="video/mp4">';
}
if (!empty($ogv_video))
{
echo '<source src="' . $ogv_video . '" type="video/ogg">';
}
echo '</video>';
echo '</div>';
}
}
51 changes: 51 additions & 0 deletions framework/elements/video/video.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<element>
<title>ASTROID_WIDGET_VIDEO_LABEL</title>
<description>ASTROID_WIDGET_VIDEO_LABEL</description>
<icon>fa-solid fa-video</icon>
<category>ASTROID_ELEMENT_CATEGORY_MEDIA</category>
<element_type>widget</element_type>
<form>
<fields>
<fieldset name="general-settings" label="TPL_ASTROID_GENERAL_SETTINGS_LABEL" addfieldpath="/libraries/astroid/framework/fields">
<field type="astroidgroup" name="widget_styles" title="ASTROID_WIDGET_STYLES_LABEL"/>

<field astroidgroup="general" type="astroidlist" name="video_type" label="ASTROID_WIDGET_VIDEO_TYPE" default="url">
<option value="url">ASTROID_WIDGET_VIDEO_URL</option>
<option value="local">ASTROID_WIDGET_VIDEO_LOCAL</option>
</field>
<field astroidgroup="general" ngShow="[video_type]=='url'" type="astroidtext" label="ASTROID_WIDGET_VIDEO_BUTTON_URL_LABEL" name="url" hint="https://www.youtube.com/watch?v=xxxxx"/>
<field astroidgroup="general" ngShow="[video_type]=='local'" name="mp4_video" type="astroidmedia" media="videos" label="ASTROID_WIDGET_VIDEO_MP4"/>
<field astroidgroup="general" ngShow="[video_type]=='local'" name="ogv_video" type="astroidmedia" media="videos" label="ASTROID_WIDGET_VIDEO_OGV"/>
<field astroidgroup="widget_styles" ngShow="[video_type]=='url'" type="astroidtext" label="ASTROID_WIDGET_VIDEO_TITLE" name="video_title"/>
<field astroidgroup="widget_styles" ngShow="[video_type]=='url'" name="show_rel_video" type="astroidradio" astroid-switch="true" default="0" label="ASTROID_WIDGET_SHOW_RELATED_VIDEOS"/>
<field astroidgroup="widget_styles" ngShow="[video_type]=='url'" name="no_cookie" type="astroidradio" astroid-switch="true" default="0" label="ASTROID_WIDGET_NO_COOKIE"/>
<field astroidgroup="widget_styles" ngShow="[video_type]=='url'" name="youtube_shorts" type="astroidradio" astroid-switch="true" default="0" label="ASTROID_WIDGET_ADD_YOUTUBE_SHORTS"/>
<field astroidgroup="widget_styles" ngShow="[video_type]=='url' AND [youtube_shorts]==1" type="astroidlist" name="aspect_ratio" label="ASTROID_WIDGET_YOUTUBE_ASPECT_RATIO" default="16x9">
<option value="1x1">1x1</option>
<option value="4x3">4x3</option>
<option value="16x9">16x9</option>
<option value="21x9">21x9</option>
</field>
<field astroidgroup="widget_styles" ngShow="[video_type]=='url'" name="vimeo_show_author" type="astroidradio" astroid-switch="true" default="0" label="ASTROID_WIDGET_VIMEO_SHOW_AUTHOR"/>
<field astroidgroup="widget_styles" ngShow="[video_type]=='url'" name="vimeo_mute_video" type="astroidradio" astroid-switch="true" default="1" label="ASTROID_WIDGET_VIMEO_MUTE_VIDEO"/>
<field astroidgroup="widget_styles" ngShow="[video_type]=='url'" name="vimeo_show_author_profile" type="astroidradio" astroid-switch="true" default="0" label="ASTROID_WIDGET_VIMEO_SHOW_AUTHOR_PROFILE"/>
<field astroidgroup="widget_styles" ngShow="[video_type]=='url'" name="vimeo_show_video_title" type="astroidradio" astroid-switch="true" default="0" label="ASTROID_WIDGET_VIMEO_SHOW_TITLE"/>

<field astroidgroup="widget_styles" name="show_control" type="astroidradio" astroid-switch="true" default="1" label="ASTROID_WIDGET_VIDEO_SHOW_CONTROL"/>
<field astroidgroup="widget_styles" name="video_loop" type="astroidradio" astroid-switch="true" default="1" label="ASTROID_WIDGET_VIDEO_VIDEO_LOOP"/>
<field astroidgroup="widget_styles" ngShow="[video_type]=='local'" name="video_mute" type="astroidradio" astroid-switch="true" default="1" label="ASTROID_WIDGET_VIDEO_VIDEO_MUTE"/>
<field astroidgroup="widget_styles" name="autoplay_video" type="astroidradio" astroid-switch="true" default="1" label="ASTROID_WIDGET_AUTOPLAY"/>
<field astroidgroup="widget_styles" ngShow="[video_type]=='local'" name="video_poster" type="astroidmedia" label="ASTROID_WIDGET_VIDEO_POSTER"/>
</fieldset>
<fieldset name="assignment-settings" label="TPL_ASTROID_ASSIGNMENT_SETTINGS_LABEL" addfieldpath="/libraries/astroid/framework/fields" articleData="false">
<field type="astroidlist" name="assignment_type" label="ASTROID_WIDGET_ASSIGN" default="1">
<option value="1">ASTROID_WIDGET_ON_ALL_PAGES</option>
<option value="0">ASTROID_WIDGET_NO_PAGES</option>
<option value="2">ASTROID_WIDGET_SELECTED_PAGES</option>
</field>
<field ngShow="[assignment_type]==2" type="astroidassignment" label="ASTROID_ASSIGN_TO_MENU_ITEM" name="assignment"/>
</fieldset>
</fields>
</form>
</element>
4 changes: 2 additions & 2 deletions framework/elements/video_button/video_button.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
<element>
<title>ASTROID_WIDGET_VIDEO_BUTTON_LABEL</title>
<description>ASTROID_WIDGET_VIDEO_BUTTON_DESC</description>
<icon>fa-solid fa-video</icon>
<icon>fa-solid fa-play</icon>
<category>ASTROID_ELEMENT_CATEGORY_MEDIA</category>
<element_type>widget</element_type>
<form>
<fields>
<fieldset name="general-settings" label="TPL_ASTROID_GENERAL_SETTINGS_LABEL" addfieldpath="/libraries/astroid/framework/fields">
<field type="astroidgroup" name="widget_styles" title="ASTROID_WIDGET_STYLES_LABEL"/>
<field astroidgroup="general" type="astroidtext" label="ASTROID_WIDGET_VIDEO_BUTTON_URL_LABEL" name="url" hint="https://www.youtube.com/@astroidframework2932"/>
<field astroidgroup="general" type="astroidtext" label="ASTROID_WIDGET_VIDEO_BUTTON_URL_LABEL" name="url" hint="https://www.youtube.com/watch?v=xxxxx"/>
<field astroidgroup="widget_styles" name="button_size" type="astroidrange" min="1" max="300" step="1" postfix="px" default="24" label="ASTROID_WIDGET_BUTTON_SIZE_LABEL" />
<field astroidgroup="widget_styles" name="ripple_color" type="astroidcolor" label="ASTROID_WIDGET_RIPPLE_COLOR_LABEL"/>
<field astroidgroup="widget_styles" name="width" type="astroidrange" min="10" max="500" step="1" postfix="px" default="150" label="ASTROID_WIDGET_WIDTH_LABEL" />
Expand Down
21 changes: 21 additions & 0 deletions language/en-GB/en-GB.astroid.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1414,6 +1414,27 @@ ASTROID_WIDGET_VIDEO_BUTTON_URL_LABEL="Video Button URL"
ASTROID_WIDGET_RIPPLE_COLOR_LABEL="Ripple Color"
ASTROID_WIDGET_BUTTON_SIZE_LABEL="Button Size"

; Video Widget
ASTROID_WIDGET_VIDEO_LABEL="Video"
ASTROID_WIDGET_VIDEO_TYPE="Video Type"
ASTROID_WIDGET_VIDEO_LOCAL="Local Video"
ASTROID_WIDGET_VIDEO_URL="Youtube/Vimeo"
ASTROID_WIDGET_VIDEO_TITLE="Video Title"
ASTROID_WIDGET_VIDEO_MP4="MP4 Video"
ASTROID_WIDGET_VIDEO_OGV="OGV Video"
ASTROID_WIDGET_SHOW_RELATED_VIDEOS="Show Related Videos"
ASTROID_WIDGET_NO_COOKIE="Turn Off Youtube Cookie"
ASTROID_WIDGET_ADD_YOUTUBE_SHORTS="Add YouTube Shorts"
ASTROID_WIDGET_YOUTUBE_ASPECT_RATIO="Aspect Ratio"
ASTROID_WIDGET_VIMEO_SHOW_AUTHOR="Show Author (Vimeo)"
ASTROID_WIDGET_VIMEO_SHOW_AUTHOR_PROFILE="Show Author’s Profile Image (Vimeo)"
ASTROID_WIDGET_VIMEO_MUTE_VIDEO="Mute Video (Vimeo)"
ASTROID_WIDGET_VIMEO_SHOW_TITLE="Show Video Title (Vimeo)"
ASTROID_WIDGET_VIDEO_SHOW_CONTROL="Show Control"
ASTROID_WIDGET_VIDEO_VIDEO_LOOP="Video Loop"
ASTROID_WIDGET_VIDEO_VIDEO_MUTE="Video Mute"
ASTROID_WIDGET_VIDEO_POSTER="Video Poster"

; Article Widget
ASTROID_WIDGET_ARTICLES_LABEL="Articles"
ASTROID_WIDGET_ARTICLES_DESC="Define different styles for Articles"
Expand Down

0 comments on commit b0df665

Please sign in to comment.