YaWK  24.1
Yet another WebKit
youtube.php
Go to the documentation of this file.
1 <?php
3 {
4  /**
5  * @details<b>Embed any Facebook Video on your page.</b>
6  *
7  * <p>With this widget, you are able to embed any public YouTube Video that you like. Simply enter the
8  * Video URL, set a few settings and you're good to go. You do not need to play around with html or
9  * any YouTube embed code. This widget does all the work for you!</p>
10  *
11  *
12  * @author Daniel Retzl <[email protected]>
13  * @copyright 2018 Daniel Retzl
14  * @version 1.0.0
15  * @brief Embed any YouTube Video on your pages.
16  */
17  class youtube
18  {
19  /** @param object global widget object data */
20  public $widget = '';
21  /** @param string YouTube Video URL */
22  public $youtubeVideoUrl = '';
23  /** @param bool true|false Allow embeded full screen mode? */
24  public $youtubeFullscreen = true;
25  /** @param bool string html markup for fullscreen mode */
26  public $youtubeFullscreenMarkup = '';
27  /** @param bool true|false Should the video automatically start on page load? */
28  public $youtubeAutoplay = true;
29  /** @param bool string autoplay html markup */
30  public $youtubeAutoplayMarkup = '';
31  /** @param string Video Height */
32  public $youtubeHeight = '720';
33  /** @param string Video Width */
34  public $youtubeWidth = '100%';
35  /** @param string Heading above video */
36  public $youtubeHeading = '';
37  /** @param string Subtext beside heading */
38  public $youtubeSubtext = '';
39  /** @param string Description below heading */
40  public $youtubeDescription = '';
41  /** @param string CSS Class */
42  public $youtubeCssClass = '';
43  /** @param string heading markup above video */
44  public $headlineMarkup = '';
45  /** @param string description markup below video */
46  public $descriptionMarkup = '';
47  /** @param string css markup */
48  public $cssMarkup = '';
49 
50  /**
51  * @details Load all widget settings from database and fill object params
52  * @param object $db Database Object
53  * @brief Load all widget settings on object init.
54  */
55  public function __construct($db)
56  {
57  // load this widget settings from db
58  $this->widget = new \YAWK\widget();
60  foreach ($settings as $property => $value) {
61  $this->$property = $value;
62  }
63  }
64 
65  /**
66  * @brief Print all object data
67  * @details (for development and testing purpose)
68  */
69  public function printObject()
70  {
71  echo "<pre>";
72  print_r($this);
73  echo "</pre>";
74  }
75 
76  /**
77  * @brief Check settings and embed the YouTube Video
78  * @details This method does the setup and embed job
79  */
80  public function embedVideo()
81  {
82  // check if full screen is allowed
83  if ($this->youtubeFullscreen == "true")
84  { // set full screen html markup
85  $this->youtubeFullscreenMarkup = "allowfullscreen=\"true\"";
86  }
87  else
88  { // no full screen, no markup
89  $this->youtubeFullscreenMarkup = '';
90  }
91 
92  // check if autoplay is allowed
93  if ($this->youtubeAutoplay == "true")
94  { // set autoplay markup
95  $this->youtubeAutoplayMarkup = "?autoplay=1";
96  }
97  else
98  { // no autoplay, no markup
99  $this->youtubeAutoplayMarkup = '';
100  }
101 
102  // check if a class is set
103  if (isset($this->youtubeCssClass) && (!empty($this->youtubeCssClass)))
104  {
105  $this->cssMarkup = " class=\"$this->youtubeCssClass\"";
106  }
107  else
108  {
109  $this->cssMarkup = '';
110  }
111 
112  // if a heading is set and not empty
113  if (isset($this->youtubeHeading) && (!empty($this->youtubeHeading)))
114  {
115  // check if subtext is set, add <small> subtext to string
116  if (isset($this->youtubeSubtext) && (!empty($this->youtubeSubtext)))
117  { // build a headline with heading and subtext
118  $this->youtubeSubtext = "<small>$this->youtubeSubtext</small>";
119  $this->headlineMarkup = "<h1>$this->youtubeHeading&nbsp;$this->youtubeSubtext</h1>";
120  }
121  else
122  { // build headline - without subtext
123  $this->headlineMarkup = "<h1>$this->youtubeHeading</h1>"; // draw just the heading
124  }
125  }
126  else
127  { // leave empty if it's not set
128  $this->headlineMarkup = '';
129  }
130 
131  // if description is set, add <p> to string
132  if (isset($this->youtubeDescription) && (!empty($this->youtubeDescription)))
133  { // set description string
134  $this->descriptionMarkup = "<p>$this->youtubeDescription</p>";
135  }
136  else
137  { // no description is set
138  $this->descriptionMarkup = '';
139  }
140 
141  // switch plain youtube url to correct embed url string
142  $this->youtubeVideoUrl = str_replace("watch?v=","embed/",$this->youtubeVideoUrl.$this->youtubeAutoplayMarkup);
143 
144 
145  // css style to make it responsive
146  echo "
147 
148 <style>
149 .videoWrapper {
150  position: relative;
151  padding-bottom: 56.25%; /* 16:9 */
152  padding-top: 25px;
153  height: 0;
154 }
155 .videoWrapper iframe {
156  position: absolute;
157  top: 0;
158  left: 0;
159  width: 100%;
160  height: 100%;
161 }
162 </style>";
163 
164  // YouTube Video Embed HTML markup
165  echo "
166 $this->headlineMarkup
167 <!-- youtube video wrapper -->
168 <div class=\"videoWrapper\">
169 <!-- youtube video iframe -->
170 <iframe width=\"$this->youtubeWidth\"
171  height=\"$this->youtubeHeight\"
172  src=\"$this->youtubeVideoUrl\"
173  frameborder=\"0\"
174  scrolling=\"no\"
175  $this->youtubeFullscreenMarkup
176  $this->cssMarkup>
177 </iframe>
178 $this->descriptionMarkup
179 <!-- end video wrapper -->
180 </div>";
181  }
182  }
183 }
Embed any YouTube Video on your pages.
Definition: youtube.php:17
__construct($db)
Load all widget settings on object init.
Definition: youtube.php:54
embedVideo()
Check settings and embed the YouTube Video.
Definition: youtube.php:79
printObject()
Print all object data.
Definition: youtube.php:68
Widgets are small, useful tools that you can include everywhere in your website.
Definition: widget.php:22
getWidgetSettingsArray($db)
Get widget settings and return it as array.
Definition: widget.php:69
if(isset($_POST['save'])) $settings
$value