-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathText.php
More file actions
96 lines (86 loc) · 3.08 KB
/
Copy pathText.php
File metadata and controls
96 lines (86 loc) · 3.08 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
<?php
namespace ModStart\Field;
use ModStart\Core\Dao\ModelUtil;
use ModStart\Core\Util\StrUtil;
use ModStart\Field\Type\FieldRenderMode;
class Text extends AbstractField
{
protected function setup()
{
$this->addVariables([
'autoTrim' => false,
'blankToNull' => false,
]);
}
/**
* 保存时自动清除空格
* @param bool $enable
* @return $this
*/
public function autoTrim($enable = true)
{
$this->addVariables(['autoTrim' => $enable]);
return $this;
}
/**
* 保存时空字符串转为null
* @param $enable
* @return $this
*/
public function blankToNull($enable = true)
{
$this->addVariables(['blankToNull' => $enable]);
return $this;
}
public function prepareInput($value, $dataSubmitted)
{
if ($this->variables['autoTrim']) {
$value = StrUtil::filterSpecialChars($value);
$value = trim($value);
}
if ($this->variables['blankToNull'] && '' === $value) {
$value = null;
}
return $value;
}
public function asLink($linkTemplate = null, $openInBlank = true)
{
$this->hookRendering(function (AbstractField $field, $item, $index) use ($linkTemplate, $openInBlank) {
switch ($field->renderMode()) {
case FieldRenderMode::DETAIL:
case FieldRenderMode::GRID:
if (null !== $linkTemplate) {
if ($linkTemplate instanceof \Closure || strpos($linkTemplate, '::') !== false) {
$linkUrl = call_user_func($linkTemplate, $item);
} else {
$linkUrl = $linkTemplate;
if (preg_match_all('/\\{(.+?)\\}/', $linkUrl, $mat)) {
foreach ($mat[1] as $i => $k) {
$v = ModelUtil::traverse($item, $k);
$linkUrl = str_replace($mat[0][$i], $v, $linkUrl);
}
}
}
$linkTitle = ModelUtil::traverse($item, $field->column());
} else {
$linkUrl = ModelUtil::traverse($item, $field->column());
$linkTitle = $linkUrl;
}
if (!empty($linkTitle)) {
$html = [
'<a href="', $linkUrl, '" target="_blank" ref="noreferrer noopener" ',
($openInBlank ? 'target="_blank"' : ''),
'>',
($openInBlank ? '<i class="iconfont icon-link"></i> ' : ''),
htmlspecialchars($linkTitle),
'</a>',
];
} else {
$html = [];
}
return AutoRenderedFieldValue::make(join('', $html));
}
});
return $this;
}
}