1 <?php
2 /**
3 * @copyright Copyright © 2amigOS! Consulting Group 2013-
4 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
5 * @package foundation.components
6 * @version 1.0.0
7 */
8 namespace foundation\widgets;
9
10 use foundation\helpers\Html;
11 use foundation\enum\Enum;
12 use foundation\exception\InvalidConfigException;
13
14 /**
15 * FlexVideo embeds a video from YouTube, Vimeo or another site that uses iframe, embed or object elements, creating
16 * an intrinsic ration that will properly scale the video to any device.
17 *
18 * @see http://foundation.zurb.com/docs/components/flex-video.html
19 *
20 * @author Antonio Ramirez <amigo.cobos@gmail.com>
21 * @package foundation\widgets
22 */
23 class FlexVideo extends base\Widget
24 {
25 /**
26 * @var string the source of the video
27 */
28 public $source;
29 /**
30 * @var int the width of the video
31 */
32 public $width = 420;
33 /**
34 * @var int the height of the video
35 */
36 public $height = 315;
37 /**
38 * @var bool whether to allow fullscreen or not
39 */
40 public $allowFullScreen = true;
41 /**
42 * @var bool whether to give the player a widescreen aspect ratio or not
43 */
44 public $widescreen = false;
45 /**
46 * @var bool whether to ensure not adding extra padding since Vimeo has control with the player, itself
47 */
48 public $vimeo = false;
49
50
51 /**
52 * Initializes the widget
53 * @throws InvalidConfigException
54 */
55 public function init()
56 {
57 if ($this->source === null) {
58 throw new InvalidConfigException('"source" cannot be null.');
59 }
60 Html::addCssClass($this->htmlOptions, Enum::VIDEO_FLEXVIDEO);
61 if ($this->widescreen) {
62 Html::addCssClass($this->htmlOptions, Enum::VIDEO_WIDESCREEN);
63 }
64 if ($this->vimeo) {
65 Html::addCssClass($this->htmlOptions, Enum::VIDEO_VIMEO);
66 }
67 parent::init();
68 }
69
70 /**
71 * Renders the widget
72 */
73 public function run()
74 {
75 echo $this->renderVideo();
76 }
77
78 /**
79 * Renders video
80 * @return string the resulting tag
81 */
82 public function renderVideo()
83 {
84 $iframe = \CHtml::tag(
85 'iframe',
86 array(
87 'height' => $this->height,
88 'width' => $this->width,
89 'src' => $this->source,
90 'allowfullscreen' => $this->allowFullScreen ? 'allowfullscreen' : null,
91 'frameborder' => 0,
92 )
93 );
94 return \CHtml::tag('div', $this->htmlOptions, $iframe);
95 }
96 }