Elgg  Version 2.3
Objects.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database\Seeds;
4 
10 class Objects extends Seed {
11 
12  private $status = [
13  'unsaved_draft',
14  'draft',
15  'published',
16  ];
17 
21  public function seed() {
22 
23  $count_blogs = function () {
25  'types' => 'object',
26  'subtypes' => 'blog',
27  'metadata_names' => '__faker',
28  'count' => true,
29  ]);
30  };
31 
32  while ($count_blogs() < $this->limit) {
33 
34  $metadata = [
35  'status' => $this->getRandomStatus(),
36  'comments_on' => $this->faker->boolean() ? 'On' : 'Off',
37  'excerpt' => $this->faker->sentence(),
38  ];
39 
40  $attributes = [
41  'subtype' => 'blog',
42  ];
43 
44  $blog = $this->createObject($attributes, $metadata);
45 
46  if (!$blog) {
47  continue;
48  }
49 
50  if ($blog->status == 'unsaved_draft' || $blog->status == 'draft') {
51  $blog->future_access = $blog->access_id;
52  $blog->access_id = ACCESS_PRIVATE;
53  }
54 
55  if ($blog->status == 'published') {
57  'view' => 'river/object/blog/create',
58  'action_type' => 'create',
59  'subject_guid' => $blog->owner_guid,
60  'object_guid' => $blog->guid,
61  'target_guid' => $blog->container_guid,
62  ]);
63 
64  elgg_trigger_event('publish', 'object', $blog);
65  }
66 
67  if ($this->faker->boolean()) {
68  $blog->annotate('blog_auto_save', $this->faker->text(500), ACCESS_PRIVATE, $blog->owner_guid);
69  }
70 
71  if ($this->faker->boolean() && $blog->status != 'unsaved_draft') {
72  $blog->annotate('blog_revision', $blog->description, ACCESS_PRIVATE, $blog->owner_guid);
73  $blog->description = $this->faker->text(500);
74  }
75 
76  $blog->save();
77  }
78  }
79 
83  public function unseed() {
84 
86  'types' => 'object',
87  'subtypes' => 'blog',
88  'metadata_names' => '__faker',
89  'limit' => 0,
90  'batch' => true,
91  ]);
92 
93  /* @var $blogs \ElggBatch */
94 
95  $blogs->setIncrementOffset(false);
96 
97  foreach ($blogs as $blog) {
98  if ($blog->delete()) {
99  $this->log("Deleted blog $blog->guid");
100  } else {
101  $this->log("Failed to delete blog $blog->guid");
102  }
103  }
104  }
105 
110  public function getRandomStatus() {
111  $key = array_rand($this->status, 1);
112 
113  return $this->status[$key];
114  }
115 }
elgg_get_entities_from_metadata(array $options=array())
interfaces
Definition: metadata.php:276
$metadata
Definition: entity.php:19
log($msg, $level= 'NOTICE')
Log a message.
Definition: Seed.php:685
elgg_create_river_item(array $options=array())
Adds an item to the river.
Definition: river.php:39
$key
Definition: summary.php:34
const ACCESS_PRIVATE
Definition: elgglib.php:2082
Abstract seed.
Definition: Seed.php:20
elgg_trigger_event($event, $object_type, $object=null)
Definition: elgglib.php:614
$attributes
Definition: ajax_loader.php:13
createObject(array $attributes=[], array $metadata=[])
Create a new faker object.
Definition: Seed.php:244
getRandomStatus()
Returns random blog status.
Definition: Objects.php:110