67 lines
1.7 KiB
PHP
67 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace Deblan\Bundle\BlogBundle\Page;
|
|
|
|
use Trinity\Bundle\ContentManagerBundle\Model\Page;
|
|
use Deblan\Bundle\BlogBundle\Model\PostPeer;
|
|
|
|
class SearchEnginePage extends HomePage
|
|
{
|
|
public function __construct($template = 'DeblanBlogBundle:Default:searchEngine.html.twig')
|
|
{
|
|
$this->setObject(array());
|
|
|
|
parent::__construct($template);
|
|
}
|
|
|
|
public function getConfiguration()
|
|
{
|
|
return $this->configuration
|
|
->setDefaultController('DeblanBlogBundle:Default:searchEngine')
|
|
->setDefaultControllerClass('Deblan\Bundle\BlogBundle\Controller\DefaultController::searchEngineAction');
|
|
}
|
|
|
|
public function getQuery()
|
|
{
|
|
$query = parent::getQuery();
|
|
|
|
foreach ($this->getObject() as $k => $v) {
|
|
if ($k === 0) {
|
|
$query
|
|
->where(PostPeer::TITLE.' LIKE ?', '%'.$v.'%')
|
|
->orWhere(PostPeer::TAGS.' LIKE ?', '%'.$v.'%')
|
|
->orWhere(PostPeer::CONTENT.' LIKE ?', '%'.$v.'%');
|
|
} else {
|
|
$query
|
|
->orWhere(PostPeer::TITLE.' LIKE ?', '%'.$v.'%')
|
|
->orWhere(PostPeer::TAGS.' LIKE ?', '%'.$v.'%')
|
|
->orWhere(PostPeer::CONTENT.' LIKE ?', '%'.$v.'%');
|
|
}
|
|
}
|
|
|
|
return $query;
|
|
}
|
|
|
|
public function setObject($words)
|
|
{
|
|
if (!is_array($words)) {
|
|
$words = explode(' ', $words);
|
|
}
|
|
|
|
foreach ($words as $k => $v) {
|
|
if (trim($v) === '') {
|
|
unset($words[$k]);
|
|
}
|
|
}
|
|
|
|
parent::setObject($words);
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getWords()
|
|
{
|
|
return implode(' ', $this->getObject());
|
|
}
|
|
}
|