Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id$
4
 *
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 *
17
 * This software consists of voluntary contributions made by many individuals
18
 * and is licensed under the LGPL. For more information, see
19
 * <http://www.doctrine-project.org>.
20
 */
21
 
22
/**
23
 * Doctrine_Pager_Range_Sliding
24
 *
25
 * @author      Guilherme Blanco <guilhermeblanco@hotmail.com>
26
 * @package     Doctrine
27
 * @subpackage  Pager
28
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
29
 * @version     $Revision$
30
 * @link        www.doctrine-project.org
31
 * @since       0.9
32
 */
33
class Doctrine_Pager_Range_Sliding extends Doctrine_Pager_Range
34
{
35
    /**
36
     * @var int $_chunkLength     Chunk length to be returned
37
     */
38
    private $_chunkLength;
39
 
40
    /**
41
     * _initialize
42
     *
43
     * Initialize Doctrine_Pager_Range_Sliding and does custom assignments
44
     *
45
     * @return void
46
     */
47
    protected function _initialize()
48
    {
49
        if (isset($this->_options['chunk'])) {
50
            $this->_setChunkLength($this->_options['chunk']);
51
        } else {
52
            throw new Doctrine_Pager_Exception('Missing parameter \'chunk\' that must be defined in options.');
53
        }
54
    }
55
 
56
    /**
57
     * getChunkLength
58
     *
59
     * Returns the size of the chunk defined
60
     *
61
     * @return int        Chunk length
62
     */
63
    public function getChunkLength()
64
    {
65
        return $this->_chunkLength;
66
    }
67
 
68
    /**
69
     * _setChunkLength
70
     *
71
     * Defines the size of the chunk
72
     *
73
     * @param $chunkLength       Chunk length
74
     * @return void
75
     */
76
    protected function _setChunkLength($chunkLength)
77
    {
78
        $chunkLength = (int) $chunkLength;
79
        if ( !$chunkLength) {
80
            $chunkLength = 1;
81
        } else {
82
            $this->_chunkLength = $chunkLength;
83
        }
84
    }
85
 
86
    /**
87
     * rangeAroundPage
88
     *
89
     * Calculate and returns an array representing the range around the current page
90
     *
91
     * @return array
92
     */
93
    public function rangeAroundPage()
94
    {
95
        $pager = $this->getPager();
96
 
97
        if ($pager->getExecuted()) {
98
            $page  = $pager->getPage();
99
            $pages = $pager->getLastPage();
100
 
101
            $chunk = $this->getChunkLength();
102
 
103
            if ($chunk > $pages) {
104
                $chunk = $pages;
105
            }
106
 
107
            $chunkStart = $page - (floor($chunk / 2));
108
            $chunkEnd   = $page + (ceil($chunk / 2)-1);
109
 
110
            if ($chunkStart < 1) {
111
                $adjust = 1 - $chunkStart;
112
                $chunkStart = 1;
113
                $chunkEnd = $chunkEnd + $adjust;
114
            }
115
 
116
            if ($chunkEnd > $pages) {
117
                $adjust = $chunkEnd - $pages;
118
                $chunkStart = $chunkStart - $adjust;
119
                $chunkEnd = $pages;
120
            }
121
 
122
            return range($chunkStart, $chunkEnd);
123
        }
124
 
125
        throw new Doctrine_Pager_Exception(
126
            'Cannot retrieve the range around the page of a not yet executed Pager query'
127
        );
128
    }
129
}