ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilSearchResultPresentation.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23
33{
34 public const MODE_LUCENE = 1;
35 public const MODE_STANDARD = 2;
36
37 protected int $mode;
38
40 protected ilLanguage $lng;
41 protected ilCtrl $ctrl;
42 protected ilAccess $access;
43 protected ilTree $tree;
45 protected Factory $refinery;
46
47
48 private array $results = [];
49 private array $subitem_ids = [];
50 private array $has_more_ref_ids = [];
51 private array $all_references = [];
52 private ?ilLuceneSearcher $searcher = null;
53 private ?object $container = null;
54
55 protected string $prev;
56 protected string $next;
57 protected string $html;
58 protected string $thtml;
59
64 public function __construct(?object $container = null, int $a_mode = self::MODE_LUCENE)
65 {
66 global $DIC;
67
68 $this->tpl = $DIC->ui()->mainTemplate();
69 $this->lng = $DIC->language();
70 $this->ctrl = $DIC->ctrl();
71 $this->access = $DIC->access();
72 $this->tree = $DIC->repositoryTree();
73 $this->http = $DIC->http();
74 $this->refinery = $DIC->refinery();
75 $this->mode = $a_mode;
76 $this->container = $container;
77
78 $this->initReferences();
79
80 if ($this->http->wrapper()->query()->has('details')) {
82 $this->http->wrapper()->query()->retrieve(
83 'details',
84 $this->refinery->kindlyTo()->int()
85 )
86 );
87 }
88 }
89
93 public function getContainer(): object
94 {
95 return $this->container;
96 }
97
98 public function getMode(): int
99 {
100 return $this->mode;
101 }
102
106 public function setResults(array $a_result_data): void
107 {
108 $this->results = $a_result_data;
109 }
110
114 public function getResults(): array
115 {
116 return $this->results;
117 }
118
125 public function setSubitemIds(array $a_subids): void
126 {
127 $this->subitem_ids = $a_subids;
128 }
129
134 public function getSubitemIds(): array
135 {
136 return $this->subitem_ids;
137 }
138
142 public function getSubitemIdsByObject(int $a_obj_id): array
143 {
144 return (isset($this->subitem_ids[$a_obj_id]) && $this->subitem_ids[$a_obj_id]) ?
145 $this->subitem_ids[$a_obj_id] :
146 array();
147 }
148
149
150
154 protected function parseResultReferences(): void
155 {
156 foreach ($this->getResults() as $ref_id => $obj_id) {
157 $this->all_references[$ref_id][] = $ref_id;
158 $counter = 0;
159 foreach (ilObject::_getAllReferences((int) $obj_id) as $new_ref) {
160 if ($new_ref == $ref_id) {
161 continue;
162 }
163 if (!$this->access->checkAccess('read', '', $new_ref)) {
164 continue;
165 }
166 $this->all_references[$ref_id][] = $new_ref;
167 ++$counter;
168 }
169 $this->has_more_ref_ids[$ref_id] = $counter;
170 }
171 }
172
173 protected function getMoreReferencesCounter(int $a_ref_id): int
174 {
175 $references = ilSession::get('vis_references') ?? [];
176 if (!isset($this->has_more_ref_ids[$a_ref_id]) or
177 !$this->has_more_ref_ids[$a_ref_id] or
178 array_key_exists($a_ref_id, $references)) {
179 return 0;
180 }
181 return (int) $this->has_more_ref_ids[$a_ref_id];
182 }
183
184 protected function getAllReferences(int $a_ref_id): array
185 {
186 $references = ilSession::get('vis_references') ?? [];
187 if (array_key_exists($a_ref_id, $references)) {
188 return $this->all_references[$a_ref_id] ?: array();
189 } else {
190 return array($a_ref_id);
191 }
192 }
193
198 public function getHTML(): string
199 {
200 return $this->thtml;
201 }
202
206 public function setSearcher(ilLuceneSearcher $a_searcher): void
207 {
208 $this->searcher = $a_searcher;
209 }
210
214 public function render(): bool
215 {
216 return $this->renderItemList();
217 }
218
222 public function setPreviousNext(string $a_p, string $a_n): void
223 {
224 $this->prev = $a_p;
225 $this->next = $a_n;
226 }
227
228
232 protected function renderItemList(): bool
233 {
234 $this->html = '';
235
236 $this->parseResultReferences();
237
238 $this->lng->loadLanguageModule("cntr"); // #16834
239
241
242 $set = array();
243 foreach ($this->getResults() as $c_ref_id => $obj_id) {
244 $c_ref_id = (int) $c_ref_id;
245 $obj_id = (int) $obj_id;
246 foreach ($this->getAllReferences($c_ref_id) as $ref_id) {
247 if (!$this->tree->isInTree($ref_id)) {
248 continue;
249 }
250
251 $obj_type = ilObject::_lookupType($obj_id);
252
253 $set[] = array(
254 "ref_id" => $ref_id,
255 "obj_id" => $obj_id,
256 "title" => $this->lookupTitle($obj_id, 0),
257 "title_sort" => ilObject::_lookupTitle((int) $obj_id),
258 "description" => $this->lookupDescription((int) $obj_id, 0),
259 "type" => $obj_type,
260 "relevance" => $this->getRelevance($obj_id),
261 "s_relevance" => sprintf("%03d", $this->getRelevance($obj_id)),
262 'create_date' => ilObject::_lookupCreationDate($obj_id)
263 );
264
265 $preloader->addItem($obj_id, $obj_type, $ref_id);
266 }
267 }
268
269 if (!count($set)) {
270 return false;
271 }
272
273 $preloader->preload();
274 unset($preloader);
275
276 $result_table = new ilSearchResultTableGUI($this->container, "showSavedResults", $this);
277 $result_table->setCustomPreviousNext($this->prev, $this->next);
278
279 $result_table->setDataAndApplySortation($set);
280 $this->thtml = $result_table->getHTML();
281 return true;
282 }
283
284
285 public function getRelevance(int $a_obj_id): float
286 {
287 if ($this->getMode() == self::MODE_LUCENE) {
288 return $this->searcher->getResult()->getRelevance($a_obj_id);
289 }
290 return 0;
291 }
292
293 public function lookupTitle(int $a_obj_id, int $a_sub_id): string
294 {
295 if ($this->getMode() != self::MODE_LUCENE or !is_object($this->searcher->getHighlighter())) {
296 return ilObject::_lookupTitle($a_obj_id);
297 }
298 if (strlen($title = $this->searcher->getHighlighter()->getTitle($a_obj_id, $a_sub_id))) {
299 return $title;
300 }
301 return ilObject::_lookupTitle($a_obj_id);
302 }
303
304 public function lookupDescription(int $a_obj_id, int $a_sub_id): string
305 {
306 if ($this->getMode() != self::MODE_LUCENE or !is_object($this->searcher->getHighlighter())) {
307 return ilObject::_lookupDescription($a_obj_id);
308 }
309 if (strlen($title = $this->searcher->getHighlighter()->getDescription($a_obj_id, $a_sub_id))) {
310 return $title;
311 }
312 return ilObject::_lookupDescription($a_obj_id);
313 }
314
315 public function lookupContent(int $a_obj_id, int $a_sub_id): string
316 {
317 if ($this->getMode() != self::MODE_LUCENE or !is_object($this->searcher->getHighlighter())) {
318 return '';
319 }
320 return $this->searcher->getHighlighter()->getContent($a_obj_id, $a_sub_id);
321 }
322
327 ilObjectListGUI $item_list_gui,
328 int $ref_id,
329 int $obj_id,
330 string $type
331 ): void {
332 $sub = $this->appendSubItems($item_list_gui, $ref_id, $obj_id, $type);
333 $path = $this->appendPath($ref_id);
334 $more = $this->appendMorePathes($ref_id);
335
336 if (!strlen($sub) and
337 !strlen($path) and
338 !strlen($more)) {
339 return;
340 }
341 $tpl = new ilTemplate('tpl.lucene_additional_information.html', true, true, 'components/ILIAS/Search');
342 $tpl->setVariable('SUBITEM', $sub);
343 if (strlen($path)) {
344 $tpl->setVariable('PATH', $path);
345 }
346 if (strlen($more)) {
347 $tpl->setVariable('MORE_PATH', $more);
348 }
349
350 $item_list_gui->setAdditionalInformation($tpl->get());
351 }
352
353
354 protected function appendPath(int $a_ref_id): string
355 {
356 $path_gui = new ilPathGUI();
357 $path_gui->enableTextOnly(false);
358 $path_gui->setUseImages(false);
359
360 $tpl = new ilTemplate('tpl.lucene_path.html', true, true, 'components/ILIAS/Search');
361 $tpl->setVariable('PATH_ITEM', $path_gui->getPath(ROOT_FOLDER_ID, $a_ref_id));
362 return $tpl->get();
363 }
364
365 protected function appendMorePathes(int $a_ref_id): string
366 {
367 if ($this->getMode() != self::MODE_LUCENE) {
368 return '';
369 }
370
371
372 if (!$num_refs = $this->getMoreReferencesCounter($a_ref_id)) {
373 return '';
374 }
375 $tpl = new ilTemplate('tpl.lucene_more_references.html', true, true, 'components/ILIAS/Search');
376 $this->ctrl->setParameter($this->getContainer(), 'refs', $a_ref_id);
377 $tpl->setVariable('MORE_REFS_LINK', $this->ctrl->getLinkTarget($this->getContainer(), ''));
378 $this->ctrl->clearParameters($this->getContainer());
379
380 $tpl->setVariable('TXT_MORE_REFS', sprintf($this->lng->txt('lucene_all_occurrences'), $num_refs));
381 return $tpl->get();
382 }
383
384
385 protected function appendSubItems(
386 ilObjectListGUI $item_list_gui,
387 int $ref_id,
388 int $obj_id,
389 string $a_type
390 ): string {
391 $subitem_ids = array();
392 $highlighter = null;
393 if ($this->getMode() == self::MODE_STANDARD) {
394 $subitem_ids = $this->getSubitemIdsByObject($obj_id);
395 } elseif (is_object($this->searcher->getHighlighter())) {
396 $subitem_ids = $this->searcher->getHighlighter()->getSubitemIds($obj_id);
397 $highlighter = $this->searcher->getHighlighter();
398 }
399
400 if (!count($subitem_ids)) {
401 return '';
402 }
403
404 // Build subitem list
405 $sub_list = ilLuceneSubItemListGUIFactory::getInstanceByType($a_type, $this->getContainer());
406 $sub_list->setHighlighter($highlighter);
407 $sub_list->init($item_list_gui, $ref_id, $subitem_ids);
408 return $sub_list->getHTML();
409 }
410
411 protected function initReferences(): void
412 {
413 $session_references = ilSession::get('vis_references') ?? [];
414 if ($this->http->wrapper()->post()->has('refs')) {
415 $refs = $this->http->wrapper()->post()->retrieve(
416 'refs',
417 $this->refinery->kindlyTo()->int()
418 );
419 $session_references[$refs] = $refs;
420 ilSession::set('vis_references', $session_references);
421 }
422 }
423}
Builds data types.
Definition: Factory.php:36
Class ilAccessHandler Checks access for ILIAS objects.
Class ilCtrl provides processing control methods.
language handling
Reads and parses lucene search results.
static getInstanceByType(string $a_type, object $a_cmd_class)
get instance by type
Preloader for object list GUIs.
setAdditionalInformation(?string $val)
static _lookupType(int $id, bool $reference=false)
static _getAllReferences(int $id)
get all reference ids for object ID
static _lookupCreationDate(int $obj_id)
static _lookupTitle(int $obj_id)
static _lookupDescription(int $obj_id)
Presentation of search results using object list gui.
setSubitemIds(array $a_subids)
Set subitem ids Used for like and fulltext search.
appendAdditionalInformation(ilObjectListGUI $item_list_gui, int $ref_id, int $obj_id, string $type)
Append path, relevance information.
setPreviousNext(string $a_p, string $a_n)
Set previous next.
lookupContent(int $a_obj_id, int $a_sub_id)
__construct(?object $container=null, int $a_mode=self::MODE_LUCENE)
Constructor.
lookupTitle(int $a_obj_id, int $a_sub_id)
parseResultReferences()
Check if more than one reference is visible.
appendSubItems(ilObjectListGUI $item_list_gui, int $ref_id, int $obj_id, string $a_type)
lookupDescription(int $a_obj_id, int $a_sub_id)
setSearcher(ilLuceneSearcher $a_searcher)
set searcher
getSubitemIdsByObject(int $a_obj_id)
Get subitem ids for an object.
setResults(array $a_result_data)
Set result array.
TableGUI class for search results.
static get(string $a_var)
static set(string $a_var, $a_val)
Set a value.
static setShowDetails(int $obj_id)
set show details.
special template class to simplify handling of ITX/PEAR
Tree class data representation in hierachical trees using the Nested Set Model with Gaps by Joe Celco...
const ROOT_FOLDER_ID
Definition: constants.php:32
Interface GlobalHttpState.
setVariable(string $variable, $value='')
Sets the given variable to the given value.
get(string $part=self::DEFAULT_BLOCK)
Renders the given block and returns the html string.
$ref_id
Definition: ltiauth.php:66
$path
Definition: ltiservices.php:30
static http()
Fetches the global http state from ILIAS.
global $DIC
Definition: shib_login.php:26
$counter