Layout::getImagePath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Layout class.
4
 *
5
 * @package App
6
 *
7
 * @copyright YetiForce S.A.
8
 * @license   YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Mariusz Krzaczkowski <[email protected]>
10
 * @author    Radosław Skrzypczak <[email protected]>
11
 */
12
13
namespace App;
14
15
/**
16
 * Layout class.
17
 */
18
class Layout
19
{
20
	/**
21
	 * Get active layout name.
22 2
	 *
23
	 * @return string
24 2
	 */
25 2
	public static function getActiveLayout()
26
	{
27 1
		if (Session::has('layout')) {
28
			return Session::get('layout');
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Session::get('layout') also could return the type array which is incompatible with the documented return type string.
Loading history...
29
		}
30
		return \App\Config::main('defaultLayout');
31
	}
32
33
	/**
34
	 * Get file from layout.
35
	 *
36
	 * @param string $name
37 1
	 *
38
	 * @return string
39 1
	 */
40 1
	public static function getLayoutFile($name)
41 1
	{
42 1
		$basePath = 'layouts' . '/' . \App\Config::main('defaultLayout') . '/';
43 1
		$filePath = \Vtiger_Loader::resolveNameToPath('~' . $basePath . $name);
44
		if (is_file($filePath)) {
45
			if (!IS_PUBLIC_DIR) {
46 1
				$basePath = 'public_html/' . $basePath;
47
			}
48 1
			return $basePath . $name;
49 1
		}
50 1
		$basePath = 'layouts' . '/' . \Vtiger_Viewer::getDefaultLayoutName() . '/';
51
		if (!IS_PUBLIC_DIR) {
52 1
			$basePath = 'public_html/' . $basePath;
53
		}
54
		return $basePath . $name;
55
	}
56
57
	/**
58
	 * Gets layout paths.
59
	 *
60 2
	 * @return array
61
	 */
62 2
	public static function getLayoutPaths(): array
63
	{
64 2
		$basePrefix = 'layouts/' . self::getActiveLayout() . \DIRECTORY_SEPARATOR;
65
		$defaultPrefix = 'layouts/' . \Vtiger_Viewer::getDefaultLayoutName() . \DIRECTORY_SEPARATOR;
66 2
		if (\App\Config::performance('LOAD_CUSTOM_FILES')) {
67 1
			$layoutsPath['custom/'] = 'custom/';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$layoutsPath was never initialized. Although not strictly required by PHP, it is generally a good practice to add $layoutsPath = array(); before regardless.
Loading history...
68
			$layoutsPath["custom/{$basePrefix}"] = "custom/{$basePrefix}";
69 2
			$layoutsPath["custom/{$defaultPrefix}"] = "custom/{$defaultPrefix}";
70
		}
71
		$layoutsPath[''] = '';
72
		$layoutsPath[$basePrefix] = $basePrefix;
73
		$layoutsPath[$defaultPrefix] = $defaultPrefix;
74
		return $layoutsPath;
75
	}
76
77
	/**
78
	 * Get all layouts list.
79
	 *
80 2
	 * @return string[]
81
	 */
82 2
	public static function getAllLayouts()
83 2
	{
84 1
		$all = (new \App\Db\Query())->select(['name', 'label'])->from('vtiger_layout')->all();
85
		$folders = [
86 2
			'basic' => Language::translate('LBL_DEFAULT'),
87 2
		];
88
		foreach ($all as $row) {
89 2
			$folders[$row['name']] = Language::translate($row['label']);
90
		}
91
		return $folders;
92
	}
93
94
	/**
95
	 * Get public url from file.
96
	 *
97
	 * @param string $name
98
	 * @param bool   $full
99 1
	 *
100
	 * @return string
101 1
	 */
102
	public static function getPublicUrl($name, $full = false)
103
	{
104
		$basePath = '';
105
		if ($full) {
106
			$basePath .= \App\Config::main('site_URL');
107
		}
108
		if (!IS_PUBLIC_DIR) {
109
			$basePath .= 'public_html/';
110
		}
111
		return $basePath . $name;
112 1
	}
113
114 1
	/**
115
	 * The function get path  to the image.
116
	 *
117
	 * @param string $imageName
118
	 *
119
	 * @return array
120
	 */
121
	public static function getImagePath($imageName)
122
	{
123
		return \Vtiger_Theme::getImagePath($imageName);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Vtiger_Theme::getImagePath($imageName) returns the type false|string which is incompatible with the documented return type array.
Loading history...
124 1
	}
125
126 1
	/**
127
	 * Function takes a template path.
128
	 *
129
	 * @param string $templateName
130
	 * @param string $moduleName
131
	 *
132
	 * @return string
133
	 */
134
	public static function getTemplatePath(string $templateName, string $moduleName = ''): string
135
	{
136
		return \Vtiger_Viewer::getInstance()->getTemplatePath($templateName, $moduleName);
137
	}
138
139
	/**
140
	 * Check if template exists.
141
	 *
142
	 * @param string $templateName
143
	 * @param string $moduleName
144
	 *
145
	 * @return bool
146
	 */
147
	public static function checkTemplatePath(string $templateName, string $moduleName = ''): bool
148
	{
149
		self::getTemplatePath($templateName, $moduleName);
150
		return file_exists(\Vtiger_Viewer::$completeTemplatePath);
151
	}
152
153
	/**
154
	 * Get unique id for HTML ids.
155
	 *
156
	 * @param string $name
157
	 *
158
	 * @return string
159
	 */
160
	public static function getUniqueId($name = '')
161
	{
162
		return str_replace([' ', '"', "'"], '', $name) . random_int(100, 99999);
163
	}
164
165
	/**
166
	 * Truncating plain text and adding a button showing all the text.
167
	 *
168
	 * @param string $text
169
	 * @param int    $length
170
	 * @param bool   $showIcon
171
	 * @param bool   $nl2br
172
	 *
173
	 * @return string
174
	 */
175
	public static function truncateText(string $text, int $length, bool $showIcon = false, bool $nl2br = false): string
176
	{
177
		if (\mb_strlen($text) < $length) {
178
			return $nl2br ? nl2br($text) : $text;
179
		}
180
		$teaser = Purifier::encodeHtml(TextUtils::textTruncate($text, $length));
181
		$text = Purifier::encodeHtml($text);
182
		if ($showIcon) {
183
			$btn = '<span class="mdi mdi-overscan"></span>';
184
		} else {
185
			$btn = Language::translate('LBL_MORE_BTN');
186
		}
187
		return "<div class=\"js-more-content c-text-divider\"><pre class=\"teaserContent u-pre\">$teaser</pre><span class=\"fullContent d-none\"><pre class=\"u-pre\">$text</pre></span><span class=\"text-right\"><button type=\"button\" class=\"btn btn-link btn-sm p-0 js-more\">{$btn}</button></span></div>";
188
	}
189
190
	/**
191
	 * Truncating HTML and adding a button showing all the text.
192
	 *
193
	 * @param string $html
194
	 * @param string $size
195
	 * @param int    $length
196
	 * @param mixed  $showBtn
197
	 *
198
	 * @return string
199
	 */
200
	public static function truncateHtml(?string $html, ?string $size = 'medium', ?int $length = 200, $showBtn = false): string
201
	{
202
		if (empty($html)) {
203
			return '';
204
		}
205
		$teaser = $css = $btn = '';
206
		$loadData = $iframe = true;
207
		$btnTemplate = function (string $popoverText = '', ?string $btnClass = '', string $data = 'data-iframe="true"'): string {
208
			$popoverText = \App\Language::translate($popoverText);
209
			return "<a href=\"#\" class=\"js-more noLinkBtn font-weight-lighter js-popover-tooltip ml-2 {$btnClass}\" {$data} data-content=\"{$popoverText}\"><span class=\"mdi mdi-overscan\"></span></a>";
210
		};
211
		$iframeClass = 'modal-iframe js-modal-iframe';
212
		switch ($size) {
213
			case 'full':
214
				$iframeClass = 'js-iframe-full-height';
215
				break;
216
			case 'medium':
217
				$btn = $btnTemplate('LBL_FULLSCREEN', 'c-btn-floating-right-bottom btn btn-primary');
218
				break;
219
			case 'mini':
220
				$btn = $btnTemplate('LBL_SHOW_ORIGINAL_CONTENT');
221
				$css = 'display: none;';
222
				$teaser = TextUtils::textTruncate(trim(strip_tags($html)), $length);
223
				$loadData = false;
224
				break;
225
			case 'miniHtml':
226
				$btn = $btnTemplate('LBL_SHOW_ORIGINAL_CONTENT', '', 'data-modal-size="modal-md"');
227
				$css = 'display: none;';
228
				$teaserBefore = str_replace('<br>', '', $html);
229
				$teaser = TextUtils::htmlTruncateByWords(str_replace('<br>', '', $teaserBefore), $length);
230
				if (false === $showBtn && $teaserBefore == $teaser) {
231
					$html = $btn = '';
232
				}
233
				$iframe = false;
234
				break;
235
			default:
236
				break;
237
		}
238
		if ($iframe) {
239
			$html = Purifier::encodeHtml($html);
240
			$content = "<div class=\"js-iframe-content\">{$teaser}<iframe sandbox=\"allow-same-origin allow-popups allow-popups-to-escape-sandbox\" class=\"w-100 {$iframeClass}\" frameborder=\"0\" style=\"{$css}\" " . ($loadData ? 'srcdoc' : 'srcdoctemp') . "=\"{$html}\"></iframe>";
241
		} else {
242
			$content = "<div class=\"js-more-content\">{$teaser}<div class=\"w-100 {$iframeClass} fullContent\" style=\"{$css}\">{$html}</div>";
243
		}
244
		return $content . $btn . '</div>';
245
	}
246
}
247