1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace XoopsModules\Extgallery; |
4
|
|
|
|
5
|
|
|
use XoopsModules\Extgallery; |
6
|
|
|
use XoopsModules\Extgallery\Common; |
7
|
|
|
|
8
|
|
|
//use XoopsModules\Extgallery\Constants; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Utility |
12
|
|
|
*/ |
13
|
|
|
class Utility extends Common\SysUtility |
14
|
|
|
{ |
15
|
|
|
//--------------- Custom module methods ----------------------------- |
16
|
|
|
/** |
17
|
|
|
* @param $option |
18
|
|
|
* @return bool|mixed |
19
|
|
|
*/ |
20
|
|
|
public static function getModuleOption($option) |
21
|
|
|
{ |
22
|
|
|
global $xoopsModule; |
23
|
|
|
/** @var \XoopsModules\Extgallery\Helper $helper */ |
24
|
|
|
$helper = \XoopsModules\Extgallery\Helper::getInstance(); |
|
|
|
|
25
|
|
|
static $tbloptions = []; |
26
|
|
|
if (\is_array($tbloptions) && \array_key_exists($option, $tbloptions)) { |
27
|
|
|
return $tbloptions[$option]; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$retval = false; |
31
|
|
|
if (isset($xoopsModuleConfig) |
|
|
|
|
32
|
|
|
&& (\is_object($xoopsModule) && 'extgallery' === $xoopsModule->getVar('dirname') |
33
|
|
|
&& $xoopsModule->getVar('isactive'))) { |
34
|
|
|
if (isset($xoopsModuleConfig[$option])) { |
35
|
|
|
$retval = $xoopsModuleConfig[$option]; |
36
|
|
|
} |
37
|
|
|
} else { |
38
|
|
|
/** @var \XoopsModuleHandler $moduleHandler */ |
39
|
|
|
$moduleHandler = \xoops_getHandler('module'); |
40
|
|
|
$module = $moduleHandler->getByDirname('extgallery'); |
41
|
|
|
|
42
|
|
|
/** @var \XoopsModuleHandler $moduleHandler */ |
43
|
|
|
$configHandler = \xoops_getHandler('config'); |
44
|
|
|
if ($module) { |
45
|
|
|
$configurator = $configHandler->getConfigsByCat(0, $module->getVar('mid')); |
|
|
|
|
46
|
|
|
if (isset($configurator[$option])) { |
47
|
|
|
$retval = $configurator[$option]; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
$tbloptions[$option] = $retval; |
52
|
|
|
|
53
|
|
|
return $retval; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param $caption |
58
|
|
|
* @param $name |
59
|
|
|
* @param $value |
60
|
|
|
* @param $rows |
61
|
|
|
* @param $cols |
62
|
|
|
* @param $width |
63
|
|
|
* @param $height |
64
|
|
|
* @param $supplemental |
65
|
|
|
* |
66
|
|
|
* @return bool|\XoopsFormEditor |
67
|
|
|
*/ |
68
|
|
|
public static function getWysiwygForm($caption, $name, $value, $rows, $cols, $width, $height, $supplemental) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$editor_option = mb_strtolower(static::getModuleOption('form_options')); |
|
|
|
|
71
|
|
|
$editor = false; |
|
|
|
|
72
|
|
|
$editor_configs = []; |
73
|
|
|
$editor_configs['name'] = $name; |
74
|
|
|
$editor_configs['value'] = $value; |
75
|
|
|
$editor_configs['rows'] = $rows; |
76
|
|
|
$editor_configs['cols'] = $cols; |
77
|
|
|
$editor_configs['width'] = $width; |
78
|
|
|
$editor_configs['height'] = $height; |
79
|
|
|
$editor_configs['editor'] = $editor_option; |
80
|
|
|
|
81
|
|
|
$editor = new \XoopsFormEditor($caption, $name, $editor_configs); |
82
|
|
|
|
83
|
|
|
return $editor; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* truncateHtml can truncate a string up to a number of characters while preserving whole words and HTML tags |
88
|
|
|
* www.gsdesign.ro/blog/cut-html-string-without-breaking-the-tags |
89
|
|
|
* www.cakephp.org |
90
|
|
|
* |
91
|
|
|
* @param string $text String to truncate. |
92
|
|
|
* @param int $length Length of returned string, including ellipsis. |
93
|
|
|
* @param string $ending Ending to be appended to the trimmed string. |
94
|
|
|
* @param bool $exact If false, $text will not be cut mid-word |
95
|
|
|
* @param bool $considerHtml If true, HTML tags would be handled correctly |
96
|
|
|
* |
97
|
|
|
* @return string Trimmed string. |
98
|
|
|
*/ |
99
|
|
|
public static function truncateHtml($text, $length = 100, $ending = '...', $exact = false, $considerHtml = true) |
100
|
|
|
{ |
101
|
|
|
$open_tags = []; |
102
|
|
|
if ($considerHtml) { |
103
|
|
|
// if the plain text is shorter than the maximum length, return the whole text |
104
|
|
|
if (mb_strlen(\preg_replace('/<.*?' . '>/', '', $text)) <= $length) { |
105
|
|
|
return $text; |
106
|
|
|
} |
107
|
|
|
// splits all html-tags to scanable lines |
108
|
|
|
\preg_match_all('/(<.+?' . '>)?([^<>]*)/s', $text, $lines, \PREG_SET_ORDER); |
109
|
|
|
$total_length = mb_strlen($ending); |
110
|
|
|
$truncate = ''; |
111
|
|
|
foreach ($lines as $line_matchings) { |
112
|
|
|
// if there is any html-tag in this line, handle it and add it (uncounted) to the output |
113
|
|
|
if (!empty($line_matchings[1])) { |
114
|
|
|
// if it's an "empty element" with or without xhtml-conform closing slash |
115
|
|
|
if (\preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) { |
116
|
|
|
// do nothing |
117
|
|
|
// if tag is a closing tag |
118
|
|
|
} elseif (\preg_match('/^<\s*\/(\S+?)\s*>$/s', $line_matchings[1], $tag_matchings)) { |
119
|
|
|
// delete tag from $open_tags list |
120
|
|
|
$pos = \array_search($tag_matchings[1], $open_tags, true); |
121
|
|
|
if (false !== $pos) { |
122
|
|
|
unset($open_tags[$pos]); |
123
|
|
|
} |
124
|
|
|
// if tag is an opening tag |
125
|
|
|
} elseif (\preg_match('/^<\s*([^\s>!]+).*?' . '>$/s', $line_matchings[1], $tag_matchings)) { |
126
|
|
|
// add tag to the beginning of $open_tags list |
127
|
|
|
\array_unshift($open_tags, mb_strtolower($tag_matchings[1])); |
128
|
|
|
} |
129
|
|
|
// add html-tag to $truncate'd text |
130
|
|
|
$truncate .= $line_matchings[1]; |
131
|
|
|
} |
132
|
|
|
// calculate the length of the plain text part of the line; handle entities as one character |
133
|
|
|
$content_length = mb_strlen(\preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', $line_matchings[2])); |
134
|
|
|
if ($total_length + $content_length > $length) { |
135
|
|
|
// the number of characters which are left |
136
|
|
|
$left = $length - $total_length; |
137
|
|
|
$entities_length = 0; |
138
|
|
|
// search for html entities |
139
|
|
|
if (\preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', $line_matchings[2], $entities, \PREG_OFFSET_CAPTURE)) { |
140
|
|
|
// calculate the real length of all entities in the legal range |
141
|
|
|
foreach ($entities[0] as $entity) { |
142
|
|
|
if ($left >= $entity[1] + 1 - $entities_length) { |
143
|
|
|
$left--; |
144
|
|
|
$entities_length += mb_strlen($entity[0]); |
145
|
|
|
} else { |
146
|
|
|
// no more characters left |
147
|
|
|
break; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
$truncate .= mb_substr($line_matchings[2], 0, $left + $entities_length); |
152
|
|
|
// maximum lenght is reached, so get off the loop |
153
|
|
|
break; |
154
|
|
|
} |
155
|
|
|
$truncate .= $line_matchings[2]; |
156
|
|
|
$total_length += $content_length; |
157
|
|
|
|
158
|
|
|
// if the maximum length is reached, get off the loop |
159
|
|
|
if ($total_length >= $length) { |
160
|
|
|
break; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} else { |
164
|
|
|
if (mb_strlen($text) <= $length) { |
165
|
|
|
return $text; |
166
|
|
|
} |
167
|
|
|
$truncate = mb_substr($text, 0, $length - mb_strlen($ending)); |
168
|
|
|
} |
169
|
|
|
// if the words shouldn't be cut in the middle... |
170
|
|
|
if (!$exact) { |
171
|
|
|
// ...search the last occurance of a space... |
172
|
|
|
$spacepos = mb_strrpos($truncate, ' '); |
173
|
|
|
if (isset($spacepos)) { |
174
|
|
|
// ...and cut the text in this position |
175
|
|
|
$truncate = mb_substr($truncate, 0, $spacepos); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
// add the defined ending to the text |
179
|
|
|
$truncate .= $ending; |
180
|
|
|
if ($considerHtml) { |
181
|
|
|
// close all unclosed html-tags |
182
|
|
|
foreach ($open_tags as $tag) { |
183
|
|
|
$truncate .= '</' . $tag . '>'; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $truncate; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|