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