CPortlet.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * CPortlet class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CPortlet is the base class for portlet widgets.
  12. *
  13. * A portlet displays a fragment of content, usually in terms of a block
  14. * on the side bars of a Web page.
  15. *
  16. * To specify the content of the portlet, override the {@link renderContent}
  17. * method, or insert the content code between the {@link CController::beginWidget}
  18. * and {@link CController::endWidget} calls. For example,
  19. *
  20. * <pre>
  21. * <?php $this->beginWidget('zii.widgets.CPortlet'); ?>
  22. * ...insert content here...
  23. * <?php $this->endWidget(); ?>
  24. * </pre>
  25. *
  26. * A portlet also has an optional {@link title}. One may also override {@link renderDecoration}
  27. * to further customize the decorative display of a portlet (e.g. adding min/max buttons).
  28. *
  29. * @author Qiang Xue <qiang.xue@gmail.com>
  30. * @package zii.widgets
  31. * @since 1.1
  32. */
  33. class CPortlet extends CWidget
  34. {
  35. /**
  36. * @var string the tag name for the portlet container tag. Defaults to 'div'.
  37. */
  38. public $tagName='div';
  39. /**
  40. * @var array the HTML attributes for the portlet container tag.
  41. */
  42. public $htmlOptions=array('class'=>'portlet');
  43. /**
  44. * @var string the title of the portlet. Defaults to null.
  45. * When this is not set, Decoration will not be displayed.
  46. * Note that the title will not be HTML-encoded when rendering.
  47. */
  48. public $title;
  49. /**
  50. * @var string the CSS class for the decoration container tag. Defaults to 'portlet-decoration'.
  51. */
  52. public $decorationCssClass='portlet-decoration';
  53. /**
  54. * @var string the CSS class for the portlet title tag. Defaults to 'portlet-title'.
  55. */
  56. public $titleCssClass='portlet-title';
  57. /**
  58. * @var string the CSS class for the content container tag. Defaults to 'portlet-content'.
  59. */
  60. public $contentCssClass='portlet-content';
  61. /**
  62. * @var boolean whether to hide the portlet when the body content is empty. Defaults to true.
  63. * @since 1.1.4
  64. */
  65. public $hideOnEmpty=true;
  66. private $_openTag;
  67. /**
  68. * Initializes the widget.
  69. * This renders the open tags needed by the portlet.
  70. * It also renders the decoration, if any.
  71. */
  72. public function init()
  73. {
  74. ob_start();
  75. ob_implicit_flush(false);
  76. if(isset($this->htmlOptions['id']))
  77. $this->id=$this->htmlOptions['id'];
  78. else
  79. $this->htmlOptions['id']=$this->id;
  80. echo CHtml::openTag($this->tagName,$this->htmlOptions)."\n";
  81. $this->renderDecoration();
  82. echo "<div class=\"{$this->contentCssClass}\">\n";
  83. $this->_openTag=ob_get_contents();
  84. ob_clean();
  85. }
  86. /**
  87. * Renders the content of the portlet.
  88. */
  89. public function run()
  90. {
  91. $this->renderContent();
  92. $content=ob_get_clean();
  93. if($this->hideOnEmpty && trim($content)==='')
  94. return;
  95. echo $this->_openTag;
  96. echo $content;
  97. echo "</div>\n";
  98. echo CHtml::closeTag($this->tagName);
  99. }
  100. /**
  101. * Renders the decoration for the portlet.
  102. * The default implementation will render the title if it is set.
  103. */
  104. protected function renderDecoration()
  105. {
  106. if($this->title!==null)
  107. {
  108. echo "<div class=\"{$this->decorationCssClass}\">\n";
  109. echo "<div class=\"{$this->titleCssClass}\">{$this->title}</div>\n";
  110. echo "</div>\n";
  111. }
  112. }
  113. /**
  114. * Renders the content of the portlet.
  115. * Child classes should override this method to render the actual content.
  116. */
  117. protected function renderContent()
  118. {
  119. }
  120. }