Skip to content

Commit

Permalink
add feature delete unreferenced elements
Browse files Browse the repository at this point in the history
  • Loading branch information
christianruhstaller committed Jul 7, 2017
1 parent 05160fb commit 305014a
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 18 deletions.
52 changes: 36 additions & 16 deletions safedelete/controllers/SafeDeleteController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Craft;

class SafeDeleteController extends BaseController
Expand All @@ -11,25 +12,30 @@ public function actionTryDelete()
$settings = craft()->plugins->getPlugin('safeDelete')->getSettings();

$relations = craft()->safeDelete->getUsagesFor($ids, $type);
if($relations === null || count($relations) === 0) { // safe to delete

if ($relations === null || count($relations) === 0) { // safe to delete

return $this->doAction($ids, $type);
} else {
$html = craft()->templates->render('safeDelete/deleteOverlay', ['relations' => $relations, 'allowForceDelete' => (bool)$settings->allowForceDelete]);
$html = craft()->templates->render(
'safeDelete/deleteOverlay',
['relations' => $relations, 'allowForceDelete' => (bool)$settings->allowForceDelete]
);

return $this->returnJson([
'html' => $html,
'success'=> true,
]);
return $this->returnJson(
[
'html' => $html,
'success' => true,
]
);
}
}

protected function doAction($ids, $type)
{
$message = '';

switch($type) {
switch ($type) {
case 'asset':
craft()->assets->deleteFiles($ids);
$message = Craft::t('Assets deleted.');
Expand All @@ -40,10 +46,12 @@ protected function doAction($ids, $type)
break;
}

return $this->returnJson([
'success'=> true,
'message' => $message,
]);
return $this->returnJson(
[
'success' => true,
'message' => $message,
]
);
}

public function actionForceDelete()
Expand All @@ -53,13 +61,25 @@ public function actionForceDelete()

$settings = craft()->plugins->getPlugin('safeDelete')->getSettings();

if($settings->allowForceDelete) {
if ($settings->allowForceDelete) {

return $this->doAction($ids, $type);
}

return $this->returnJson([
'success'=> false,
]);
return $this->returnJson(
[
'success' => false,
]
);
}

public function actionDeleteUnreferenced()
{
$ids = craft()->request->getPost('ids');
$type = craft()->request->getPost('type');

$ids = craft()->safeDelete->filterReferencedIds($ids, $type);

return $this->doAction($ids, $type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,20 @@ public function getTriggerHtml()
modal.hide();
});
\$html.find('.submit').on('click', function() {
\$html.find('.submit[name=forceDelete]').on('click', function() {
Craft.postActionRequest('safeDelete/forceDelete', {'ids': ids, type: '$deletionType'}, function(res) {
Craft.elementIndex.updateElements();
modal.hide();
});
});
\$html.find('.submit[name=deleteUnreferencedElements]').on('click', function() {
Craft.postActionRequest('safeDelete/deleteUnreferenced', {'ids': ids, type: '$deletionType'}, function(res) {
Craft.elementIndex.updateElements();
modal.hide();
});
});
\$html.find('.reload').on('click', function() {
modal.hide();
doAction(ids);
Expand Down
30 changes: 30 additions & 0 deletions safedelete/services/SafeDeleteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,36 @@ public function getUsagesFor($ids, $type)
return $relations;
}

/**
* Returns an array only with ids which are
* not referneced and safe to delete.
*
* @param $ids
* @param $type
* @return array
*/
public function filterReferencedIds($ids, $type)
{
$arrIds = [];
$arrRet = [];

$relations = craft()->safeDelete->getUsagesFor($ids, $type);

foreach ($relations as $elements) {
foreach($elements as $element) {
$arrIds[] = $element['sourceElement']->id;
}
}

foreach($ids as $id) {
if (!in_array($id, $arrIds)) {
$arrRet[] = $id;
}
}

return $arrRet;
}

protected function getRelationsForElement($id)
{
$arrReturn = [];
Expand Down
2 changes: 1 addition & 1 deletion safedelete/templates/deleteOverlay.twig
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
</div>
{% if allowForceDelete %}
<p style="margin-top: 20px;">
<input type="button" class="btn submit" value="{{ 'Delete all elements'|t }}">
<input type="button" class="btn submit" name="forceDelete" value="{{ 'Delete all elements'|t }}">
</p>
<p style="margin-top: 10px;">{{ 'WARNING! Deletion of referenced elements can lead to unexpected consequences and errors in the frontend.'|t }}</p>
{% endif %}
Expand Down

0 comments on commit 305014a

Please sign in to comment.