diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 67fe092..6147873 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -3,6 +3,8 @@ name: PHPUnit Plugin Tests on: [push, pull_request] jobs: - lint: + phpunit: name: Run PHPUnit test suites uses: ColdTrick/.github/.github/workflows/phpunit.yml@master + with: + elgg_major_version: 6 diff --git a/README.md b/README.md index 0a17331..74e012b 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ Widget Manager ============== -![Elgg 5.1](https://img.shields.io/badge/Elgg-5.1-green.svg) +![Elgg 6.0](https://img.shields.io/badge/Elgg-6.0-green.svg) ![Lint Checks](https://github.com/ColdTrick/widget_manager/actions/workflows/lint.yml/badge.svg?event=push) [![Latest Stable Version](https://poser.pugx.org/coldtrick/widget_manager/v/stable.svg)](https://packagist.org/packages/coldtrick/widget_manager) [![License](https://poser.pugx.org/coldtrick/widget_manager/license.svg)](https://packagist.org/packages/coldtrick/widget_manager) diff --git a/actions/widget_manager/force_tool_widgets.php b/actions/widget_manager/force_tool_widgets.php index e86fafd..71cc05e 100644 --- a/actions/widget_manager/force_tool_widgets.php +++ b/actions/widget_manager/force_tool_widgets.php @@ -12,6 +12,7 @@ 'limit' => false, 'batch' => true, ]); + foreach ($groups as $group) { $group->save(); $counter++; diff --git a/actions/widget_manager/groups/update_widget_access.php b/actions/widget_manager/groups/update_widget_access.php index 8a9a47a..9217f0c 100644 --- a/actions/widget_manager/groups/update_widget_access.php +++ b/actions/widget_manager/groups/update_widget_access.php @@ -28,11 +28,9 @@ 'limit' => false, ]); -if ($widgets) { - foreach ($widgets as $widget) { - $widget->access_id = (int) $new_access; - $widget->save(); - } +foreach ($widgets as $widget) { + $widget->access_id = (int) $new_access; + $widget->save(); } return elgg_ok_response('', elgg_echo('widget_manager:action:groups:update_widget_access:success'), $group->getURL()); diff --git a/classes/ColdTrick/WidgetManager/Access.php b/classes/ColdTrick/WidgetManager/Access.php index b6f09ed..5e674eb 100644 --- a/classes/ColdTrick/WidgetManager/Access.php +++ b/classes/ColdTrick/WidgetManager/Access.php @@ -14,18 +14,18 @@ class Access { * * @param \Elgg\Event $event 'access:collections:write', 'all' * - * @return array + * @return null|array */ - public static function setWriteAccess(\Elgg\Event $event) { + public static function setWriteAccess(\Elgg\Event $event): ?array { $input_params = $event->getParam('input_params', []); if (elgg_extract('entity_type', $input_params) !== 'object' || elgg_extract('entity_subtype', $input_params) !== 'widget') { - return; + return null; } $container = get_entity(elgg_extract('container_guid', $input_params)); if (!$container instanceof \ElggEntity) { - return; + return null; } if ($container instanceof \ElggGroup) { @@ -42,7 +42,7 @@ public static function setWriteAccess(\Elgg\Event $event) { $widget = elgg_extract('entity', $input_params); if (!$widget instanceof \ElggWidget) { - return; + return null; } if (elgg_can_edit_widget_layout($widget->context)) { @@ -54,6 +54,8 @@ public static function setWriteAccess(\Elgg\Event $event) { ]; } } + + return null; } /** @@ -61,29 +63,26 @@ public static function setWriteAccess(\Elgg\Event $event) { * * @param \Elgg\Event $event 'permissions_check', 'site' * - * @return array + * @return null|bool */ - public static function writeAccessForIndexManagers(\Elgg\Event $event) { + public static function writeAccessForIndexManagers(\Elgg\Event $event): ?bool { $result = $event->getValue(); - if ($result) { - return; + return $result; } $entity = $event->getEntityParam(); if (!$entity instanceof \ElggSite) { - return; + return null; } $user = $event->getUserParam(); if (!$user instanceof \ElggUser) { - return; + return null; } $index_managers = explode(',', elgg_get_plugin_setting('index_managers', 'widget_manager', '')); - if (in_array($user->guid, $index_managers)) { - return true; - } + return in_array($user->guid, $index_managers) ?: null; } /** @@ -91,22 +90,18 @@ public static function writeAccessForIndexManagers(\Elgg\Event $event) { * * @param \Elgg\Event $event 'access:collections:read', 'user' * - * @return array + * @return null|array */ - public static function addLoggedOutReadAccess(\Elgg\Event $event) { + public static function addLoggedOutReadAccess(\Elgg\Event $event): ?array { if (elgg_is_logged_in() && !elgg_is_admin_logged_in()) { - return; + return null; } - $return_value = $event->getValue(); + $return_value = $event->getValue() ?: []; - if (empty($return_value)) { - $return_value = []; - } else { - if (!is_array($return_value)) { - $return_value = [$return_value]; - } + if (!is_array($return_value)) { + $return_value = [$return_value]; } $return_value[] = ACCESS_LOGGED_OUT; @@ -119,25 +114,23 @@ public static function addLoggedOutReadAccess(\Elgg\Event $event) { * * @param \Elgg\Event $event 'permissions_check', 'object' * - * @return boolean + * @return null|bool */ - public static function canEditWidgetOnManagedLayout(\Elgg\Event $event) { + public static function canEditWidgetOnManagedLayout(\Elgg\Event $event): ?bool { $user = $event->getUserParam(); $entity = $event->getEntityParam(); if ($event->getValue() || !$user instanceof \ElggUser || !$entity instanceof \ElggWidget) { - return; + return null; } if (!$entity->getOwnerEntity() instanceof \ElggSite) { // special permission is only for widget owned by site - return; + return null; } $context = $entity->context; - if ($context) { - return elgg_can_edit_widget_layout($context, $user->guid); - } + return $context ? elgg_can_edit_widget_layout($context, $user->guid) : null; } /** @@ -147,7 +140,7 @@ public static function canEditWidgetOnManagedLayout(\Elgg\Event $event) { * * @return void */ - public static function moreRightsForWidgetManager(\Elgg\Event $event) { + public static function moreRightsForWidgetManager(\Elgg\Event $event): void { if ($event->getType() === 'widgets/add') { elgg_register_event_handler('permissions_check', 'site', '\ColdTrick\WidgetManager\Access::writeAccessForIndexManagers'); return; @@ -171,7 +164,7 @@ public static function moreRightsForWidgetManager(\Elgg\Event $event) { elgg_register_event_handler('get_sql', 'access', function(\Elgg\Event $event) use ($widget_guid) { if ($event->getParam('ignore_access')) { // no need to give extra access - return; + return null; } /** diff --git a/classes/ColdTrick/WidgetManager/Groups.php b/classes/ColdTrick/WidgetManager/Groups.php index 8529cff..b0c77c4 100644 --- a/classes/ColdTrick/WidgetManager/Groups.php +++ b/classes/ColdTrick/WidgetManager/Groups.php @@ -14,16 +14,16 @@ class Groups { * * @param \Elgg\Event $event 'get_list', 'default_widgets' * - * @return string + * @return null|array */ - public static function addGroupsContextToDefaultWidgets(\Elgg\Event $event) { + public static function addGroupsContextToDefaultWidgets(\Elgg\Event $event): ?array { if (!elgg_is_active_plugin('groups')) { - return; + return null; } $group_enable = elgg_get_plugin_setting('group_enable', 'widget_manager'); if (!in_array($group_enable, ['yes', 'forced'])) { - return; + return null; } $return_value = $event->getValue(); @@ -53,7 +53,7 @@ public static function addGroupsContextToDefaultWidgets(\Elgg\Event $event) { * * @return void */ - public static function updateGroupWidgets(\Elgg\Event $event) { + public static function updateGroupWidgets(\Elgg\Event $event): void { $object = $event->getObject(); if (!$object instanceof \ElggGroup || !elgg_is_active_plugin('groups')) { return; @@ -211,23 +211,23 @@ public static function updateGroupWidgets(\Elgg\Event $event) { * * @param \Elgg\Event $event 'view_vars', 'groups/profile/widgets' * - * @return array + * @return null|array */ - public static function getGroupWidgetsLayout(\Elgg\Event $event) { + public static function getGroupWidgetsLayout(\Elgg\Event $event): ?array { $vars = $event->getValue(); $group = elgg_extract('entity', $vars); if (!$group instanceof \ElggGroup) { - return; + return null; } $group_enable = elgg_get_plugin_setting('group_enable', 'widget_manager'); if (!in_array($group_enable, ['forced', 'yes'])) { - return; + return null; } if ($group_enable === 'yes' && !$group->isToolEnabled('widget_manager')) { - return; + return null; } // need context = groups to fix the issue with the new group_profile context @@ -252,12 +252,12 @@ public static function getGroupWidgetsLayout(\Elgg\Event $event) { * * @param \Elgg\Event $event 'tool_options', 'group' * - * @return array + * @return null|array */ - public static function registerGroupWidgetsTool(\Elgg\Event $event) { + public static function registerGroupWidgetsTool(\Elgg\Event $event): ?array { $plugin = elgg_get_plugin_from_id('widget_manager'); if ($plugin->getSetting('group_enable') !== 'yes') { - return; + return null; } $result = $event->getValue(); @@ -280,7 +280,7 @@ public static function registerGroupWidgetsTool(\Elgg\Event $event) { * * @return void */ - public static function addGroupWidget(\Elgg\Event $event) { + public static function addGroupWidget(\Elgg\Event $event): void { $object = $event->getObject(); if (!$object instanceof \ElggWidget || elgg_in_context('widget_manager_group_tool_widgets') || !elgg_is_active_plugin('groups')) { return; @@ -320,7 +320,7 @@ public static function addGroupWidget(\Elgg\Event $event) { * * @return void */ - public static function addGroupWidgetShutdown(\Elgg\Event $event) { + public static function addGroupWidgetShutdown(\Elgg\Event $event): void { global $widget_manager_group_guids; if (empty($widget_manager_group_guids)) { @@ -378,7 +378,7 @@ public static function addGroupWidgetShutdown(\Elgg\Event $event) { * * @return void */ - public static function deleteGroupWidget(\Elgg\Event $event) { + public static function deleteGroupWidget(\Elgg\Event $event): void { $object = $event->getObject(); if (!$object instanceof \ElggWidget || elgg_in_context('widget_manager_group_tool_widgets') || !elgg_is_active_plugin('groups')) { return; diff --git a/classes/ColdTrick/WidgetManager/Menus.php b/classes/ColdTrick/WidgetManager/Menus.php index d320c66..f4b55d5 100644 --- a/classes/ColdTrick/WidgetManager/Menus.php +++ b/classes/ColdTrick/WidgetManager/Menus.php @@ -2,6 +2,8 @@ namespace ColdTrick\WidgetManager; +use Elgg\Menu\MenuItems; + /** * Menu callbacks */ @@ -12,11 +14,11 @@ class Menus { * * @param \Elgg\Event $event 'register', 'menu:admin_header' * - * @return boolean + * @return null|MenuItems */ - public static function registerAdminHeaderMenu(\Elgg\Event $event) { + public static function registerAdminHeaderMenu(\Elgg\Event $event): ?MenuItems { if (!elgg_is_admin_logged_in()) { - return; + return null; } $return_value = $event->getValue(); @@ -71,12 +73,12 @@ public static function registerAdminHeaderMenu(\Elgg\Event $event) { * * @param \Elgg\Event $event 'register', 'menu:entity' * - * @return array + * @return null|MenuItems */ - public static function addWidgetPageEntityMenuItems(\Elgg\Event $event) { + public static function addWidgetPageEntityMenuItems(\Elgg\Event $event): ?MenuItems { $entity = $event->getEntityParam(); if (!$entity instanceof \WidgetPage || !$entity->canEdit()) { - return; + return null; } $result = $event->getValue(); @@ -100,16 +102,16 @@ public static function addWidgetPageEntityMenuItems(\Elgg\Event $event) { * * @param \Elgg\Event $event 'register', 'title:widgets' * - * @return array + * @return null|MenuItems */ - public static function addWidgetsContentToggle(\Elgg\Event $event) { + public static function addWidgetsContentToggle(\Elgg\Event $event): ?MenuItems { if (!elgg_get_plugin_setting('show_collapse_content', 'widget_manager')) { - return; + return null; } if (!$event->getParam('show_collapse_content', false)) { - return; + return null; } $result = $event->getValue(); diff --git a/classes/ColdTrick/WidgetManager/Seeder.php b/classes/ColdTrick/WidgetManager/Seeder.php index 63b9cfe..b45e306 100644 --- a/classes/ColdTrick/WidgetManager/Seeder.php +++ b/classes/ColdTrick/WidgetManager/Seeder.php @@ -24,7 +24,7 @@ class Seeder extends Seed { ]; /** - * {@inheritDoc} + * {@inheritdoc} */ public function seed() { $this->advance($this->getCount()); @@ -65,7 +65,7 @@ public function seed() { } /** - * {@inheritDoc} + * {@inheritdoc} */ public function unseed() { /* @var $entities \ElggBatch */ @@ -96,21 +96,21 @@ public function unseed() { } /** - * {@inheritDoc} + * {@inheritdoc} */ public static function getType(): string { return \WidgetPage::SUBTYPE; } /** - * {@inheritDoc} + * {@inheritdoc} */ public static function getDefaultLimit(): int { return 5; } /** - * {@inheritDoc} + * {@inheritdoc} */ protected function getCountOptions(): array { return [ diff --git a/classes/ColdTrick/WidgetManager/Settings.php b/classes/ColdTrick/WidgetManager/Settings.php index 9a75a58..f1ea2a5 100644 --- a/classes/ColdTrick/WidgetManager/Settings.php +++ b/classes/ColdTrick/WidgetManager/Settings.php @@ -12,20 +12,20 @@ class Settings { * * @param \Elgg\Event $event 'setting', 'plugin' * - * @return void + * @return null|string */ - public static function implodeSettings(\Elgg\Event $event) { + public static function implodeSettings(\Elgg\Event $event): ?string { if ($event->getParam('plugin_id') !== 'widget_manager') { - return; + return null; } if ($event->getParam('name') !== 'index_managers') { - return; + return null; } $current_value = $event->getValue(); if (!is_array($current_value)) { - return; + return null; } return implode(',', $current_value); diff --git a/classes/ColdTrick/WidgetManager/Upgrades/CreateWidgetPages.php b/classes/ColdTrick/WidgetManager/Upgrades/CreateWidgetPages.php deleted file mode 100644 index a427bd9..0000000 --- a/classes/ColdTrick/WidgetManager/Upgrades/CreateWidgetPages.php +++ /dev/null @@ -1,104 +0,0 @@ -countItems()); - } - - /** - * {@inheritDoc} - */ - public function countItems(): int { - - $plugin = elgg_get_plugin_from_id('widget_manager'); - $contexts = elgg_string_to_array((string) $plugin->extra_contexts); - - return count($contexts); - } - - /** - * {@inheritDoc} - */ - public function needsIncrementOffset(): bool { - return false; - } - - /** - * {@inheritDoc} - */ - public function run(Result $result, $offset): Result { - - $plugin = elgg_get_plugin_from_id('widget_manager'); - $contexts = elgg_string_to_array((string) $plugin->extra_contexts); - - $contexts_config = json_decode($plugin->extra_contexts_config, true); - if (!is_array($contexts_config)) { - $contexts_config = []; - } - - foreach ($contexts as $context) { - $context_config = elgg_extract($context, $contexts_config, []); - - $widget_page = new \WidgetPage(); - $widget_page->url = $context; - $widget_page->layout = elgg_extract('layout', $context_config, '33|33|33'); - - $widget_page->save(); - - $managers = elgg_string_to_array((string) elgg_extract('manager', $context_config, '')); - foreach ($managers as $username) { - $user = elgg_get_user_by_username($username); - if ($user) { - $widget_page->addManager($user); - } - } - - $this->migrateWidgets($context, $widget_page); - } - - $plugin->unsetSetting('extra_contexts'); - $plugin->unsetSetting('extra_contexts_config'); - - $result->addSuccesses(count($contexts)); - } - - protected function migrateWidgets($old_context, \WidgetPage $new_page) { - $widgets = elgg_get_entities([ - 'type' => 'object', - 'subtype' => 'widget', - 'metadata_name' => 'context', - 'metadata_value' => $old_context, - 'batch' => true, - 'batch_inc_offset' => false, - 'limit' => false, - ]); - - foreach ($widgets as $widget) { - $widget->owner_guid = $new_page->guid; - $widget->container_guid = $new_page->guid; - - $widget->context = 'index'; - $widget->save(); - } - } -} diff --git a/classes/ColdTrick/WidgetManager/Upgrades/MigrateWidgetSettings.php b/classes/ColdTrick/WidgetManager/Upgrades/MigrateWidgetSettings.php deleted file mode 100644 index 796367f..0000000 --- a/classes/ColdTrick/WidgetManager/Upgrades/MigrateWidgetSettings.php +++ /dev/null @@ -1,118 +0,0 @@ -countItems()); - } - - /** - * @inheritDoc - */ - public function needsIncrementOffset(): bool { - return false; - } - - /** - * @inheritDoc - */ - public function countItems(): int { - global $jerome; - $jerome = true; - $r = elgg_get_metadata($this->getOptions(['count' => true])); - $jerome = false; - return $r; - } - - /** - * @inheritDoc - */ - public function run(Result $result, $offset): Result { - /* @var $batch \ElggBatch */ - $batch = elgg_get_metadata($this->getOptions([ - 'offset' => $offset, - ])); - /* @var $metadata \ElggMetadata */ - foreach ($batch as $metadata) { - $json = @json_decode($metadata->value, true); - if (!isset($json)) { - // not json, leave intact - $result->addFailures(); - continue; - } - - $json = array_filter($json); - if (empty($json)) { - // nothing left after clean, so remove - if ($metadata->delete()) { - $result->addSuccesses(); - } else { - $result->addFailures(); - } - continue; - } - - $json = array_values($json); - if (count($json) === 1) { - // only one value, so save as the new value - $metadata->value = $json[0]; - if ($metadata->save()) { - $result->addSuccesses(); - } else { - $result->addFailures(); - } - continue; - } - - // multiple value, so save the array with the original entity - /* @var $widget \ElggWidget */ - $widget = $metadata->getEntity(); - $metadata_name = $metadata->name; - $widget->{$metadata_name} = $json; - - $result->addSuccesses(); - } - - return $result; - } - - /** - * Get options for metadata query - * - * @param array $options additional options - * - * @return array - * @see elgg_get_metadata() - */ - protected function getOptions(array $options = []): array { - $defaults = [ - 'type' => 'object', - 'subtype' => 'widget', - 'limit' => 100, - 'batch' => true, - 'metadata_name_value_pairs' => [ - [ - 'value' => '["%"]', - 'operand' => 'LIKE', - 'case_sensitive' => false, - ], - ], - ]; - - return array_merge($defaults, $options); - } -} diff --git a/classes/ColdTrick/WidgetManager/Widgets.php b/classes/ColdTrick/WidgetManager/Widgets.php index eb96ed7..da83d04 100644 --- a/classes/ColdTrick/WidgetManager/Widgets.php +++ b/classes/ColdTrick/WidgetManager/Widgets.php @@ -5,7 +5,7 @@ use Elgg\WidgetDefinition; /** - * Wdigets + * Widgets */ class Widgets { @@ -16,7 +16,7 @@ class Widgets { * * @return void */ - public static function fixPrivateAccess(\Elgg\Event $event) { + public static function fixPrivateAccess(\Elgg\Event $event): void { $object = $event->getObject(); if (!$object instanceof \ElggWidget) { return; @@ -45,9 +45,9 @@ public static function fixPrivateAccess(\Elgg\Event $event) { * * @param \Elgg\Event $event 'handlers', 'widgets' * - * @return void + * @return array */ - public static function applyWidgetsConfig(\Elgg\Event $event) { + public static function applyWidgetsConfig(\Elgg\Event $event): array { $return_value = $event->getValue(); foreach ($return_value as $id => $widget_definition) { if (!isset($widget_definition->originals)) { @@ -103,9 +103,9 @@ public static function applyWidgetsConfig(\Elgg\Event $event) { * * @param \Elgg\Event $event 'handlers', 'widgets' * - * @return void + * @return array */ - public static function addManageWidgetsContext(\Elgg\Event $event) { + public static function addManageWidgetsContext(\Elgg\Event $event): array { $return_value = $event->getValue(); foreach ($return_value as $id => $widget_definition) { $widget_definition->context[] = 'manage_widgets'; @@ -120,21 +120,21 @@ public static function addManageWidgetsContext(\Elgg\Event $event) { * * @param \Elgg\Event $event 'view_vars', 'object/widget/body' * - * @return array + * @return null|array */ - public static function getContentFromCache(\Elgg\Event $event) { + public static function getContentFromCache(\Elgg\Event $event): ?array { $widget = elgg_extract('entity', $event->getValue()); if (!$widget instanceof \ElggWidget) { - return; + return null; } if (!self::isCacheableWidget($widget)) { - return; + return null; } $cached_data = elgg_load_system_cache("widget_cache_{$widget->guid}"); if (empty($cached_data)) { - return; + return null; } $result = $event->getValue(); @@ -148,20 +148,20 @@ public static function getContentFromCache(\Elgg\Event $event) { * * @param \Elgg\Event $event 'view_vars', 'object/widget/elements/controls' * - * @return array + * @return null|array */ - public static function preventControls(\Elgg\Event $event) { + public static function preventControls(\Elgg\Event $event): ?array { $widget = elgg_extract('widget', $event->getValue()); if (!$widget instanceof \ElggWidget) { - return; + return null; } if ($widget->widget_manager_hide_header !== 'yes') { - return; + return null; } if ($widget->canEdit()) { - return; + return null; } $result = $event->getValue(); @@ -175,9 +175,9 @@ public static function preventControls(\Elgg\Event $event) { * * @param \Elgg\Event $event 'view', 'object/widget/body' * - * @return array + * @return void */ - public static function saveContentInCache(\Elgg\Event $event) { + public static function saveContentInCache(\Elgg\Event $event): void { $widget = elgg_extract('entity', elgg_extract('vars', $event->getParams())); if (!$widget instanceof \ElggWidget) { return; @@ -195,9 +195,9 @@ public static function saveContentInCache(\Elgg\Event $event) { * * @param \Elgg\Event $event 'update:after', 'object' * - * @return bool + * @return void */ - public static function clearWidgetCacheOnUpdate(\Elgg\Event $event) { + public static function clearWidgetCacheOnUpdate(\Elgg\Event $event): void { $widget = $event->getObject(); if (!$widget instanceof \ElggWidget) { return; @@ -217,7 +217,7 @@ public static function clearWidgetCacheOnUpdate(\Elgg\Event $event) { * * @return bool */ - protected static function isCacheableWidget(\ElggWidget $widget) { + protected static function isCacheableWidget(\ElggWidget $widget): bool { static $cacheable_handlers; if (!isset($cacheable_handlers)) { $cacheable_handlers = elgg_trigger_event_results('cacheable_handlers', 'widget_manager', [], []); @@ -231,18 +231,16 @@ protected static function isCacheableWidget(\ElggWidget $widget) { * * @param \Elgg\Event $event 'entity:url', 'object' * - * @return string + * @return null|string */ - public static function getWidgetURL(\Elgg\Event $event) { + public static function getWidgetURL(\Elgg\Event $event): ?string { $widget = $event->getEntityParam(); if (!$widget instanceof \ElggWidget) { - return; + return null; } // custom urls always trump existing values - if ($widget->widget_manager_custom_url) { - return $widget->widget_manager_custom_url; - } + return $widget->widget_manager_custom_url ?: null; } /** @@ -250,18 +248,18 @@ public static function getWidgetURL(\Elgg\Event $event) { * * @param \Elgg\Event $event 'handlers', 'widgets' * - * @return void|\Elgg\WidgetDefinition[] + * @return null|\Elgg\WidgetDefinition[] */ - public static function addDiscussionsWidgetToGroup(\Elgg\Event $event) { + public static function addDiscussionsWidgetToGroup(\Elgg\Event $event): ?array { $context = $event->getParam('context'); if ($context !== 'groups') { - return; + return null; } $container = $event->getParam('container'); if (!$container instanceof \ElggGroup || !$container->isToolEnabled('forum')) { - return; + return null; } $return_value = $event->getValue(); @@ -279,18 +277,18 @@ public static function addDiscussionsWidgetToGroup(\Elgg\Event $event) { * * @param \Elgg\Event $event 'group_tool_widgets', 'widget_manager' * - * @return void|array + * @return null|array */ - public static function groupToolWidgets(\Elgg\Event $event) { + public static function groupToolWidgets(\Elgg\Event $event): ?array { $entity = $event->getEntityParam(); if (!$entity instanceof \ElggGroup) { - return; + return null; } $return_value = $event->getValue(); if (!is_array($return_value)) { - return; + return null; } // check different group tools for which we supply widgets @@ -308,12 +306,12 @@ public static function groupToolWidgets(\Elgg\Event $event) { * * @param \Elgg\Event $event 'permissions_check', 'widget_layout' * - * @return boolean + * @return null|bool */ - public static function layoutPermissionsCheck(\Elgg\Event $event) { + public static function layoutPermissionsCheck(\Elgg\Event $event): ?bool { $user = $event->getUserParam(); if (!$user instanceof \ElggUser || $event->getValue()) { - return; + return null; } // check if widgetpage manager can manage @@ -323,17 +321,15 @@ public static function layoutPermissionsCheck(\Elgg\Event $event) { return true; } - return; + return null; } // check if it is an index manager if ($event->getParam('context') !== 'index') { - return; + return null; } $index_managers = explode(',', elgg_get_plugin_setting('index_managers', 'widget_manager', '')); - if (in_array($user->guid, $index_managers)) { - return true; - } + return in_array($user->guid, $index_managers) ?: null; } } diff --git a/classes/WidgetManagerWidget.php b/classes/WidgetManagerWidget.php index fa8b347..71558c6 100644 --- a/classes/WidgetManagerWidget.php +++ b/classes/WidgetManagerWidget.php @@ -8,7 +8,7 @@ class WidgetManagerWidget extends \ElggWidget { /** - * {@inheritDoc} + * {@inheritdoc} */ public function getDisplayName(): string { return $this->widget_manager_custom_title ?: parent::getDisplayName(); diff --git a/classes/WidgetPage.php b/classes/WidgetPage.php index 4229833..b31c5bc 100644 --- a/classes/WidgetPage.php +++ b/classes/WidgetPage.php @@ -15,7 +15,7 @@ class WidgetPage extends ElggObject { const MANAGER_RELATIONSHIP = 'manager'; /** - * {@inheritDoc} + * {@inheritdoc} */ protected function initializeAttributes() { parent::initializeAttributes(); @@ -27,7 +27,7 @@ protected function initializeAttributes() { } /** - * {@inheritDoc} + * {@inheritdoc} */ public function canEdit(int $user_guid = 0): bool { $user_guid = $user_guid ?: elgg_get_logged_in_user_guid(); @@ -39,7 +39,7 @@ public function canEdit(int $user_guid = 0): bool { } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getURL(): string { return elgg_normalize_url((string) $this->url); @@ -121,7 +121,7 @@ public function getNumColumns(): int { } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getDisplayName(): string { if (empty($this->title)) { diff --git a/composer.json b/composer.json index 254b04c..7378489 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,6 @@ } }, "conflict": { - "elgg/elgg": "<5.1" + "elgg/elgg": "<6.0" } } diff --git a/composer.lock b/composer.lock index 6728c18..50d9b31 100644 --- a/composer.lock +++ b/composer.lock @@ -4,25 +4,25 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "3e12cf77c1667c97c4cdd690bbb50086", + "content-hash": "868cf70c025943127026cbd2d7228857", "packages": [ { "name": "mobiledetect/mobiledetectlib", - "version": "4.8.04", + "version": "4.8.06", "source": { "type": "git", "url": "https://github.com/serbanghita/Mobile-Detect.git", - "reference": "b73a5bdbcec74a6d33389cc8fe3a5af5d0965d01" + "reference": "af088b54cecc13b3264edca7da93a89ba7aa2d9e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/serbanghita/Mobile-Detect/zipball/b73a5bdbcec74a6d33389cc8fe3a5af5d0965d01", - "reference": "b73a5bdbcec74a6d33389cc8fe3a5af5d0965d01", + "url": "https://api.github.com/repos/serbanghita/Mobile-Detect/zipball/af088b54cecc13b3264edca7da93a89ba7aa2d9e", + "reference": "af088b54cecc13b3264edca7da93a89ba7aa2d9e", "shasum": "" }, "require": { "php": ">=8.0", - "psr/simple-cache": "^3.0" + "psr/simple-cache": "^2 || ^3" }, "require-dev": { "friendsofphp/php-cs-fixer": "^v3.35.1", @@ -60,7 +60,7 @@ ], "support": { "issues": "https://github.com/serbanghita/Mobile-Detect/issues", - "source": "https://github.com/serbanghita/Mobile-Detect/tree/4.8.04" + "source": "https://github.com/serbanghita/Mobile-Detect/tree/4.8.06" }, "funding": [ { @@ -68,7 +68,7 @@ "type": "github" } ], - "time": "2023-10-28T16:04:34+00:00" + "time": "2024-03-01T22:28:42+00:00" }, { "name": "npm-asset/muuri", @@ -142,5 +142,5 @@ "prefer-lowest": false, "platform": [], "platform-dev": [], - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.6.0" } diff --git a/elgg-plugin.php b/elgg-plugin.php index 85eefce..1a9b21e 100644 --- a/elgg-plugin.php +++ b/elgg-plugin.php @@ -54,10 +54,6 @@ ], ], ], - 'upgrades' => [ - CreateWidgetPages::class, - MigrateWidgetSettings::class, - ], 'actions' => [ 'widget_manager/lazy_load_widgets' => [ 'access' => 'public', @@ -79,22 +75,19 @@ ], 'views' => [ 'default' => [ - 'muuri.js' => $composer_path . 'vendor/npm-asset/muuri/dist/muuri.js', + 'muuri.mjs' => $composer_path . 'vendor/npm-asset/muuri/dist/muuri.js', ], ], 'view_extensions' => [ - 'css/admin' => [ - 'css/widget_manager/admin.css' => [], + 'admin.css' => [ + 'forms/widget_manager/manage_widgets/widget.css' => [], ], 'elements/widgets.css' => [ - 'css/widget_manager/site.css' => [], + 'widget_manager/site.css' => [], ], 'groups/edit/settings' => [ 'widget_manager/forms/groups_widget_access' => [], ], - 'elgg.js' => [ - 'js/widget_manager/site.js' => [], - ], 'object/widget/elements/content' => [ 'widget_manager/widgets/custom_more' => [], ], diff --git a/languages/en.php b/languages/en.php index d58ca09..d3722a4 100644 --- a/languages/en.php +++ b/languages/en.php @@ -112,9 +112,4 @@ 'widget_manager:layout:content:hide' => "Hide widget content", 'widget_manager:layout:content:show' => "Show widget content", - // upgrades - 'widget_manager:upgrade:2018052400:title' => "Widget contexts to pages", - 'widget_manager:upgrade:2018052400:description' => "Moves plugin settings for extra widget pages to actual entities", - 'widget_manager:upgrade:2023051101:title' => "Migrate widget settings", - 'widget_manager:upgrade:2023051101:description' => "Previously Widget Manager allows the saving of arrays as widget settings, this is now supported in Elgg core. This upgrade will migrate the old settings.", ]; diff --git a/languages/nl.php b/languages/nl.php index c31d449..9480947 100644 --- a/languages/nl.php +++ b/languages/nl.php @@ -5,8 +5,6 @@ */ return array ( - 'widget_manager:upgrade:2023051101:title' => 'Migreer widget instellingen', - 'widget_manager:upgrade:2023051101:description' => 'In vorige versies van WIdget Manager werd het mogelijk gemaakt om widget instellingen als arrays op te slaan, dit wordt nu ondersteund vanuit Elgg core. Deze migratie zal de oude instellingen overzetten.', 'widget_manager:settings:widget_layout:fluid' => 'Fluïde layout', 'widget_manager:settings:other' => 'Overig', 'widget_manager:settings:show_collapse_content' => 'Toon widget content verbergen optie bij bewerken layout', @@ -31,8 +29,6 @@ 'widget_manager:widget_page:title:help' => 'Het optioneel opslaan van een titel toont deze op de widget pagina.', 'widget_manager:widget_page:show_description' => "Toon omschrijving op widget pagina", 'widget_manager:widget_page:show_description:help' => "Toont de omschrijving op de widget indien geactiveerd. Als het niet is geactiveerd zie je de omschrijving mogelijk enkel in lijsten en zoekresultaten.", - 'widget_manager:upgrade:2018052400:title' => 'Widget contexten naar widget pagina\'s', - 'widget_manager:upgrade:2018052400:description' => 'Maakt van de plugininstellingen voor widget pagina\'s werkelijke widget pagina entiteiten', 'widget_manager:forms:manage_widgets:context' => 'Beschikbaar in de context', 'widget_manager:forms:manage_widgets:can_add' => 'Kan worden toegevoegd', 'widget_manager:forms:manage_widgets:multiple' => 'Meerdere widgets toegestaan', diff --git a/views/default/admin/widgets/manage.mjs b/views/default/admin/widgets/manage.mjs new file mode 100644 index 0000000..f35f091 --- /dev/null +++ b/views/default/admin/widgets/manage.mjs @@ -0,0 +1,24 @@ +import 'jquery'; +import i18n from 'elgg/i18n'; +import Ajax from 'elgg/Ajax'; + +$(document).on('click', '.widget-manager-unsupported-context .elgg-input-checkbox', function (e, elem) { + if (!$(this).is(':checked')) { + return; + } + + if (!confirm(i18n.echo('widget_manager:forms:manage_widgets:unsupported_context:confirm'))) { + return false; + } +}); + +$(document).on('change', '.elgg-input-checkbox[name^="widgets_config"]', function (e, elem) { + var ajax = new Ajax(); + + ajax.action('widget_manager/manage_widgets', { + data: { + [$(this).attr('name')]: $(this).is(':checked') ? 1 : 0 + }, + showSuccessMessages: false + }); +}); diff --git a/views/default/admin/widgets/manage.php b/views/default/admin/widgets/manage.php index 5d3bde8..c986d5b 100644 --- a/views/default/admin/widgets/manage.php +++ b/views/default/admin/widgets/manage.php @@ -25,7 +25,7 @@ return; } -elgg_require_js('widget_manager/manage_widgets'); +elgg_import_esm('admin/widgets/manage'); $widget_output = []; diff --git a/views/default/elgg/widgets.js b/views/default/elgg/widgets.js deleted file mode 100644 index fa371a8..0000000 --- a/views/default/elgg/widgets.js +++ /dev/null @@ -1,136 +0,0 @@ -define(['jquery', 'elgg/Ajax', 'elgg/lightbox', 'jquery-ui/widgets/sortable'], function ($, Ajax, lightbox) { - - /** - * Persist the widget's new position - * - * @param {Object} event - * @param {Object} ui - * - * @return void - */ - function moveWidget(event, ui) { - // elgg-widget- - var guidString = ui.item.attr('id'); - guidString = guidString.substring(guidString.indexOf('elgg-widget-') + "elgg-widget-".length); - - var ajax = new Ajax(false); - ajax.action('widgets/move', { - data: { - widget_guid: guidString, - column: ui.item.parent().data('widgetColumn'), - position: ui.item.index() - } - }); - }; - - /** - * Removes a widget from the layout - * - * Event callback the uses Ajax to delete the widget and removes its HTML - * - * @param {Object} event - * @return void - */ - function removeWidget(event) { - event.preventDefault(); - - var $layout = $(this).closest('.elgg-layout-widgets'); - var $widget = $(this).closest('.elgg-module-widget'); - $widget.remove(); - - // delete the widget through ajax - var ajax = new Ajax(false); - ajax.action($(this).attr('href')); - - $layout.trigger({ - type: 'widgetRemove', - layout: $layout, - widget: $widget - }); - }; - - /** - * Save a widget's settings - * - * Uses Ajax to save the settings and updates the HTML. - * - * @param {Object} event - * @return void - */ - function saveWidgetSettings(event) { - event.preventDefault(); - - var guid = $(this).find('[name="guid"]').val(); - var $widget = $('#elgg-widget-' + guid); - var $widgetContent = $widget.find('.elgg-widget-content'); - - // stick the ajax loader in there - $widgetContent.html('
'); - - var ajax = new Ajax(false); - ajax.action('widgets/save', { - data: ajax.objectify(this), - success: function (result) { - lightbox.close(); - - $widgetContent.html(result.content); - - if (result.title !== '') { - var $widgetTitle = $widget.find('.elgg-widget-title'); - - var newWidgetTitle = result.title; - if (result.href !== '') { - newWidgetTitle = "" + newWidgetTitle + ""; - } - - $widgetTitle.html(newWidgetTitle); - } - - $widget.trigger({ - type: 'saveSettings', - widget: $widget - }); - } - }); - }; - - $('.elgg-layout-widgets:not(.widgets-fluid-columns)').find('.elgg-widgets').each(function() { - - var opts = $(this).data().sortableOptions; - var defaults = { - items: 'div.elgg-module-widget.elgg-state-draggable', - connectWith: '.elgg-widgets', - handle: '.elgg-widget-handle', - forcePlaceholderSize: true, - placeholder: 'elgg-widget-placeholder', - opacity: 0.8, - revert: 500, - stop: moveWidget - }; - var settings = $.extend({}, defaults, opts); - - $(this).sortable(settings); - }); - - // regular layouts - $(document).on('click', '.elgg-menu-title-widgets .elgg-menu-item-hide-widget-contents a, .elgg-menu-title-widgets .elgg-menu-item-show-widget-contents a', function() { - var $layout = $(this).closest('.elgg-layout-widgets'); - $layout.find('.elgg-menu-item-hide-widget-contents, .elgg-menu-item-show-widget-contents').toggleClass('hidden'); - - $layout.toggleClass('elgg-widgets-hide-content'); - - return false; - }); - - // widget page layouts with a title menu - $(document).on('click', '.elgg-menu-title .elgg-menu-item-hide-widget-contents a, .elgg-menu-title .elgg-menu-item-show-widget-contents a', function() { - $('.elgg-menu-title').find('.elgg-menu-item-hide-widget-contents, .elgg-menu-item-show-widget-contents').toggleClass('hidden'); - - $('.elgg-layout-widgets').toggleClass('elgg-widgets-hide-content'); - - return false; - }); - - $(document).on('click', 'a.elgg-widget-delete-button', removeWidget); - $(document).on('submit', '.elgg-form-widgets-save', saveWidgetSettings); -}); diff --git a/views/default/elgg/widgets.mjs b/views/default/elgg/widgets.mjs new file mode 100644 index 0000000..1050868 --- /dev/null +++ b/views/default/elgg/widgets.mjs @@ -0,0 +1,134 @@ +import 'jquery'; +import 'jquery-ui'; +import Ajax from 'elgg/Ajax'; +import lightbox from 'elgg/lightbox'; + +/** + * Persist the widget's new position + * + * @param {Object} event + * @param {Object} ui + * + * @return void + */ +function moveWidget(event, ui) { + // elgg-widget- + var guidString = ui.item.attr('id'); + guidString = guidString.substring(guidString.indexOf('elgg-widget-') + "elgg-widget-".length); + + var ajax = new Ajax(false); + ajax.action('widgets/move', { + data: { + widget_guid: guidString, + column: ui.item.parent().data('widgetColumn'), + position: ui.item.index() + } + }); +}; + +/** + * Removes a widget from the layout + * + * Event callback the uses Ajax to delete the widget and removes its HTML + * + * @param {Object} event + * @return void + */ +function removeWidget(event) { + event.preventDefault(); + + var $layout = $(this).closest('.elgg-layout-widgets'); + var $widget = $(this).closest('.elgg-module-widget'); + $widget.remove(); + + // delete the widget through ajax + var ajax = new Ajax(false); + ajax.action($(this).attr('href')); + + $layout.trigger({ + type: 'widgetRemove', + layout: $layout, + widget: $widget + }); +}; + +/** + * Save a widget's settings + * + * Uses Ajax to save the settings and updates the HTML. + * + * @param {Object} event + * @return void + */ +function saveWidgetSettings(event) { + event.preventDefault(); + + var guid = $(this).find('[name="guid"]').val(); + var $widget = $('#elgg-widget-' + guid); + var $widgetContent = $widget.find('.elgg-widget-content'); + + var ajax = new Ajax(); + ajax.action('widgets/save', { + data: ajax.objectify(this), + success: function (result) { + lightbox.close(); + + $widgetContent.html(result.content); + if (result.title !== '') { + var $widgetTitle = $widget.find('.elgg-widget-title'); + + var newWidgetTitle = result.title; + if (result.href !== '') { + newWidgetTitle = "" + newWidgetTitle + ""; + } + + $widgetTitle.html(newWidgetTitle); + } + + $widget.trigger({ + type: 'saveSettings', + widget: $widget + }); + } + }); +}; + +$('.elgg-layout-widgets:not(.widgets-fluid-columns)').find('.elgg-widgets').each(function() { + + var opts = $(this).data().sortableOptions; + var defaults = { + items: '.elgg-module-widget.elgg-state-draggable', + connectWith: '.elgg-widgets', + handle: '.elgg-widget-handle', + forcePlaceholderSize: true, + placeholder: 'elgg-widget-placeholder', + opacity: 0.8, + revert: 500, + stop: moveWidget + }; + var settings = $.extend({}, defaults, opts); + + $(this).sortable(settings); +}); + +// regular layouts +$(document).on('click', '.elgg-menu-title-widgets .elgg-menu-item-hide-widget-contents a, .elgg-menu-title-widgets .elgg-menu-item-show-widget-contents a', function() { + var $layout = $(this).closest('.elgg-layout-widgets'); + $layout.find('.elgg-menu-item-hide-widget-contents, .elgg-menu-item-show-widget-contents').toggleClass('hidden'); + + $layout.toggleClass('elgg-widgets-hide-content'); + + return false; +}); + +// widget page layouts with a title menu +$(document).on('click', '.elgg-menu-title .elgg-menu-item-hide-widget-contents a, .elgg-menu-title .elgg-menu-item-show-widget-contents a', function() { + $('.elgg-menu-title').find('.elgg-menu-item-hide-widget-contents, .elgg-menu-item-show-widget-contents').toggleClass('hidden'); + + $('.elgg-layout-widgets').toggleClass('elgg-widgets-hide-content'); + + return false; +}); + +$(document).on('click', 'a.elgg-widget-delete-button', removeWidget); +$(document).on('submit', '.elgg-form-widgets-save', saveWidgetSettings); diff --git a/views/default/css/widget_manager/admin.css b/views/default/forms/widget_manager/manage_widgets/widget.css similarity index 100% rename from views/default/css/widget_manager/admin.css rename to views/default/forms/widget_manager/manage_widgets/widget.css diff --git a/views/default/forms/widget_manager/widget_page.php b/views/default/forms/widget_manager/widget_page.php index 119f701..0a48c0a 100644 --- a/views/default/forms/widget_manager/widget_page.php +++ b/views/default/forms/widget_manager/widget_page.php @@ -38,7 +38,7 @@ '#label' => elgg_echo('widget_manager:widget_page:show_description'), '#help' => elgg_echo('widget_manager:widget_page:show_description:help'), 'name' => 'show_description', - 'checked' => $entity->show_description !== false, + 'checked' => $entity?->show_description !== false, 'switch' => true, 'default' => false, 'value' => true, diff --git a/views/default/forms/widgets/advanced.js b/views/default/forms/widgets/advanced.js deleted file mode 100644 index 2d08161..0000000 --- a/views/default/forms/widgets/advanced.js +++ /dev/null @@ -1,5 +0,0 @@ -define(['jquery'], function ($) { - $(document).on('open', '.elgg-tabs-component.widget-settings .elgg-tabs > li', function() { - $(window).trigger('resize.lightbox'); - }) -}); diff --git a/views/default/forms/widgets/advanced.mjs b/views/default/forms/widgets/advanced.mjs new file mode 100644 index 0000000..c38cce9 --- /dev/null +++ b/views/default/forms/widgets/advanced.mjs @@ -0,0 +1,5 @@ +import 'jquery'; + +$(document).on('open', '.elgg-tabs-component.widget-settings .elgg-tabs > li', function() { + $(window).trigger('resize.lightbox'); +}); diff --git a/views/default/forms/widgets/advanced.php b/views/default/forms/widgets/advanced.php index 2743f37..485dd6f 100644 --- a/views/default/forms/widgets/advanced.php +++ b/views/default/forms/widgets/advanced.php @@ -72,4 +72,7 @@ } } -echo elgg_view('input/fieldset', ['fields' => $fields]); +echo elgg_view_field([ + '#type' => 'fieldset', + 'fields' => $fields, +]); diff --git a/views/default/forms/widgets/save.php b/views/default/forms/widgets/save.php index ed71519..1b460f2 100644 --- a/views/default/forms/widgets/save.php +++ b/views/default/forms/widgets/save.php @@ -7,12 +7,7 @@ * @uses $vars['show_access'] (bool) should widget access setting be available default: true */ -$widget = elgg_extract('widget', $vars); -if ($widget !== null) { - elgg_deprecated_notice('Passing the widget entity in $vars["widget"] is deprecated. Update your code to provide it in $vars["entity"].', '5.1'); -} - -$widget = elgg_extract('entity', $vars, $widget); +$widget = elgg_extract('entity', $vars); if (!$widget instanceof \ElggWidget) { return; } @@ -60,7 +55,7 @@ ], ]); - elgg_require_js('forms/widgets/advanced'); + elgg_import_esm('forms/widgets/advanced'); } echo elgg_view_field([ diff --git a/views/default/js/widget_manager/fluid.js b/views/default/js/widget_manager/fluid.js deleted file mode 100644 index e5febbf..0000000 --- a/views/default/js/widget_manager/fluid.js +++ /dev/null @@ -1,120 +0,0 @@ -define(['jquery', 'elgg/Ajax', 'muuri', 'elgg/widgets'], function ($, Ajax, Muuri) { - - var grid; - var grid_selector = '.widgets-fluid-columns .elgg-widget-col-1'; - var grid_options = { - items: '.elgg-module-widget', - dragEnabled: false, - dragPlaceholder: { - enabled: true, - }, - itemPlaceholderClass: 'fluid-placeholder', - layoutDuration: 0, - showDuration: 0, - layoutOnInit: false, - dragHandle: '.elgg-widget-handle', - dragStartPredicate: { - distance: 10, - delay: 10 - }, - }; - - if ($('.widgets-fluid-columns.elgg-layout-can-edit').length) { - grid_options.dragEnabled = true; - } - - var ajax = new Ajax(); - - // Returns a function, that, as long as it continues to be invoked, will not - // be triggered. The function will be called after it stops being called for - // N milliseconds. If `immediate` is passed, trigger the function on the - // leading edge, instead of the trailing. - var debounce = function(func, wait, immediate) { - var timeout; - return function() { - var context = this, args = arguments; - var later = function() { - timeout = null; - if (!immediate) func.apply(context, args); - }; - var callNow = immediate && !timeout; - clearTimeout(timeout); - timeout = setTimeout(later, wait); - if (callNow) func.apply(context, args); - }; - }; - - function setItemSizes($elem) { - var container_width = $elem.width(); - - var $widgets = $elem.find('.elgg-module-widget'); - if (container_width > 1200) { - $widgets.css('width', 'calc(33% - 28px)'); - } else if (container_width > 800) { - $widgets.css('width', 'calc(50% - 28px)'); - } else { - $widgets.css('width', 'calc(100% - 28px)'); - } - }; - - function gridcheck() { - if (grid) { - setTimeout(function() { - $(grid_selector).each(function() { - setItemSizes($(this)); - grid.refreshItems().layout(); - }); - }, 200); - } - }; - - function initGrid() { - setTimeout(function() { - // added bit of delay to allow images to load (also required for correct working of delete event) - if (grid) { - grid.destroy(); - } - - setItemSizes($(grid_selector)); - - grid = new Muuri(grid_selector, grid_options); - - grid.on('dragEnd', function (item, event) { - if (event.distance === 0) { - return; - } - - // update dom with new positions - grid.synchronize(); - - var guids = []; - $.each(grid.getItems(), function(index, item) { - var guidString = $(item._element).attr('id'); - guidString = guidString.substr(guidString.indexOf('elgg-widget-') + 'elgg-widget-'.length); - - guids.push(guidString); - }); - - ajax.action('widget_manager/fluid_order', { - data: { - guids: guids - } - }); - }).on('layoutEnd', function() { - $(grid_selector).css('visibility', 'visible'); - }).layout(); - }, 200); - }; - - initGrid(); - - $(window).resize(debounce(gridcheck, 50)); - - $(document).on('saveSettings', '.elgg-layout-widgets .elgg-module-widget', gridcheck); - $(document).on('lazyLoaded', '.elgg-layout-widgets', gridcheck); - - $(document).on('widgetAdd widgetRemove', '.elgg-layout-widgets', initGrid); - - // mmenu support - $(document).on('mmenu.toggle', gridcheck); -}); diff --git a/views/default/js/widget_manager/manage_widgets.js b/views/default/js/widget_manager/manage_widgets.js deleted file mode 100644 index c1828aa..0000000 --- a/views/default/js/widget_manager/manage_widgets.js +++ /dev/null @@ -1,23 +0,0 @@ -define(['jquery', 'elgg/i18n', 'elgg/Ajax'], function ($, i18n, Ajax) { - - $(document).on('click', '.widget-manager-unsupported-context .elgg-input-checkbox', function (e, elem) { - if (!$(this).is(':checked')) { - return; - } - - if (!confirm(i18n.echo('widget_manager:forms:manage_widgets:unsupported_context:confirm'))) { - return false; - } - }); - - $(document).on('change', '.elgg-input-checkbox[name^="widgets_config"]', function (e, elem) { - var ajax = new Ajax(); - - ajax.action('widget_manager/manage_widgets', { - data: { - [$(this).attr('name')]: $(this).is(':checked') ? 1 : 0 - }, - showSuccessMessages: false - }); - }); -}); diff --git a/views/default/js/widget_manager/site.js b/views/default/js/widget_manager/site.js deleted file mode 100644 index 1427395..0000000 --- a/views/default/js/widget_manager/site.js +++ /dev/null @@ -1,36 +0,0 @@ -require(['jquery', 'elgg', 'elgg/Ajax', 'elgg/widgets'], function($, elgg, Ajax) { - if ($('.elgg-module-widget.lazy-loading').length) { - $('.elgg-layout-widgets').each(function(i, layout) { - var $lazy_widgets = $(layout).find('.elgg-module-widget.lazy-loading'); - if (!$lazy_widgets.length) { - return; - } - - var guids = []; - - $lazy_widgets.each(function(index, item) { - var guidString = $(item).attr('id'); - guids.push(guidString.substr(guidString.indexOf('elgg-widget-') + 'elgg-widget-'.length)); - }); - - var ajax = new Ajax(false); - ajax.action('widget_manager/lazy_load_widgets', { - data: { - guids: guids, - page_owner_guid: $(layout).data().pageOwnerGuid, - context_stack: $(layout).data().contextStack, - }, - success: function (result) { - $.each(result, function(guid, body) { - $('#elgg-widget-' + guid + ' > .elgg-body').html(body); - }); - - $(layout).trigger({ - type: 'lazyLoaded', - layout: $(layout) - }); - } - }); - }); - } -}); diff --git a/views/default/object/widget.php b/views/default/object/widget.php index 3c62d08..065cbc7 100644 --- a/views/default/object/widget.php +++ b/views/default/object/widget.php @@ -53,6 +53,7 @@ } if ($widget instanceof WidgetManagerWidget && WidgetsSettingsConfig::instance()->showLazyLoaded($widget, (array) elgg_extract('layout_info', $vars, []))) { + elgg_import_esm('widget_manager/lazy_loading'); $body = elgg_view('graphics/ajax_loader', ['hidden' => false]); $widget_class[] = 'lazy-loading'; } else { diff --git a/views/default/page/layouts/widgets.php b/views/default/page/layouts/widgets.php index f3e58e0..9eab2e6 100644 --- a/views/default/page/layouts/widgets.php +++ b/views/default/page/layouts/widgets.php @@ -31,7 +31,7 @@ return; } -elgg_require_js('elgg/widgets'); +elgg_import_esm('elgg/widgets'); // Underlying views and functions assume that the page owner is the owner of the widgets if (empty($page_owner) || $owner->guid !== $page_owner->guid) { diff --git a/views/default/plugins/widget_manager/settings.php b/views/default/plugins/widget_manager/settings.php index 7fcfb80..0efe771 100644 --- a/views/default/plugins/widget_manager/settings.php +++ b/views/default/plugins/widget_manager/settings.php @@ -2,9 +2,7 @@ $plugin = elgg_extract('entity', $vars); -$index_settings = ''; - -$index_settings .= elgg_view_field([ +$index_settings = elgg_view_field([ '#type' => 'select', '#label' => elgg_echo('widget_manager:settings:custom_index'), 'name' => 'params[custom_index]', @@ -47,8 +45,7 @@ echo elgg_view_module('info', elgg_echo('widget_manager:settings:index'), $index_settings); if (elgg_is_active_plugin('groups')) { - $group_settings = ''; - $group_settings .= elgg_view_field([ + $group_settings = elgg_view_field([ '#type' => 'select', '#label' => elgg_echo('widget_manager:settings:group:enable'), '#help' => elgg_view('output/url', [ @@ -64,6 +61,7 @@ 'forced' => elgg_echo('widget_manager:settings:group:enable:forced'), ], ]); + $group_settings .= elgg_view_field([ '#type' => 'checkbox', '#label' => elgg_echo('widget_manager:settings:group:option_default_enabled'), @@ -73,6 +71,7 @@ 'default' => 'no', 'value' => 'yes', ]); + $group_settings .= elgg_view_field([ '#type' => 'checkbox', '#label' => elgg_echo('widget_manager:settings:group:option_admin_only'), @@ -82,6 +81,7 @@ 'default' => 'no', 'value' => 'yes', ]); + $group_settings .= elgg_view_field([ '#type' => 'number', '#label' => elgg_echo('widget_manager:settings:group:group_column_count'), diff --git a/views/default/resources/widget_manager/custom_index.php b/views/default/resources/widget_manager/custom_index.php index 3d55f80..3fd59f4 100644 --- a/views/default/resources/widget_manager/custom_index.php +++ b/views/default/resources/widget_manager/custom_index.php @@ -20,7 +20,7 @@ $classes[] = 'widgets-fluid-columns'; $num_columns = 1; - elgg_require_js('widget_manager/fluid'); + elgg_import_esm('widget_manager/fluid'); break; case '33|33|33': $classes[] = 'widgets-3-columns'; diff --git a/views/default/resources/widgets/add_panel.js b/views/default/resources/widgets/add_panel.js deleted file mode 100644 index 999381f..0000000 --- a/views/default/resources/widgets/add_panel.js +++ /dev/null @@ -1,69 +0,0 @@ -define(['jquery', 'elgg', 'elgg/Ajax'], function($, elgg, Ajax) { - - /** - * Adds a new widget - * - * Makes Ajax call to add new widget and inserts the widget html - * - * @param {Object} event - * @return void - */ - function addWidget(event) { - event.preventDefault(); - - var $item = $(this).closest('li'); - - // if multiple instances not allow, disable this widget type add button - if (!$item.is('.elgg-widget-multiple')) { - $item.toggleClass('elgg-state-unavailable elgg-state-available'); - } - - var href = $(this).attr('href'); - var ajax = new Ajax(); - - ajax.path(href).done(function(output) { - var query_parts = elgg.parse_url(href, 'query', true); - var selector = ''; - var context = query_parts['context']; - var page_owner_guid = query_parts['page_owner_guid']; - var new_widget_column = query_parts['new_widget_column'] || 1; - var new_widget_position = query_parts['new_widget_position'] || 'top'; - - if (context && page_owner_guid) { - // target the correct widget layout - selector = '.elgg-layout-widgets-' + context + '[data-page-owner-guid="' + page_owner_guid + '"] .elgg-widget-col-' + new_widget_column; - } else { - selector = '.elgg-widget-col-' + new_widget_column; - } - - if (new_widget_position === 'top') { - $(selector).prepend(output); - } else { - $(selector).append(output); - } - - var $layout = $(selector).closest('.elgg-layout-widgets'); - $layout.trigger({ - type: 'widgetAdd', - layout: $layout - }); - }); - }; - - $(document).on('click', '.elgg-widgets-add-panel .elgg-widgets-add-actions .elgg-button-submit', addWidget); - - $(document).on('keyup', '.elgg-widgets-add-panel input[name="widget_search"]', function() { - var $container = $('.elgg-widgets-add-panel'); - var $items = $container.find('> .elgg-body > ul > li'); - var q = $(this).val(); - - if (q === '') { - $items.show(); - } else { - $items.hide(); - $items.filter(function () { - return $(this).text().toUpperCase().indexOf(q.toUpperCase()) >= 0; - }).show(); - } - }); -}); diff --git a/views/default/resources/widgets/add_panel.mjs b/views/default/resources/widgets/add_panel.mjs new file mode 100644 index 0000000..b8efd5e --- /dev/null +++ b/views/default/resources/widgets/add_panel.mjs @@ -0,0 +1,70 @@ +import 'jquery'; +import elgg from 'elgg'; +import Ajax from 'elgg/Ajax'; + +/** + * Adds a new widget + * + * Makes Ajax call to add new widget and inserts the widget html + * + * @param {Object} event + * @return void + */ +function addWidget(event) { + event.preventDefault(); + + var $item = $(this).closest('li'); + + // if multiple instances not allow, disable this widget type add button + if (!$item.is('.elgg-widget-multiple')) { + $item.toggleClass('elgg-state-unavailable elgg-state-available'); + } + + var href = $(this).attr('href'); + var ajax = new Ajax(); + + ajax.path(href).done(function(output) { + var query_parts = elgg.parse_url(href, 'query', true); + var selector = ''; + var context = query_parts['context']; + var page_owner_guid = query_parts['page_owner_guid']; + var new_widget_column = query_parts['new_widget_column'] || 1; + var new_widget_position = query_parts['new_widget_position'] || 'top'; + + if (context && page_owner_guid) { + // target the correct widget layout + selector = '.elgg-layout-widgets-' + context + '[data-page-owner-guid="' + page_owner_guid + '"] .elgg-widget-col-' + new_widget_column; + } else { + selector = '.elgg-widget-col-' + new_widget_column; + } + + if (new_widget_position === 'top') { + $(selector).prepend(output); + } else { + $(selector).append(output); + } + + var $layout = $(selector).closest('.elgg-layout-widgets'); + $layout.trigger({ + type: 'widgetAdd', + layout: $layout + }); + }); +} + +$(document).on('click', '.elgg-widgets-add-panel .elgg-widgets-add-actions .elgg-button-submit', addWidget); + +$(document).on('keyup', '.elgg-widgets-add-panel input[name="widget_search"]', function() { + var $container = $('.elgg-widgets-add-panel'); + var $items = $container.find('> .elgg-body > ul > li'); + var q = $(this).val(); + + if (q === '') { + $items.show(); + } else { + $items.hide(); + $items.filter(function () { + return $(this).text().toUpperCase().indexOf(q.toUpperCase()) >= 0; + }).show(); + } +}); diff --git a/views/default/resources/widgets/add_panel.php b/views/default/resources/widgets/add_panel.php index 5c9ae3b..8c20402 100644 --- a/views/default/resources/widgets/add_panel.php +++ b/views/default/resources/widgets/add_panel.php @@ -18,7 +18,7 @@ elgg_set_context_stack($context_stack); } -elgg_require_js('resources/widgets/add_panel'); +elgg_import_esm('resources/widgets/add_panel'); $context = (string) elgg_extract('context', $vars, get_input('context')); $owner_guid = (int) elgg_extract('owner_guid', $vars, (int) get_input('owner_guid')); @@ -93,7 +93,7 @@ ]); $action .= ''; - $description = elgg_format_element('h4', [], $widget_type->name); + $description = elgg_format_element('div', ['class' => 'elgg-loud'], $widget_type->name); if ($widget_type->description) { $description .= elgg_format_element('div', ['class' => 'elgg-quiet'], $widget_type->description); diff --git a/views/default/widget_manager/fluid.mjs b/views/default/widget_manager/fluid.mjs new file mode 100644 index 0000000..a5bc72e --- /dev/null +++ b/views/default/widget_manager/fluid.mjs @@ -0,0 +1,122 @@ +import 'jquery'; +import 'elgg/widgets'; +import 'muuri'; +import Ajax from 'elgg/Ajax'; + +var grid; +var grid_selector = '.widgets-fluid-columns .elgg-widget-col-1'; +var grid_options = { + items: '.elgg-module-widget', + dragEnabled: false, + dragPlaceholder: { + enabled: true, + }, + itemPlaceholderClass: 'fluid-placeholder', + layoutDuration: 0, + showDuration: 0, + layoutOnInit: false, + dragHandle: '.elgg-widget-handle', + dragStartPredicate: { + distance: 10, + delay: 10 + }, +}; + +if ($('.widgets-fluid-columns.elgg-layout-can-edit').length) { + grid_options.dragEnabled = true; +} + +var ajax = new Ajax(); + +// Returns a function, that, as long as it continues to be invoked, will not +// be triggered. The function will be called after it stops being called for +// N milliseconds. If `immediate` is passed, trigger the function on the +// leading edge, instead of the trailing. +var debounce = function(func, wait, immediate) { + var timeout; + return function() { + var context = this, args = arguments; + var later = function() { + timeout = null; + if (!immediate) func.apply(context, args); + }; + var callNow = immediate && !timeout; + clearTimeout(timeout); + timeout = setTimeout(later, wait); + if (callNow) func.apply(context, args); + }; +} + +function setItemSizes($elem) { + var container_width = $elem.width(); + + var $widgets = $elem.find('.elgg-module-widget'); + if (container_width > 1200) { + $widgets.css('width', 'calc(33% - 28px)'); + } else if (container_width > 800) { + $widgets.css('width', 'calc(50% - 28px)'); + } else { + $widgets.css('width', 'calc(100% - 28px)'); + } +} + +function gridcheck() { + if (grid) { + setTimeout(function() { + $(grid_selector).each(function() { + setItemSizes($(this)); + grid.refreshItems().layout(); + }); + }, 200); + } +} + +function initGrid() { + setTimeout(function() { + // added bit of delay to allow images to load (also required for correct working of delete event) + if (grid) { + grid.destroy(); + } + + setItemSizes($(grid_selector)); + + grid = new Muuri(grid_selector, grid_options); + + grid.on('dragEnd', function (item, event) { + if (event.distance === 0) { + return; + } + + // update dom with new positions + grid.synchronize(); + + var guids = []; + $.each(grid.getItems(), function(index, item) { + var guidString = $(item._element).attr('id'); + guidString = guidString.substr(guidString.indexOf('elgg-widget-') + 'elgg-widget-'.length); + + guids.push(guidString); + }); + + ajax.action('widget_manager/fluid_order', { + data: { + guids: guids + } + }); + }).on('layoutEnd', function() { + $(grid_selector).css('visibility', 'visible'); + }).layout(); + }, 200); +} + +initGrid(); + +$(window).resize(debounce(gridcheck, 50)); + +$(document).on('saveSettings', '.elgg-layout-widgets .elgg-module-widget', gridcheck); +$(document).on('lazyLoaded', '.elgg-layout-widgets', gridcheck); + +$(document).on('widgetAdd widgetRemove', '.elgg-layout-widgets', initGrid); + +// mmenu support +$(document).on('mmenu.toggle', gridcheck); diff --git a/views/default/widget_manager/lazy_loading.mjs b/views/default/widget_manager/lazy_loading.mjs new file mode 100644 index 0000000..d1fbf4e --- /dev/null +++ b/views/default/widget_manager/lazy_loading.mjs @@ -0,0 +1,39 @@ +import 'jquery'; +import 'elgg'; +import 'elgg/widgets'; +import Ajax from 'elgg/Ajax'; + +if ($('.elgg-module-widget.lazy-loading').length) { + $('.elgg-layout-widgets').each(function(i, layout) { + var $lazy_widgets = $(layout).find('.elgg-module-widget.lazy-loading'); + if (!$lazy_widgets.length) { + return; + } + + var guids = []; + + $lazy_widgets.each(function(index, item) { + var guidString = $(item).attr('id'); + guids.push(guidString.substr(guidString.indexOf('elgg-widget-') + 'elgg-widget-'.length)); + }); + + var ajax = new Ajax(false); + ajax.action('widget_manager/lazy_load_widgets', { + data: { + guids: guids, + page_owner_guid: $(layout).data().pageOwnerGuid, + context_stack: $(layout).data().contextStack, + }, + success: function (result) { + $.each(result, function(guid, body) { + $('#elgg-widget-' + guid + ' > .elgg-body').html(body); + }); + + $(layout).trigger({ + type: 'lazyLoaded', + layout: $(layout) + }); + } + }); + }); +} diff --git a/views/default/css/widget_manager/site.css b/views/default/widget_manager/site.css similarity index 100% rename from views/default/css/widget_manager/site.css rename to views/default/widget_manager/site.css