sfSphinxPropelPager.class.php 3.02 KB
Newer Older
Игорь's avatar
init    
Игорь committed
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
<?php
/**
 * sfSphinx Propel pager class
 * @package sfSphinxPlugin
 * @author  Hung Dao <hungdao@mahshelf.com>
 */

class sfSphinxPropelPager extends sfPropelPager
{
  protected
    $peer_method_name = 'retrieveByPKs',
    $keyword          = null,
    $sphinx           = null;

  /**
   * Constructor
   * @param object         $class
   * @param integer        $maxPerPage
   * @param sfSphinxClient $sphinx
   */
  public function __construct($class, $maxPerPage = 10, sfSphinxClient $sphinx)
  {
    parent::__construct($class, $maxPerPage);
    $this->sphinx = $sphinx;
  }

  /**
   * A function to be called after parameters have been set
   */
  public function init()
  {
    $hasMaxRecordLimit = ($this->getMaxRecordLimit() !== false);
    $maxRecordLimit = $this->getMaxRecordLimit();

    $res = $this->sphinx->getRes();
    if ($res === false)
    {
      return;
    }

    $count = $res['total_found'];

    $this->setNbResults($hasMaxRecordLimit ? min($count, $maxRecordLimit) : $count);

    if (($this->getPage() == 0 || $this->getMaxPerPage() == 0))
    {
      $this->setLastPage(0);
    }
    else
    {
      $this->setLastPage(ceil($this->getNbResults() / $this->getMaxPerPage()));

      $offset = ($this->getPage() - 1) * $this->getMaxPerPage();

      if ($hasMaxRecordLimit)
      {
        $maxRecordLimit = $maxRecordLimit - $offset;
        if ($maxRecordLimit > $this->getMaxPerPage())
        {
          $limit = $this->getMaxPerPage();
        }
        else
        {
          $limit = $maxRecordLimit;
        }
      }
      else
      {
        $limit= $this->getMaxPerPage();
      }
      $this->sphinx->SetLimits($offset, $limit);
    }
  }

  /**
   * Retrieve an object of a certain model with offset
   * used internally by getCurrent()
   * @param  integer $offset
   * @return object
   */
  protected function retrieveObject($offset)
  {
    $this->sphinx->SetLimits($offset - 1, 1); // We only need one object

    $res = $this->sphinx->getRes();
    if ($res['total_found'] == 0)
    {
      return null;
    }

    $ids = array();
    foreach ($res['matches'] as $match)
    {
      $ids[] = $match['id'];
    }

    // be smart and try to use best peer method
    $peer_method = $this->getPeerMethod();
    if ($peer_method == 'retrieveByPks')
    {
      $results = call_user_func(array($this->getClassPeer(), $peer_method), $ids);
    }
    else
    {
      $results = call_user_func(array($this->getClassPeer(), $peer_method), $this->getCriteria());
    }

    return is_array($results) && isset($results[0]) ? $results[0] : null;
  }

  /**
   * Return results for given page
   * @return array
   */
  public function getResults()
  {
    $res = $this->sphinx->getRes();
    if ($res['total_found'] == 0)
    {
      return array();
    }

    // First we need to get the Ids
    $ids = array();
    foreach ($res['matches'] as $match)
    {
      $ids[] = $match['id'];
    }
    // Then we retrieve the objects correspoding to the found Ids

    return call_user_func(array($this->getClassPeer(), $this->getPeerMethod()), $ids);
  }

}