0
Elgg версии 2.0Как добавить иконку изображения к объекту Elgg
Как я могу идти о создании сущности Elgg с иконкой изображения, точно так же как, как можно сделать при создании группы Эльг?
Я попытался реализации следующего, но изображение не получает загружен:
Действия файл:
<?php
// get the form inputs
$title = get_input('title');
$body = get_input('body');
$tags = string_to_tag_array(get_input('tags'));
// create a new my_blog object
$blog = new ElggObject();
$blog->subtype = "nganya";
$blog->title = $title;
$blog->description = $body;
// for now make all my_blog posts public
$blog->access_id = ACCESS_PUBLIC;
// owner is logged in user
$blog->owner_guid = elgg_get_logged_in_user_guid();
// save tags as metadata
$blog->tags = $tags;
// save to database and get id of the new my_blog
$blog_guid = $blog->save();
// if the my_blog was saved, we want to display the new post
// otherwise, we want to register an error and forward back to the form
if ($blog_guid) {
system_message("Your nganya post was saved >>>>>>>>>> " . $blog_guid);
forward($blog->getURL());
} else {
register_error("The nganya post could not be saved");
forward(REFERER); // REFERER is a global variable that defines the previous page
}
$has_uploaded_icon = (!empty($_FILES['icon']['type']) && substr_count($_FILES['icon']['type'], 'image/'));
if ($has_uploaded_icon) {
$icon_sizes = elgg_get_config('icon_sizes');
$prefix = "nganya/" . $blog->guid;
$filehandler = new ElggFile();
$filehandler->owner_guid = $blog_guid;
$filehandler->setFilename($prefix . ".jpg");
$filehandler->open("write");
$filehandler->write(get_uploaded_file('icon'));
$filehandler->close();
$filename = $filehandler->getFilenameOnFilestore();
$sizes = array('tiny', 'small', 'medium', 'large', 'master');
$thumbs = array();
foreach ($sizes as $size) {
$thumbs[$size] = get_resized_image_from_existing_file(
$filename,
$icon_sizes[$size]['w'],
$icon_sizes[$size]['h'],
$icon_sizes[$size]['square']
);
}
if ($thumbs['tiny']) { // just checking if resize successful
$thumb = new ElggFile();
$thumb->owner_guid = $blog->owner_guid;
$thumb->setMimeType('image/jpeg');
foreach ($sizes as $size) {
$thumb->setFilename("{$prefix}{$size}.jpg");
$thumb->open("write");
$thumb->write($thumbs[$size]);
$thumb->close();
}
$blog->icontime = time();
}
}
формы:
<div>
<label><?php echo elgg_echo("groups:icon"); ?></label><br />
<?php echo elgg_view("input/file", array("name" => "icon")); ?>
</div>
<div>
<label for="title"><?= elgg_echo("title"); ?></label><br />
<?= elgg_view('input/text', ['name' => 'title', 'id' => 'title']); ?>
</div>
<div>
<label for="body"><?= elgg_echo("body"); ?></label><br />
<?= elgg_view('input/longtext', ['name' => 'body', 'id' => 'body']); ?>
</div>
<div>
<label for="tags"><?= elgg_echo("tags"); ?></label><br />
<?= elgg_view('input/tags', ['name' => 'tags', 'id' => 'tags']); ?>
</div>
<div>
<?= elgg_view('input/submit', ['value' => elgg_echo('save')]); ?>
</div>