-
Notifications
You must be signed in to change notification settings - Fork 0
/
legacysearchadapter.class.inc.php
352 lines (336 loc) · 15.1 KB
/
legacysearchadapter.class.inc.php
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
/**
* Copyright (C) 2012-2024 Combodo SAS
*
* This file is part of iTop.
*
* iTop is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* iTop is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with iTop. If not, see <http://www.gnu.org/licenses/>
*/
/**
* Class LegacySearchBlock
*
* Can be used to bring back the legacy search box in the console by using the following snippet:
* $oSearchBlock = new LegacySearchBlock($oFilter);
* $oSearchBlock->Display($oPage, 'some_html_id');
*
* Note: Those methods are inspired by the search box as in iTop 2.4.
*/
class LegacySearchBlock
{
/** @var DBSearch $oFilter */
protected $oFilter;
/** @var CMDBObjectSet $oSet */
protected $oSet;
/** @var array $aExtraParams */
protected $aExtraParams;
CONST XML_LEGACY_VERSION = '1.7';
/**
* Compare static::XML_LEGACY_VERSION with ITOP_DESIGN_LATEST_VERSION and returns true if the later is <= to the former.
* If static::XML_LEGACY_VERSION, return false
*
* @return bool
*
* @since 1.1.0
*/
public static function UseLegacy(){
return static::XML_LEGACY_VERSION !== '' ? version_compare(ITOP_DESIGN_LATEST_VERSION, static::XML_LEGACY_VERSION, '<=') : false;
}
/**
* LegacySearchBlock constructor.
*
* @param DBSearch $oFilter
* @param array $aExtraParams
*/
public function __construct(DBSearch $oFilter, $aExtraParams = array())
{
$this->oFilter = $oFilter;
$this->aExtraParams = $aExtraParams;
}
/**
* @param WebPage $oPage
* @param $sId
*
* @throws CoreException
* @throws DictExceptionMissingString
*/
public function Display(WebPage $oPage, $sId)
{
if($this->oSet === null)
{
$this->oSet = new CMDBObjectSet($this->oFilter);
}
$oPage->add_linked_script(utils::GetAbsoluteUrlModulesRoot().'/itop-legacy-search-base/js/legacy-search.js');
if(static::UseLegacy()){
$oPage->add_saas('env-'.utils::GetCurrentEnvironment().'/itop-legacy-search-base/legacy/css/legacy-search.scss');
}
else{
$oPage->add_saas('env-'.utils::GetCurrentEnvironment().'/itop-legacy-search-base/css/legacy-search.scss');
}
if (version_compare(ITOP_DESIGN_LATEST_VERSION , '3.0') < 0) {
$sStyle = (isset($this->aExtraParams['open']) && ($this->aExtraParams['open'] == 'true')) ? 'SearchDrawer' : 'SearchDrawer DrawerClosed';
$sHtml = "<div id=\"ds_$sId\" class=\"$sStyle\">\n";
$oPage->add_ready_script(
<<<EOF
$("#dh_$sId").click( function() {
$("#ds_$sId").slideToggle('normal', function() {
$("#ds_$sId").parent().resize();
/* Not part of the module */
/*FixSearchFormsDisposition();*/
$("#dh_$sId").trigger('toggle_complete'); } );
$("#dh_$sId").toggleClass('open');
});
EOF
);
$this->aExtraParams['currentId'] = $sId;
$sHtml .= static::GetSearchForm($oPage, $this->oSet, $this->aExtraParams);
$sHtml .= "</div>\n";
$sHtml .= "<div class=\"HRDrawer\"></div>\n";
$sHtml .= "<div id=\"dh_$sId\" class=\"DrawerHandle\">".Dict::S('UI:SearchToggle')."</div>\n";
} else {
$sStyle = (isset($this->aExtraParams['open']) && ($this->aExtraParams['open'] == 'true')) ? ' ibo-is-opened' : '';
$sHtml = "<div id=\"ds_$sId\" class=\" ibo-panel ibo-content-block ibo-block ibo-search-form-panel display_block ibo-is-cyan ibo-is-opened $sStyle\">\n";
$oPage->add_ready_script(
<<<EOF
$("#ds_$sId .ibo-panel--header").click( function() {
$("#ds_$sId").toggleClass('ibo-is-opened');
});
EOF
);
$this->aExtraParams['currentId'] = $sId;
$sHtml .= static::GetSearchForm($oPage, $this->oSet, $this->aExtraParams);
$sHtml .= "</div>\n";
$sHtml .= "<div id=\"dh_$sId\" class=\"ibo-is-hidden\">".Dict::S('UI:SearchToggle')."</div>\n";
}
$oPage->add($sHtml);
}
/**
* @param WebPage $oPage
* @param CMDBObjectSet $oSet
* @param array $aExtraParams
*
* @return string
*
* @throws CoreException
* @throws DictExceptionMissingString
* @throws Exception
*/
public static function GetSearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
{
static $iSearchFormId = 0;
$bMultiSelect = false;
$oAppContext = new ApplicationContext();
$sHtml = '';
$sClassName = $oSet->GetFilter()->GetClass();
// Simple search form
if (isset($aExtraParams['currentId']))
{
$sSearchFormId = $aExtraParams['currentId'];
}
else
{
$iSearchFormId = utils::GetUniqueId();
$sSearchFormId = 'SimpleSearchForm'.$iSearchFormId;
$sHtml .= "<div id=\"ds_$sSearchFormId\" class=\"mini_tab{$iSearchFormId}\">\n";
}
// Check if the current class has some sub-classes
if (isset($aExtraParams['baseClass']))
{
$sRootClass = $aExtraParams['baseClass'];
}
else
{
$sRootClass = $sClassName;
}
$aSubClasses = MetaModel::GetSubclasses($sRootClass);
if (count($aSubClasses) > 0)
{
$aOptions = array();
$aOptions[MetaModel::GetName($sRootClass)] = "<option value=\"$sRootClass\">".MetaModel::GetName($sRootClass)."</options>\n";
foreach($aSubClasses as $sSubclassName)
{
$aOptions[MetaModel::GetName($sSubclassName)] = "<option value=\"$sSubclassName\">".MetaModel::GetName($sSubclassName)."</options>\n";
}
$aOptions[MetaModel::GetName($sClassName)] = "<option selected value=\"$sClassName\">".MetaModel::GetName($sClassName)."</options>\n";
ksort($aOptions);
$sContext = $oAppContext->GetForLink();
$sClassesCombo = "<select name=\"class\" onChange=\"ReloadSearchForm('$sSearchFormId', this.value, '$sRootClass', '$sContext')\">\n".implode('', $aOptions)."</select>\n";
}
else
{
$sClassesCombo = MetaModel::GetName($sClassName);
}
$oUnlimitedFilter = new DBObjectSearch($sClassName);
$sAction = (isset($aExtraParams['action'])) ? $aExtraParams['action'] : utils::GetAbsoluteUrlAppRoot().'pages/UI.php';
$index = 0;
if (version_compare(ITOP_DESIGN_LATEST_VERSION , '3.0') < 0) {
$sHtml .= "<form id=\"fs_{$sSearchFormId}\" action=\"{$sAction}\">\n"; // Don't use $_SERVER['SCRIPT_NAME'] since the form may be called asynchronously (from ajax.php)
$sHtml .= "<h2>".Dict::Format('UI:SearchFor_Class_Objects', $sClassesCombo)."</h2>\n";
$sHtml .= "<div>\n";
} else {
$sHtml .= "<div class='ibo-panel--header'><div class=''ibo-panel--titles'><div class='ibo-panel--title'>".Dict::Format('UI:SearchFor_Class_Objects', $sClassesCombo)."</div></div></div>\n";
$sHtml .= "<div class='ibo-panel--body SearchDrawer'>\n";
$sHtml .= "<form id=\"fs_{$sSearchFormId}\" action=\"{$sAction}\">\n"; // Don't use $_SERVER['SCRIPT_NAME'] since the form may be called asynchronously (from ajax.php)
}
$aMapCriteria = array();
$aList = MetaModel::GetZListItems($sClassName, 'standard_search');
$aConsts = $oSet->ListConstantFields(); // Some fields are constants based on the query/context
$sClassAlias = $oSet->GetFilter()->GetClassAlias();
foreach($aList as $sFilterCode)
{
//$oAppContext->Reset($sFilterCode); // Make sure the same parameter will not be passed twice
$sHtml .= '<div class="SearchAttribute" style="white-space: nowrap;padding:5px;display:inline-block;">';
$sFilterValue = isset($aConsts[$sClassAlias][$sFilterCode]) ? $aConsts[$sClassAlias][$sFilterCode] : '';
$sFilterValue = utils::ReadParam($sFilterCode, $sFilterValue, false, 'raw_data');
$sFilterOpCode = null; // Use the default 'loose' OpCode
if (empty($sFilterValue))
{
if (isset($aMapCriteria[$sFilterCode]))
{
if (count($aMapCriteria[$sFilterCode]) > 1)
{
$sFilterValue = Dict::S('UI:SearchValue:Mixed');
}
else
{
$sFilterValue = $aMapCriteria[$sFilterCode][0]['value'];
$sFilterOpCode = $aMapCriteria[$sFilterCode][0]['opcode'];
}
// Todo: Investigate...
if ($sFilterCode != 'company')
{
$oUnlimitedFilter->AddCondition($sFilterCode, $sFilterValue, $sFilterOpCode);
}
}
}
$oAttDef = MetaModel::GetAttributeDef($sClassName, $sFilterCode);
if ($oAttDef->IsExternalKey(EXTKEY_ABSOLUTE))
{
$oKeyAttDef = $oAttDef->GetFinalAttDef();
$sKeyAttClass = $oKeyAttDef->GetHostClass();
$sKeyAttCode = $oKeyAttDef->GetCode();
$sTargetClass = $oKeyAttDef->GetTargetClass();
$oSearch = new DBObjectSearch($sTargetClass);
$oSearch->SetModifierProperty('UserRightsGetSelectFilter', 'bSearchMode', true);
$oAllowedValues = new DBObjectSet($oSearch);
$sHtml .= "<label>".MetaModel::GetLabel($sKeyAttClass, $sKeyAttCode).":</label> ";
$aExtKeyParams = $aExtraParams;
$aExtKeyParams['iFieldSize'] = $oKeyAttDef->GetMaxSize();
$aExtKeyParams['iMinChars'] = $oKeyAttDef->GetMinAutoCompleteChars();
$sHtml .= UIExtKeyWidget::DisplayFromAttCode($oPage, $sKeyAttCode, $sKeyAttClass, $oAttDef->GetLabel(), $oAllowedValues, $sFilterValue, $sSearchFormId.'search_'.$sFilterCode, false, $sFilterCode, '', $aExtKeyParams, true);
}
else
{
$aAllowedValues = $oAttDef->GetAllowedValues();//MetaModel::GetAllowedValues_flt($sClassName, $sFilterCode, $aExtraParams);
if (is_null($aAllowedValues))
{
// Any value is possible, display an input box
$sHtml .= "<label>".MetaModel::GetLabel($sClassName, $sFilterCode).":</label> <input class=\"textSearch\" name=\"$sFilterCode\" value=\"".htmlentities($sFilterValue, ENT_QUOTES, 'utf-8')."\"/>\n";
}
else
{
//Enum field, display a multi-select combo
$sValue = "<select class=\"multiselect\" size=\"1\" name=\"{$sFilterCode}[]\" multiple>\n";
$bMultiSelect = true;
//$sValue .= "<option value=\"\">".Dict::S('UI:SearchValue:Any')."</option>\n";
asort($aAllowedValues);
foreach($aAllowedValues as $key => $value)
{
if (is_array($sFilterValue) && in_array($key, $sFilterValue))
{
$sSelected = ' selected';
}
else if ($sFilterValue == $key)
{
$sSelected = ' selected';
}
else
{
$sSelected = '';
}
$sValue .= "<option value=\"$key\" $sSelected >$value</option>\n";
}
$sValue .= "</select>\n";
$sHtml .= "<label>".MetaModel::GetLabel($sClassName, $sFilterCode).":</label> $sValue\n";
}
}
unset($aExtraParams[$sFilterCode]);
// Finally, add a tooltip if one is defined for this attribute definition
$sTip = $oAttDef->GetHelpOnSmartSearch();
if (strlen($sTip) > 0)
{
$sTip = addslashes($sTip);
$sTip = str_replace(array("\n", "\r"), " ", $sTip);
// :input does represent in form visible input (INPUT, SELECT, TEXTAREA)
if(self::UseLegacy()){
$oPage->add_ready_script("$('form#fs_$sSearchFormId :input[name={$sFilterCode}]').qtip( { content: '$sTip', show: 'mouseover', hide: 'mouseout', style: { name: 'dark', tip: 'leftTop' }, position: { corner: { target: 'rightMiddle', tooltip: 'leftTop' }} } );");
}
else{
$oPage->add_ready_script(
<<<JS
$('form#fs_$sSearchFormId :input[name={$sFilterCode}]')
.attr('data-tooltip-html-enabled', true)
.attr('data-tooltip-content', '$sTip');
CombodoTooltip.InitTooltipFromMarkup($('form#fs_$sSearchFormId :input[name={$sFilterCode}]'));
JS
);
}
}
$index++;
$sHtml .= '</div> ';
}
if (version_compare(ITOP_DESIGN_LATEST_VERSION , '3.0') < 0) {
$sHtml .= "</div>\n";
}
$sHtml .= "<p align=\"right\"><input type=\"submit\" class=\"ibo-button ibo-is-regular ibo-is-primary\" value=\"".Dict::S('UI:Button:Search')."\"></p>\n";
if (isset($aExtraParams['table_id']))
{
// Rename to avoid collisions...
$aExtraParams['_table_id_'] = $aExtraParams['table_id'];
unset($aExtraParams['table_id']);
}
foreach($aExtraParams as $sName => $sValue)
{
if (is_scalar($sValue))
{
$sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"".htmlentities($sValue, ENT_QUOTES, 'UTF-8')."\" />\n";
}
}
$sHtml .= "<input type=\"hidden\" name=\"class\" value=\"$sClassName\" />\n";
$sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
$sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_form\" />\n";
$sHtml .= $oAppContext->GetForForm();
$sHtml .= "</form>\n";
if (version_compare(ITOP_DESIGN_LATEST_VERSION , '3.0') >= 0) {
$sHtml .= "</div>\n";
}
if (!isset($aExtraParams['currentId']))
{
$sHtml .= "</div><!-- Simple search form -->\n";
}
if ($bMultiSelect)
{
$aOptions = array(
'header' => true,
'checkAllText' => Dict::S('UI:SearchValue:CheckAll'),
'uncheckAllText' => Dict::S('UI:SearchValue:UncheckAll'),
'noneSelectedText' => Dict::S('UI:SearchValue:Any'),
'selectedText' => Dict::S('UI:SearchValue:NbSelected'),
'selectedList' => 1,
);
$sJSOptions = json_encode($aOptions);
$oPage->add_ready_script("$('.multiselect').multiselect($sJSOptions);");
}
return $sHtml;
}
}