1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This file is part of the Superdesk Web Publisher Content Bundle. |
7
|
|
|
* |
8
|
|
|
* Copyright 2016 Sourcefabric z.ú. and contributors. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please see the |
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @copyright 2016 Sourcefabric z.ú |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace SWP\Bundle\ContentBundle\Doctrine\ODM\PHPCR; |
18
|
|
|
|
19
|
|
|
use Jackalope\Query\SqlQuery; |
20
|
|
|
use PHPCR\Query\QueryInterface; |
21
|
|
|
use SWP\Bundle\ContentBundle\Doctrine\ArticleRepositoryInterface; |
22
|
|
|
use SWP\Bundle\StorageBundle\Doctrine\ODM\PHPCR\DocumentRepository; |
23
|
|
|
|
24
|
|
|
class ArticleRepository extends DocumentRepository implements ArticleRepositoryInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
18 |
|
public function findOneBySlug($slug) |
30
|
|
|
{ |
31
|
18 |
|
return $this->findOneBy(['slug' => $slug]); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function findAllArticles() |
38
|
|
|
{ |
39
|
|
|
return $this->createQueryBuilder('o')->getQuery(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $identifier |
44
|
|
|
* @param array $order |
45
|
|
|
* |
46
|
|
|
* @return SqlQuery |
47
|
|
|
* |
48
|
|
|
* @throws \Exception |
49
|
|
|
*/ |
50
|
6 |
|
public function getQueryForRouteArticles(string $identifier, array $order) : SqlQuery |
51
|
|
|
{ |
52
|
6 |
|
$queryStr = sprintf("SELECT * FROM nt:unstructured as S WHERE S.phpcr:class='%s' AND S.route=%s AND S.status=published", Article::class, $identifier); |
53
|
6 |
|
$allowedOrders = ['ASC', 'DESC']; |
54
|
6 |
|
if (count($order) !== 2 || !in_array(strtoupper($order[1]), $allowedOrders)) { |
55
|
|
|
throw new \Exception('Order filter must have two parameters with second one asc or desc, e.g. order(id, desc)'); |
56
|
|
|
} |
57
|
|
|
|
58
|
6 |
|
if ($order[0] === 'id') { |
59
|
1 |
|
$order[0] = 'jcr:uuid'; |
60
|
|
|
} else { |
61
|
|
|
// Check that the given parameter is actually a field name of a route |
62
|
5 |
|
$metaData = $this->dm->getClassMetadata('SWP\Bundle\ContentBundle\Doctrine\ODM\PHPCR\Article'); |
63
|
5 |
|
if (!in_array($order[0], $metaData->getFieldNames())) { |
64
|
1 |
|
throw new \Exception(sprintf('Only those parameters are allowed: %s. %s was given ', implode(', ', $metaData->getFieldNames()), $order[0])); |
65
|
|
|
} |
66
|
|
|
} |
67
|
5 |
|
$queryStr .= sprintf(' ORDER BY S.%s %s', $order[0], $order[1]); |
68
|
|
|
|
69
|
5 |
|
return $this->dm->createPhpcrQuery($queryStr, QueryInterface::JCR_SQL2); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|