1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace DigitalWand\AdminHelper\Widget; |
4
|
|
|
|
5
|
|
|
use Bitrix\Main\Localization\Loc; |
6
|
|
|
use DigitalWand\AdminHelper\Helper\AdminEditHelper; |
7
|
|
|
use DigitalWand\AdminHelper\Helper\AdminListHelper; |
8
|
|
|
use DigitalWand\AdminHelper\Helper\AdminSectionListHelper; |
9
|
|
|
|
10
|
|
|
Loc::loadMessages(__FILE__); |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Виджет строки с текстом. |
14
|
|
|
* |
15
|
|
|
* Доступные опции: |
16
|
|
|
* <ul> |
17
|
|
|
* <li> <b>EDIT_LINK</b> - отображать в виде ссылки на редактирование элемента </li> |
18
|
|
|
* <li> <b>STYLE</b> - inline-стили для input </li> |
19
|
|
|
* <li> <b>SIZE</b> - значение атрибута size для input </li> |
20
|
|
|
* <li> <b>TRANSLIT</b> - true, если поле будет транслитерироваться в символьный код</li> |
21
|
|
|
* <li> <b>MULTIPLE</b> - поддерживается множественный ввод. В таблице требуется наличие поля VALUE</li> |
22
|
|
|
* </ul> |
23
|
|
|
*/ |
24
|
|
|
class StringWidget extends HelperWidget |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
static protected $defaults = array( |
27
|
|
|
'FILTER' => '%', //Фильтрация по подстроке, а не по точному соответствию. |
28
|
|
|
'EDIT_IN_LIST' => true |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritdoc |
33
|
|
|
*/ |
34
|
|
|
protected function getEditHtml() |
35
|
|
|
{ |
36
|
|
|
$style = $this->getSettings('STYLE'); |
37
|
|
|
$size = $this->getSettings('SIZE'); |
38
|
|
|
|
39
|
|
|
$link = ''; |
40
|
|
|
|
41
|
|
|
if ($this->getSettings('TRANSLIT')) { |
42
|
|
|
|
43
|
|
|
//TODO: refactor this! |
44
|
|
|
$uniqId = get_class($this->entityName) . '_' . $this->getCode(); |
45
|
|
|
$nameId = 'name_link_' . $uniqId; |
46
|
|
|
$linkedFunctionName = 'set_linked_' . get_class($this->entityName) . '_CODE';//FIXME: hardcode here!!! |
|
|
|
|
47
|
|
|
|
48
|
|
|
if (isset($this->entityName->{$this->entityName->pk()})) { |
|
|
|
|
49
|
|
|
$pkVal = $this->entityName->{$this->entityName->pk()}; |
|
|
|
|
50
|
|
|
} else { |
51
|
|
|
$pkVal = '_new_'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$nameId .= $pkVal; |
55
|
|
|
$linkedFunctionName .= $pkVal; |
56
|
|
|
|
57
|
|
|
$link = '<image id="' . $nameId . '" title="' . Loc::getMessage("IBSEC_E_LINK_TIP") . '" class="linked" src="/bitrix/themes/.default/icons/iblock/link.gif" onclick="' . $linkedFunctionName . '()" />'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return '<input type="text" |
61
|
|
|
name="' . $this->getEditInputName() . '" |
62
|
|
|
value="' . static::prepareToTagAttr($this->getValue()) . '" |
63
|
|
|
size="' . $size . '" |
64
|
|
|
style="' . $style . '"/>' . $link; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function getMultipleEditHtml() |
68
|
|
|
{ |
69
|
|
|
$style = $this->getSettings('STYLE'); |
70
|
|
|
$size = $this->getSettings('SIZE'); |
71
|
|
|
$uniqueId = $this->getEditInputHtmlId(); |
72
|
|
|
|
73
|
|
|
$rsEntityData = null; |
74
|
|
|
|
75
|
|
View Code Duplication |
if (!empty($this->data['ID'])) { |
|
|
|
|
76
|
|
|
$entityName = $this->entityName; |
77
|
|
|
$rsEntityData = $entityName::getList(array( |
78
|
|
|
'select' => array('REFERENCE_' => $this->getCode() . '.*'), |
79
|
|
|
'filter' => array('=ID' => $this->data['ID']) |
80
|
|
|
)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
ob_start(); |
84
|
|
|
?> |
85
|
|
|
|
86
|
|
|
<div id="<?= $uniqueId ?>-field-container" class="<?= $uniqueId ?>"> |
87
|
|
|
</div> |
88
|
|
|
|
89
|
|
|
<script> |
90
|
|
|
var multiple = new MultipleWidgetHelper( |
91
|
|
|
'#<?= $uniqueId ?>-field-container', |
92
|
|
|
'{{field_original_id}}<input type="text" name="<?= $this->getCode()?>[{{field_id}}][<?=$this->getMultipleField('VALUE')?>]" style="<?=$style?>" size="<?=$size?>" value="{{value}}">' |
93
|
|
|
); |
94
|
|
|
<? |
|
|
|
|
95
|
|
|
if ($rsEntityData) |
96
|
|
|
{ |
97
|
|
|
while($referenceData = $rsEntityData->fetch()) |
98
|
|
|
{ |
99
|
|
|
if (empty($referenceData['REFERENCE_' . $this->getMultipleField('ID')])) |
100
|
|
|
{ |
101
|
|
|
continue; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
?> |
105
|
|
|
multiple.addField({ |
106
|
|
|
value: '<?= static::prepareToJs($referenceData['REFERENCE_' . $this->getMultipleField('VALUE')]) ?>', |
107
|
|
|
field_original_id: '<input type="hidden" name="<?= $this->getCode()?>[{{field_id}}][<?= $this->getMultipleField('ID') ?>]"' + |
108
|
|
|
' value="<?= $referenceData['REFERENCE_' . $this->getMultipleField('ID')] ?>">', |
109
|
|
|
field_id: <?= $referenceData['REFERENCE_' . $this->getMultipleField('ID')] ?> |
110
|
|
|
}); |
111
|
|
|
<? |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
?> |
115
|
|
|
|
116
|
|
|
// TODO Добавление созданных полей |
117
|
|
|
multiple.addField(); |
118
|
|
|
</script> |
119
|
|
|
<? |
120
|
|
|
return ob_get_clean(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
protected function getMultipleValueReadonly() |
124
|
|
|
{ |
125
|
|
|
$rsEntityData = null; |
126
|
|
View Code Duplication |
if (!empty($this->data['ID'])) { |
|
|
|
|
127
|
|
|
$entityName = $this->entityName; |
128
|
|
|
$rsEntityData = $entityName::getList(array( |
129
|
|
|
'select' => array('REFERENCE_' => $this->getCode() . '.*'), |
130
|
|
|
'filter' => array('=ID' => $this->data['ID']) |
131
|
|
|
)); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$result = ''; |
135
|
|
|
if ($rsEntityData) { |
136
|
|
|
while ($referenceData = $rsEntityData->fetch()) { |
137
|
|
|
if (empty($referenceData['REFERENCE_VALUE'])) { |
138
|
|
|
continue; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$result .= '<div class="wrap_text" style="margin-bottom: 5px">' . |
142
|
|
|
static::prepareToOutput($referenceData['REFERENCE_VALUE']) . '</div>'; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $result; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Генерирует HTML для поля в списке |
151
|
|
|
* @see AdminListHelper::addRowCell(); |
152
|
|
|
* @param \CAdminListRow $row |
153
|
|
|
* @param array $data - данные текущей строки |
154
|
|
|
*/ |
155
|
|
|
public function generateRow(&$row, $data) |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
if ($this->getSettings('MULTIPLE')) { |
158
|
|
|
} else { |
159
|
|
|
if ($this->getSettings('EDIT_LINK') || $this->getSettings('SECTION_LINK')) { |
160
|
|
|
$pk = $this->helper->pk(); |
161
|
|
|
|
162
|
|
|
if ($this->getSettings('SECTION_LINK')) { |
163
|
|
|
$params = $this->helper->isPopup() ? $_GET : array(); |
|
|
|
|
164
|
|
|
$params['ID'] = $this->data[$pk]; |
165
|
|
|
$listHelper = $this->helper->getHelperClass($this->helper->isPopup() ? AdminSectionListHelper::className() : AdminListHelper::className()); |
166
|
|
|
$pageUrl = $listHelper::getUrl($params); |
167
|
|
|
$value = '<span class="adm-submenu-item-link-icon adm-list-table-icon iblock-section-icon"></span>'; |
168
|
|
|
} else { |
169
|
|
|
$editHelper = $this->helper->getHelperClass(AdminEditHelper::className()); |
170
|
|
|
$pageUrl = $editHelper::getUrl(array( |
171
|
|
|
'ID' => $this->data[$pk] |
172
|
|
|
)); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$value .= '<a href="' . $pageUrl . '">' . static::prepareToOutput($this->getValue()) . '</a>'; |
|
|
|
|
176
|
|
|
} else { |
177
|
|
|
$value = static::prepareToOutput($this->getValue()); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
View Code Duplication |
if ($this->getSettings('EDIT_IN_LIST') AND !$this->getSettings('READONLY')) { |
|
|
|
|
181
|
|
|
$row->AddInputField($this->getCode(), array('style' => 'width:90%')); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$row->AddViewField($this->getCode(), $value); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @inheritdoc |
190
|
|
|
*/ |
191
|
|
|
public function showFilterHtml() |
192
|
|
|
{ |
193
|
|
|
if ($this->getSettings('MULTIPLE')) { |
194
|
|
|
} else { |
195
|
|
|
print '<tr>'; |
196
|
|
|
print '<td>' . $this->getSettings('TITLE') . '</td>'; |
197
|
|
|
|
198
|
|
|
if ($this->isFilterBetween()) { |
199
|
|
|
list($from, $to) = $this->getFilterInputName(); |
200
|
|
|
print '<td> |
201
|
|
|
<div class="adm-filter-box-sizing"> |
202
|
|
|
<span style="display: inline-block; left: 11px; top: 5px; position: relative;">От:</span> |
203
|
|
|
<div class="adm-input-wrap" style="display: inline-block"> |
204
|
|
|
<input type="text" class="adm-input" name="' . $from . '" value="' . $$from . '"> |
205
|
|
|
</div> |
206
|
|
|
<span style="display: inline-block; left: 11px; top: 5px; position: relative;">До:</span> |
207
|
|
|
<div class="adm-input-wrap" style="display: inline-block"> |
208
|
|
|
<input type="text" class="adm-input" name="' . $to . '" value="' . $$to . '"> |
209
|
|
|
</div> |
210
|
|
|
</div> |
211
|
|
|
</td> '; |
212
|
|
|
} else { |
213
|
|
|
print '<td><input type="text" name="' . $this->getFilterInputName() . '" size="47" value="' . $this->getCurrentFilterValue() . '"></td>'; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
print '</tr>'; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
} |
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.