1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Handles polymorphic relation for commentlist |
5
|
|
|
* |
6
|
|
|
* Uses elements of PolymorphicHasManyList in 3.2 |
7
|
|
|
* |
8
|
|
|
* @author dmooyman |
9
|
|
|
*/ |
10
|
|
|
class CommentList extends HasManyList |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Retrieve the name of the class this relation is filtered by |
15
|
|
|
* |
16
|
|
|
* @return string |
17
|
|
|
*/ |
18
|
|
|
public function getForeignClass() |
19
|
|
|
{ |
20
|
|
|
return $this->dataQuery->getQueryParam('Foreign.Class'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function __construct($parentClassName) |
24
|
|
|
{ |
25
|
|
|
parent::__construct('Comment', 'ParentID'); |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
// Ensure underlying DataQuery globally references the class filter |
29
|
|
|
$this->dataQuery->setQueryParam('Foreign.Class', $parentClassName); |
30
|
|
|
|
31
|
|
|
// For queries with multiple foreign IDs (such as that generated by |
32
|
|
|
// DataList::relation) the filter must be generalised to filter by subclasses |
33
|
|
|
$classNames = Convert::raw2sql(ClassInfo::subclassesFor($parentClassName)); |
34
|
|
|
$this->dataQuery->where(sprintf( |
35
|
|
|
"\"BaseClass\" IN ('%s')", implode("', '", $classNames) |
36
|
|
|
)); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Adds the item to this relation. |
41
|
|
|
* |
42
|
|
|
* @param Comment $item The comment to be added |
43
|
|
|
*/ |
44
|
|
|
public function add($item) |
45
|
|
|
{ |
46
|
|
|
// Check item given |
47
|
|
|
if (is_numeric($item)) { |
48
|
|
|
$item = Comment::get()->byID($item); |
49
|
|
|
} |
50
|
|
|
if (!($item instanceof Comment)) { |
51
|
|
|
throw new InvalidArgumentException("CommentList::add() expecting a Comment object, or ID value"); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Validate foreignID |
55
|
|
|
$foreignID = $this->getForeignID(); |
56
|
|
|
if (!$foreignID || is_array($foreignID)) { |
57
|
|
|
throw new InvalidArgumentException("CommentList::add() can't be called until a single foreign ID is set"); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$item->ParentID = $foreignID; |
61
|
|
|
$item->BaseClass = $this->getForeignClass(); |
62
|
|
|
$item->write(); |
63
|
|
|
} |
64
|
|
|
/** |
65
|
|
|
* Remove a Comment from this relation by clearing the foreign key. Does not actually delete the comment. |
66
|
|
|
* |
67
|
|
|
* @param Comment $item The Comment to be removed |
68
|
|
|
*/ |
69
|
|
|
public function remove($item) |
70
|
|
|
{ |
71
|
|
|
// Check item given |
72
|
|
|
if (is_numeric($item)) { |
73
|
|
|
$item = Comment::get()->byID($item); |
74
|
|
|
} |
75
|
|
|
if (!($item instanceof Comment)) { |
76
|
|
|
throw new InvalidArgumentException("CommentList::remove() expecting a Comment object, or ID", |
77
|
|
|
E_USER_ERROR); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// Don't remove item with unrelated class key |
81
|
|
|
$foreignClass = $this->getForeignClass(); |
82
|
|
|
$classNames = ClassInfo::subclassesFor($foreignClass); |
83
|
|
|
if (!in_array($item->BaseClass, $classNames)) { |
84
|
|
|
return; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// Don't remove item which doesn't belong to this list |
88
|
|
|
$foreignID = $this->getForeignID(); |
89
|
|
|
if (empty($foreignID) |
90
|
|
|
|| (is_array($foreignID) && in_array($item->ParentID, $foreignID)) |
91
|
|
|
|| $foreignID == $item->ParentID |
92
|
|
|
) { |
93
|
|
|
$item->ParentID = null; |
94
|
|
|
$item->BaseClass = null; |
95
|
|
|
$item->write(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.