Skip to content

Instantly share code, notes, and snippets.

@c33s
Forked from vworldat/admingen_sortable.yml
Last active August 29, 2015 14:07
Show Gist options
  • Save c33s/4ab4a31c075d01758a62 to your computer and use it in GitHub Desktop.
Save c33s/4ab4a31c075d01758a62 to your computer and use it in GitHub Desktop.
# MyModel-generator.yml (admingen)
# you might want to add a pos. label. If not, ignore this part
params:
fields:
Rank:
label: Pos.
# Change list to nested_list and add rank if needed
builders:
nested_list:
params:
display:
- Rank
# admingenerator.yml (routing)
# Change admingen routing type from admingenerator to admingenerator_nested to enable the *_nested_move route
my_namespace_admin_gen_my_model:
resource: "@MyNamespaceAdminGenBundle/Controller/MyModel/"
# type: admingenerator
type: admingenerator_nested
prefix: /my_model
<?php
// Model class
// Fake tree (flat list)
class MyModelQuery extends BaseMyModelQuery
{
public function findTree()
{
return $this
->orderByRank()
->find()
;
}
}
// Model query class
// Fake parent call (there is none)
class MyModel extends BaseMyModel
{
public function getParent()
{
return null;
}
}
// Admingen list controller class
// Generic override of the existing nested_moveAction() to sort instead of move
class ListController extends BaseListController
{
public function nested_moveAction($dragged, $action, $dropped)
{
if(!in_array($action, array('before', 'after'))) {
throw new NotFoundHttpException("Unknown action");
}
$dragged = $this->buildQuery()->findPk($dragged);
$dropped = $this->buildQuery()->findPk($dropped);
if (!$dragged || !$dropped) {
throw new NotFoundHttpException("Could not find the nodes");
}
switch($action) {
case 'before':
$dragged->moveToRank($dragged->getRank() > $dropped->getRank() ? $dropped->getRank() : $dropped->getRank() - 1);
break;
case 'after':
$dragged->moveToRank($dragged->getRank() > $dropped->getRank() ? $dropped->getRank() + 1 : $dropped->getRank());
break;
}
$dragged->save();
return new Response();
}
}
// unfortunately the existing tree stylesheet is a mess of !important statements, making it impossible to
// override any borders, so we have to improvise by defining background radients used as insertion markers
.treeTable tbody tr.droppable-before
{
#gradient > .vertical-three-colors(@start-color: rgba(0, 0, 0, 0.4); @mid-color: rgba(255, 255, 255, 0.1); @color-stop: 30%; @end-color: rgba(255, 255, 255, 0.0))
}
.treeTable tbody tr.droppable-after
{
#gradient > .vertical-three-colors(@start-color: rgba(255, 255, 255, 0.0); @mid-color: rgba(255, 255, 255, 0.1); @color-stop: 70%; @end-color: rgba(0, 0, 0, 0.4))
}
.table-striped > tbody > tr.droppable-before:nth-child(2n+1) > td, .table-striped > tbody > tr.droppable-before:nth-child(2n+1) > th {
background-color: transparent;
}
.table-striped > tbody > tr.droppable-after:nth-child(2n+1) > td, .table-striped > tbody > tr.droppable-after:nth-child(2n+1) > th {
background-color: transparent;
}
{# MyModelList/index.html.twig #}
{% extends_admingenerated "MyNamespaceAdminGenBundle:MyModelList:index.html.twig" %}
{# Adding patched library source #}
{% block javascripts %}
{{ parent() }}
<script type="text/javascript" src="{{ asset("bundles/c33score/js/nestedset-override.js") }}"></script>
{% endblock javascripts %}
{# MyModelList/results.html.twig #}
{% extends_admingenerated "MyNamespaceAdminGenBundle:MyModelList:results.html.twig" %}
{# By default the count is reduced by 1 because the tree root would be hidden. To fix this, override this block #}
{% block list_nbresults %}
<div class="list_nbresults">
{% trans with {'%count%': Services.count,} from "Admingenerator" %}list.display.all{% endtrans %}
</div>
{% endblock list_nbresults %}
{# Overriding the included js library to use our patched one #}
{% block endform_batch_actions %}
{{ parent() }}
<script type="text/javascript">
;(function(window, $, undefined){
$(function(){
var original = $.fn['agen$nestedTree'];
$.fn['agen$nestedTree'] = function() {
return $.fn['agen$nestedTreeOverride'].apply(this, arguments);
};
});
})(this, jQuery);
</script>
{% endblock endform_batch_actions %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment