-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented twig methods sulu_article_load_recent
sulu_article_load_similar
#193
Changes from 9 commits
95587e9
02b2592
84a84a0
b3e9203
d4b3e33
f5d8f5e
5d6ac14
d51cb00
197bfb5
5f9dfaf
751fbaf
840334c
faa6ab3
1656f5f
9e2af12
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) MASSIVE ART WebServices GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ArticleBundle\Content; | ||
|
||
use ProxyManager\Factory\LazyLoadingValueHolderFactory; | ||
use ProxyManager\Proxy\LazyLoadingInterface; | ||
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument; | ||
use Sulu\Bundle\ArticleBundle\Document\ArticleViewDocumentInterface; | ||
use Sulu\Component\DocumentManager\DocumentManagerInterface; | ||
|
||
class ArticleResourceItemFactory | ||
{ | ||
/** | ||
* @var DocumentManagerInterface | ||
*/ | ||
protected $documentManager; | ||
|
||
/** | ||
* @var LazyLoadingValueHolderFactory | ||
*/ | ||
protected $proxyFactory; | ||
|
||
/** | ||
* @param DocumentManagerInterface $documentManager | ||
* @param LazyLoadingValueHolderFactory $proxyFactory | ||
*/ | ||
public function __construct( | ||
DocumentManagerInterface $documentManager, | ||
LazyLoadingValueHolderFactory $proxyFactory | ||
) { | ||
$this->documentManager = $documentManager; | ||
$this->proxyFactory = $proxyFactory; | ||
} | ||
|
||
/** | ||
* Creates and returns article source item with proxy document. | ||
* | ||
* @param ArticleViewDocumentInterface $articleViewDocument | ||
* | ||
* @return ArticleResourceItem | ||
*/ | ||
public function getResourceItem(ArticleViewDocumentInterface $articleViewDocument) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
{ | ||
return new ArticleResourceItem( | ||
$articleViewDocument, | ||
$this->getResource($articleViewDocument->getUuid(), $articleViewDocument->getLocale()) | ||
); | ||
} | ||
|
||
/** | ||
* Returns Proxy document for uuid. | ||
* | ||
* @param string $uuid | ||
* @param string $locale | ||
* | ||
* @return object | ||
*/ | ||
private function getResource($uuid, $locale) | ||
{ | ||
return $this->proxyFactory->createProxy( | ||
ArticleDocument::class, | ||
function ( | ||
&$wrappedObject, | ||
LazyLoadingInterface $proxy, | ||
$method, | ||
array $parameters, | ||
&$initializer | ||
) use ($uuid, $locale) { | ||
$initializer = null; | ||
$wrappedObject = $this->documentManager->find($uuid, $locale); | ||
|
||
return true; | ||
} | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,6 +86,17 @@ public function getConfigTreeBuilder() | |
->end() | ||
->end() | ||
->scalarNode('display_tab_all')->defaultTrue()->info("Display tab 'all' in list view")->end() | ||
->arrayNode('search_fields') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. update config:dump in |
||
->prototype('scalar')->end()->defaultValue([ | ||
'title', | ||
'excerpt.title', | ||
'excerpt.description', | ||
'excerpt.seo.title', | ||
'excerpt.seo.description', | ||
'excerpt.seo.keywords', | ||
'teaser_description', | ||
]) | ||
->end() | ||
->end(); | ||
|
||
return $treeBuilder; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) MASSIVE ART WebServices GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ArticleBundle\Document\Repository; | ||
|
||
use ONGR\ElasticsearchBundle\Result\DocumentIterator; | ||
use ONGR\ElasticsearchBundle\Service\Manager; | ||
use ONGR\ElasticsearchBundle\Service\Repository; | ||
use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery; | ||
use ONGR\ElasticsearchDSL\Query\Specialized\MoreLikeThisQuery; | ||
use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery; | ||
use ONGR\ElasticsearchDSL\Search; | ||
use ONGR\ElasticsearchDSL\Sort\FieldSort; | ||
use Sulu\Bundle\ArticleBundle\Metadata\ArticleViewDocumentIdTrait; | ||
use Sulu\Component\DocumentManager\DocumentManagerInterface; | ||
|
||
class ArticleViewDocumentRepository | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. phpdoc |
||
{ | ||
use ArticleViewDocumentIdTrait; | ||
|
||
/** | ||
* @var Manager | ||
*/ | ||
protected $searchManager; | ||
|
||
/** | ||
* @var DocumentManagerInterface | ||
*/ | ||
protected $documentManager; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $articleDocumentClass; | ||
|
||
/** | ||
* @var Repository | ||
*/ | ||
protected $repository; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $searchFields; | ||
|
||
/** | ||
* @param Manager $searchManager | ||
* @param string $articleDocumentClass | ||
* @param array $searchFields | ||
*/ | ||
public function __construct( | ||
Manager $searchManager, | ||
$articleDocumentClass, | ||
array $searchFields | ||
) { | ||
$this->searchManager = $searchManager; | ||
$this->articleDocumentClass = $articleDocumentClass; | ||
$this->repository = $this->searchManager->getRepository($this->articleDocumentClass); | ||
$this->searchFields = $searchFields; | ||
} | ||
|
||
/** | ||
* Finds recent articles for given parameters sorted by field `published`. | ||
* | ||
* @param null|string $excludeUuid | ||
* @param int $limit | ||
* @param null|array $types | ||
* @param string $locale | ||
* | ||
* @return DocumentIterator | ||
*/ | ||
public function findRecent($excludeUuid = null, $limit, array $types = null, $locale) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. provide a default for |
||
{ | ||
$search = $this->createSearch($limit, $types, $locale); | ||
|
||
if ($excludeUuid) { | ||
$search->addQuery(new TermQuery('uuid', $excludeUuid), BoolQuery::MUST_NOT); | ||
} | ||
|
||
$search->addSort(new FieldSort('published')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm in my opinion should be |
||
|
||
return $this->repository->findDocuments($search); | ||
} | ||
|
||
/** | ||
* Finds similar articles for given `uuid` with given parameters. | ||
* | ||
* @param string $uuid | ||
* @param int $limit | ||
* @param null|array $types | ||
* @param string $locale | ||
* | ||
* @return DocumentIterator | ||
*/ | ||
public function findSimilar($uuid, $limit, array $types = null, $locale) | ||
{ | ||
$search = $this->createSearch($limit, $types, $locale); | ||
|
||
$search->addQuery( | ||
new MoreLikeThisQuery( | ||
null, | ||
[ | ||
'fields' => $this->searchFields, | ||
'min_term_freq' => 1, | ||
'min_doc_freq' => 2, | ||
'ids' => [$this->getViewDocumentId($uuid, $locale)], | ||
] | ||
) | ||
); | ||
|
||
return $this->repository->findDocuments($search); | ||
} | ||
|
||
/** | ||
* @param int $limit | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. always add a description for function |
||
* @param null|array $types | ||
* @param string $locale | ||
* | ||
* @return Search | ||
*/ | ||
private function createSearch($limit, array $types = null, $locale) | ||
{ | ||
$search = $this->repository->createSearch(); | ||
|
||
$search->addQuery(new TermQuery('locale', $locale), BoolQuery::FILTER); | ||
$search->setSize($limit); | ||
|
||
if ($types) { | ||
$typesQuery = new BoolQuery(); | ||
foreach ($types as $type) { | ||
$typesQuery->add(new TermQuery('type', $type), BoolQuery::SHOULD); | ||
} | ||
$search->addQuery($typesQuery); | ||
} | ||
|
||
return $search; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) MASSIVE ART WebServices GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ArticleBundle\Exception; | ||
|
||
/** | ||
* Thrown when no article-page is found in request. | ||
*/ | ||
class ArticleInRequestNotFoundException extends \Exception | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct('No article in request found'); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
phpdoc also on classes and interfaces