让flea的FLEA_Helper_Pager类支持数组分页

不知道是不是我没发现,我没有看到flea有数组分组的功能,于是昨晚针对这个问题改写了它的FLEA_Helper_Pager类,让他支持这种类型的数组分页了:

1
2
3
4
5
array(
   [0]=>object,
   [1]=>object,
   ......
);

改的位置在
行175-行183
行253-行263

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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
class FLEA_Helper_Pager
{
    /**
     * 如果 $this->source 是一个 FLEA_Db_TableDataGateway 对象,则调用
     * $this->source->findAll() 来获取记录集。
     *
     * 否则通过 $this->dbo->selectLimit() 来获取记录集。
     *
     * @var FLEA_Db_TableDataGateway|string
     */
    var $source;
 
    /**
     * 数据库访问对象,当 $this->source 参数为 SQL 语句时,必须调用
     * $this->setDBO() 设置查询时要使用的数据库访问对象。
     *
     * @var SDBO
     */
    var $dbo;
 
    /**
     * 查询条件
     *
     * @var mixed
     */
    var $_conditions;
 
    /**
     * 排序
     *
     * @var string
     */
    var $_sortby;
 
    /**
     * 计算实际页码时的基数
     *
     * @var int
     */
    var $_basePageIndex = 0;
 
    /**
     * 每页记录数
     *
     * @var int
     */
    var $pageSize = -1;
 
    /**
     * 数据表中符合查询条件的记录总数
     *
     * @var int
     */
    var $totalCount = -1;
 
    /**
     * 数据表中符合查询条件的记录总数
     *
     * @var int
     */
    var $count = -1;
 
    /**
     * 符合条件的记录页数
     *
     * @var int
     */
    var $pageCount = -1;
 
    /**
     * 第一页的索引,从 0 开始
     *
     * @var int
     */
    var $firstPage = -1;
 
    /**
     * 第一页的页码
     *
     * @var int
     */
    var $firstPageNumber = -1;
 
    /**
     * 最后一页的索引,从 0 开始
     *
     * @var int
     */
    var $lastPage = -1;
 
    /**
     * 最后一页的页码
     *
     * @var int
     */
    var $lastPageNumber = -1;
 
    /**
     * 上一页的索引
     *
     * @var int
     */
    var $prevPage = -1;
 
    /**
     * 上一页的页码
     *
     * @var int
     */
    var $prevPageNumber = -1;
 
    /**
     * 下一页的索引
     *
     * @var int
     */
    var $nextPage = -1;
 
    /**
     * 下一页的页码
     *
     * @var int
     */
    var $nextPageNumber = -1;
 
    /**
     * 当前页的索引
     *
     * @var int
     */
    var $currentPage = -1;
 
    /**
     * 构造函数中提供的当前页索引,用于 setBasePageIndex() 后重新计算页码
     *
     * @var int
     */
    var $_currentPage = -1;
 
    /**
     * 当前页的页码
     *
     * @var int
     */
    var $currentPageNumber = -1;
 
