ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 
25  // block (unique) ids
26  protected $rendered_blocks = array(); // [array]
27  protected $bl_cnt = 0; // [int]
28  protected $cur_row_type; // [string]
29 
30  // ordering
31  protected $block_pos = array(); // [array]
32  protected $block_custom_pos = array(); // [array]
33  protected $order_cnt = 0; // [int]
34 
35  const UNIQUE_SEPARATOR = "-";
36 
46  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)
47  {
48  $this->enable_manage_select_all = (bool)$a_enable_manage_select_all;
49  $this->enable_multi_download = (bool)$a_enable_multi_download;
50  $this->active_block_ordering = (bool)$a_active_block_ordering;
51  $this->block_custom_pos = $a_block_custom_positions;
52  }
53 
54 
55  //
56  // blocks
57  //
58 
67  public function addTypeBlock($a_type, $a_prefix = null, $a_postfix = null)
68  {
69  if($a_type != "itgr" &&
70  !$this->hasTypeBlock($a_type))
71  {
72  $this->type_blocks[$a_type] = array(
73  "prefix" => $a_prefix
74  ,"postfix" => $a_postfix
75  );
76  return true;
77  }
78  return false;
79  }
80 
87  public function hasTypeBlock($a_type)
88  {
89  return array_key_exists($a_type, $this->type_blocks);
90  }
91 
100  public function addCustomBlock($a_id, $a_caption, $a_actions = null)
101  {
102  if(!$this->hasCustomBlock($a_id))
103  {
104  $this->custom_blocks[$a_id] = array(
105  "caption" => $a_caption
106  ,"actions" => $a_actions
107  );
108  return true;
109  }
110  return false;
111  }
112 
119  public function hasCustomBlock($a_id)
120  {
121  return array_key_exists($a_id, $this->custom_blocks);
122  }
123 
130  public function isValidBlock($a_id)
131  {
132  return ($this->hasTypeBlock($a_id) ||
133  $this->hasCustomBlock($a_id));
134  }
135 
136 
137  //
138  // items
139  //
140 
146  public function hideItem($a_id)
147  {
148  // see hasItem();
149  $this->hidden_items[] = $a_id;
150 
151  // #16629 - do not remove hidden items from other blocks
152  // $this->removeItem($a_id);
153  }
154 
160  public function removeItem($a_id)
161  {
162  foreach(array_keys($this->items) as $item_id)
163  {
164  if(array_pop(explode(self::UNIQUE_SEPARATOR, $item_id)) == $a_id)
165  {
166  unset($this->items[$item_id]);
167  }
168  }
169 
170  foreach($this->block_items as $block_id => $items)
171  {
172  foreach($items as $idx => $item_id)
173  {
174  if(array_pop(explode(self::UNIQUE_SEPARATOR, $item_id)) == $a_id)
175  {
176  unset($this->block_items[$block_id][$idx]);
177  if(!sizeof($this->block_items[$block_id]))
178  {
179  unset($this->block_items[$block_id]);
180  }
181  break;
182  }
183  }
184  }
185  }
186 
193  public function hasItem($a_id)
194  {
195  foreach(array_keys($this->items) as $item_id)
196  {
197  if(array_pop(explode(self::UNIQUE_SEPARATOR, $item_id)) == $a_id)
198  {
199  return true;
200  }
201  }
202  return in_array($a_id, $this->hidden_items);
203  }
204 
215  public function addItemToBlock($a_block_id, $a_item_type, $a_item_id, $a_item_html, $a_force = false)
216  {
217  if($this->isValidBlock($a_block_id) &&
218  $a_item_type != "itgr" &&
219  (!$this->hasItem($a_item_id) || $a_force) &&
220  trim($a_item_html))
221  {
222  // #16563 - item_id (== ref_id) is NOT unique, adding parent block id
223  $uniq_id = $a_block_id.self::UNIQUE_SEPARATOR.$a_item_id;
224 
225  $this->items[$uniq_id] = array(
226  "type" => $a_item_type
227  ,"html" => $a_item_html
228  );
229  $this->block_items[$a_block_id][] = $uniq_id;
230  return true;
231  }
232  return false;
233  }
234 
242  public function addDetailsLevel($a_level, $a_url, $a_active = false)
243  {
244  $this->details[$a_level] = array(
245  "url" => $a_url
246  ,"active" => (bool)$a_active
247  );
248  }
249 
253  public function resetDetails()
254  {
255  $this->details = array();
256  }
257 
258 
259  //
260  // render
261  //
262 
269  public function setBlockPosition($a_block_id, $a_pos)
270  {
271  if($this->isValidBlock($a_block_id))
272  {
273  $this->block_pos[$a_block_id] = $a_pos;
274  }
275  }
276 
282  public function getHTML()
283  {
284  $valid = false;
285 
286  $block_tpl = $this->initBlockTemplate();
287 
288  foreach($this->processBlockPositions() as $block_id)
289  {
290  if(array_key_exists($block_id, $this->custom_blocks))
291  {
292  if($this->renderHelperCustomBlock($block_tpl, $block_id))
293  {
294  $this->addSeparatorRow($block_tpl);
295  $valid = true;
296  }
297  }
298  if(array_key_exists($block_id, $this->type_blocks))
299  {
300  if($this->renderHelperTypeBlock($block_tpl, $block_id))
301  {
302  $this->addSeparatorRow($block_tpl);
303  $valid = true;
304  }
305  }
306  }
307 
308  if($valid)
309  {
310  $this->renderDetails($block_tpl);
311 
312  return $block_tpl->get();
313  }
314  }
315 
322  public function renderSingleTypeBlock($a_type)
323  {
324  $block_tpl = $this->initBlockTemplate();
325 
326  if($this->renderHelperTypeBlock($block_tpl, $a_type, true))
327  {
328  return $block_tpl->get();
329  }
330  }
331 
338  public function renderSingleCustomBlock($a_id)
339  {
340  $block_tpl = $this->initBlockTemplate();
341 
342  if($this->renderHelperCustomBlock($block_tpl, $a_id, true))
343  {
344  return $block_tpl->get();
345  }
346  }
347 
348 
349  //
350  // render (helper)
351  //
352 
358  protected function processBlockPositions()
359  {
360  // manual order
361  if(sizeof($this->block_custom_pos))
362  {
363  $tmp = $this->block_pos;
364  $this->block_pos = array();
365  foreach($this->block_custom_pos as $idx => $block_id)
366  {
367  if($this->isValidBlock($block_id))
368  {
369  $this->block_pos[$block_id] = $idx;
370  }
371  }
372 
373  // at least some manual are valid
374  if(sizeof($this->block_pos))
375  {
376  // append missing blocks from default order
377  $last = max($this->block_pos);
378  foreach(array_keys($tmp) as $block_id)
379  {
380  if(!array_key_exists($block_id, $this->block_pos))
381  {
382  $this->block_pos[$block_id] = ++$last;
383  }
384  }
385  }
386  // all manual invalid, use default
387  else
388  {
389  $this->block_pos = $tmp;
390  }
391  }
392 
393  // add missing blocks to order
394  $last = sizeof($this->block_pos)
395  ? max($this->block_pos)
396  : 0;
397  foreach(array_keys($this->custom_blocks) as $block_id)
398  {
399  if(!array_key_exists($block_id, $this->block_pos))
400  {
401  $this->block_pos[$block_id] = ++$last;
402  }
403  }
404  foreach(array_keys($this->type_blocks) as $block_id)
405  {
406  if(!array_key_exists($block_id, $this->block_pos))
407  {
408  $this->block_pos[$block_id] = ++$last;
409  }
410  }
411 
412  asort($this->block_pos);
413 
414  return array_keys($this->block_pos);
415  }
416 
425  protected function renderHelperCustomBlock(ilTemplate $a_block_tpl, $a_block_id, $a_is_single = false)
426  {
427  if($this->hasCustomBlock($a_block_id))
428  {
429  return $this->renderHelperGeneric($a_block_tpl, $a_block_id, $this->custom_blocks[$a_block_id], $a_is_single);
430  }
431  return false;
432  }
433 
442  protected function renderHelperTypeBlock(ilTemplate $a_block_tpl, $a_type, $a_is_single = false)
443  {
444  if($this->hasTypeBlock($a_type))
445  {
446  $block = $this->type_blocks[$a_type];
447  $block["type"] = $a_type;
448 
449  return $this->renderHelperGeneric($a_block_tpl, $a_type, $block, $a_is_single);
450  }
451  return false;
452  }
453 
463  protected function renderHelperGeneric(ilTemplate $a_block_tpl, $a_block_id, array $a_block, $a_is_single = false)
464  {
465  if(!in_array($a_block_id, $this->rendered_blocks))
466  {
467  $this->rendered_blocks[] = $a_block_id;
468 
469  $block_types = array();
470  if(is_array($this->block_items[$a_block_id]))
471  {
472  foreach($this->block_items[$a_block_id] as $item_id)
473  {
474  if(isset($this->items[$item_id]["type"]))
475  {
476  $block_types[] = $this->items[$item_id]["type"];
477  }
478  }
479  }
480 
481  // #14610 - manage empty item groups
482  if(is_array($this->block_items[$a_block_id]) ||
483  is_numeric($a_block_id))
484  {
485 
486  $order_id = (!$a_is_single && $this->active_block_ordering)
487  ? $a_block_id
488  : null;
489  $this->addHeaderRow($a_block_tpl, $a_block["type"], $a_block["caption"], array_unique($block_types), $a_block["actions"], $order_id);
490 
491  if($a_block["prefix"])
492  {
493  $this->addStandardRow($a_block_tpl, $a_block["prefix"]);
494  }
495 
496  if(is_array($this->block_items[$a_block_id]))
497  {
498  foreach($this->block_items[$a_block_id] as $item_id)
499  {
500  $this->addStandardRow($a_block_tpl, $this->items[$item_id]["html"], $item_id);
501  }
502  }
503 
504  if($a_block["postfix"])
505  {
506  $this->addStandardRow($a_block_tpl, $a_block["postfix"]);
507  }
508 
509  return true;
510  }
511  }
512 
513  return false;
514  }
515 
521  protected function initBlockTemplate()
522  {
523  // :TODO: obsolete?
524  $this->cur_row_type = "row_type_1";
525 
526  return new ilTemplate("tpl.container_list_block.html", true, true,
527  "Services/Container");
528  }
529 
540  protected function addHeaderRow(ilTemplate $a_tpl, $a_type = "", $a_text = "", array $a_types_in_block = null, $a_commands_html = null, $a_order_id = null)
541  {
542  global $lng, $ilSetting, $objDefinition;
543 
544  $a_tpl->setVariable("CB_ID", ' id="bl_cntr_'.(++$this->bl_cnt).'"');
545 
546  if ($this->enable_manage_select_all)
547  {
548  $this->renderSelectAllBlock($a_tpl);
549  }
550  else if ($this->enable_multi_download)
551  {
552  if ($a_type)
553  {
554  $a_types_in_block = array($a_type);
555  }
556  foreach($a_types_in_block as $type)
557  {
558  if (in_array($type, $this->getDownloadableTypes()))
559  {
560  $this->renderSelectAllBlock($a_tpl);
561  break;
562  }
563  }
564  }
565 
566  if ($a_text == "" && $a_type != "")
567  {
568  if (!$objDefinition->isPlugin($a_type))
569  {
570  $title = $lng->txt("objs_".$a_type);
571  }
572  else
573  {
574  include_once("./Services/Component/classes/class.ilPlugin.php");
575  $title = ilPlugin::lookupTxt("rep_robj", $a_type, "objs_".$a_type);
576  }
577  }
578  else
579  {
580  $title = $a_text;
581  }
582 
583  if ($ilSetting->get("icon_position_in_lists") != "item_rows" &&
584  $a_type != "")
585  {
586  $icon = ilUtil::getImagePath("icon_".$a_type.".svg");
587 
588  $a_tpl->setCurrentBlock("container_header_row_image");
589  $a_tpl->setVariable("HEADER_IMG", $icon);
590  $a_tpl->setVariable("HEADER_ALT", $title);
591  }
592  else
593  {
594  $a_tpl->setCurrentBlock("container_header_row");
595  }
596 
597  if($a_order_id)
598  {
599  $a_tpl->setVariable("BLOCK_HEADER_ORDER_NAME", "position[blocks][".$a_order_id."]");
600  $a_tpl->setVariable("BLOCK_HEADER_ORDER_NUM", (++$this->order_cnt)*10);
601  }
602 
603  $a_tpl->setVariable("BLOCK_HEADER_CONTENT", $title);
604  $a_tpl->setVariable("CHR_COMMANDS", $a_commands_html);
605  $a_tpl->parseCurrentBlock();
606 
607  $a_tpl->touchBlock("container_row");
608 
609  $this->resetRowType();
610  }
611 
619  protected function addStandardRow(ilTemplate $a_tpl, $a_html, $a_ref_id = 0)
620  {
621  // :TODO: obsolete?
622  $this->cur_row_type = ($this->cur_row_type == "row_type_1")
623  ? "row_type_2"
624  : "row_type_1";
625 
626  if ($a_ref_id > 0)
627  {
628  $a_tpl->setCurrentBlock($this->cur_row_type);
629  $a_tpl->setVariable("ROW_ID", 'id="item_row_'.$a_ref_id.'"');
630  $a_tpl->parseCurrentBlock();
631  }
632  else
633  {
634  $a_tpl->touchBlock($this->cur_row_type);
635  }
636 
637  $a_tpl->setCurrentBlock("container_standard_row");
638  $a_tpl->setVariable("BLOCK_ROW_CONTENT", $a_html);
639  $a_tpl->parseCurrentBlock();
640 
641  $a_tpl->touchBlock("container_row");
642  }
643 
649  protected function renderSelectAllBlock(ilTemplate $a_tpl)
650  {
651  global $lng;
652 
653  $a_tpl->setCurrentBlock("select_all_row");
654  $a_tpl->setVariable("CHECKBOXNAME", "bl_cb_".$this->bl_cnt);
655  $a_tpl->setVariable("SEL_ALL_PARENT", "bl_cntr_".$this->bl_cnt);
656  $a_tpl->setVariable("SEL_ALL_PARENT", "bl_cntr_".$this->bl_cnt);
657  $a_tpl->setVariable("TXT_SELECT_ALL", $lng->txt("select_all"));
658  $a_tpl->parseCurrentBlock();
659  }
660 
666  protected function addSeparatorRow(ilTemplate $a_tpl)
667  {
668  $a_tpl->setCurrentBlock("container_block");
669  $a_tpl->parseCurrentBlock();
670  }
671 
675  protected function resetRowType()
676  {
677  // :TODO: obsolete?
678  $this->cur_row_type = "";
679  }
680 
686  protected function getDownloadableTypes()
687  {
688  return array("fold", "file");
689  }
690 
696  public function renderDetails(ilTemplate $a_tpl)
697  {
698  global $lng;
699 
700  if(sizeof($this->details))
701  {
702  $a_tpl->setCurrentBlock('container_details_row');
703  $a_tpl->setVariable('TXT_DETAILS', $lng->txt('details'));
704  $a_tpl->parseCurrentBlock();
705  }
706  }
707 }