Permalink
Browse files

feature(actions): adds a generic delete action

Plugins can now use action/delete?guid=$guid to delete an entity.
The action file provides for an easy way to define custom translation keys,
as well as a forward URL.

Refs #5922
  • Loading branch information...
hypeJunction committed Dec 8, 2015
1 parent e47785f commit 4c35fe26b26e8f76919b4ac08e9b0246c047497e
Showing with 107 additions and 15 deletions.
  1. +2 −13 actions/entities/delete.php
  2. +66 −0 actions/entity/delete.php
  3. +32 −0 docs/guides/actions.rst
  4. +2 −0 engine/lib/elgglib.php
  5. +5 −2 languages/en.php
@@ -6,17 +6,6 @@
* @subpackage Core
*/
$guid = get_input('guid');
$entity = get_entity($guid);
elgg_deprecated_notice('entities/delete action file has been deprecated. Use entity/delete action instead', '2.1');
if (($entity) && ($entity->canEdit())) {
if ($entity->delete()) {
system_message(elgg_echo('entity:delete:success', array($guid)));
} else {
register_error(elgg_echo('entity:delete:fail', array($guid)));
}
} else {
register_error(elgg_echo('entity:delete:fail', array($guid)));
}
forward(REFERER);
return require dirname(dirname(__FILE__)) . '/entity/delete.php';
View
@@ -0,0 +1,66 @@
<?php
/**
* Default entity delete action
*/
$guid = get_input('guid');
$entity = get_entity($guid);
if (!$entity instanceof ElggEntity) {
register_error(elgg_echo('entity:delete:item_not_found'));
forward(REFERRER);
}
if (!$entity->canDelete()) {
register_error(elgg_echo('entity:delete:permission_denied'));
forward(REFERRER);
}
set_time_limit(0);
// determine what name to show on success
$display_name = $entity->getDisplayName();
if (!$display_name) {
$display_name = elgg_echo('entity:delete:item');
}
$type = $entity->getType();
$subtype = $entity->getSubtype();
$container = $entity->getContainerEntity();
if (!$entity->delete()) {
register_error(elgg_echo('entity:delete:fail', array($display_name)));
forward(REFERRER);
}
// determine forward URL
$forward_url = get_input('forward_url');
if (!$forward_url) {
$forward_url = REFERRER;
$referrer_url = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$site_url = elgg_get_site_url();
if ($referrer_url && 0 == strpos($referrer_url, $site_url)) {
$referrer_path = substr($referrer_url, 0, strlen($site_url));
$segments = explode('/', $referrer_path);
if (in_array($guid, $segments)) {
// referrer URL contains a reference to the entity that will be deleted
$forward_url = ($container) ? $container->getURL() : '';
}
} else if ($container) {
$forward_url = $container->getURL() ? : '';
}
}
$success_keys = array(
"entity:delete:$type:$subtype:success",
"entity:delete:$type:success",
"entity:delete:success",
);
foreach ($success_keys as $success_key) {
if (elgg_language_key_exists($success_key)) {
system_message(elgg_echo($success_key, array($display_name)));
break;
}
}
forward($forward_url);
View
@@ -137,6 +137,38 @@ This is done as follows:
This lets a plugin extend an existing action without the need to replace the whole action. In the case of the captcha plugin it allows the plugin to provide captcha support in a very loosely coupled way.
Actions available in core
=========================
``entity/delete``
-------------------
If your plugin does not implement any custom logic when deleting an entity, you can use bundled delete action
.. code-block:: php
$guid = 123;
// You can provide optional forward path as a URL query parameter
$forward_url = 'path/to/forward/to';
echo elgg_view('output/url', array(
'text' => elgg_echo('delete'),
'href' => "action/entity/delete?guid=$guid&forward_url=$forward_url",
'confirm' => true,
));
You can customize the success message keys for your entity type and subtype, using ``"entity:delete:$type:$subtype:success"`` and ``"entity:delete:$type:success"`` keys.
.. code-block:: php
// to add a custom message when a blog post or file is deleted
// add the translations keys in your language files
return array(
'entity:delete:object:blog:success' => 'Blog post has been deleted,
'entity:delete:object:file:success' => 'File titled %s has been deleted',
);
Forms
=====
View
@@ -1924,6 +1924,8 @@ function _elgg_engine_boot() {
function _elgg_init() {
global $CONFIG;
elgg_register_action('entity/delete');
elgg_register_action('comment/save');
elgg_register_action('comment/delete');
View
@@ -1287,8 +1287,11 @@
'entity:default:strapline' => 'Created %s by %s',
'entity:default:missingsupport:popup' => 'This entity cannot be displayed correctly. This may be because it requires support provided by a plugin that is no longer installed.',
'entity:delete:success' => 'Entity %s has been deleted',
'entity:delete:fail' => 'Entity %s could not be deleted',
'entity:delete:item' => 'Item',
'entity:delete:item_not_found' => 'Item not found.',
'entity:delete:permission_denied' => 'You do not have permissions to delete this item.',
'entity:delete:success' => '%s has been deleted.',
'entity:delete:fail' => '%s could not be deleted.',
'entity:can_delete:invaliduser' => 'Cannot check canDelete() for user_guid [%s] as the user does not exist.',

0 comments on commit 4c35fe2

Please sign in to comment.