From 187aa620cfbedd6d7a55dc9a6df541fa152ad907 Mon Sep 17 00:00:00 2001 From: Nicolas Le Goff Date: Mon, 21 Oct 2013 16:42:36 +0200 Subject: [PATCH] Add login template variable to get last published items --- lib/Alchemy/Phrasea/Controller/Root/Login.php | 1 + lib/classes/Feed/Entry/Item.php | 69 +++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/lib/Alchemy/Phrasea/Controller/Root/Login.php b/lib/Alchemy/Phrasea/Controller/Root/Login.php index 681b4407f7..b89caf0068 100644 --- a/lib/Alchemy/Phrasea/Controller/Root/Login.php +++ b/lib/Alchemy/Phrasea/Controller/Root/Login.php @@ -50,6 +50,7 @@ class Login implements ControllerProviderInterface public static function getDefaultTemplateVariables(Application $app) { return array( + 'last_publication_items' => \Feed_Entry_Item::loadLatest($app, 20), 'instance_title' => $app['phraseanet.registry']->get('GV_homeTitle'), 'has_terms_of_use' => $app->hasTermsOfUse(), 'meta_description' => $app['phraseanet.registry']->get('GV_metaDescription'), diff --git a/lib/classes/Feed/Entry/Item.php b/lib/classes/Feed/Entry/Item.php index 6f54976a63..5246b216d2 100644 --- a/lib/classes/Feed/Entry/Item.php +++ b/lib/classes/Feed/Entry/Item.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +use Alchemy\Phrasea\Application; + /** * * @package Feeds @@ -207,6 +209,73 @@ class Feed_Entry_Item implements Feed_Entry_ItemInterface, cache_cacheableInterf return new self($appbox, $entry, $item_id); } + public static function loadLatest(Application $app, $nbItems = 20) + { + $execution = 0; + $items = array(); + + do { + $feeds = $entries = array(); + + $sql = 'SELECT en.id AS entry, f.id AS feed + FROM feed_entry_elements AS el + INNER JOIN feed_entries AS en ON (el.entry_id = en.id) + INNER JOIN feeds AS f ON (f.id = en.feed_id) + WHERE f.public = 1 AND f.base_id IS null + ORDER BY en.updated_on DESC + LIMIT ' . ($nbItems * $execution) .','. $nbItems; + + $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql); + $stmt->execute(); + $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC); + $stmt->closeCursor(); + + foreach($rows as $row) { + if (!isset($feeds[$row['feed']])) { + $feeds[$row['feed']] = new Feed_Adapter($app, $row['feed']); + } + + if (!isset($entries[$row['entry']])) { + $entries[$row['entry']] = new Feed_Entry_Adapter($app, $feeds[$row['feed']], $row['entry']); + } + } + + foreach($entries as $entry) { + foreach ($entry->get_content() as $item) { + if (null !== $preview = $item->get_record()->get_subdef('preview')) { + if (null !== $permalink = $preview->get_permalink()) { + if (!isset($items[$item->get_id()])) { + $items[$item->get_id()] = array( + 'entry' => $entry, + 'record' => $item->get_record(), + 'preview' => $preview, + 'permalink' => $permalink + ); + + if (count($items) >= $nbItems) { + break; + } + } + } + } + } + + if (count($items) > $nbItems) { + break; + } + } + + if (count($rows) <= $nbItems) { + break; + } + + $execution++; + } + while (count($items) < $nbItems); + + return $items; + } + public function get_cache_key($option = null) { return 'feedentryitem_' . $this->get_id() . '_' . ($option ? '_' . $option : '');