From a13d29d1d55274a38e0fbe8360bb4ac8483b0e9b Mon Sep 17 00:00:00 2001 From: Steve Clay Date: Wed, 24 Dec 2014 17:01:55 -0500 Subject: [PATCH] feature(plugins): adds several reported content features Ajaxifies the reporting form (form page still BC) and delete/archive actions. Sorts un-archived reports at the top of results. More compact report display, and clicking reported link opens page in an iframe in colorbox. Fixes #5379, #6082, #5380 --- .../actions/reportedcontent/add.php | 49 ++++++------ .../actions/reportedcontent/archive.php | 27 ++++--- .../actions/reportedcontent/delete.php | 30 ++++---- mod/reportedcontent/start.php | 13 ++-- .../administer_utilities/reportedcontent.php | 10 ++- .../default/forms/reportedcontent/add.php | 23 ++++-- .../views/default/js/reportedcontent.js | 50 +++++++++++++ .../views/default/object/reported_content.php | 74 +++++++++---------- .../resources/reportedcontent/add_form.php | 3 + .../widgets/reportedcontent/content.php | 9 ++- 10 files changed, 183 insertions(+), 105 deletions(-) create mode 100644 mod/reportedcontent/views/default/js/reportedcontent.js create mode 100644 mod/reportedcontent/views/default/resources/reportedcontent/add_form.php diff --git a/mod/reportedcontent/actions/reportedcontent/add.php b/mod/reportedcontent/actions/reportedcontent/add.php index f0a1b05c8ed..c06c521229e 100644 --- a/mod/reportedcontent/actions/reportedcontent/add.php +++ b/mod/reportedcontent/actions/reportedcontent/add.php @@ -9,31 +9,32 @@ $address = get_input('address'); $access = ACCESS_PRIVATE; //this is private and only admins can see it -if ($title && $address) { +$fail = function () use ($address) { + register_error(elgg_echo('reportedcontent:failed')); + forward($address); +}; - $report = new ElggObject; - $report->subtype = "reported_content"; - $report->owner_guid = elgg_get_logged_in_user_guid(); - $report->title = $title; - $report->address = $address; - $report->description = $description; - $report->access_id = $access; +if (!$title || !$address) { + $fail(); +} - if ($report->save()) { - if (!elgg_trigger_plugin_hook('reportedcontent:add', 'system', array('report' => $report), true)) { - $report->delete(); - register_error(elgg_echo('reportedcontent:failed')); - } else { - system_message(elgg_echo('reportedcontent:success')); - $report->state = "active"; - } - forward($address); - } else { - register_error(elgg_echo('reportedcontent:failed')); - forward($address); - } -} else { +$report = new ElggObject; +$report->subtype = "reported_content"; +$report->owner_guid = elgg_get_logged_in_user_guid(); +$report->title = $title; +$report->address = $address; +$report->description = $description; +$report->access_id = $access; - register_error(elgg_echo('reportedcontent:failed')); - forward($address); +if (!$report->save()) { + $fail(); +} + +if (!elgg_trigger_plugin_hook('reportedcontent:add', 'system', array('report' => $report), true)) { + $report->delete(); + $fail(); } + +system_message(elgg_echo('reportedcontent:success')); +$report->state = "active"; +forward($address); diff --git a/mod/reportedcontent/actions/reportedcontent/archive.php b/mod/reportedcontent/actions/reportedcontent/archive.php index dd5c6aef1b0..37102141bbb 100644 --- a/mod/reportedcontent/actions/reportedcontent/archive.php +++ b/mod/reportedcontent/actions/reportedcontent/archive.php @@ -8,20 +8,19 @@ $guid = (int) get_input('guid'); $report = get_entity($guid); +if (!$report || $report->getSubtype() !== "reported_content" || !$report->canEdit()) { + register_error(elgg_echo("reportedcontent:notarchived")); + forward(REFERER); +} -// Make sure we actually have permission to edit -if ($report->getSubtype() == "reported_content" && $report->canEdit()) { - - // allow another plugin to override - if (!elgg_trigger_plugin_hook('reportedcontent:archive', 'system', array('report' => $report), TRUE)) { - system_message(elgg_echo("reportedcontent:notarchived")); - forward(REFERER); - } - - // change the state - $report->state = "archived"; - - system_message(elgg_echo("reportedcontent:archived")); - +// allow another plugin to override +if (!elgg_trigger_plugin_hook('reportedcontent:archive', 'system', ['report' => $report], true)) { + register_error(elgg_echo("reportedcontent:notarchived")); forward(REFERER); } + +// change the state +$report->state = "archived"; + +system_message(elgg_echo("reportedcontent:archived")); +forward(REFERER); diff --git a/mod/reportedcontent/actions/reportedcontent/delete.php b/mod/reportedcontent/actions/reportedcontent/delete.php index f7d4e210767..c15bc88257b 100644 --- a/mod/reportedcontent/actions/reportedcontent/delete.php +++ b/mod/reportedcontent/actions/reportedcontent/delete.php @@ -8,21 +8,21 @@ $guid = (int) get_input('guid'); $report = get_entity($guid); +if (!$report || $report->getSubtype() !== "reported_content" || !$report->canEdit()) { + register_error(elgg_echo("reportedcontent:notdeleted")); + forward(REFERER); +} -// Make sure we actually have permission to delete -if ($report->getSubtype() == "reported_content" && $report->canEdit()) { - - // give another plugin a chance to override - if (!elgg_trigger_plugin_hook('reportedcontent:delete', 'system', array('report' => $report), TRUE)) { - register_error(elgg_echo("reportedcontent:notdeleted")); - forward(REFERER); - } - - if ($report->delete()) { - system_message(elgg_echo("reportedcontent:deleted")); - } else { - register_error(elgg_echo("reportedcontent:notdeleted")); - } - +// give another plugin a chance to override +if (!elgg_trigger_plugin_hook('reportedcontent:delete', 'system', array('report' => $report), true)) { + register_error(elgg_echo("reportedcontent:notdeleted")); forward(REFERER); } + +if ($report->delete()) { + system_message(elgg_echo("reportedcontent:deleted")); +} else { + register_error(elgg_echo("reportedcontent:notdeleted")); +} + +forward(REFERER); diff --git a/mod/reportedcontent/start.php b/mod/reportedcontent/start.php index 09556060574..1b9c778e147 100644 --- a/mod/reportedcontent/start.php +++ b/mod/reportedcontent/start.php @@ -21,19 +21,17 @@ function reportedcontent_init() { // Extend footer with report content link if (elgg_is_logged_in()) { - $href = "javascript:elgg.forward('reportedcontent/add'"; - $href .= "+'?address='+encodeURIComponent(location.href)"; - $href .= "+'&title='+encodeURIComponent(document.title));"; - elgg_register_menu_item('extras', array( 'name' => 'report_this', - 'href' => $href, + 'href' => 'reportedcontent/add', 'title' => elgg_echo('reportedcontent:this:tooltip'), 'text' => elgg_view_icon('report-this'), 'priority' => 500, 'section' => 'default', + 'link_class' => 'elgg-lightbox', )); } + elgg_require_js('reportedcontent'); elgg_register_plugin_hook_handler('register', 'menu:user_hover', 'reportedcontent_user_hover_menu'); @@ -66,6 +64,11 @@ function reportedcontent_page_handler($page) { // only logged in users can report things elgg_gatekeeper(); + if (elgg_extract(0, $page) === 'add' && elgg_is_xhr()) { + echo elgg_view('resources/reportedcontent/add_form'); + return true; + } + $title = elgg_echo('reportedcontent:this'); $content = elgg_view_form('reportedcontent/add'); diff --git a/mod/reportedcontent/views/default/admin/administer_utilities/reportedcontent.php b/mod/reportedcontent/views/default/admin/administer_utilities/reportedcontent.php index ed52a536d65..8af200aed54 100644 --- a/mod/reportedcontent/views/default/admin/administer_utilities/reportedcontent.php +++ b/mod/reportedcontent/views/default/admin/administer_utilities/reportedcontent.php @@ -5,7 +5,15 @@ * @package ElggReportedContent */ -$list = elgg_list_entities(array('type' => 'object', 'subtype' => 'reported_content')); +$list = elgg_list_entities_from_metadata([ + 'type' => 'object', + 'subtype' => 'reported_content', + 'order_by_metadata' => [ + 'name' => 'state', + 'direction' => 'ASC', + 'as' => 'text', + ], +]); if (!$list) { $list = '

