ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilContainerRenderer.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3require_once('./Services/Repository/classes/class.ilObjectPlugin.php');
4
12{
16 protected $lng;
17
21 protected $settings;
22
26 protected $obj_definition;
27
28 // switches
29 protected $enable_manage_select_all; // [bool]
30 protected $enable_multi_download; // [bool]
31 protected $active_block_ordering; // [bool]
32
33 // properties
34 protected $type_blocks = array(); // [array]
35 protected $custom_blocks = array(); // [array]
36 protected $items = array(); // [array]
37 protected $hidden_items = array(); // [array]
38 protected $block_items = array(); // [array]
39 protected $details = array(); // [array]
40 protected $item_ids = array(); // [array]
41
42 // block (unique) ids
43 protected $rendered_blocks = array(); // [array]
44 protected $bl_cnt = 0; // [int]
45 protected $cur_row_type; // [string]
46
47 // ordering
48 protected $block_pos = array(); // [array]
49 protected $block_custom_pos = array(); // [array]
50 protected $order_cnt = 0; // [int]
51
52 const UNIQUE_SEPARATOR = "-";
53
63 public function __construct($a_enable_manage_select_all = false, $a_enable_multi_download = false, $a_active_block_ordering = false, array $a_block_custom_positions = null)
64 {
65 global $DIC;
66
67 $this->lng = $DIC->language();
68 $this->settings = $DIC->settings();
69 $this->obj_definition = $DIC["objDefinition"];
70 $this->enable_manage_select_all = (bool) $a_enable_manage_select_all;
71 $this->enable_multi_download = (bool) $a_enable_multi_download;
72 $this->active_block_ordering = (bool) $a_active_block_ordering;
73 $this->block_custom_pos = $a_block_custom_positions;
74 }
75
76
77 //
78 // blocks
79 //
80
89 public function addTypeBlock($a_type, $a_prefix = null, $a_postfix = null)
90 {
91 if ($a_type != "itgr" &&
92 !$this->hasTypeBlock($a_type)) {
93 $this->type_blocks[$a_type] = array(
94 "prefix" => $a_prefix
95 ,"postfix" => $a_postfix
96 );
97 return true;
98 }
99 return false;
100 }
101
108 public function hasTypeBlock($a_type)
109 {
110 return array_key_exists($a_type, $this->type_blocks);
111 }
112
121 public function addCustomBlock($a_id, $a_caption, $a_actions = null, $a_data = array())
122 {
123 if (!$this->hasCustomBlock($a_id)) {
124 $this->custom_blocks[$a_id] = array(
125 "caption" => $a_caption
126 ,"actions" => $a_actions
127 ,"data" => $a_data
128 );
129 return true;
130 }
131 return false;
132 }
133
140 public function hasCustomBlock($a_id)
141 {
142 return array_key_exists($a_id, $this->custom_blocks);
143 }
144
151 public function isValidBlock($a_id)
152 {
153 return ($this->hasTypeBlock($a_id) ||
154 $this->hasCustomBlock($a_id));
155 }
156
157
158 //
159 // items
160 //
161
167 public function hideItem($a_id)
168 {
169 // see hasItem();
170 $this->hidden_items[$a_id] = true;
171
172 // #16629 - do not remove hidden items from other blocks
173 // $this->removeItem($a_id);
174 }
175
181 public function removeItem($a_id)
182 {
183 if (!$this->hasItem($a_id)) {
184 return;
185 }
186
187 unset($this->item_ids[$a_id]);
188 unset($this->hidden_items[$a_id]);
189
190 foreach (array_keys($this->items) as $item_id) {
191 if (array_pop(explode(self::UNIQUE_SEPARATOR, $item_id)) == $a_id) {
192 unset($this->items[$item_id]);
193 }
194 }
195
196 foreach ($this->block_items as $block_id => $items) {
197 foreach ($items as $idx => $item_id) {
198 if (array_pop(explode(self::UNIQUE_SEPARATOR, $item_id)) == $a_id) {
199 unset($this->block_items[$block_id][$idx]);
200 if (!sizeof($this->block_items[$block_id])) {
201 unset($this->block_items[$block_id]);
202 }
203 break;
204 }
205 }
206 }
207 }
208
215 public function hasItem($a_id)
216 {
217 return (array_key_exists($a_id, $this->item_ids) ||
218 array_key_exists($a_id, $this->hidden_items));
219 }
220
231 public function addItemToBlock($a_block_id, $a_item_type, $a_item_id, $a_item_html, $a_force = false)
232 {
233 if ($this->isValidBlock($a_block_id) &&
234 $a_item_type != "itgr" &&
235 (!$this->hasItem($a_item_id) || $a_force) &&
236 trim($a_item_html)) {
237 // #16563 - item_id (== ref_id) is NOT unique, adding parent block id
238 $uniq_id = $a_block_id . self::UNIQUE_SEPARATOR . $a_item_id;
239
240 $this->items[$uniq_id] = array(
241 "type" => $a_item_type
242 ,"html" => $a_item_html
243 );
244
245 // #18326
246 $this->item_ids[$a_item_id] = true;
247
248 $this->block_items[$a_block_id][] = $uniq_id;
249 return true;
250 }
251 return false;
252 }
253
261 public function addDetailsLevel($a_level, $a_url, $a_active = false)
262 {
263 $this->details[$a_level] = array(
264 "url" => $a_url
265 ,"active" => (bool) $a_active
266 );
267 }
268
272 public function resetDetails()
273 {
274 $this->details = array();
275 }
276
277
278 //
279 // render
280 //
281
288 public function setBlockPosition($a_block_id, $a_pos)
289 {
290 if ($this->isValidBlock($a_block_id)) {
291 $this->block_pos[$a_block_id] = $a_pos;
292 }
293 }
294
300 public function getHTML()
301 {
302 $valid = false;
303
304 $block_tpl = $this->initBlockTemplate();
305
306 foreach ($this->processBlockPositions() as $block_id) {
307 if (array_key_exists($block_id, $this->custom_blocks)) {
308 if ($this->renderHelperCustomBlock($block_tpl, $block_id)) {
309 $this->addSeparatorRow($block_tpl);
310 $valid = true;
311 }
312 }
313 if (array_key_exists($block_id, $this->type_blocks)) {
314 if ($this->renderHelperTypeBlock($block_tpl, $block_id)) {
315 $this->addSeparatorRow($block_tpl);
316 $valid = true;
317 }
318 }
319 }
320
321 if ($valid) {
322 $this->renderDetails($block_tpl);
323
324 return $block_tpl->get();
325 }
326 }
327
335 {
336 $block_tpl = $this->initBlockTemplate();
337
338 if ($this->renderHelperTypeBlock($block_tpl, $a_type, true)) {
339 return $block_tpl->get();
340 }
341 }
342
349 public function renderSingleCustomBlock($a_id)
350 {
351 $block_tpl = $this->initBlockTemplate();
352
353 if ($this->renderHelperCustomBlock($block_tpl, $a_id, true)) {
354 return $block_tpl->get();
355 }
356 }
357
358
359 //
360 // render (helper)
361 //
362
368 protected function processBlockPositions()
369 {
370 // manual order
371 if (is_array($this->block_custom_pos) && sizeof($this->block_custom_pos)) {
372 $tmp = $this->block_pos;
373 $this->block_pos = array();
374 foreach ($this->block_custom_pos as $idx => $block_id) {
375 if ($this->isValidBlock($block_id)) {
376 $this->block_pos[$block_id] = $idx;
377 }
378 }
379
380 // at least some manual are valid
381 if (sizeof($this->block_pos)) {
382 // append missing blocks from default order
383 $last = max($this->block_pos);
384 foreach (array_keys($tmp) as $block_id) {
385 if (!array_key_exists($block_id, $this->block_pos)) {
386 $this->block_pos[$block_id] = ++$last;
387 }
388 }
389 }
390 // all manual invalid, use default
391 else {
392 $this->block_pos = $tmp;
393 }
394 }
395
396 // add missing blocks to order
397 $last = sizeof($this->block_pos)
398 ? max($this->block_pos)
399 : 0;
400 foreach (array_keys($this->custom_blocks) as $block_id) {
401 if (!array_key_exists($block_id, $this->block_pos)) {
402 $this->block_pos[$block_id] = ++$last;
403 }
404 }
405 foreach (array_keys($this->type_blocks) as $block_id) {
406 if (!array_key_exists($block_id, $this->block_pos)) {
407 $this->block_pos[$block_id] = ++$last;
408 }
409 }
410
411 asort($this->block_pos);
412
413 return array_keys($this->block_pos);
414 }
415
424 protected function renderHelperCustomBlock(ilTemplate $a_block_tpl, $a_block_id, $a_is_single = false)
425 {
426 if ($this->hasCustomBlock($a_block_id)) {
427 return $this->renderHelperGeneric($a_block_tpl, $a_block_id, $this->custom_blocks[$a_block_id], $a_is_single);
428 }
429 return false;
430 }
431
440 protected function renderHelperTypeBlock(ilTemplate $a_block_tpl, $a_type, $a_is_single = false)
441 {
442 if ($this->hasTypeBlock($a_type)) {
443 $block = $this->type_blocks[$a_type];
444 $block["type"] = $a_type;
445
446 return $this->renderHelperGeneric($a_block_tpl, $a_type, $block, $a_is_single);
447 }
448 return false;
449 }
450
460 protected function renderHelperGeneric(ilTemplate $a_block_tpl, $a_block_id, array $a_block, $a_is_single = false)
461 {
462 if (!in_array($a_block_id, $this->rendered_blocks)) {
463 $this->rendered_blocks[] = $a_block_id;
464
465 $block_types = array();
466 if (is_array($this->block_items[$a_block_id])) {
467 foreach ($this->block_items[$a_block_id] as $item_id) {
468 if (isset($this->items[$item_id]["type"])) {
469 $block_types[] = $this->items[$item_id]["type"];
470 }
471 }
472 }
473
474 // #14610 - manage empty item groups
475 if (is_array($this->block_items[$a_block_id]) ||
476 is_numeric($a_block_id)) {
477 $order_id = (!$a_is_single && $this->active_block_ordering)
478 ? $a_block_id
479 : null;
480 $this->addHeaderRow($a_block_tpl, $a_block["type"], $a_block["caption"], array_unique($block_types), $a_block["actions"], $order_id, $a_block["data"]);
481
482 if ($a_block["prefix"]) {
483 $this->addStandardRow($a_block_tpl, $a_block["prefix"]);
484 }
485
486 if (is_array($this->block_items[$a_block_id])) {
487 foreach ($this->block_items[$a_block_id] as $item_id) {
488 $this->addStandardRow($a_block_tpl, $this->items[$item_id]["html"], $item_id);
489 }
490 }
491
492 if ($a_block["postfix"]) {
493 $this->addStandardRow($a_block_tpl, $a_block["postfix"]);
494 }
495
496 return true;
497 }
498 }
499
500 return false;
501 }
502
508 protected function initBlockTemplate()
509 {
510 // :TODO: obsolete?
511 $this->cur_row_type = "row_type_1";
512
513 return new ilTemplate(
514 "tpl.container_list_block.html",
515 true,
516 true,
517 "Services/Container"
518 );
519 }
520
531 protected function addHeaderRow(ilTemplate $a_tpl, $a_type = "", $a_text = "", array $a_types_in_block = null, $a_commands_html = null, $a_order_id = null, $a_data = array())
532 {
535 $objDefinition = $this->obj_definition;
536
537 $a_tpl->setVariable("CB_ID", ' id="bl_cntr_' . (++$this->bl_cnt) . '"');
538
539 if ($this->enable_manage_select_all) {
540 $this->renderSelectAllBlock($a_tpl);
541 } elseif ($this->enable_multi_download) {
542 if ($a_type) {
543 $a_types_in_block = array($a_type);
544 }
545 foreach ($a_types_in_block as $type) {
546 if (in_array($type, $this->getDownloadableTypes())) {
547 $this->renderSelectAllBlock($a_tpl);
548 break;
549 }
550 }
551 }
552
553 if ($a_text == "" && $a_type != "") {
554 if (!$objDefinition->isPlugin($a_type)) {
555 $title = $lng->txt("objs_" . $a_type);
556 } else {
557 include_once("./Services/Component/classes/class.ilPlugin.php");
559 $title= $pl->txt("objs_" . $a_type);
560 }
561 } else {
562 $title = $a_text;
563 }
564
565 include_once("./Modules/ItemGroup/classes/class.ilItemGroupBehaviour.php");
566 if (is_array($a_data)) {
567 foreach ($a_data as $k => $v) {
568 $a_tpl->setCurrentBlock("cb_data");
569 $a_tpl->setVariable("DATA_KEY", $k);
570 $a_tpl->setVariable("DATA_VALUE", $v);
571 $a_tpl->parseCurrentBlock();
572
573 if ($k == "behaviour" && $v == ilItemGroupBehaviour::EXPANDABLE_CLOSED) {
574 $a_tpl->touchBlock("container_items_hide");
575 }
576 }
577 }
578
579 if ($ilSetting->get("icon_position_in_lists") != "item_rows" &&
580 $a_type != "") {
581 $icon = ilUtil::getImagePath("icon_" . $a_type . ".svg");
582
583 $a_tpl->setCurrentBlock("container_header_row_image");
584 $a_tpl->setVariable("HEADER_IMG", $icon);
585 $a_tpl->setVariable("HEADER_ALT", $title);
586 } else {
587 $a_tpl->setCurrentBlock("container_header_row");
588 }
589
590 if ($a_order_id) {
591 $a_tpl->setVariable("BLOCK_HEADER_ORDER_NAME", "position[blocks][" . $a_order_id . "]");
592 $a_tpl->setVariable("BLOCK_HEADER_ORDER_NUM", (++$this->order_cnt)*10);
593 }
594
595 $a_tpl->setVariable("BLOCK_HEADER_CONTENT", $title);
596 $a_tpl->setVariable("CHR_COMMANDS", $a_commands_html);
597 $a_tpl->parseCurrentBlock();
598
599 //$a_tpl->touchBlock("container_row");
600
601 $this->resetRowType();
602 }
603
611 protected function addStandardRow(ilTemplate $a_tpl, $a_html, $a_ref_id = 0)
612 {
613 // :TODO: obsolete?
614 $this->cur_row_type = ($this->cur_row_type == "row_type_1")
615 ? "row_type_2"
616 : "row_type_1";
617
618 if ($a_ref_id > 0) {
619 $a_tpl->setCurrentBlock($this->cur_row_type);
620 $a_tpl->setVariable("ROW_ID", 'id="item_row_' . $a_ref_id . '"');
621 $a_tpl->parseCurrentBlock();
622 } else {
623 $a_tpl->touchBlock($this->cur_row_type);
624 }
625
626 $a_tpl->setCurrentBlock("container_standard_row");
627 $a_tpl->setVariable("BLOCK_ROW_CONTENT", $a_html);
628 $a_tpl->parseCurrentBlock();
629
630 $a_tpl->touchBlock("container_row");
631 }
632
638 protected function renderSelectAllBlock(ilTemplate $a_tpl)
639 {
641
642 $a_tpl->setCurrentBlock("select_all_row");
643 $a_tpl->setVariable("CHECKBOXNAME", "bl_cb_" . $this->bl_cnt);
644 $a_tpl->setVariable("SEL_ALL_PARENT", "bl_cntr_" . $this->bl_cnt);
645 $a_tpl->setVariable("SEL_ALL_PARENT", "bl_cntr_" . $this->bl_cnt);
646 $a_tpl->setVariable("TXT_SELECT_ALL", $lng->txt("select_all"));
647 $a_tpl->parseCurrentBlock();
648 }
649
655 protected function addSeparatorRow(ilTemplate $a_tpl)
656 {
657 $a_tpl->setCurrentBlock("container_block");
658 $a_tpl->parseCurrentBlock();
659 }
660
664 protected function resetRowType()
665 {
666 // :TODO: obsolete?
667 $this->cur_row_type = "";
668 }
669
675 protected function getDownloadableTypes()
676 {
677 return array("fold", "file");
678 }
679
685 public function renderDetails(ilTemplate $a_tpl)
686 {
688
689 if (sizeof($this->details)) {
690 $a_tpl->setCurrentBlock('container_details_row');
691 $a_tpl->setVariable('TXT_DETAILS', $lng->txt('details'));
692 $a_tpl->parseCurrentBlock();
693 }
694 }
695}
An exception for terminatinating execution or to throw for unit testing.
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:613
Class ilContainerRenderer.
removeItem($a_id)
Remove item (from any block)
getDownloadableTypes()
Get downloadable repository object types.
renderHelperGeneric(ilTemplate $a_block_tpl, $a_block_id, array $a_block, $a_is_single=false)
Render block.
renderHelperTypeBlock(ilTemplate $a_block_tpl, $a_type, $a_is_single=false)
Render type block.
resetRowType()
Reset internal row type.
addTypeBlock($a_type, $a_prefix=null, $a_postfix=null)
Add type block.
addItemToBlock($a_block_id, $a_item_type, $a_item_id, $a_item_html, $a_force=false)
Add item to existing block.
renderSingleCustomBlock($a_id)
Get rendered html of single custom block.
addDetailsLevel($a_level, $a_url, $a_active=false)
Add details level.
hideItem($a_id)
Mark item id as used, but do not render.
getHTML()
Get rendered html (of all blocks)
addHeaderRow(ilTemplate $a_tpl, $a_type="", $a_text="", array $a_types_in_block=null, $a_commands_html=null, $a_order_id=null, $a_data=array())
Render block header.
hasCustomBlock($a_id)
Custom block already exists?
hasTypeBlock($a_type)
Type block already exists?
renderHelperCustomBlock(ilTemplate $a_block_tpl, $a_block_id, $a_is_single=false)
Render custom block.
addStandardRow(ilTemplate $a_tpl, $a_html, $a_ref_id=0)
Render item row.
hasItem($a_id)
Item with id exists?
renderSingleTypeBlock($a_type)
Get rendered html of single type block.
renderSelectAllBlock(ilTemplate $a_tpl)
Render "select all".
setBlockPosition($a_block_id, $a_pos)
Set block position.
addSeparatorRow(ilTemplate $a_tpl)
Render separator row.
processBlockPositions()
Process block positions.
resetDetails()
Reset/remove all detail levels.
renderDetails(ilTemplate $a_tpl)
Render detail level.
isValidBlock($a_id)
Any block with id exists?
addCustomBlock($a_id, $a_caption, $a_actions=null, $a_data=array())
Add custom block.
__construct($a_enable_manage_select_all=false, $a_enable_multi_download=false, $a_active_block_ordering=false, array $a_block_custom_positions=null)
Constructor.
static getRepoPluginObjectByType($type)
Return either a repoObject plugin or a orgunit extension plugin or null if the type is not a plugin.
special template class to simplify handling of ITX/PEAR
touchBlock($block)
overwrites ITX::touchBlock.
parseCurrentBlock($part="DEFAULT")
Überladene Funktion, die auf den aktuelle Block vorher noch ein replace ausführt @access public.
setCurrentBlock($part="DEFAULT")
Überladene Funktion, die sich hier lokal noch den aktuellen Block merkt.
static getImagePath($img, $module_path="", $mode="output", $offline=false)
get image path (for images located in a template directory)
$valid
global $ilSetting
Definition: privfeed.php:17
$type
global $DIC
Definition: saml.php:7
settings()
Definition: settings.php:2
$a_type
Definition: workflow.php:92