|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the CCDNForum ForumBundle |
|
5
|
|
|
* |
|
6
|
|
|
* (c) CCDN (c) CodeConsortium <http://www.codeconsortium.com/> |
|
7
|
|
|
* |
|
8
|
|
|
* Available on github <http://www.github.com/codeconsortium/> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace CCDNForum\ForumBundle\Model\Component\Manager; |
|
15
|
|
|
|
|
16
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
17
|
|
|
|
|
18
|
|
|
use CCDNForum\ForumBundle\Model\Component\Manager\ManagerInterface; |
|
19
|
|
|
use CCDNForum\ForumBundle\Model\Component\Manager\BaseManager; |
|
20
|
|
|
|
|
21
|
|
|
use CCDNForum\ForumBundle\Entity\Board; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* |
|
25
|
|
|
* @category CCDNForum |
|
26
|
|
|
* @package ForumBundle |
|
27
|
|
|
* |
|
28
|
|
|
* @author Reece Fowell <[email protected]> |
|
29
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
30
|
|
|
* @version Release: 2.0 |
|
31
|
|
|
* @link https://github.com/codeconsortium/CCDNForumForumBundle |
|
32
|
|
|
* |
|
33
|
|
|
*/ |
|
34
|
|
|
class BoardManager extends BaseManager implements ManagerInterface |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* |
|
38
|
|
|
* @access public |
|
39
|
|
|
* @return \CCDNForum\ForumBundle\Entity\Board |
|
40
|
|
|
*/ |
|
41
|
|
|
public function createBoard() |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->gateway->createBoard(); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* |
|
48
|
|
|
* @access public |
|
49
|
|
|
* @param \CCDNForum\ForumBundle\Entity\Board $board |
|
50
|
|
|
* @return \CCDNForum\ForumBundle\Manager\ManagerInterface |
|
|
|
|
|
|
51
|
|
|
*/ |
|
52
|
|
|
public function saveBoard(Board $board) |
|
53
|
|
|
{ |
|
54
|
|
|
$boardCount = $this->model->getBoardCount(); |
|
|
|
|
|
|
55
|
|
|
$board->setListOrderPriority(++$boardCount); |
|
56
|
|
|
|
|
57
|
|
|
$this->gateway->saveBoard($board); |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* |
|
64
|
|
|
* @access public |
|
65
|
|
|
* @param \CCDNForum\ForumBundle\Entity\Board $board |
|
66
|
|
|
* @return \CCDNForum\ForumBundle\Manager\ManagerInterface |
|
|
|
|
|
|
67
|
|
|
*/ |
|
68
|
|
|
public function updateBoard(Board $board) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->gateway->updateBoard($board); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* |
|
77
|
|
|
* @access public |
|
78
|
|
|
* @param \CCDNForum\ForumBundle\Entity\Board $board |
|
79
|
|
|
* @return \CCDNForum\ForumBundle\Manager\ManagerInterface |
|
|
|
|
|
|
80
|
|
|
*/ |
|
81
|
|
|
public function deleteBoard(Board $board) |
|
82
|
|
|
{ |
|
83
|
|
|
// If we do not refresh the board, AND we have reassigned the topics to null, |
|
84
|
|
|
// then its lazy-loaded topics are dirty, as the topics in memory will still |
|
85
|
|
|
// have the old board id set. Removing the board will cascade into deleting |
|
86
|
|
|
// topics aswell, even though in the db the relation has been set to null. |
|
87
|
|
|
$this->refresh($board); |
|
88
|
|
|
$this->remove($board)->flush(); |
|
89
|
|
|
|
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* |
|
95
|
|
|
* @access public |
|
96
|
|
|
* @param \Doctrine\Common\Collections\ArrayCollection $topics |
|
97
|
|
|
* @param \CCDNForum\ForumBundle\Entity\Board $board |
|
|
|
|
|
|
98
|
|
|
* @return \CCDNForum\ForumBundle\Manager\ManagerInterface |
|
|
|
|
|
|
99
|
|
|
*/ |
|
100
|
|
|
public function reassignTopicsToBoard(ArrayCollection $topics, Board $board = null) |
|
101
|
|
|
{ |
|
102
|
|
|
foreach ($topics as $topic) { |
|
103
|
|
|
$topic->setBoard($board); |
|
104
|
|
|
$this->persist($topic); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$this->flush(); |
|
108
|
|
|
|
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* |
|
114
|
|
|
* @access public |
|
115
|
|
|
* @param Array $boards |
|
116
|
|
|
* @param \CCDNForum\ForumBundle\Entity\Board $boardShift |
|
117
|
|
|
* @param int $direction |
|
118
|
|
|
* @return \CCDNForum\ForumBundle\Manager\ManagerInterface |
|
|
|
|
|
|
119
|
|
|
*/ |
|
120
|
|
|
public function reorderBoards($boards, Board $boardShift, $direction) |
|
121
|
|
|
{ |
|
122
|
|
|
$boardCount = (count($boards) - 1); |
|
123
|
|
|
|
|
124
|
|
|
// Find board in collection to shift and use list order as array key for easier editing. |
|
125
|
|
|
$sorted = array(); |
|
126
|
|
|
$shiftIndex = null; |
|
127
|
|
|
foreach ($boards as $boardIndex => $board) { |
|
128
|
|
|
if ($boards[$boardIndex]->getId() == $boardShift->getId()) { |
|
129
|
|
|
$shiftIndex = $boardIndex; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$sorted[$boardIndex] = $board; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
$incrementKeysAfterIndex = function ($index, $arr) { |
|
|
|
|
|
|
136
|
|
|
$hydrated = array(); |
|
137
|
|
|
|
|
138
|
|
|
foreach ($arr as $key => $el) { |
|
139
|
|
|
if ($key > $index) { |
|
140
|
|
|
$hydrated[$key + 1] = $el; |
|
141
|
|
|
} else { |
|
142
|
|
|
$hydrated[$key] = $el; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return $hydrated; |
|
147
|
|
|
}; |
|
148
|
|
|
|
|
149
|
|
|
$decrementKeysBeforeIndex = function ($index, $arr) { |
|
|
|
|
|
|
150
|
|
|
$hydrated = array(); |
|
151
|
|
|
|
|
152
|
|
|
foreach ($arr as $key => $el) { |
|
153
|
|
|
if ($key < $index) { |
|
154
|
|
|
$hydrated[$key - 1] = $el; |
|
155
|
|
|
} else { |
|
156
|
|
|
$hydrated[$key] = $el; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return $hydrated; |
|
161
|
|
|
}; |
|
162
|
|
|
|
|
163
|
|
|
$shifted = array(); |
|
164
|
|
|
|
|
165
|
|
|
// First Board needs reordering?? |
|
166
|
|
|
if ($shiftIndex == 0) { |
|
167
|
|
|
if ($direction) { // Down (move down 1) |
|
168
|
|
|
$shifted = $sorted; |
|
169
|
|
|
$shifted[$shiftIndex] = $sorted[$shiftIndex + 1]; |
|
170
|
|
|
$shifted[$shiftIndex + 1] = $sorted[$shiftIndex]; |
|
171
|
|
|
} else { // Up (send to bottom) |
|
172
|
|
|
$shifted[$boardCount] = $sorted[0]; |
|
173
|
|
|
unset($sorted[0]); |
|
174
|
|
|
$shifted = array_merge($decrementKeysBeforeIndex($boardCount + 1, $sorted), $shifted); |
|
175
|
|
|
} |
|
176
|
|
|
} else { |
|
177
|
|
|
// Last board needs reordering?? |
|
178
|
|
|
if ($shiftIndex == $boardCount) { |
|
179
|
|
|
if ($direction) { // Down (send to top) |
|
180
|
|
|
$shifted[0] = $sorted[$boardCount]; |
|
181
|
|
|
unset($sorted[$boardCount]); |
|
182
|
|
|
$shifted = array_merge($shifted, $incrementKeysAfterIndex(-1, $sorted)); |
|
183
|
|
|
} else { // Up (move up 1) |
|
184
|
|
|
$shifted = $sorted; |
|
185
|
|
|
$shifted[$shiftIndex] = $sorted[$shiftIndex - 1]; |
|
186
|
|
|
$shifted[$shiftIndex - 1] = $sorted[$shiftIndex]; |
|
187
|
|
|
} |
|
188
|
|
|
} else { |
|
189
|
|
|
// Swap 2 boards not at beginning or end. |
|
190
|
|
|
$shifted = $sorted; |
|
191
|
|
|
if ($direction) { // Down (move down 1) |
|
192
|
|
|
$shifted[$shiftIndex] = $sorted[$shiftIndex + 1]; |
|
193
|
|
|
$shifted[$shiftIndex + 1] = $sorted[$shiftIndex]; |
|
194
|
|
|
} else { // Up (move up 1) |
|
195
|
|
|
$shifted[$shiftIndex] = $sorted[$shiftIndex - 1]; |
|
196
|
|
|
$shifted[$shiftIndex - 1] = $sorted[$shiftIndex]; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
// Set the order from the $index arranged prior and persist. |
|
202
|
|
|
foreach ($shifted as $index => $board) { |
|
203
|
|
|
$board->setListOrderPriority($index); |
|
204
|
|
|
$this->persist($board); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
$this->flush(); |
|
208
|
|
|
|
|
209
|
|
|
return $this; |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: