From 92fe657e80904a5bc72139a378a295f28461fb28 Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Thu, 10 Oct 2013 11:17:07 +0200 Subject: [PATCH] Fix #1528 : Restrict collection ordering on available collections --- .../Phrasea/Controller/Admin/Databox.php | 15 ++-- lib/classes/collection.php | 20 ++++- lib/classes/patch/3819.php | 90 +++++++++++++++++++ .../web/admin/collection/reorder.html.twig | 11 ++- 4 files changed, 123 insertions(+), 13 deletions(-) create mode 100644 lib/classes/patch/3819.php diff --git a/lib/Alchemy/Phrasea/Controller/Admin/Databox.php b/lib/Alchemy/Phrasea/Controller/Admin/Databox.php index ad51da8b44..172b7fce5d 100644 --- a/lib/Alchemy/Phrasea/Controller/Admin/Databox.php +++ b/lib/Alchemy/Phrasea/Controller/Admin/Databox.php @@ -972,7 +972,7 @@ class Databox implements ControllerProviderInterface public function getReorder(Application $app, Request $request, $databox_id) { return $app['twig']->render('admin/collection/reorder.html.twig', array( - 'databox' => $app['phraseanet.appbox']->get_databox($databox_id), + 'collections' => $app['authentication']->getUser()->ACL()->get_granted_base(array(), array($databox_id)), )); } @@ -986,19 +986,14 @@ class Databox implements ControllerProviderInterface */ public function setReorder(Application $app, Request $request, $databox_id) { - $success = false; - try { - foreach ($request->request->get('order', array()) as $order => $data) { - $baseId = $data['id']; - $collection = \collection::get_from_base_id($app, $baseId); - $app['phraseanet.appbox']->set_collection_order($collection, $order); - unset($collection); + foreach ($request->request->get('order', array()) as $data) { + $collection = \collection::get_from_base_id($app, $data['id']); + $collection->set_ord($data['offset']); } - $success = true; } catch (\Exception $e) { - + $success = false; } if ('json' === $app['request']->getRequestFormat()) { diff --git a/lib/classes/collection.php b/lib/classes/collection.php index f378c3bd37..8e5dacc3c7 100644 --- a/lib/classes/collection.php +++ b/lib/classes/collection.php @@ -36,6 +36,7 @@ class collection implements cache_cacheableInterface protected $databox; protected $is_active; protected $binary_logo; + protected $ord; protected $app; const PIC_LOGO = 'minilogos'; @@ -63,6 +64,7 @@ class collection implements cache_cacheableInterface $this->available = $datas['available']; $this->pub_wm = $datas['pub_wm']; $this->name = $datas['name']; + $this->ord = $datas['ord']; $this->prefs = $datas['prefs']; $this->labels = $datas['labels']; @@ -96,7 +98,7 @@ class collection implements cache_cacheableInterface $conn = connection::getPDOConnection($this->app); - $sql = 'SELECT server_coll_id, sbas_id, base_id, active FROM bas + $sql = 'SELECT server_coll_id, sbas_id, base_id, active, ord FROM bas WHERE server_coll_id = :coll_id AND sbas_id = :sbas_id'; $stmt = $conn->prepare($sql); @@ -109,6 +111,7 @@ class collection implements cache_cacheableInterface if ($row) { $this->is_active = ! ! $row['active']; $this->base_id = (int) $row['base_id']; + $this->ord = (int) $row['ord']; } $stmt->closeCursor(); @@ -119,6 +122,7 @@ class collection implements cache_cacheableInterface , 'available' => $this->available , 'pub_wm' => $this->pub_wm , 'name' => $this->name + , 'ord' => $this->ord , 'prefs' => $this->prefs , 'labels' => $this->labels ); @@ -144,6 +148,20 @@ class collection implements cache_cacheableInterface return $this; } + public function get_ord() + { + return $this->ord; + } + + public function set_ord($ord) + { + $this->app['phraseanet.appbox']->set_collection_order($this, $ord); + $this->delete_data_from_cache(); + $this->app['phraseanet.appbox']->delete_data_from_cache(appbox::CACHE_LIST_BASES); + + return $this; + } + public function disable(appbox $appbox) { $sql = 'UPDATE bas SET active=0 WHERE base_id = :base_id'; diff --git a/lib/classes/patch/3819.php b/lib/classes/patch/3819.php new file mode 100644 index 0000000000..cb44bc11e4 --- /dev/null +++ b/lib/classes/patch/3819.php @@ -0,0 +1,90 @@ +release; + } + + /** + * {@inheritdoc} + */ + public function require_all_upgrades() + { + return false; + } + + /** + * {@inheritdoc} + */ + public function concern() + { + return $this->concern; + } + + /** + * {@inheritdoc} + */ + public function apply(base $appbox, Application $app) + { + $sql = 'SELECT base_id, ord, sbas_id + FROM `bas` + ORDER BY sbas_id, ord'; + $stmt = $appbox->get_connection()->prepare($sql); + $stmt->execute(); + $rs = $stmt->fetchAll(\PDO::FETCH_ASSOC); + $stmt->closeCursor(); + + $sbasData = array(); + $sbas_id = null; + $reorder = array(); + foreach ($rs as $row) { + $sbasData[$row['sbas_id']][] = array('base_id' => $row['base_id']); + if ($sbas_id !== $row['sbas_id']) { + $orders = array(); + } + $sbas_id = $row['sbas_id']; + if (in_array($row['ord'], $orders, true)) { + $reorder[] = $row['sbas_id']; + } + $orders[] = $row['ord']; + } + $reorder = array_unique($reorder); + + if (count($reorder) > 0) { + $sql = 'UPDATE bas SET ord = :ord WHERE base_id = :base_id'; + $stmt = $appbox->get_connection()->prepare($sql); + foreach ($reorder as $sbas_id) { + $i = 1; + foreach ($sbasData[$sbas_id] as $data) { + $stmt->execute(array('base_id' => $data['base_id'], 'ord' => $i++)); + } + } + $stmt->closeCursor(); + } + + return true; + } +} diff --git a/templates/web/admin/collection/reorder.html.twig b/templates/web/admin/collection/reorder.html.twig index 34131abeb1..fefbf8401c 100644 --- a/templates/web/admin/collection/reorder.html.twig +++ b/templates/web/admin/collection/reorder.html.twig @@ -6,7 +6,7 @@ @@ -35,6 +35,11 @@