' . elgg_echo('reportedcontent:none') . '

'; } diff --git a/mod/reportedcontent/views/default/forms/reportedcontent/add.php b/mod/reportedcontent/views/default/forms/reportedcontent/add.php index 64cb6a37c5b..6a0ebb0f47d 100644 --- a/mod/reportedcontent/views/default/forms/reportedcontent/add.php +++ b/mod/reportedcontent/views/default/forms/reportedcontent/add.php @@ -29,11 +29,11 @@ @@ -42,7 +42,7 @@ 'description', 'value' => $description, )); @@ -53,5 +53,16 @@ echo elgg_view('input/submit', array( 'value' => elgg_echo('reportedcontent:report'), )); + if (elgg_is_xhr()) { + $js = 'elgg.ui.lightbox.close()'; + } else { + $js = 'history.go(-1)'; + } + echo elgg_view('output/url', [ + 'value' => "javascript:$js", + 'text' => elgg_echo('cancel'), + 'class' => 'elgg-button elgg-button-cancel mls', + 'is_trusted' => true, + ]); ?> diff --git a/mod/reportedcontent/views/default/js/reportedcontent.js b/mod/reportedcontent/views/default/js/reportedcontent.js new file mode 100644 index 00000000000..c6dafd91ac7 --- /dev/null +++ b/mod/reportedcontent/views/default/js/reportedcontent.js @@ -0,0 +1,50 @@ +define(function (require) { + var elgg = require('elgg'), + $ = require('jquery'); + + var $a = $('.elgg-menu-item-report-this a'); + if ($a.length) { + $a[0].href += '?address=' + encodeURIComponent(location.href) + + '&title=' + encodeURIComponent(document.title); + } + + $(document).on('submit', '.elgg-form-reportedcontent-add', function (e) { + e.preventDefault(); + var $form = $(this); + elgg.action($form[0].action, { + data: $form.serialize(), + success: function (data) { + if (data.status == 0) { + elgg.ui.lightbox.close(); + } + } + }); + }); + + $(document).on('click', '.elgg-item-object-reported_content', function (e) { + var $clicked = $(e.target), + $li = $(this); + + if ($clicked.is('a[data-elgg-action]')) { + var action = $clicked.data('elggAction'); + elgg.action(action.name, { + data: action.data, + success: function (data) { + if (data.status == -1) { + return; + } + + if (action.name === 'reportedcontent/delete') { + $li.slideUp(); + } else { + $clicked.fadeOut(); + $li.find('.reported-content-active') + .removeClass('reported-content-active') + .addClass('reported-content-archived'); + } + } + }); + return false; + } + }) +}); diff --git a/mod/reportedcontent/views/default/object/reported_content.php b/mod/reportedcontent/views/default/object/reported_content.php index 13e3ff36fe1..4f8196b81ba 100644 --- a/mod/reportedcontent/views/default/object/reported_content.php +++ b/mod/reportedcontent/views/default/object/reported_content.php @@ -6,11 +6,9 @@ */ $report = $vars['entity']; +/* @var ElggObject $report */ $reporter = $report->getOwnerEntity(); -$archive_url = elgg_get_site_url() . "action/reportedcontent/archive?guid=$report->guid"; -$delete_url = elgg_get_site_url() . "action/reportedcontent/delete?guid=$report->guid"; - //find out if the report is current or archive if ($report->state == 'archived') { $reportedcontent_background = "reported-content-archived"; @@ -26,59 +24,59 @@ state != 'archived') { $params = array( - 'href' => $archive_url, + 'href' => 'javascript:', 'text' => elgg_echo('reportedcontent:archive'), - 'is_action' => true, 'is_trusted' => true, 'class' => 'elgg-button elgg-button-action', + 'data-elgg-action' => json_encode([ + 'name' => 'reportedcontent/archive', + 'data' => [ + 'guid' => $report->guid, + ] + ]), ); echo elgg_view('output/url', $params); } $params = array( - 'href' => $delete_url, + 'href' => 'javascript:', 'text' => elgg_echo('reportedcontent:delete'), - 'is_action' => true, 'is_trusted' => true, 'class' => 'elgg-button elgg-button-action', + 'data-elgg-action' => json_encode([ + 'name' => 'reportedcontent/delete', + 'data' => [ + 'guid' => $report->guid, + ] + ]), ); echo elgg_view('output/url', $params); ?> -

