ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 */
3 
11 {
12  // switches
13  protected $enable_manage_select_all; // [bool]
14  protected $enable_multi_download; // [bool]
15  protected $active_block_ordering; // [bool]
16 
17  // properties
18  protected $type_blocks = array(); // [array]
19  protected $custom_blocks = array(); // [array]
20  protected $items = array(); // [array]
21  protected $hidden_items = array(); // [array]
22  protected $block_items = array(); // [array]
23  protected $details = array(); // [array]
24  protected $item_ids = array(); // [array]
25 
26  // block (unique) ids
27  protected $rendered_blocks = array(); // [array]
28  protected $bl_cnt = 0; // [int]
29  protected $cur_row_type; // [string]
30 
31  // ordering
32  protected $block_pos = array(); // [array]
33  protected $block_custom_pos = array(); // [array]
34  protected $order_cnt = 0; // [int]
35 
36  const UNIQUE_SEPARATOR = "-";
37 
47  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)
48  {
49  $this->enable_manage_select_all = (bool)$a_enable_manage_select_all;
50  $this->enable_multi_download = (bool)$a_enable_multi_download;
51  $this->active_block_ordering = (bool)$a_active_block_ordering;
52  $this->block_custom_pos = $a_block_custom_positions;
53  }
54 
55 
56  //
57  // blocks
58  //
59 
68  public function addTypeBlock($a_type, $a_prefix = null, $a_postfix = null)
69  {
70  if($a_type != "itgr" &&
71  !$this->hasTypeBlock($a_type))
72  {
73  $this->type_blocks[$a_type] = array(
74  "prefix" => $a_prefix
75  ,"postfix" => $a_postfix
76  );
77  return true;
78  }
79  return false;
80  }
81 
88  public function hasTypeBlock($a_type)
89  {
90  return array_key_exists($a_type, $this->type_blocks);
91  }
92 
101  public function addCustomBlock($a_id, $a_caption, $a_actions = null)
102  {
103  if(!$this->hasCustomBlock($a_id))
104  {
105  $this->custom_blocks[$a_id] = array(
106  "caption" => $a_caption
107  ,"actions" => $a_actions
108  );
109  return true;
110  }
111  return false;
112  }
113 
120  public function hasCustomBlock($a_id)
121  {
122  return array_key_exists($a_id, $this->custom_blocks);
123  }
124 
131  public function isValidBlock($a_id)
132  {
133  return ($this->hasTypeBlock($a_id) ||
134  $this->hasCustomBlock($a_id));
135  }
136 
137 
138  //
139  // items
140  //
141 
147  public function hideItem($a_id)
148  {
149  // see hasItem();
150  $this->hidden_items[$a_id] = true;
151 
152  // #16629 - do not remove hidden items from other blocks
153  // $this->removeItem($a_id);
154  }
155 
161  public function removeItem($a_id)
162  {
163  if(!$this->hasItem($a_id))
164  {
165  return;
166  }
167 
168  unset($this->item_ids[$a_id]);
169  unset($this->hidden_items[$a_id]);
170 
171  foreach(array_keys($this->items) as $item_id)
172  {
173  if(array_pop(explode(self::UNIQUE_SEPARATOR, $item_id)) == $a_id)
174  {
175  unset($this->items[$item_id]);
176  }
177  }
178 
179  foreach($this->block_items as $block_id => $items)
180  {
181  foreach($items as $idx => $item_id)
182  {
183  if(array_pop(explode(self::UNIQUE_SEPARATOR, $item_id)) == $a_id)
184  {
185  unset($this->block_items[$block_id][$idx]);
186  if(!sizeof($this->block_items[$block_id]))
187  {
188  unset($this->block_items[$block_id]);
189  }
190  break;
191  }
192  }
193  }
194  }
195 
202  public function hasItem($a_id)
203  {
204  return (array_key_exists($a_id, $this->item_ids) ||
205  array_key_exists($a_id, $this->hidden_items));
206  }
207 
218  public function addItemToBlock($a_block_id, $a_item_type, $a_item_id, $a_item_html, $a_force = false)
219  {
220  if($this->isValidBlock($a_block_id) &&
221  $a_item_type != "itgr" &&
222  (!$this->hasItem($a_item_id) || $a_force) &&
223  trim($a_item_html))
224  {
225  // #16563 - item_id (== ref_id) is NOT unique, adding parent block id
226  $uniq_id = $a_block_id.self::UNIQUE_SEPARATOR.$a_item_id;
227 
228  $this->items[$uniq_id] = array(
229  "type" => $a_item_type
230  ,"html" => $a_item_html
231  );
232 
233  // #18326
234  $this->item_ids[$a_item_id] = true;
235 
236  $this->block_items[$a_block_id][] = $uniq_id;
237 
238  return true;
239  }
240  return false;
241  }
242 
250  public function addDetailsLevel($a_level, $a_url, $a_active = false)
251  {
252  $this->details[$a_level] = array(
253  "url" => $a_url
254  ,"active" => (bool)$a_active
255  );
256  }
257 
261  public function resetDetails()
262  {
263  $this->details = array();
264  }
265 
266 
267  //
268  // render
269  //
270 
277  public function setBlockPosition($a_block_id, $a_pos)
278  {
279  if($this->isValidBlock($a_block_id))
280  {
281  $this->block_pos[$a_block_id] = $a_pos;
282  }
283  }
284 
290  public function getHTML()
291  {
292  $valid = false;
293 
294  $block_tpl = $this->initBlockTemplate();
295 
296  foreach($this->processBlockPositions() as $block_id)
297  {
298  if(array_key_exists($block_id, $this->custom_blocks))
299  {
300  if($this->renderHelperCustomBlock($block_tpl, $block_id))
301  {
302  $this->addSeparatorRow($block_tpl);
303  $valid = true;
304  }
305  }
306  if(array_key_exists($block_id, $this->type_blocks))
307  {
308  if($this->renderHelperTypeBlock($block_tpl, $block_id))
309  {
310  $this->addSeparatorRow($block_tpl);
311  $valid = true;
312  }
313  }
314  }
315 
316  if($valid)
317  {
318  $this->renderDetails($block_tpl);
319 
320  return $block_tpl->get();
321  }
322  }
323 
330  public function renderSingleTypeBlock($a_type)
331  {
332  $block_tpl = $this->initBlockTemplate();
333 
334  if($this->renderHelperTypeBlock($block_tpl, $a_type, true))
335  {
336  return $block_tpl->get();
337  }
338  }
339 
346  public function renderSingleCustomBlock($a_id)
347  {
348  $block_tpl = $this->initBlockTemplate();
349 
350  if($this->renderHelperCustomBlock($block_tpl, $a_id, true))
351  {
352  return $block_tpl->get();
353  }
354  }
355 
356 
357  //
358  // render (helper)
359  //
360 
366  protected function processBlockPositions()
367  {
368  // manual order
369  if(sizeof($this->block_custom_pos))
370  {
371  $tmp = $this->block_pos;
372  $this->block_pos = array();
373  foreach($this->block_custom_pos as $idx => $block_id)
374  {
375  if($this->isValidBlock($block_id))
376  {
377  $this->block_pos[$block_id] = $idx;
378  }
379  }
380 
381  // at least some manual are valid
382  if(sizeof($this->block_pos))
383  {
384  // append missing blocks from default order
385  $last = max($this->block_pos);
386  foreach(array_keys($tmp) as $block_id)
387  {
388  if(!array_key_exists($block_id, $this->block_pos))
389  {
390  $this->block_pos[$block_id] = ++$last;
391  }
392  }
393  }
394  // all manual invalid, use default
395  else
396  {
397  $this->block_pos = $tmp;
398  }
399  }
400 
401  // add missing blocks to order
402  $last = sizeof($this->block_pos)
403  ? max($this->block_pos)
404  : 0;
405  foreach(array_keys($this->custom_blocks) as $block_id)
406  {
407  if(!array_key_exists($block_id, $this->block_pos))
408  {
409  $this->block_pos[$block_id] = ++$last;
410  }
411  }
412  foreach(array_keys($this->type_blocks) as $block_id)
413  {
414  if(!array_key_exists($block_id, $this->block_pos))
415  {
416  $this->block_pos[$block_id] = ++$last;
417  }
418  }
419 
420  asort($this->block_pos);
421 
422  return array_keys($this->block_pos);
423  }
424 
433  protected function renderHelperCustomBlock(ilTemplate $a_block_tpl, $a_block_id, $a_is_single = false)
434  {
435  if($this->hasCustomBlock($a_block_id))
436  {
437  return $this->renderHelperGeneric($a_block_tpl, $a_block_id, $this->custom_blocks[$a_block_id], $a_is_single);
438  }
439  return false;
440  }
441 
450  protected function renderHelperTypeBlock(ilTemplate $a_block_tpl, $a_type, $a_is_single = false)
451  {
452  if($this->hasTypeBlock($a_type))
453  {
454  $block = $this->type_blocks[$a_type];
455  $block["type"] = $a_type;
456 
457  return $this->renderHelperGeneric($a_block_tpl, $a_type, $block, $a_is_single);
458  }
459  return false;
460  }
461 
471  protected function renderHelperGeneric(ilTemplate $a_block_tpl, $a_block_id, array $a_block, $a_is_single = false)
472  {
473  if(!in_array($a_block_id, $this->rendered_blocks))
474  {
475  $this->rendered_blocks[] = $a_block_id;
476 
477  $block_types = array();
478  if(is_array($this->block_items[$a_block_id]))
479  {
480  foreach($this->block_items[$a_block_id] as $item_id)
481  {
482  if(isset($this->items[$item_id]["type"]))
483  {
484  $block_types[] = $this->items[$item_id]["type"];
485  }
486  }
487  }
488 
489  // #14610 - manage empty item groups
490  if(is_array($this->block_items[$a_block_id]) ||
491  is_numeric($a_block_id))
492  {
493 
494  $order_id = (!$a_is_single && $this->active_block_ordering)
495  ? $a_block_id
496  : null;
497  $this->addHeaderRow($a_block_tpl, $a_block["type"], $a_block["caption"], array_unique($block_types), $a_block["actions"], $order_id);
498 
499  if($a_block["prefix"])
500  {
501  $this->addStandardRow($a_block_tpl, $a_block["prefix"]);
502  }
503 
504  if(is_array($this->block_items[$a_block_id]))
505  {
506  foreach($this->block_items[$a_block_id] as $item_id)
507  {
508  $this->addStandardRow($a_block_tpl, $this->items[$item_id]["html"], $item_id);
509  }
510  }
511 
512  if($a_block["postfix"])
513  {
514  $this->addStandardRow($a_block_tpl, $a_block["postfix"]);
515  }
516 
517  return true;
518  }
519  }
520 
521  return false;
522  }
523 
529  protected function initBlockTemplate()
530  {
531  // :TODO: obsolete?
532  $this->cur_row_type = "row_type_1";
533 
534  return new ilTemplate("tpl.container_list_block.html", true, true,
535  "Services/Container");
536  }
537 
548  protected function addHeaderRow(ilTemplate $a_tpl, $a_type = "", $a_text = "", array $a_types_in_block = null, $a_commands_html = null, $a_order_id = null)
549  {
550  global $lng, $ilSetting, $objDefinition;
551 
552  $a_tpl->setVariable("CB_ID", ' id="bl_cntr_'.(++$this->bl_cnt).'"');
553 
554  if ($this->enable_manage_select_all)
555  {
556  $this->renderSelectAllBlock($a_tpl);
557  }
558  else if ($this->enable_multi_download)
559  {
560  if ($a_type)
561  {
562  $a_types_in_block = array($a_type);
563  }
564  foreach($a_types_in_block as $type)
565  {
566  if (in_array($type, $this->getDownloadableTypes()))
567  {
568  $this->renderSelectAllBlock($a_tpl);
569  break;
570  }
571  }
572  }
573 
574  if ($a_text == "" && $a_type != "")
575  {
576  if (!$objDefinition->isPlugin($a_type))
577  {
578  $title = $lng->txt("objs_".$a_type);
579  }
580  else
581  {
582  include_once("./Services/Component/classes/class.ilPlugin.php");
583  $title = ilPlugin::lookupTxt("rep_robj", $a_type, "objs_".$a_type);
584  }
585  }
586  else
587  {
588  $title = $a_text;
589  }
590 
591  if ($ilSetting->get("icon_position_in_lists") != "item_rows" &&
592  $a_type != "")
593  {
594  $icon = ilUtil::getImagePath("icon_".$a_type.".svg");
595 
596  $a_tpl->setCurrentBlock("container_header_row_image");
597  $a_tpl->setVariable("HEADER_IMG", $icon);
598  $a_tpl->setVariable("HEADER_ALT", $title);
599  }
600  else
601  {
602  $a_tpl->setCurrentBlock("container_header_row");
603  }
604 
605  if($a_order_id)
606  {
607  $a_tpl->setVariable("BLOCK_HEADER_ORDER_NAME", "position[blocks][".$a_order_id."]");
608  $a_tpl->setVariable("BLOCK_HEADER_ORDER_NUM", (++$this->order_cnt)*10);
609  }
610 
611  $a_tpl->setVariable("BLOCK_HEADER_CONTENT", $title);
612  $a_tpl->setVariable("CHR_COMMANDS", $a_commands_html);
613  $a_tpl->parseCurrentBlock();
614 
615  $a_tpl->touchBlock("container_row");
616 
617  $this->resetRowType();
618  }
619 
627  protected function addStandardRow(ilTemplate $a_tpl, $a_html, $a_ref_id = 0)
628  {
629  // :TODO: obsolete?
630  $this->cur_row_type = ($this->cur_row_type == "row_type_1")
631  ? "row_type_2"
632  : "row_type_1";
633 
634  if ($a_ref_id > 0)
635  {
636  $a_tpl->setCurrentBlock($this->cur_row_type);
637  $a_tpl->setVariable("ROW_ID", 'id="item_row_'.$a_ref_id.'"');
638  $a_tpl->parseCurrentBlock();
639  }
640  else
641  {
642  $a_tpl->touchBlock($this->cur_row_type);
643  }
644 
645  $a_tpl->setCurrentBlock("container_standard_row");
646  $a_tpl->setVariable("BLOCK_ROW_CONTENT", $a_html);
647  $a_tpl->parseCurrentBlock();
648 
649  $a_tpl->touchBlock("container_row");
650  }
651 
657  protected function renderSelectAllBlock(ilTemplate $a_tpl)
658  {
659  global $lng;
660 
661  $a_tpl->setCurrentBlock("select_all_row");
662  $a_tpl->setVariable("CHECKBOXNAME", "bl_cb_".$this->bl_cnt);
663  $a_tpl->setVariable("SEL_ALL_PARENT", "bl_cntr_".$this->bl_cnt);
664  $a_tpl->setVariable("SEL_ALL_PARENT", "bl_cntr_".$this->bl_cnt);
665  $a_tpl->setVariable("TXT_SELECT_ALL", $lng->txt("select_all"));
666  $a_tpl->parseCurrentBlock();
667  }
668 
674  protected function addSeparatorRow(ilTemplate $a_tpl)
675  {
676  $a_tpl->setCurrentBlock("container_block");
677  $a_tpl->parseCurrentBlock();
678  }
679 
683  protected function resetRowType()
684  {
685  // :TODO: obsolete?
686  $this->cur_row_type = "";
687  }
688 
694  protected function getDownloadableTypes()
695  {
696  return array("fold", "file");
697  }
698 
704  public function renderDetails(ilTemplate $a_tpl)
705  {
706  global $lng;
707 
708  if(sizeof($this->details))
709  {
710  $a_tpl->setCurrentBlock('container_details_row');
711  $a_tpl->setVariable('TXT_DETAILS', $lng->txt('details'));
712  $a_tpl->parseCurrentBlock();
713  }
714  }
715 }
getDownloadableTypes()
Get downloadable repository object types.
renderSingleCustomBlock($a_id)
Get rendered html of single custom block.
$valid
addSeparatorRow(ilTemplate $a_tpl)
Render separator row.
addCustomBlock($a_id, $a_caption, $a_actions=null)
Add custom block.
addStandardRow(ilTemplate $a_tpl, $a_html, $a_ref_id=0)
Render item row.
addTypeBlock($a_type, $a_prefix=null, $a_postfix=null)
Add type block.
hasItem($a_id)
Item with id exists?
static lookupTxt($a_mod_prefix, $a_pl_id, $a_lang_var)
Lookup language text.
renderDetails(ilTemplate $a_tpl)
Render detail level.
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:626
addHeaderRow(ilTemplate $a_tpl, $a_type="", $a_text="", array $a_types_in_block=null, $a_commands_html=null, $a_order_id=null)
Render block header.
__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 getImagePath($img, $module_path="", $mode="output", $offline=false)
get image path (for images located in a template directory)
touchBlock($block)
overwrites ITX::touchBlock.
special template class to simplify handling of ITX/PEAR
resetDetails()
Reset/remove all detail levels.
renderHelperCustomBlock(ilTemplate $a_block_tpl, $a_block_id, $a_is_single=false)
Render custom block.
setCurrentBlock($part="DEFAULT")
Überladene Funktion, die sich hier lokal noch den aktuellen Block merkt.
isValidBlock($a_id)
Any block with id exists?
hasTypeBlock($a_type)
Type block already exists?
hasCustomBlock($a_id)
Custom block already exists?
hideItem($a_id)
Mark item id as used, but do not render.
renderHelperGeneric(ilTemplate $a_block_tpl, $a_block_id, array $a_block, $a_is_single=false)
Render block.
renderSingleTypeBlock($a_type)
Get rendered html of single type block.
global $ilSetting
Definition: privfeed.php:40
global $lng
Definition: privfeed.php:40
removeItem($a_id)
Remove item (from any block)
addDetailsLevel($a_level, $a_url, $a_active=false)
Add details level.
renderHelperTypeBlock(ilTemplate $a_block_tpl, $a_type, $a_is_single=false)
Render type block.
Class ilContainerRenderer.
parseCurrentBlock($part="DEFAULT")
Überladene Funktion, die auf den aktuelle Block vorher noch ein replace ausführt public...
processBlockPositions()
Process block positions.
resetRowType()
Reset internal row type.
renderSelectAllBlock(ilTemplate $a_tpl)
Render "select all".
setBlockPosition($a_block_id, $a_pos)
Set block position.
addItemToBlock($a_block_id, $a_item_type, $a_item_id, $a_item_html, $a_force=false)
Add item to existing block.
getHTML()
Get rendered html (of all blocks)