1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* The simple implementation of a recursive query. |
5
|
|
|
*/ |
6
|
|
|
namespace Rocket\Taxonomy\Utils; |
7
|
|
|
|
8
|
|
|
use Illuminate\Support\Arr; |
9
|
|
|
use Illuminate\Support\Collection; |
10
|
|
|
use Illuminate\Support\Facades\DB; |
11
|
|
|
use Rocket\Taxonomy\Model\Hierarchy; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* This Class handles recusive queries to retrieve |
15
|
|
|
* parent-child relations for terms. |
16
|
|
|
*/ |
17
|
|
|
class RecursiveQuery implements RecursiveQueryInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string The table in which hierarchical data is stored |
21
|
|
|
*/ |
22
|
|
|
protected $hierarchyTable; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* This Class handles recusive queries to retrieve |
26
|
|
|
* parent-child relations for terms. |
27
|
|
|
*/ |
28
|
105 |
|
public function __construct() |
29
|
|
|
{ |
30
|
105 |
|
$this->hierarchyTable = (new Hierarchy)->getTable(); |
31
|
105 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get all ancestors of a term |
35
|
|
|
* |
36
|
|
|
* @param int $id The term ID |
37
|
|
|
* @return \Illuminate\Support\Collection |
38
|
|
|
*/ |
39
|
27 |
|
public function getAncestry($id) |
40
|
|
|
{ |
41
|
27 |
|
$all_results = new Collection(DB::select($this->getAncestryInitialQuery(), [':id' => $id])); |
42
|
|
|
|
43
|
27 |
|
if (count($all_results)) { |
44
|
20 |
|
$this->getRecursiveAncestry($all_results, $all_results->pluck('parent_id')); |
45
|
20 |
|
} |
46
|
|
|
|
47
|
27 |
|
return $all_results; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get the query to initiate the recursive query. |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
57 |
|
protected function getAncestryInitialQuery() |
56
|
|
|
{ |
57
|
57 |
|
return "select term_id, parent_id from $this->hierarchyTable where term_id = :id"; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get the ancestry recursively |
62
|
|
|
* |
63
|
|
|
* @param Collection $all_results |
64
|
|
|
* @param int[] $ids |
65
|
|
|
*/ |
66
|
20 |
|
protected function getRecursiveAncestry(Collection $all_results, $ids) |
67
|
|
|
{ |
68
|
20 |
|
$all_results->merge($results = DB::table($this->hierarchyTable)->whereIn('term_id', $ids)->get()); |
69
|
|
|
|
70
|
20 |
|
if (count($results)) { |
71
|
16 |
|
$this->getRecursiveAncestry($all_results, Arr::pluck($results, 'parent_id')); |
72
|
16 |
|
} |
73
|
20 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get all descendants of a term. |
77
|
|
|
* |
78
|
|
|
* @param int $id The term ID |
79
|
|
|
* @return \Illuminate\Support\Collection |
80
|
|
|
*/ |
81
|
27 |
|
public function getDescent($id) |
82
|
|
|
{ |
83
|
27 |
|
$all_results = new Collection(DB::select($this->getDescentInitialQuery(), [':id' => $id])); |
84
|
|
|
|
85
|
27 |
|
if (count($all_results)) { |
86
|
17 |
|
$this->getRecursiveDescent($all_results, $all_results->pluck('term_id')); |
87
|
17 |
|
} |
88
|
|
|
|
89
|
27 |
|
return $all_results; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get the query to initiate the recursive query. |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
57 |
|
protected function getDescentInitialQuery() |
98
|
|
|
{ |
99
|
57 |
|
return "select term_id, parent_id from $this->hierarchyTable where parent_id = :id"; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the descent recursively. |
104
|
|
|
* |
105
|
|
|
* @param Collection $all_results |
106
|
|
|
* @param int[] $ids |
107
|
|
|
*/ |
108
|
17 |
|
protected function getRecursiveDescent(Collection $all_results, $ids) |
109
|
|
|
{ |
110
|
17 |
|
$all_results->merge($results = DB::table($this->hierarchyTable)->whereIn('parent_id', $ids)->get()); |
111
|
|
|
|
112
|
17 |
|
if (count($results)) { |
113
|
13 |
|
$this->getRecursiveDescent($all_results, Arr::pluck($results, 'term_id')); |
114
|
13 |
|
} |
115
|
17 |
|
} |
116
|
|
|
} |
117
|
|
|
|