    /**
     * 构造函数
     *
     * 如果 $source 参数是一个 TableDataGateway 对象,则 FLEA_Helper_Pager 会调用
     * 该 TDG 对象的 findCount() 和 findAll() 来确定记录总数并返回记录集。
     *
     * 如果 $source 参数是一个字符串,则假定为 SQL 语句。这时,FLEA_Helper_Pager
     * 不会自动调用计算各项分页参数。必须通过 setCount() 方法来设置作为分页计算
     * 基础的记录总数。
     *
     * 如果 $source 参数是一个键值由0递增的数组。
     *
     * 同时,如果 $source 参数为一个字符串,则不需要 $conditions 和 $sortby 参数。
     * 而且可以通过 setDBO() 方法设置要使用的数据库访问对象。否则 FLEA_Helper_Pager
     * 将尝试获取一个默认的数据库访问对象。
     *
     * @param TableDataGateway|string $source|array $source
     * @param int $currentPage
     * @param int $pageSize
     * @param mixed $conditions
     * @param string $sortby
     * @param int $basePageIndex
     *
     * @return FLEA_Helper_Pager
     */
    function FLEA_Helper_Pager(& $source, $currentPage, $pageSize = 20, $conditions = null, $sortby = null, $basePageIndex = 0)
    {
        $this->_basePageIndex = $basePageIndex;
        $this->_currentPage = $this->currentPage = $currentPage;
        $this->pageSize = $pageSize;
 
        if (is_object($source)||is_array($source)) {
            $this->source =& $source;
            $this->_conditions = $conditions;
            $this->_sortby = $sortby;
            if(!is_array($source))
            $this->totalCount = $this->count = (int)$this->source->findCount($conditions);
            else
            $this->totalCount = $this->count = count($this->source);
            $this->computingPage();
        } elseif (!empty($source)) {
            $this->source = $source;
            $sql = "SELECT COUNT(*) FROM ( $source ) as _count_table";
            $this->dbo =& FLEA::getDBO();
            $this->totalCount = $this->count = (int)$this->dbo->getOne($sql);
            $this->computingPage();
        }
    }
 
    /**
     * 设置分页索引第一页的基数
     *
     * @param int $index
     */
    function setBasePageIndex($index)
    {
        $this->_basePageIndex = $index;
        $this->currentPage = $this->_currentPage;
        $this->computingPage();
    }
 
    /**
     * 设置当前页码,以便用 findAll() 获得其他页的数据
     *
     * @param int $page
     */
    function setPage($page)
    {
        $this->_currentPage = $page;
        $this->currentPage = $page;
        $this->computingPage();
    }
 
    /**
     * 设置记录总数,从而更新分页参数
     *
     * @param int $count
     */
    function setCount($count)
    {
        $this->count = $count;
        $this->computingPage();
    }
 
    /**
     * 设置数据库访问对象
     *
     * @param SDBO $dbo
     */
    function setDBO(& $dbo)
    {
        $this->dbo =& $dbo;
    }
 
    /**
     * 返回当前页对应的记录集
     *
     * @param string $fields
     * @param boolean $queryLinks
     *
     * @return array
     */
    function & findAll($fields = '*', $queryLinks = true)
    {
        if ($this->count == -1) {
            $this->count = 20;
        }
 
        $offset = ($this->currentPage - $this->_basePageIndex) * $this->pageSize;
        if (is_object($this->source)||is_array($this->source)) {
            $limit = array($this->pageSize, $offset);
            if(!is_array($this->source))
            $rowset = $this->source->findAll($this->_conditions, $this->_sortby, $limit, $fields, $queryLinks);
            else {
                    $rowset=array();
                    $r= $this->source;
                    for ($i=$offset;$i<=$offset+$this->pageSize;$i++)
                    if(isset($r[$i]))$rowset[]=$r[$i];
            }
        } else {
            if (is_null($this->dbo)) {
                $this->dbo =& FLEA::getDBO(false);
            }
            $rs = $this->dbo->selectLimit($this->source, $this->pageSize, $offset);
            $rowset = $this->dbo->getAll($rs);
        }
        return $rowset;
    }
 
    /**
     * 返回分页信息,方便在模版中使用
     *
     * @param boolean $returnPageNumbers
     *
     * @return array
     */
    function getPagerData($returnPageNumbers = true)
    {
        $data = array(
            'pageSize' => $this->pageSize,
            'totalCount' => $this->totalCount,
            'count' => $this->count,
            'pageCount' => $this->pageCount,
            'firstPage' => $this->firstPage,
            'firstPageNumber' => $this->firstPageNumber,
            'lastPage' => $this->lastPage,
            'lastPageNumber' => $this->lastPageNumber,
            'prevPage' => $this->prevPage,
            'prevPageNumber' => $this->prevPageNumber,
            'nextPage' => $this->nextPage,
            'nextPageNumber' => $this->nextPageNumber,
            'currentPage' => $this->currentPage,
            'currentPageNumber' => $this->currentPageNumber,
        );
 
        if ($returnPageNumbers) {
            $data['pagesNumber'] = array();
            for ($i = 0; $i < $this->pageCount; $i++) {
                $data['pagesNumber'][$i] = $i + 1;
            }
        }
 
        return $data;
    }
 
