ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 require_once('./Services/Repository/classes/class.ilObjectPlugin.php');
4 
12 {
13  // switches
14  protected $enable_manage_select_all; // [bool]
15  protected $enable_multi_download; // [bool]
16  protected $active_block_ordering; // [bool]
17 
18  // properties
19  protected $type_blocks = array(); // [array]
20  protected $custom_blocks = array(); // [array]
21  protected $items = array(); // [array]
22  protected $hidden_items = array(); // [array]
23  protected $block_items = array(); // [array]
24  protected $details = array(); // [array]
25  protected $item_ids = array(); // [array]
26 
27  // block (unique) ids
28  protected $rendered_blocks = array(); // [array]
29  protected $bl_cnt = 0; // [int]
30  protected $cur_row_type; // [string]
31 
32  // ordering
33  protected $block_pos = array(); // [array]
34  protected $block_custom_pos = array(); // [array]
35  protected $order_cnt = 0; // [int]
36 
37  const UNIQUE_SEPARATOR = "-";
38 
48  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)
49  {
50  $this->enable_manage_select_all = (bool)$a_enable_manage_select_all;
51  $this->enable_multi_download = (bool)$a_enable_multi_download;
52  $this->active_block_ordering = (bool)$a_active_block_ordering;
53  $this->block_custom_pos = $a_block_custom_positions;
54  }
55 
56 
57  //
58  // blocks
59  //
60 
69  public function addTypeBlock($a_type, $a_prefix = null, $a_postfix = null)
70  {
71  if($a_type != "itgr" &&
72  !$this->hasTypeBlock($a_type))
73  {
74  $this->type_blocks[$a_type] = array(
75  "prefix" => $a_prefix
76  ,"postfix" => $a_postfix
77  );
78  return true;
79  }
80  return false;
81  }
82 
89  public function hasTypeBlock($a_type)
90  {
91  return array_key_exists($a_type, $this->type_blocks);
92  }
93 
102  public function addCustomBlock($a_id, $a_caption, $a_actions = null)
103  {
104  if(!$this->hasCustomBlock($a_id))
105  {
106  $this->custom_blocks[$a_id] = array(
107  "caption" => $a_caption
108  ,"actions" => $a_actions
109  );
110  return true;
111  }
112  return false;
113  }
114 
121  public function hasCustomBlock($a_id)
122  {
123  return array_key_exists($a_id, $this->custom_blocks);
124  }
125 
132  public function isValidBlock($a_id)
133  {
134  return ($this->hasTypeBlock($a_id) ||
135  $this->hasCustomBlock($a_id));
136  }
137 
138 
139  //
140  // items
141  //
142 
148  public function hideItem($a_id)
149  {
150  // see hasItem();
151  $this->hidden_items[$a_id] = true;
152 
153  // #16629 - do not remove hidden items from other blocks
154  // $this->removeItem($a_id);
155  }
156 
162  public function removeItem($a_id)
163  {
164  if(!$this->hasItem($a_id))
165  {
166  return;
167  }
168 
169  unset($this->item_ids[$a_id]);
170  unset($this->hidden_items[$a_id]);
171 
172  foreach(array_keys($this->items) as $item_id)
173  {
174  if(array_pop(explode(self::UNIQUE_SEPARATOR, $item_id)) == $a_id)
175  {
176  unset($this->items[$item_id]);
177  }
178  }
179 
180  foreach($this->block_items as $block_id => $items)
181  {
182  foreach($items as $idx => $item_id)
183  {
184  if(array_pop(explode(self::UNIQUE_SEPARATOR, $item_id)) == $a_id)
185  {
186  unset($this->block_items[$block_id][$idx]);
187  if(!sizeof($this->block_items[$block_id]))
188  {
189  unset($this->block_items[$block_id]);
190  }
191  break;
192  }
193  }
194  }
195  }
196 
203  public function hasItem($a_id)
204  {
205  return (array_key_exists($a_id, $this->item_ids) ||
206  array_key_exists($a_id, $this->hidden_items));
207  }
208 
219  public function addItemToBlock($a_block_id, $a_item_type, $a_item_id, $a_item_html, $a_force = false)
220  {
221  if($this->isValidBlock($a_block_id) &&
222  $a_item_type != "itgr" &&
223  (!$this->hasItem($a_item_id) || $a_force) &&
224  trim($a_item_html))
225  {
226  // #16563 - item_id (== ref_id) is NOT unique, adding parent block id
227  $uniq_id = $a_block_id.self::UNIQUE_SEPARATOR.$a_item_id;
228 
229  $this->items[$uniq_id] = array(
230  "type" => $a_item_type
231  ,"html" => $a_item_html
232  );
233 
234  // #18326
235  $this->item_ids[$a_item_id] = true;
236 
237  $this->block_items[$a_block_id][] = $uniq_id;
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 
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");
584  $title= $pl->txt("objs_".$a_type);
585  }
586  }
587  else
588  {
589  $title = $a_text;
590  }
591 
592  if ($ilSetting->get("icon_position_in_lists") != "item_rows" &&
593  $a_type != "")
594  {
595  $icon = ilUtil::getImagePath("icon_".$a_type.".svg");
596 
597  $a_tpl->setCurrentBlock("container_header_row_image");
598  $a_tpl->setVariable("HEADER_IMG", $icon);
599  $a_tpl->setVariable("HEADER_ALT", $title);
600  }
601  else
602  {
603  $a_tpl->setCurrentBlock("container_header_row");
604  }
605 
606  if($a_order_id)
607  {
608  $a_tpl->setVariable("BLOCK_HEADER_ORDER_NAME", "position[blocks][".$a_order_id."]");
609  $a_tpl->setVariable("BLOCK_HEADER_ORDER_NUM", (++$this->order_cnt)*10);
610  }
611 
612  $a_tpl->setVariable("BLOCK_HEADER_CONTENT", $title);
613  $a_tpl->setVariable("CHR_COMMANDS", $a_commands_html);
614  $a_tpl->parseCurrentBlock();
615 
616  $a_tpl->touchBlock("container_row");
617 
618  $this->resetRowType();
619  }
620 
628  protected function addStandardRow(ilTemplate $a_tpl, $a_html, $a_ref_id = 0)
629  {
630  // :TODO: obsolete?
631  $this->cur_row_type = ($this->cur_row_type == "row_type_1")
632  ? "row_type_2"
633  : "row_type_1";
634 
635  if ($a_ref_id > 0)
636  {
637  $a_tpl->setCurrentBlock($this->cur_row_type);
638  $a_tpl->setVariable("ROW_ID", 'id="item_row_'.$a_ref_id.'"');
639  $a_tpl->parseCurrentBlock();
640  }
641  else
642  {
643  $a_tpl->touchBlock($this->cur_row_type);
644  }
645 
646  $a_tpl->setCurrentBlock("container_standard_row");
647  $a_tpl->setVariable("BLOCK_ROW_CONTENT", $a_html);
648  $a_tpl->parseCurrentBlock();
649 
650  $a_tpl->touchBlock("container_row");
651  }
652 
658  protected function renderSelectAllBlock(ilTemplate $a_tpl)
659  {
660  global $lng;
661 
662  $a_tpl->setCurrentBlock("select_all_row");
663  $a_tpl->setVariable("CHECKBOXNAME", "bl_cb_".$this->bl_cnt);
664  $a_tpl->setVariable("SEL_ALL_PARENT", "bl_cntr_".$this->bl_cnt);
665  $a_tpl->setVariable("SEL_ALL_PARENT", "bl_cntr_".$this->bl_cnt);
666  $a_tpl->setVariable("TXT_SELECT_ALL", $lng->txt("select_all"));
667  $a_tpl->parseCurrentBlock();
668  }
669 
675  protected function addSeparatorRow(ilTemplate $a_tpl)
676  {
677  $a_tpl->setCurrentBlock("container_block");
678  $a_tpl->parseCurrentBlock();
679  }
680 
684  protected function resetRowType()
685  {
686  // :TODO: obsolete?
687  $this->cur_row_type = "";
688  }
689 
695  protected function getDownloadableTypes()
696  {
697  return array("fold", "file");
698  }
699 
705  public function renderDetails(ilTemplate $a_tpl)
706  {
707  global $lng;
708 
709  if(sizeof($this->details))
710  {
711  $a_tpl->setCurrentBlock('container_details_row');
712  $a_tpl->setVariable('TXT_DETAILS', $lng->txt('details'));
713  $a_tpl->parseCurrentBlock();
714  }
715  }
716 }
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?
$a_type
Definition: workflow.php:93
renderDetails(ilTemplate $a_tpl)
Render detail level.
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:613
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.
static getRepoPluginObjectByType($type)
Return either a repoObject plugin or a orgunit extension plugin or null if the type is not a plugin...
Create styles array
The data for the language used.
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:17
global $lng
Definition: privfeed.php:17
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)