YaWK  24.1
Yet another WebKit
socialBar.php
Go to the documentation of this file.
1 <?php
3 {
4  /**
5  * @details<b>Social Bar - display icons with links to your social network accounts</b>
6  *
7  * <p>Embed Links to Twitter, Facebook, Instagram, Pinterest, YouTube and more.
8  * You can set any font awesome icon (or any other icon set that you have loaded
9  * in the assets section before). If a link is set, the corresponding icon will
10  * be drawn.</p>
11  *
12  *
13  * @author Daniel Retzl <[email protected]>
14  * @copyright 2018 Daniel Retzl
15  * @version 1.0.0
16  * @brief Social Bar display icons with links to your social network accounts
17  */
18  class socialBar extends \YAWK\widget
19  {
20  /** @param object global widget object data */
21  public $widget = '';
22  /** @param string Title that will be shown above widget */
23  public $socialBarHeading = '';
24  /** @param string Subtext will be displayed beside title */
25  public $socialBarSubtext = '';
26  /** @param string Facebook URL */
27  public $socialBarFacebookUrl = '';
28  /** @param string YouTube URL */
29  public $socialBarYouTubeUrl = '';
30  /** @param string Instagram URL */
31  public $socialBarInstagramUrl = '';
32  /** @param string Pinterest URL */
33  public $socialBarPinterestUrl = '';
34  /** @param string Twitter URL */
35  public $socialBarTwitterUrl = '';
36  /** @param string Tumblr URL */
37  public $socialBarTumblrUrl = '';
38  /** @param string LinkedIn URL */
39  public $socialBarLinkedInUrl = '';
40  /** @param string Xing URL */
41  public $socialBarXingUrl = '';
42  /** @param string Google Plus URL */
43  public $socialBarGooglePlusUrl = '';
44  /** @param string Facebook Icon */
45  public $socialBarFacebookIcon = '';
46  /** @param string YouTube Icon */
47  public $socialBarYouTubeIcon = '';
48  /** @param string Instagram Icon */
49  public $socialBarInstagramIcon = '';
50  /** @param string Pinterest Icon */
51  public $socialBarPinterestIcon = '';
52  /** @param string Twitter Icon */
53  public $socialBarTwitterIcon = '';
54  /** @param string Tumblr Icon */
55  public $socialBarTumblrIcon = '';
56  /** @param string LinkedIn Icon */
57  public $socialBarLinkedInIcon = '';
58  /** @param string Xing Icon */
59  public $socialBarXingIcon = '';
60  /** @param string Google Plus Icon */
61  public $socialBarGooglePlusIcon = '';
62  /** @param string Social Bar Html Markup */
63  public $socialBarMarkup = '';
64  /** @param string Social Bar Link Target */
65  public $socialBarLinkTarget = '';
66  /** @param string Social Bar Link Target */
67  public $socialBarLinkTitle = '';
68  /** @param string Social Bar Link Color */
69  public $socialBarLinkColor = '';
70  /** @param array Social Bar Elements */
71  public $socialBarElements = '';
72  /** @param string Social Bar Size */
73  public $socialBarSize = 'H2';
74  /** @param string Social Bar Alignment (horizontal or vertical */
75  public $socialBarAlign = 'horizontal';
76  /** @param string Social Bar CSS Class */
77  public $socialBarClass = '';
78 
79  /**
80  * @brief Load all widget settings from database and fill object
81  * @param object $db Database Object
82  * @brief Load all widget settings on object init.
83  */
84  public function __construct($db)
85  {
86  // load this widget settings from db
87  $this->widget = new \YAWK\widget();
89  foreach ($settings as $property => $value) {
90  $this->$property = $value;
91  }
92  }
93 
94  /**
95  * @brief Init example widget and call a function for demo purpose
96  * @brief Example Widget Init
97  */
98  public function init()
99  { // call any function or method you have previously defined in this class
100  echo $this->getHeading($this->socialBarHeading, $this->socialBarSubtext);
101  $this->setSocialBarElements();
102  $this->drawSocialBar();
103 
104  }
105 
106  /**
107  * @brief Set an array with all social button elements
108  * @brief Helper function that set an array with all social elements. Need to be called before drawSocialBar();
109  */
110  public function setSocialBarElements()
111  { // for every widget data setting create an element with data
112  foreach ($this->widget->data AS $element => $data)
113  { // if element is a URL
114  if (strpos($element, 'Url'))
115  { // get name of this social element
116  $substring = substr("$element", 9);
117  $substring = substr("$substring", 0, -3);
118  // store url property in array
119  $this->socialBarElements[$substring]['url'] = $data;
120  }
121  // if element is an icon
122  else if (strpos($element, 'Icon'))
123  { // get name of this social element
124  $substring = substr("$element", 9);
125  $substring = substr("$substring", 0, -4);
126  // store icon property in array
127  $this->socialBarElements[$substring]['icon'] = $data;
128  }
129  // if element is a color
130  else if (strpos($element, 'Color'))
131  { // get name of this social element
132  $substring = substr("$element", 9);
133  $substring = substr("$substring", 0, -5);
134  // store icon property in array
135  $this->socialBarElements[$substring]['color'] = $data;
136  }
137  }
138  // check if social array is set and not empty
139  if (is_array($this->socialBarElements) && (!empty($this->socialBarElements)))
140  { // all good, return social bar data array
142  }
143  else
144  { // no data here
145  return false;
146  }
147  }
148 
149  /**
150  * @brief Filter and sanitize any socialBar URL
151  * @brief filter, sanitize and strip tags from URLs
152  */
153  public function filterUrl($url)
154  { // check if url is set and not empty
155  if (isset($url) && (!empty($url)))
156  {
157  // remove illegal chars from url
158  $url = filter_var($url, FILTER_SANITIZE_URL);
159  // strip html tags
160  $url = strip_tags($url);
161  return $url;
162  }
163  else
164  { // nothing to return
165  return null;
166  }
167  }
168 
169  /**
170  * @brief Draw social bar
171  * @brief Set settings and draw social bar
172  */
173  public function drawSocialBar()
174  {
175  // check if social bar elements array is set
176  if (isset($this->socialBarElements) && (is_array($this->socialBarElements)))
177  { // walk through elements
178  echo "<div class=\"row\">";
179  echo "<div class=\"col-md-12\">";
180  // markup <H2, H3, H4, P... start>
181  echo "<".$this->socialBarSize.">";
182  foreach ($this->socialBarElements AS $element => $data)
183  { // only if url is set...
184  if (!empty($data['url']))
185  {
186  // filter url before output
187  $data['url'] = $this->filterUrl($data['url']);
188 
189  // check if color is set
190  if (isset($data['color']) && (!empty($data['color'])))
191  { // set css style tag
192  $this->socialBarLinkColor = " style=\"color:#$data[color];\"";
193  }
194  else
195  { // no link color - no css markup
196  $this->socialBarLinkColor = '';
197  }
198 
199  // set title
200  if (isset($this->socialBarLinkTitle) && (!empty($this->socialBarLinkTitle)))
201  {
202  $title = " title=\"".$this->socialBarLinkTitle." $element\"";
203  }
204  else
205  {
206  $title = '';
207  }
208 
209  // check alignment
210  if (isset($this->socialBarAlign) && (!empty($this->socialBarAlign)))
211  { // if alignment is set to vertical
212  if ($this->socialBarAlign === 'vertical')
213  { // generate linebreak markup
214  $lineBreak = "<br>";
215  }
216  else
217  { // no linebreak, two blank spaces instead
218  $lineBreak = '&nbsp;&nbsp;';
219  }
220  }
221  else
222  { // no alignment set - two blank spaces are default
223  $lineBreak = '&nbsp;&nbsp;';
224  }
225 
226  // check if icon is set
227  if (isset($data['icon']) && (!empty($data['icon'])))
228  {
229  // check social bar element class
230  if (isset($this->socialBarClass) && (!empty($this->socialBarClass)))
231  { // set css class for this element
232  $cssClass = " ".$this->socialBarClass."";
233  }
234  else
235  { // no class required, leave empty
236  $cssClass = '';
237  }
238 
239  // draw social button
240  echo "<a href=\"$data[url]\"".$this->socialBarLinkColor."$title target=\"$this->socialBarLinkTarget\"><i class=\"$data[icon]".$cssClass."\"></i></a>".$lineBreak."";
241  }
242  else
243  {
244  // check social bar element class
245  if (isset($this->socialBarClass) && (!empty($this->socialBarClass)))
246  { // set css class for this element
247  $cssClass = " class=\"".$this->socialBarClass."\"";
248  }
249  else
250  { // no class required, leave empty
251  $cssClass = '';
252  }
253 
254  // draw textlink
255  echo "<a href=\"$data[url]\" title=\"$this->socialBarLinkTitle\" target=\"$this->socialBarLinkTarget\"".$cssClass.">$data[url]</a>".$lineBreak."";
256  }
257  }
258  }
259  echo "</".$this->socialBarSize.">";
260  echo "</div>";
261  echo "</div>";
262  }
263  }
264  }
265 }
266 ?>
Social Bar display icons with links to your social network accounts.
Definition: socialBar.php:18
setSocialBarElements()
Set an array with all social button elements.
Definition: socialBar.php:109
init()
Init example widget and call a function for demo purpose.
Definition: socialBar.php:97
filterUrl($url)
Filter and sanitize any socialBar URL.
Definition: socialBar.php:152
__construct($db)
Load all widget settings from database and fill object.
Definition: socialBar.php:83
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
getHeading($heading, $subtext)
Get widget heading and subtext, return headline.
Definition: widget.php:669
if(isset($_POST['save'])) $settings
$url
Definition: user-new.php:101
$value