/framework/Core/lib/Horde/Core/Block/Layout.php

https://github.com/ewandor/horde · PHP · 125 lines · 59 code · 12 blank · 54 comment · 7 complexity · 998d2368ae4aa22404564033c717ddb7 MD5 · raw file

  1. <?php
  2. /**
  3. * Provides basic functionality for both managing and displaying blocks.
  4. *
  5. * Copyright 2003-2012 Horde LLC (http://www.horde.org/)
  6. *
  7. * See the enclosed file COPYING for license information (LGPL). If you
  8. * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  9. *
  10. * @author Mike Cochrane <mike@graftonhall.co.nz>
  11. * @author Jan Schneider <jan@horde.org>
  12. * @author Michael Slusarz <slusarz@horde.org>
  13. * @category Horde
  14. * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
  15. * @package Core
  16. */
  17. class Horde_Core_Block_Layout
  18. {
  19. /**
  20. * Edit URL.
  21. *
  22. * @var string
  23. */
  24. protected $_editUrl;
  25. /**
  26. * View URL.
  27. *
  28. * @var string
  29. */
  30. protected $_viewUrl;
  31. /**
  32. * Returns whether the specified block may be removed.
  33. *
  34. * @param integer $row A layout row.
  35. * @param integer $col A layout column.
  36. *
  37. * @return boolean True if this block may be removed.
  38. */
  39. public function isRemovable($row, $col)
  40. {
  41. global $conf;
  42. $app = $this->_layout[$row][$col]['app'];
  43. $type = $this->_layout[$row][$col]['params']['type2'];
  44. $block = $app . ':' . $type;
  45. /* Check if the block is a fixed block. */
  46. if (!in_array($block, $conf['portal']['fixed_blocks'])) {
  47. return true;
  48. }
  49. /* Check if we have still another block of the same type. */
  50. $found = false;
  51. foreach ($this->_layout as $cur_row) {
  52. foreach ($cur_row as $cur_col) {
  53. if (isset($cur_col['app']) &&
  54. $cur_col['app'] == $app &&
  55. $cur_col['params']['type2'] == $type) {
  56. if ($found) {
  57. return true;
  58. }
  59. $found = true;
  60. }
  61. }
  62. }
  63. return false;
  64. }
  65. /**
  66. * Returns an URL triggering an action to a block.
  67. *
  68. * @param string $action An action to trigger.
  69. * @param integer $row A layout row.
  70. * @param integer $col A layout column.
  71. *
  72. * @return Horde_Url An URL with all necessary parameters.
  73. */
  74. public function getActionUrl($action, $row, $col)
  75. {
  76. return Horde::url($this->_editUrl)->unique()->setAnchor('block')->add(array(
  77. 'col' => $col,
  78. 'row' => $row,
  79. 'action' => $action,
  80. 'url' => $this->_viewUrl
  81. ));
  82. }
  83. /**
  84. * Returns the actions for the block header.
  85. *
  86. * @param integer $row A layout row.
  87. * @param integer $col A layout column.
  88. * @param boolean $edit Whether to include the edit icon.
  89. * @param $url TODO
  90. *
  91. * @return string HTML code for the block action icons.
  92. */
  93. public function getHeaderIcons($row, $col, $edit, $url = null)
  94. {
  95. $icons = '';
  96. if ($edit) {
  97. $icons .= Horde::link($this->getActionUrl('edit', $row, $col),
  98. Horde_Core_Translation::t("Edit"))
  99. . Horde::img('edit.png', Horde_Core_Translation::t("Edit"))
  100. . '</a>';
  101. }
  102. if ($this->isRemovable($row, $col)) {
  103. $icons .= Horde::link(
  104. $this->getActionUrl('removeBlock', $row, $col), Horde_Core_Translation::t("Remove"),
  105. '', '',
  106. 'return window.confirm(\''
  107. . addslashes(Horde_Core_Translation::t("Really delete this block?")) . '\')')
  108. . Horde::img('delete.png', Horde_Core_Translation::t("Remove"))
  109. . '</a>';
  110. }
  111. return $icons;
  112. }
  113. }