Skip to content

Commit

Permalink
changed: updated for Elgg 6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jeabakker committed Nov 22, 2024
1 parent 11094d6 commit 72f3f2c
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 273 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Blog Tools
==========

![Elgg 6.0](https://img.shields.io/badge/Elgg-6.0-green.svg)
![Elgg 6.1](https://img.shields.io/badge/Elgg-6.1-green.svg)
![Lint Checks](https://github.com/ColdTrick/blog_tools/actions/workflows/lint.yml/badge.svg?event=push)
[![Latest Stable Version](https://poser.pugx.org/coldtrick/blog_tools/v/stable.svg)](https://packagist.org/packages/coldtrick/blog_tools)
[![License](https://poser.pugx.org/coldtrick/blog_tools/license.svg)](https://packagist.org/packages/coldtrick/blog_tools)
Expand Down
94 changes: 37 additions & 57 deletions actions/blog/save.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
$preview = (bool) get_input('preview');

// edit or create a new entity
$guid = (int) get_input('guid');
$new_post = true;
$revision_text = null;

if ($guid) {
$entity = get_entity($guid);
if ($entity instanceof ElggBlog && $entity->canEdit()) {
$blog = $entity;
} else {
$guid = (int) get_input('guid');
if (!empty($guid)) {
$blog = get_entity($guid);
if (!$blog instanceof \ElggBlog || !$blog->canEdit()) {
return elgg_error_response(elgg_echo('blog:error:post_not_found'));
}

Expand All @@ -31,65 +30,45 @@
$new_post = false;
} else {
$blog = new \ElggBlog();

$container_guid = (int) get_input('container_guid');
$container = get_entity($container_guid);
if (!$container || !$container->canWriteToContainer(0, 'object', 'blog')) {
return elgg_error_response(elgg_echo('blog:error:cannot_write_to_container'));
}

$blog->container_guid = $container->guid;
}

// set the previous status for the events to update the time_created and river entries
$old_status = $blog->status;

// set defaults and required values.
$values = [
'title' => '',
'description' => '',
'status' => 'draft',
'access_id' => ACCESS_DEFAULT,
'comments_on' => 'On',
'excerpt' => '',
'tags' => '',
'container_guid' => (int) get_input('container_guid'),
// ColdTrick additions
'publication_date' => '',
'publication_time' => 0,
'publication' => null,
];

// fail if a required entity isn't set
$required = ['title', 'description'];

// load from POST and do sanity and access checking
foreach ($values as $name => $default) {
if ($name === 'title') {
$values = [];
$fields = elgg()->fields->get('object', 'blog');
foreach ($fields as $field) {
$value = null;

$name = (string) elgg_extract('name', $field);
if (elgg_extract('#type', $field) === 'tags') {
$value = elgg_string_to_array((string) get_input($name));
} elseif ($name === 'title') {
$value = elgg_get_title_input();
} else {
$value = get_input($name, $default);
$value = get_input($name);
}

if (in_array($name, $required) && empty($value)) {
if (elgg_is_empty($value) && elgg_extract('required', $field)) {
return elgg_error_response(elgg_echo("blog:error:missing:{$name}"));
}

$values[$name] = $value;
}

switch ($name) {
case 'tags':
$values[$name] = elgg_string_to_array((string) $value);
break;

case 'container_guid':
// this can't be empty or saving the base entity fails
if (!empty($value)) {
$container = get_entity($value);
if ($container && (!$new_post || $container->canWriteToContainer(0, 'object', 'blog'))) {
$values[$name] = $value;
} else {
return elgg_error_response(elgg_echo('blog:error:cannot_write_to_container'));
}
} else {
unset($values[$name]);
}
break;

default:
$values[$name] = $value;
break;
}
if (elgg_get_plugin_setting('advanced_publication', 'blog_tools') === 'yes') {
// can't use fields service because of fieldset
$values['publication_date'] = get_input('publication_date');
$values['publication_time'] = get_input('publication_time');
$values['publication'] = null;
}

// check publication date/time
Expand Down Expand Up @@ -120,7 +99,7 @@

// assign values to the entity
foreach ($values as $name => $value) {
$blog->$name = $value;
$blog->{$name} = $value;
}

if (!$blog->save()) {
Expand All @@ -140,14 +119,15 @@
elgg_create_river_item([
'view' => 'river/object/blog/create',
'action_type' => 'create',
'object_guid' => $blog->guid,
'subject_guid' => $blog->owner_guid,
'object_guid' => $blog->getGUID(),
'target_guid' => $blog->container_guid,
]);

elgg_trigger_event('publish', 'object', $blog);

// reset the creation time for posts that move from draft to published
if ($guid) {
if (!$new_post) {
$blog->time_created = time();
$blog->save();
}
Expand Down
38 changes: 38 additions & 0 deletions classes/ColdTrick/BlogTools/FieldsHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace ColdTrick\BlogTools;

use Elgg\Blog\Forms\PrepareFields;

/**
* Handle blog form fields
*/
class FieldsHandler {

/**
* Change fields for blogs
*
* @param \Elgg\Event $event 'fields', 'object:blog'
*
* @return null|array
*/
public function __invoke(\Elgg\Event $event): ?array {
$result = $event->getValue();

foreach ($result as $field) {
if (elgg_extract('name', $field) !== 'access_id') {
continue;
}

if (elgg_get_plugin_setting('advanced_publication', 'blog_tools') === 'yes') {
$field['#help'] = elgg_echo('blog_tools:edit:access:help:publication');
} else {
$field['#help'] = elgg_echo('blog_tools:edit:access:help');
}

break;
}

return $result;
}
}
48 changes: 48 additions & 0 deletions classes/ColdTrick/BlogTools/Forms/PrepareFields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace ColdTrick\BlogTools\Forms;

/**
* Prepare the fields for the blog/save form
*/
class PrepareFields {

/**
* Prepare the advanced publication options
*
* @param \Elgg\Event $event 'form:prepare:fields', 'blog/save'
*
* @return null|array
*/
public function __invoke(\Elgg\Event $event): ?array {
if (elgg_get_plugin_setting('advanced_publication', 'blog_tools') !== 'yes') {
return null;
}

$vars = $event->getValue();

$values = [
'publication_date' => null,
'publication_time' => null,
];

$entity = elgg_extract('entity', $vars);
if ($entity instanceof \ElggBlog) {
// load current blog values
foreach (array_keys($values) as $field) {
$metadata_name = $field;
if ($field === 'publication_time') {
$metadata_name = 'publication';
}

if (!isset($entity->{$metadata_name})) {
continue;
}

$values[$field] = $entity->{$metadata_name};
}
}

return array_merge($values, $vars);
}
}
36 changes: 35 additions & 1 deletion classes/ColdTrick/BlogTools/Views.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ColdTrick\BlogTools;

use Elgg\Values;
use Elgg\ViewsService;

/**
Expand All @@ -10,7 +11,7 @@
class Views {

/**
* Prevent the blog/sidebar/archives from being draw
* Prevent the blog/sidebar/archives from being drawn
*
* @param \Elgg\Event $event 'view_vars', 'blog/sidebar/archives'
*
Expand All @@ -26,4 +27,37 @@ public static function preventBlogArchiveSidebar(\Elgg\Event $event): ?array {

return $return;
}

/**
* Add the future publication date to the imprint of a blog
*
* @param \Elgg\Event $event 'view_vars', 'object/elements/imprint/contents'
*
* @return null|array
*/
public static function addPublicationDateImprint(\Elgg\Event $event): ?array {
$vars = $event->getValue();
$entity = elgg_extract('entity', $vars);
if (!$entity instanceof \ElggBlog || !$entity->canEdit()) {
return null;
}

if (!isset($entity->publication) || $entity->publication < time()) {
return null;
}

$dt = Values::normalizeTime($entity->publication);

$vars['imprint'][] = [
'icon_name' => 'calendar-alt',
'content' => elgg_format_element('span', [
'title' => elgg_echo('blog_tools:imprint:publication', [$dt->formatLocale(elgg_echo('friendlytime:date_format'))]),
], elgg_view('output/date', [
'value' => $entity->publication,
'format' => elgg_echo('friendlytime:date_format'),
])),
];

return $vars;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"issues": "https://github.com/ColdTrick/blog_tools/issues"
},
"conflict": {
"elgg/elgg": "<6.0"
"elgg/elgg": "<6.1"
}
}
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions elgg-plugin.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

use ColdTrick\BlogTools\FieldsHandler;
use ColdTrick\BlogTools\Forms\PrepareFields;

return [
'plugin' => [
'version' => '14.0',
Expand Down Expand Up @@ -45,6 +48,16 @@
'\ColdTrick\BlogTools\Widgets::widgetUrl' => [],
],
],
'fields' => [
'object:blog' => [
FieldsHandler::class => ['priority' => 501],
],
],
'form:prepare:fields' => [
'blog/save' => [
PrepareFields::class => [],
],
],
'group_tool_widgets' => [
'widget_manager' => [
'\ColdTrick\BlogTools\Widgets::groupTools' => [],
Expand All @@ -69,6 +82,9 @@
'blog/sidebar/archives' => [
'\ColdTrick\BlogTools\Views::preventBlogArchiveSidebar' => [],
],
'object/elements/imprint/contents' => [
'\ColdTrick\BlogTools\Views::addPublicationDateImprint' => [],
],
],
],
'view_extensions' => [
Expand Down
Loading

0 comments on commit 72f3f2c

Please sign in to comment.