-
Notifications
You must be signed in to change notification settings - Fork 290
Expand file tree
/
Copy pathChatterHelper.php
More file actions
125 lines (102 loc) · 2.8 KB
/
ChatterHelper.php
File metadata and controls
125 lines (102 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace DevDojo\Chatter\Helpers;
class ChatterHelper
{
/**
* Convert any string to a color code.
*
* @param string $string
*
* @return string
*/
public static function stringToColorCode($string)
{
$code = dechex(crc32($string));
return substr($code, 0, 6);
}
/**
* User link.
*
* @param mixed $user
*
* @return string
*/
public static function userLink($user)
{
$url = config('chatter.user.relative_url_to_profile', '');
if ('' === $url) {
return '#_';
}
return static::replaceUrlParameter($url, $user);
}
/**
* Replace url parameter.
*
* @param string $url
* @param mixed $source
*
* @return string
*/
private static function replaceUrlParameter($url, $source)
{
$parameter = static::urlParameter($url);
return str_replace('{'.$parameter.'}', $source[$parameter], $url);
}
/**
* Url parameter.
*
* @param string $url
*
* @return string
*/
private static function urlParameter($url)
{
$start = strpos($url, '{') + 1;
$length = strpos($url, '}') - $start;
return substr($url, $start, $length);
}
/**
* This function will demote H1 to H2, H2 to H3, H4 to H5, etc.
* this will help with SEO so there are not multiple H1 tags
* on the same page.
*
* @param HTML string
*
* @return HTML string
*/
public static function demoteHtmlHeaderTags($html)
{
$originalHeaderTags = [];
$demotedHeaderTags = [];
foreach (range(100, 1) as $index) {
$originalHeaderTags[] = '<h'.$index.'>';
$originalHeaderTags[] = '</h'.$index.'>';
$demotedHeaderTags[] = '<h'.($index + 1).'>';
$demotedHeaderTags[] = '</h'.($index + 1).'>';
}
return str_ireplace($originalHeaderTags, $demotedHeaderTags, $html);
}
/**
* This function construct the categories menu with nested categories.
*
* @param array $categories
*
* @return string
*/
public static function categoriesMenu($categories)
{
$menu = '<ul class="nav nav-pills nav-stacked">';
foreach ($categories as $category) {
$menu .= '<li>';
$menu .= '<a href="'.route('chatter.category.show', $category['slug']).'">';
$menu .= '<div class="chatter-box" style="background-color:'.$category['color'].'"></div>';
$menu .= $category['name'].'</a>';
if (count($category['parents'])) {
$menu .= static::categoriesMenu($category['parents']);
}
$menu .= '</li>';
}
$menu .= '</ul>';
return $menu;
}
}