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\Component\Common\Criteria\Criteria; |
22
|
|
|
use SWP\Bundle\ContentBundle\Doctrine\ArticleRepositoryInterface; |
23
|
|
|
use SWP\Bundle\StorageBundle\Doctrine\ODM\PHPCR\DocumentRepository; |
24
|
|
|
|
25
|
|
|
class ArticleRepository extends DocumentRepository implements ArticleRepositoryInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @param string $id |
29
|
|
|
* |
30
|
|
|
* @return object |
31
|
|
|
*/ |
32
|
|
|
public function findBaseNode($id) |
33
|
|
|
{ |
34
|
|
|
return $this->dm->find(null, $id); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function findOneBySlug($slug) |
41
|
|
|
{ |
42
|
|
|
return $this->findOneBy(['slug' => $slug]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function findAllArticles() |
49
|
|
|
{ |
50
|
|
|
return $this->createQueryBuilder('o')->getQuery(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getByCriteria(Criteria $criteria, array $sorting) |
54
|
|
|
{ |
55
|
|
|
$routeIdentifier = $this->dm->getNodeForDocument($route)->getIdentifier(); |
|
|
|
|
56
|
|
|
$query = $this->getRouteArticlesQuery($routeIdentifier, $parameters); |
|
|
|
|
57
|
|
|
$articles = $this->dm->getDocumentsByPhpcrQuery($query, Article::class); |
58
|
|
|
|
59
|
|
|
//$this->getRouteArticlesQuery($routeIdentifier, [])->execute()->getRows()->count() |
|
|
|
|
60
|
|
|
|
61
|
|
|
if (isset($parameters['limit'])) { |
62
|
|
|
$query->setLimit($parameters['limit']); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (isset($parameters['start'])) { |
66
|
|
|
$query->setOffset($parameters['start']); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $articles; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $identifier |
74
|
|
|
* @param array $order |
75
|
|
|
* |
76
|
|
|
* @return SqlQuery |
77
|
|
|
* |
78
|
|
|
* @throws \Exception |
79
|
|
|
*/ |
80
|
|
|
public function getQueryForRouteArticles(string $identifier, array $order = []) : SqlQuery |
81
|
|
|
{ |
82
|
|
|
$queryStr = sprintf('SELECT S.route FROM [nt:unstructured] as S WHERE S.route="%s" AND S.status="%s"', $identifier, ArticleInterface::STATUS_PUBLISHED); |
83
|
|
|
if (count($order) === 2) { |
84
|
|
|
$allowedOrders = ['ASC', 'DESC']; |
85
|
|
|
if (!in_array(strtoupper($order[1]), $allowedOrders)) { |
86
|
|
|
throw new \Exception('Order filter must have two parameters with second one asc or desc, e.g. order(id, desc)'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($order[0] === 'id') { |
90
|
|
|
$order[0] = 'jcr:uuid'; |
91
|
|
|
} else { |
92
|
|
|
// Check that the given parameter is actually a field name of a route |
93
|
|
|
$metaData = $this->dm->getClassMetadata('SWP\Bundle\ContentBundle\Doctrine\ODM\PHPCR\Article'); |
94
|
|
|
if (!in_array($order[0], $metaData->getFieldNames())) { |
95
|
|
|
throw new \Exception(sprintf('Only those parameters are allowed: %s. %s was given ', implode(', ', $metaData->getFieldNames()), $order[0])); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
$queryStr .= sprintf(' ORDER BY S.%s %s', $order[0], $order[1]); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->dm->createPhpcrQuery($queryStr, QueryInterface::JCR_SQL2); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.