    /**
     * 产生指定范围内的页面索引和页号
     *
     * @param int $currentPage
     * @param int $navbarLen
     *
     * @return array
     */
    function getNavbarIndexs($currentPage = 0, $navbarLen = 8)
    {
        $mid = intval($navbarLen / 2);
        if ($currentPage < $this->firstPage) {
            $currentPage = $this->firstPage;
        }
        if ($currentPage > $this->lastPage) {
            $currentPage = $this->lastPage;
        }
 
        $begin = $currentPage - $mid;
        if ($begin < $this->firstPage) { $begin = $this->firstPage; }
        $end = $begin + $navbarLen - 1;
        if ($end >= $this->lastPage) {
            $end = $this->lastPage;
            $begin = $end - $navbarLen + 1;
            if ($begin < $this->firstPage) { $begin = $this->firstPage; }
        }
 
        $data = array();
        for ($i = $begin; $i <= $end; $i++) {
            $data[] = array('index' => $i, 'number' => ($i + 1 - $this->_basePageIndex));
        }
        return $data;
    }
 
    /**
     * 生成一个页面选择跳转控件
     *
     * @param string $caption
     * @param string $jsfunc
     */
    function renderPageJumper($caption = '%u', $jsfunc = 'fnOnPageChanged')
    {
        $out = "                                if ($i == $this->currentPage) {                $out .= " selected";            }            $out .=">";            $out .= sprintf($caption, $i + 1 - $this->_basePageIndex);            $out .= "                \n";
        echo $out;
    }
 
    /**
     * 计算各项分页参数
     */
    function computingPage()
    {
        $this->pageCount = ceil($this->count / $this->pageSize);
        $this->firstPage = $this->_basePageIndex;
        $this->lastPage = $this->pageCount + $this->_basePageIndex - 1;
        if ($this->lastPage < $this->firstPage) { $this->lastPage = $this->firstPage; }
 
        if ($this->lastPage < $this->_basePageIndex) {
            $this->lastPage = $this->_basePageIndex;
        }
 
        if ($this->currentPage >= $this->pageCount + $this->_basePageIndex) {
            $this->currentPage = $this->lastPage;
        }
 
        if ($this->currentPage < $this->_basePageIndex) {
            $this->currentPage = $this->firstPage;
        }
 
        if ($this->currentPage < $this->lastPage - 1) {
            $this->nextPage = $this->currentPage + 1;
        } else {
            $this->nextPage = $this->lastPage;
        }
 
        if ($this->currentPage > $this->_basePageIndex) {
            $this->prevPage = $this->currentPage - 1;
        } else {
            $this->prevPage = $this->_basePageIndex;
        }
 
        $this->firstPageNumber = $this->firstPage + 1 - $this->_basePageIndex;
        $this->lastPageNumber = $this->lastPage + 1 - $this->_basePageIndex;
        $this->nextPageNumber = $this->nextPage + 1 - $this->_basePageIndex;
        $this->prevPageNumber = $this->prevPage + 1 - $this->_basePageIndex;
        $this->currentPageNumber = $this->currentPage + 1 - $this->_basePageIndex;
    }
}

1 Response to 让flea的FLEA_Helper_Pager类支持数组分... »

  1. 小丁 评论 2012-03-04 20:16

    楼主是高人,厉害,我去测试下,借用一下!

    回复

Leave a Reply

Email address is not published

You should say a Chinese word to pass spam check. If you can not input Chinese, just copy 你好 and paste them into comment text box.