From 9313f3da2b374bceff467e21d990e71110f90cd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Burnichon?= Date: Thu, 17 Mar 2016 11:20:31 +0100 Subject: [PATCH 1/2] Added new Twig filter to sort a collection set. Only currently used in admin/tree.html.twig but could be used in collection rights. --- .../Phrasea/Twig/PhraseanetExtension.php | 21 +++++++++++++++++++ templates/web/admin/tree.html.twig | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/Alchemy/Phrasea/Twig/PhraseanetExtension.php b/lib/Alchemy/Phrasea/Twig/PhraseanetExtension.php index 56c7040c7d..aa1842ac7f 100644 --- a/lib/Alchemy/Phrasea/Twig/PhraseanetExtension.php +++ b/lib/Alchemy/Phrasea/Twig/PhraseanetExtension.php @@ -8,6 +8,7 @@ use Alchemy\Phrasea\Model\Entities\User; use Alchemy\Phrasea\Model\RecordInterface; use Alchemy\Phrasea\Http\StaticFile\StaticMode; use Alchemy\Phrasea\SearchEngine\Elastic\Structure\Flag; +use Assert\Assertion; class PhraseanetExtension extends \Twig_Extension { @@ -22,6 +23,7 @@ class PhraseanetExtension extends \Twig_Extension public function getFilters() { return array( + new \Twig_SimpleFilter('sort_collections', array($this, 'sortCollections')), ); } @@ -305,6 +307,25 @@ class PhraseanetExtension extends \Twig_Extension return $this->app['border-manager']->getCheckerFromFQCN($checkerFQCN); } + /** + * @param \collection[] $collections + * @return \collection[] + */ + public function sortCollections($collections) + { + Assertion::allIsInstanceOf($collections, 'collection'); + + if ($collections instanceof \Traversable) { + $collections = iterator_to_array($collections); + } + + usort($collections, function (\collection $left, \collection $right) { + return ($left->get_ord() < $right->get_ord()) ? -1 : (($left->get_ord() < $right->get_ord()) ? 1 : 0); + }); + + return $collections; + } + public function getName() { return 'phraseanet'; diff --git a/templates/web/admin/tree.html.twig b/templates/web/admin/tree.html.twig index a682e7eb36..259ec05c4c 100644 --- a/templates/web/admin/tree.html.twig +++ b/templates/web/admin/tree.html.twig @@ -166,7 +166,7 @@ {% endif %} - {% for collection in databox.get_collections() %} + {% for collection in databox.get_collections()|sort_collections %} {% if (collection.get_base_id() in app.getAclForUser(app.getAuthenticatedUser()).get_granted_base(['canadmin'])|keys or collection.get_base_id() in app.getAclForUser(app.getAuthenticatedUser()).get_granted_base(['manage'])|keys or collection.get_base_id() in app.getAclForUser(app.getAuthenticatedUser()).get_granted_base(['modify_struct'])|keys) %} From 7c65b2a7e9a4db636d9f468baecfea609491f446 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Burnichon?= Date: Thu, 17 Mar 2016 12:21:05 +0100 Subject: [PATCH 2/2] Extract method from Twig PhraseanetExtension --- .../Phrasea/Collection/CollectionHelper.php | 39 +++++++++++++++++++ .../Phrasea/Twig/PhraseanetExtension.php | 23 +---------- 2 files changed, 41 insertions(+), 21 deletions(-) create mode 100644 lib/Alchemy/Phrasea/Collection/CollectionHelper.php diff --git a/lib/Alchemy/Phrasea/Collection/CollectionHelper.php b/lib/Alchemy/Phrasea/Collection/CollectionHelper.php new file mode 100644 index 0000000000..a56df2e356 --- /dev/null +++ b/lib/Alchemy/Phrasea/Collection/CollectionHelper.php @@ -0,0 +1,39 @@ +get_ord() < $right->get_ord()) ? -1 : (($left->get_ord() < $right->get_ord()) ? 1 : 0); + }); + + return $collections; + } +} diff --git a/lib/Alchemy/Phrasea/Twig/PhraseanetExtension.php b/lib/Alchemy/Phrasea/Twig/PhraseanetExtension.php index aa1842ac7f..b1d081ff7a 100644 --- a/lib/Alchemy/Phrasea/Twig/PhraseanetExtension.php +++ b/lib/Alchemy/Phrasea/Twig/PhraseanetExtension.php @@ -3,12 +3,12 @@ namespace Alchemy\Phrasea\Twig; use Alchemy\Phrasea\Application; +use Alchemy\Phrasea\Collection\CollectionHelper; use Alchemy\Phrasea\Model\Entities\ElasticsearchRecord; use Alchemy\Phrasea\Model\Entities\User; use Alchemy\Phrasea\Model\RecordInterface; use Alchemy\Phrasea\Http\StaticFile\StaticMode; use Alchemy\Phrasea\SearchEngine\Elastic\Structure\Flag; -use Assert\Assertion; class PhraseanetExtension extends \Twig_Extension { @@ -23,7 +23,7 @@ class PhraseanetExtension extends \Twig_Extension public function getFilters() { return array( - new \Twig_SimpleFilter('sort_collections', array($this, 'sortCollections')), + new \Twig_SimpleFilter('sort_collections', array(CollectionHelper::class, 'sort')), ); } @@ -307,25 +307,6 @@ class PhraseanetExtension extends \Twig_Extension return $this->app['border-manager']->getCheckerFromFQCN($checkerFQCN); } - /** - * @param \collection[] $collections - * @return \collection[] - */ - public function sortCollections($collections) - { - Assertion::allIsInstanceOf($collections, 'collection'); - - if ($collections instanceof \Traversable) { - $collections = iterator_to_array($collections); - } - - usort($collections, function (\collection $left, \collection $right) { - return ($left->get_ord() < $right->get_ord()) ? -1 : (($left->get_ord() < $right->get_ord()) ? 1 : 0); - }); - - return $collections; - } - public function getName() { return 'phraseanet';