1
|
|
|
<?php
|
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace DigitalWand\AdminHelper\Widget;
|
4
|
|
|
|
5
|
|
|
use Bitrix\Main\Localization\Loc;
|
6
|
|
|
|
7
|
|
|
Loc::loadMessages(__FILE__);
|
8
|
|
|
|
9
|
|
|
/**
|
10
|
|
|
* Виджет текстового поля для ввода гиперссылки.
|
11
|
|
|
*
|
12
|
|
|
* Доступные опции:
|
13
|
|
|
* <ul>
|
14
|
|
|
* <li> PROTOCOL_REQUIRED - ссылка должна иметь протокол</li>
|
15
|
|
|
* <li> STYLE - inline-стили </li>
|
16
|
|
|
* <li> SIZE - значение атрибута size для input </li>
|
17
|
|
|
* <li> MAX_URL_LEN - длина отображаемого URL</li>
|
18
|
|
|
* </ul>
|
19
|
|
|
*
|
20
|
|
|
* @author Nik Samokhvalov <[email protected]>
|
21
|
|
|
*/
|
22
|
|
|
class UrlWidget extends StringWidget
|
23
|
|
|
{
|
24
|
|
|
static protected $defaults = array(
|
25
|
|
|
'MAX_URL_LEN' => 256,
|
26
|
|
|
'PROTOCOL_REQUIRED' => false,
|
27
|
|
|
);
|
28
|
|
|
|
29
|
|
|
/**
|
30
|
|
|
* @inheritdoc
|
31
|
|
|
*/
|
32
|
|
|
public function generateRow(&$row, $data)
|
33
|
|
|
{
|
34
|
|
|
$value = $this->getValue();
|
35
|
|
|
|
36
|
|
View Code Duplication |
if ($this->getSettings('EDIT_IN_LIST') AND !$this->getSettings('READONLY')) {
|
|
|
|
|
37
|
|
|
$row->AddInputField($this->getCode(), array('style' => 'width:90%'));
|
38
|
|
|
}
|
39
|
|
|
|
40
|
|
|
$row->AddViewField($this->getCode(), $value);
|
41
|
|
|
}
|
42
|
|
|
|
43
|
|
|
/**
|
44
|
|
|
* @inheritdoc
|
45
|
|
|
*/
|
46
|
|
|
public function getValue()
|
47
|
|
|
{
|
48
|
|
|
$code = $this->getCode();
|
49
|
|
|
$value = isset($this->data[$code]) ? $this->data[$code] : null;
|
50
|
|
|
|
51
|
|
|
if ($value !== null) {
|
52
|
|
|
$urlText = static::prepareToOutput($value);
|
53
|
|
|
$urlText = preg_replace('/^javascript:/i', '', $urlText);
|
54
|
|
|
|
55
|
|
|
if (strlen($urlText) > $this->getSettings('MAX_URL_LEN')) {
|
56
|
|
|
$urlText = substr($urlText, 0, $this->getSettings('MAX_URL_LEN'));
|
57
|
|
|
}
|
58
|
|
|
|
59
|
|
|
if (($this->getSettings('READONLY') && $this->getCurrentViewType() == static::EDIT_HELPER) || $this->getCurrentViewType() == static::LIST_HELPER) {
|
60
|
|
|
$value = '<a href="' . $value . '" target="_blank">' . $urlText . '</a>';
|
61
|
|
|
} else {
|
62
|
|
|
$value = $urlText;
|
63
|
|
|
}
|
64
|
|
|
}
|
65
|
|
|
|
66
|
|
|
return $value;
|
67
|
|
|
}
|
68
|
|
|
|
69
|
|
|
/**
|
70
|
|
|
* @inheritdoc
|
71
|
|
|
*/
|
72
|
|
|
protected function getValueReadonly()
|
73
|
|
|
{
|
74
|
|
|
return $this->getValue();
|
75
|
|
|
}
|
76
|
|
|
|
77
|
|
|
/**
|
78
|
|
|
* @inheritdoc
|
79
|
|
|
*/
|
80
|
|
|
public function processEditAction()
|
81
|
|
|
{
|
82
|
|
|
$value = $this->getValue();
|
83
|
|
|
|
84
|
|
|
if (
|
85
|
|
|
$this->getSettings('PROTOCOL_REQUIRED')
|
86
|
|
|
&& !empty($value)
|
87
|
|
|
&& preg_match('/^https?:\/\//', $value) == 0
|
88
|
|
|
) {
|
89
|
|
|
|
90
|
|
|
$this->addError('PROTOCOL_REQUIRED');
|
91
|
|
|
}
|
92
|
|
|
}
|
93
|
|
|
}
|
94
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.