- : - $reporter->getURL(), - 'text' => $reporter->name, - 'is_trusted' => true, - )); - ?>, - time_created); ?> -

-

- : - title; ?> -

- : - + $report->title, 'href' => $report->address, - 'text' => elgg_echo('reportedcontent:visit'), 'is_trusted' => true, - )); + 'class' => 'elgg-reported-content-address elgg-lightbox', + 'data-colorbox-opts' => json_encode([ + 'width' => '85%', + 'height' => '85%', + 'iframe' => true, + ]), + ]); ?> -

-

- "#report-$report->guid", - 'text' => elgg_echo('more_info'), - 'rel' => "toggle", - )); + +

+ $reporter->getURL(), + 'text' => $reporter->name, + 'is_trusted' => true, + ]); + echo " " . elgg_view_friendly_time($report->time_created); ?>

- - diff --git a/mod/reportedcontent/views/default/resources/reportedcontent/add_form.php b/mod/reportedcontent/views/default/resources/reportedcontent/add_form.php new file mode 100644 index 00000000000..30128da2a29 --- /dev/null +++ b/mod/reportedcontent/views/default/resources/reportedcontent/add_form.php @@ -0,0 +1,3 @@ + 'object', 'subtype' => 'reported_content', 'limit' => $vars['entity']->num_display, 'pagination' => false, -)); + 'order_by_metadata' => [ + 'name' => 'state', + 'direction' => 'ASC', + 'as' => 'text', + ], +]); if (!$list) { $list = '

' . elgg_echo('reportedcontent:none') . '

'; }