CBaseListView.php
8.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
/**
* CBaseListView class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CBaseListView is the base class for {@link CListView} and {@link CGridView}.
*
* CBaseListView implements the common features needed by a view wiget for rendering multiple models.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package zii.widgets
* @since 1.1
*/
abstract class CBaseListView extends CWidget
{
/**
* @var IDataProvider the data provider for the view.
*/
public $dataProvider;
/**
* @var string the tag name for the view container. Defaults to 'div'.
*/
public $tagName='div';
/**
* @var array the HTML options for the view container tag.
*/
public $htmlOptions=array();
/**
* @var boolean whether to enable sorting. Note that if the {@link IDataProvider::sort} property
* of {@link dataProvider} is false, this will be treated as false as well. When sorting is enabled,
* sortable columns will have their headers clickable to trigger sorting along that column.
* Defaults to true.
* @see sortableAttributes
*/
public $enableSorting=true;
/**
* @var boolean whether to enable pagination. Note that if the {@link IDataProvider::pagination} property
* of {@link dataProvider} is false, this will be treated as false as well. When pagination is enabled,
* a pager will be displayed in the view so that it can trigger pagination of the data display.
* Defaults to true.
*/
public $enablePagination=true;
/**
* @var array|string the configuration for the pager. Defaults to <code>array('class'=>'CLinkPager')</code>.
* String value will be treated as the class name of the pager (<code>'ClassName'</code> value is similar
* to the <code>array('class'=>'ClassName')</code> value). See {@link CBasePager} and {@link CLinkPager}
* for more details about pager configuration array values.
* @see enablePagination
*/
public $pager=array('class'=>'CLinkPager');
/**
* @var string the template to be used to control the layout of various sections in the view.
* These tokens are recognized: {summary}, {items} and {pager}. They will be replaced with the
* summary text, the items, and the pager.
*/
public $template="{summary}\n{items}\n{pager}";
/**
* @var string the summary text template for the view. These tokens are recognized and will be replaced
* with the corresponding values:
* <ul>
* <li>{start}: the starting row number (1-based) currently being displayed</li>
* <li>{end}: the ending row number (1-based) currently being displayed</li>
* <li>{count}: the total number of rows</li>
* <li>{page}: the page number (1-based) current being displayed, available since version 1.1.3</li>
* <li>{pages}: the total number of pages, available since version 1.1.3</li>
* </ul>
*/
public $summaryText;
/**
* @var string the message to be displayed when {@link dataProvider} does not have any data.
*/
public $emptyText;
/**
* @var string the CSS class name for the container of all data item display. Defaults to 'items'.
*/
public $itemsCssClass='items';
/**
* @var string the CSS class name for the summary text container. Defaults to 'summary'.
*/
public $summaryCssClass='summary';
/**
* @var string the CSS class name for the pager container. Defaults to 'pager'.
*/
public $pagerCssClass='pager';
/**
* @var string the CSS class name that will be assigned to the widget container element
* when the widget is updating its content via AJAX. Defaults to 'loading'.
* @since 1.1.1
*/
public $loadingCssClass='loading';
/**
* Initializes the view.
* This method will initialize required property values and instantiate {@link columns} objects.
*/
public function init()
{
if($this->dataProvider===null)
throw new CException(Yii::t('zii','The "dataProvider" property cannot be empty.'));
$this->dataProvider->getData();
$this->htmlOptions['id']=$this->getId();
if($this->enableSorting && $this->dataProvider->getSort()===false)
$this->enableSorting=false;
if($this->enablePagination && $this->dataProvider->getPagination()===false)
$this->enablePagination=false;
}
/**
* Renders the view.
* This is the main entry of the whole view rendering.
* Child classes should mainly override {@link renderContent} method.
*/
public function run()
{
$this->registerClientScript();
echo CHtml::openTag($this->tagName,$this->htmlOptions)."\n";
$this->renderContent();
$this->renderKeys();
echo CHtml::closeTag($this->tagName);
}
/**
* Renders the main content of the view.
* The content is divided into sections, such as summary, items, pager.
* Each section is rendered by a method named as "renderXyz", where "Xyz" is the section name.
* The rendering results will replace the corresponding placeholders in {@link template}.
*/
public function renderContent()
{
ob_start();
echo preg_replace_callback("/{(\w+)}/",array($this,'renderSection'),$this->template);
ob_end_flush();
}
/**
* Renders a section.
* This method is invoked by {@link renderContent} for every placeholder found in {@link template}.
* It should return the rendering result that would replace the placeholder.
* @param array $matches the matches, where $matches[0] represents the whole placeholder,
* while $matches[1] contains the name of the matched placeholder.
* @return string the rendering result of the section
*/
protected function renderSection($matches)
{
$method='render'.$matches[1];
if(method_exists($this,$method))
{
$this->$method();
$html=ob_get_contents();
ob_clean();
return $html;
}
else
return $matches[0];
}
/**
* Renders the empty message when there is no data.
*/
public function renderEmptyText()
{
$emptyText=$this->emptyText===null ? Yii::t('zii','No results found.') : $this->emptyText;
echo CHtml::tag('span', array('class'=>'empty'), $emptyText);
}
/**
* Renders the key values of the data in a hidden tag.
*/
public function renderKeys()
{
echo CHtml::openTag('div',array(
'class'=>'keys',
'style'=>'display:none',
'title'=>Yii::app()->getRequest()->getUrl(),
));
foreach($this->dataProvider->getKeys() as $key)
echo "<span>".CHtml::encode($key)."</span>";
echo "</div>\n";
}
/**
* Renders the summary text.
*/
public function renderSummary()
{
if(($count=$this->dataProvider->getItemCount())<=0)
return;
echo '<div class="'.$this->summaryCssClass.'">';
if($this->enablePagination)
{
$pagination=$this->dataProvider->getPagination();
$total=$this->dataProvider->getTotalItemCount();
$start=$pagination->currentPage*$pagination->pageSize+1;
$end=$start+$count-1;
if($end>$total)
{
$end=$total;
$start=$end-$count+1;
}
if(($summaryText=$this->summaryText)===null)
$summaryText=Yii::t('zii','Displaying {start}-{end} of 1 result.|Displaying {start}-{end} of {count} results.',$total);
echo strtr($summaryText,array(
'{start}'=>$start,
'{end}'=>$end,
'{count}'=>$total,
'{page}'=>$pagination->currentPage+1,
'{pages}'=>$pagination->pageCount,
));
}
else
{
if(($summaryText=$this->summaryText)===null)
$summaryText=Yii::t('zii','Total 1 result.|Total {count} results.',$count);
echo strtr($summaryText,array(
'{count}'=>$count,
'{start}'=>1,
'{end}'=>$count,
'{page}'=>1,
'{pages}'=>1,
));
}
echo '</div>';
}
/**
* Renders the pager.
*/
public function renderPager()
{
if(!$this->enablePagination)
return;
$pager=array();
$class='CLinkPager';
if(is_string($this->pager))
$class=$this->pager;
else if(is_array($this->pager))
{
$pager=$this->pager;
if(isset($pager['class']))
{
$class=$pager['class'];
unset($pager['class']);
}
}
$pager['pages']=$this->dataProvider->getPagination();
if($pager['pages']->getPageCount()>1)
{
echo '<div class="'.$this->pagerCssClass.'">';
$this->widget($class,$pager);
echo '</div>';
}
else
$this->widget($class,$pager);
}
/**
* Registers necessary client scripts.
* This method is invoked by {@link run}.
* Child classes may override this method to register customized client scripts.
*/
public function registerClientScript()
{
}
/**
* Renders the data items for the view.
* Each item is corresponding to a single data model instance.
* Child classes should override this method to provide the actual item rendering logic.
*/
abstract public function renderItems();
}