1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elgg\Discussions; |
4
|
|
|
|
5
|
|
|
use Elgg\Database\Seeds\Seed; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Add database seed |
9
|
|
|
* |
10
|
|
|
* @access private |
11
|
|
|
*/ |
12
|
|
|
class Seeder extends Seed { |
13
|
|
|
|
14
|
|
|
private $status = [ |
15
|
|
|
'open', |
16
|
|
|
'closed', |
17
|
|
|
]; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
|
|
public function seed() { |
23
|
|
|
|
24
|
|
|
$count_discussions = function () { |
25
|
|
|
return elgg_get_entities([ |
26
|
|
|
'types' => 'object', |
27
|
|
|
'subtypes' => 'discussion', |
28
|
|
|
'metadata_names' => '__faker', |
29
|
|
|
'count' => true, |
30
|
|
|
]); |
31
|
|
|
}; |
32
|
|
|
|
33
|
|
|
while ($count_discussions() < $this->limit) { |
34
|
|
|
$metadata = [ |
35
|
|
|
'status' => $this->getRandomStatus(), |
36
|
|
|
'excerpt' => $this->faker()->sentence(), |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
$attributes = [ |
40
|
|
|
'subtype' => 'discussion', |
41
|
|
|
'container_guid' => $this->getRandomGroup()->guid, |
|
|
|
|
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
$discussion = $this->createObject($attributes, $metadata); |
45
|
|
|
|
46
|
|
|
if (!$discussion) { |
47
|
|
|
continue; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->createComments($discussion); |
51
|
|
|
$this->createLikes($discussion); |
52
|
|
|
|
53
|
|
|
elgg_create_river_item([ |
54
|
|
|
'action_type' => 'create', |
55
|
|
|
'subject_guid' => $discussion->owner_guid, |
56
|
|
|
'object_guid' => $discussion->guid, |
57
|
|
|
'target_guid' => $discussion->container_guid, |
58
|
|
|
]); |
59
|
|
|
|
60
|
|
|
elgg_trigger_event('publish', 'object', $discussion); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
public function unseed() { |
68
|
|
|
|
69
|
|
|
$discussions = elgg_get_entities([ |
70
|
|
|
'types' => 'object', |
71
|
|
|
'subtypes' => 'discussion', |
72
|
|
|
'metadata_names' => '__faker', |
73
|
|
|
'limit' => 0, |
74
|
|
|
'batch' => true, |
75
|
|
|
]); |
76
|
|
|
|
77
|
|
|
/* @var $discussions \ElggBatch */ |
78
|
|
|
|
79
|
|
|
$discussions->setIncrementOffset(false); |
80
|
|
|
|
81
|
|
|
foreach ($discussions as $discussion) { |
82
|
|
|
if ($discussion->delete()) { |
83
|
|
|
$this->log("Deleted discussion $discussion->guid"); |
84
|
|
|
} else { |
85
|
|
|
$this->log("Failed to delete discussion $discussion->guid"); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns random discussion status |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function getRandomStatus() { |
95
|
|
|
$key = array_rand($this->status, 1); |
96
|
|
|
|
97
|
|
|
return $this->status[$key]; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|