-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2427433
commit 8cdb8a4
Showing
9 changed files
with
374 additions
and
48 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
config/sfLuceneable15BehaviorPluginConfiguration.class.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
class sfLuceneable15BehaviorPluginConfiguration extends sfPluginConfiguration | ||
{ | ||
public function initialize() | ||
{ | ||
if ($this->configuration instanceof sfApplicationConfiguration) | ||
{ | ||
sfLuceneableToolkit::registerZend(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
/** | ||
* Returns an array of indexed columns. | ||
* @return array | ||
*/ | ||
static public function getIndexedColumns() | ||
{ | ||
return self::$_indexedColumns; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
/** | ||
* holds an array of indexed columns | ||
* | ||
*/ | ||
private static $_indexedColumns = array( | ||
<?php foreach ($data as $column => $method): ?> | ||
'<?php echo $column ?>' => '<?php echo $method ?>', | ||
<?php endforeach; ?> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
class sfLuceneModelResults implements Iterator, Countable, ArrayAccess | ||
{ | ||
protected $model; | ||
protected $results; | ||
protected $pointer = 0; | ||
protected $indexedColums = array(); | ||
protected $query = null; | ||
|
||
public function __construct($model, $results, $query) | ||
{ | ||
$this->model = $model; | ||
$this->results = $results; | ||
$this->query = $query; | ||
$this->indexedColumns = call_user_func(array($model.'Peer', 'getIndexedColumns')); | ||
} | ||
|
||
public function current() | ||
{ | ||
return $this->hydrate($this->results[$this->pointer]); | ||
} | ||
|
||
public function key() | ||
{ | ||
return $this->pointer; | ||
} | ||
|
||
public function next() | ||
{ | ||
$this->pointer++; | ||
} | ||
|
||
public function rewind() | ||
{ | ||
$this->pointer = 0; | ||
} | ||
|
||
public function valid() | ||
{ | ||
return isset($this->results[$this->pointer]); | ||
} | ||
|
||
public function count() | ||
{ | ||
return count($this->results); | ||
} | ||
|
||
public function offsetExists($offset) | ||
{ | ||
return isset($this->results[$offset]); | ||
} | ||
|
||
public function offsetGet($offset) | ||
{ | ||
return $this->results[$offset]; | ||
} | ||
|
||
public function offsetSet($offset, $set) | ||
{ | ||
$this->results[$offset] = $set; | ||
} | ||
|
||
public function offsetUnset($offset) | ||
{ | ||
unset($this->results[$offset]); | ||
} | ||
|
||
protected function hydrate($result) | ||
{ | ||
$tmp = array(); | ||
foreach ($this->indexedColumns as $col => $type) | ||
{ | ||
if ($col == 'id') $tmp[$col] = $result->pk; | ||
elseif (strtolower($type) == 'text') | ||
{ | ||
$tmp[$col] = $this->query->htmlFragmentHighlightMatches($result->$col); | ||
} | ||
else | ||
{ | ||
$tmp[$col] = $result->$col; | ||
} | ||
} | ||
$object = new $this->model; | ||
$object->fromArray($tmp, BasePeer::TYPE_FIELDNAME); | ||
$object->setNew(false); | ||
$object->resetModified(); | ||
return $object; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
<?php | ||
|
||
class sfLucenePager | ||
{ | ||
protected $model = null; | ||
protected $results = array(); | ||
protected $search = null; | ||
protected $page = 1, $perPage = 5, $nbResults = 0, $lastPage = 0; | ||
protected $maxRecordLimit = false; | ||
|
||
public function __construct($model, $search, $perPage = 5) | ||
{ | ||
$this->model = $model; | ||
$this->search = $search; | ||
$this->perPage = $perPage; | ||
} | ||
|
||
public function init() | ||
{ | ||
$hasMaxRecordLimit = ($this->getMaxRecordLimit() !== false); | ||
$maxRecordLimit = $this->getMaxRecordLimit(); | ||
|
||
$hits = sfLuceneableToolkit::getHits($this->model, $this->search); | ||
$count = count($hits); | ||
$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())); | ||
} | ||
$this->results = $hits; | ||
} | ||
|
||
public function getLinks($nb_links = 5) | ||
{ | ||
$links = array(); | ||
$tmp = $this->getPage() - floor($nb_links / 2); | ||
$check = $this->getLastPage() - $nb_links + 1; | ||
$limit = ($check > 0) ? $check : 1; | ||
$begin = ($tmp > 0) ? (($tmp > $limit) ? $limit : $tmp) : 1; | ||
|
||
$i = $begin; | ||
while (($i < $begin + $nb_links) && ($i <= $this->getLastPage())) | ||
{ | ||
$links[] = $i++; | ||
} | ||
|
||
return $links; | ||
} | ||
|
||
public function haveToPaginate() | ||
{ | ||
return (($this->getPage() != 0) && ($this->getNbResults() > $this->getMaxPerPage())); | ||
} | ||
|
||
public function getMaxPerPage() | ||
{ | ||
return $this->perPage; | ||
} | ||
|
||
public function setMaxPerPage($per) | ||
{ | ||
$this->perPage = $per; | ||
} | ||
|
||
public function setPage($page) | ||
{ | ||
$this->page = $page; | ||
} | ||
|
||
public function getNbResults() | ||
{ | ||
return $this->nbResults; | ||
} | ||
|
||
protected function setNbResults($nb) | ||
{ | ||
$this->nbResults = $nb; | ||
} | ||
|
||
public function getMaxRecordLimit() | ||
{ | ||
return $this->maxRecordLimit; | ||
} | ||
|
||
public function setMaxRecordLimit($limit) | ||
{ | ||
$this->maxRecordLimit = $limit; | ||
} | ||
|
||
public function getPage() | ||
{ | ||
return $this->page; | ||
} | ||
|
||
public function getResults() | ||
{ | ||
$offset = ($this->getPage() - 1) * $this->getMaxPerPage(); | ||
$limit = $this->getMaxPerPage(); | ||
if ($limit == 0) | ||
{ | ||
$results = $this->results; | ||
} | ||
$results = array_slice($this->results, $offset, $limit); | ||
return new sfLuceneModelResults($this->model, $results, $this->search); | ||
} | ||
|
||
public function getFirstPage() | ||
{ | ||
return 1; | ||
} | ||
|
||
public function getLastPage() | ||
{ | ||
return $this->lastPage; | ||
} | ||
|
||
protected function setLastPage($page) | ||
{ | ||
$this->lastPage = $page; | ||
if ($this->getPage() > $page) { | ||
$this->setPage($page); | ||
} | ||
} | ||
|
||
public function getNextPage() | ||
{ | ||
return min($this->getPage() + 1, $this->getLastPage()); | ||
} | ||
|
||
public function getPreviousPage() | ||
{ | ||
return max($this->getPage() - 1, $this->getFirstPage()); | ||
} | ||
|
||
public function getFirstIndice() | ||
{ | ||
if ($this->getPage() == 0) | ||
{ | ||
return 1; | ||
} | ||
else | ||
{ | ||
return ($this->getPage() - 1) * $this->getMaxPerPage() + 1; | ||
} | ||
} | ||
|
||
public function getLastIndice() | ||
{ | ||
if ($this->getPage() == 0) | ||
{ | ||
return $this->getNbResults(); | ||
} | ||
else | ||
{ | ||
if (($this->getPage() * $this->getMaxPerPage()) >= $this->getNbResults()) | ||
{ | ||
return $this->getNbResults(); | ||
} | ||
else | ||
{ | ||
return ($this->getPage() * $this->getMaxPerPage()); | ||
} | ||
} | ||
} | ||
|
||
public function isFirstPage() | ||
{ | ||
return $this->getPage() == $this->getFirstPage(); | ||
} | ||
|
||
public function isLastPage() | ||
{ | ||
return $this->getPage() == $this->getLastPage(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.