private function preFillSingle($key, $contenttype, $images)
{
$content = [];
$title = '';
$content['contenttype'] = $key;
$content['datecreated'] = date('Y-m-d H:i:s', time() - rand(0, 365 * 24 * 60 * 60));
$content['datepublish'] = date('Y-m-d H:i:s', time() - rand(0, 365 * 24 * 60 * 60));
$content['datedepublish'] = null;
$username = array_rand($this->app['users']->getUsers(), 1);
$user = $this->app['users']->getUser($username);
$content['ownerid'] = $user['id'];
$content['status'] = 'published';
shuffle($images);
foreach ($contenttype['fields'] as $field => $values) {
switch ($values['type']) {
case 'text':
if ($contenttype['slug'] === 'blocks' && $field === 'title') {
// Special case: if we're prefilling a 'blocks' contenttype add some
// sensible titles to get started.
$content[$field] = $this->getBlocksTitle();
} else {
if (strpos($field, 'link') !== false) {
// Another special case: If the field contains 'link', we guess it'll be used
// as a link, so don't prefill it with "text", but leave it blank instead.
$content[$field] = '';
} else {
$content[$field] = trim(strip_tags($this->app['prefill']->get('/1/veryshort')));
}
}
if (empty($title)) {
$title = $content[$field];
}
break;
case 'image':
// Get a random image
if (!empty($images)) {
$image = next($images);
$content[$field]['file'] = $image->getPath();
}
break;
case 'html':
case 'textarea':
case 'markdown':
if (in_array($field, ['teaser', 'introduction', 'excerpt', 'intro', 'content'])) {
$params = '/medium/decorate/link/1';
} else {
$params = '/medium/decorate/link/ol/ul/3';
}
$content[$field] = trim($this->app['prefill']->get($params));
if ($values['type'] == "markdown") {
$content[$field] = strip_tags($content[$field]);
}
break;
case 'datetime':
$content[$field] = date('Y-m-d H:i:s', time() - rand(-365 * 24 * 60 * 60, 365 * 24 * 60 * 60));
break;
case 'date':
$content[$field] = date('Y-m-d', time() - rand(-365 * 24 * 60 * 60, 365 * 24 * 60 * 60));
break;
case 'checkbox':
$content[$field] = rand(0, 1);
break;
case 'float':
case 'number':
// number is deprecated
// number is deprecated
case 'integer':
$content[$field] = rand(-1000, 1000) + rand(0, 1000) / 1000;
break;
}
}
$contentobject = $this->getContentObject($contenttype);
$contentobject->setValues($content);
// After we initially filled the content object, we get the title to set the slug.
$slug = $this->app['slugify']->slugify($contentobject->getTitle());
$contentobject->setValue('slug', $slug);
if (!empty($contenttype['taxonomy'])) {
foreach ($contenttype['taxonomy'] as $taxonomy) {
if ($this->app['config']->get('taxonomy/' . $taxonomy . '/options')) {
$options = $this->app['config']->get('taxonomy/' . $taxonomy . '/options');
$key = array_rand($options);
$contentobject->setTaxonomy($taxonomy, $key, $options[$key], rand(1, 1000));
}
if ($this->app['config']->get('taxonomy/' . $taxonomy . '/behaves_like') == 'tags') {
$contentobject->setTaxonomy($taxonomy, $this->getSomeRandomTags(5));
}
}
}
$this->saveContent($contentobject);
$output = Trans::__("Added to <tt>%key%</tt> '%title%'", ['%key%' => $key, '%title%' => $contentobject->getTitle()]) . "<br>\n";
return $output;
}