mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-18 15:33:15 +00:00
Merge branch '3.8'
Conflicts: composer.json composer.lock config/configuration.sample.yml lib/Alchemy/Phrasea/Application.php lib/Alchemy/Phrasea/Application/Api.php lib/Alchemy/Phrasea/Controller/Admin/Collection.php lib/Alchemy/Phrasea/Controller/Admin/Users.php lib/Alchemy/Phrasea/Controller/Prod/DoDownload.php lib/Alchemy/Phrasea/Controller/Prod/Export.php lib/Alchemy/Phrasea/Controller/Prod/Language.php lib/Alchemy/Phrasea/Controller/Prod/Push.php lib/Alchemy/Phrasea/Controller/Prod/Tooltip.php lib/Alchemy/Phrasea/Controller/Report/Informations.php lib/Alchemy/Phrasea/Controller/Report/Root.php lib/Alchemy/Phrasea/Controller/Root/Developers.php lib/Alchemy/Phrasea/Controller/Root/Login.php lib/Alchemy/Phrasea/Controller/Root/Session.php lib/Alchemy/Phrasea/Controller/Thesaurus/Xmlhttp.php lib/Alchemy/Phrasea/Controller/Utils/ConnectionTest.php lib/Alchemy/Phrasea/Controller/Utils/PathFileTest.php lib/Alchemy/Phrasea/Core/Version.php lib/Alchemy/Phrasea/Form/Login/PhraseaAuthenticationForm.php lib/Alchemy/Phrasea/SearchEngine/Phrasea/PhraseaEngine.php lib/Alchemy/Phrasea/SearchEngine/SphinxSearch/SphinxSearchEngine.php lib/classes/API/OAuth2/Application.php lib/classes/API/V1/adapter.php lib/classes/Feed/Adapter.php lib/classes/Feed/Aggregate.php lib/classes/Feed/Collection.php lib/classes/Feed/Entry/Adapter.php lib/classes/ZipArchiveImproved.php lib/classes/caption/Field/Value.php lib/classes/caption/record.php lib/classes/eventsmanager/notify/feed.php lib/classes/media/subdef.php lib/classes/module/report/connexion.php lib/classes/module/report/download.php lib/classes/record/adapter.php lib/classes/registry.php lib/classes/set/export.php lib/classes/setup.php lib/classes/task/abstract.php lib/classes/task/period/emptyColl.php lib/classes/uuid.php lib/conf.d/_GV_template.inc lib/conf.d/bases_structure.xml lib/conf.d/configuration.yml lib/conf.d/minifyGroupsConfig.php locale/de_DE/LC_MESSAGES/phraseanet.mo locale/de_DE/LC_MESSAGES/phraseanet.po locale/en_GB/LC_MESSAGES/phraseanet.mo locale/en_GB/LC_MESSAGES/phraseanet.po locale/fr_FR/LC_MESSAGES/phraseanet.mo locale/fr_FR/LC_MESSAGES/phraseanet.po locale/nl_NL/LC_MESSAGES/phraseanet.mo locale/nl_NL/LC_MESSAGES/phraseanet.po locale/phraseanet.pot templates/web/account/account.html.twig templates/web/account/base.html.twig templates/web/admin/databox/databox.html.twig templates/web/admin/index.html.twig templates/web/admin/setup.html.twig templates/web/admin/tree.html.twig templates/web/client/index.html.twig templates/web/common/caption.html.twig templates/web/common/caption_templates/answer.html.twig templates/web/common/caption_templates/basket_element.html.twig templates/web/common/caption_templates/internal_publi.html.twig templates/web/common/caption_templates/lazaret.html.twig templates/web/common/caption_templates/overview.html.twig templates/web/common/caption_templates/preview.html.twig templates/web/common/index_bootstrap.html.twig templates/web/common/indexfloat.html.twig templates/web/common/thumbnail.html.twig templates/web/developers/application_form.html.twig templates/web/lightbox/IE6/feed.html.twig templates/web/lightbox/IE6/validate.html.twig templates/web/lightbox/feed.html.twig templates/web/lightbox/validate.html.twig templates/web/prod/actions/Download/prepare.html.twig templates/web/prod/actions/publish/publish.html.twig templates/web/prod/index.html.twig templates/web/prod/preview/caption.html.twig templates/web/prod/results/answerlist.html.twig templates/web/thesaurus/accept.html.twig templates/web/thesaurus/export-text-dialog.html.twig templates/web/thesaurus/export-text.html.twig templates/web/thesaurus/export-topics-dialog.html.twig templates/web/thesaurus/export-topics.html.twig templates/web/thesaurus/index.html.twig templates/web/thesaurus/link-field-step1.html.twig templates/web/thesaurus/link-field-step2.html.twig templates/web/thesaurus/link-field-step3.html.twig templates/web/thesaurus/new-term.html.twig templates/web/thesaurus/properties.html.twig templates/web/thesaurus/search.html.twig templates/web/thesaurus/thesaurus.html.twig tests/Alchemy/Tests/Phrasea/Application/ApiJsonTest.php tests/Alchemy/Tests/Phrasea/Application/ApiYamlTest.php tests/Alchemy/Tests/Phrasea/Application/LightboxTest.php tests/Alchemy/Tests/Phrasea/Application/OAuth2Test.php tests/Alchemy/Tests/Phrasea/Application/OverviewTest.php tests/Alchemy/Tests/Phrasea/Authentication/PersistentCookie/ManagerTest.php tests/Alchemy/Tests/Phrasea/Cache/RedisCacheTest.php tests/Alchemy/Tests/Phrasea/Command/Compile/ConfigurationTest.php tests/Alchemy/Tests/Phrasea/Controller/Admin/FieldsTest.php tests/Alchemy/Tests/Phrasea/Controller/Admin/PublicationTest.php tests/Alchemy/Tests/Phrasea/Controller/Admin/SubdefsTest.php tests/Alchemy/Tests/Phrasea/Controller/Admin/UsersTest.php tests/Alchemy/Tests/Phrasea/Controller/Api/ApiJSONPTest.php tests/Alchemy/Tests/Phrasea/Controller/Prod/BasketTest.php tests/Alchemy/Tests/Phrasea/Controller/Prod/BridgeTest.php tests/Alchemy/Tests/Phrasea/Controller/Prod/EditTest.php tests/Alchemy/Tests/Phrasea/Controller/Prod/FeedTest.php tests/Alchemy/Tests/Phrasea/Controller/Prod/LanguageTest.php tests/Alchemy/Tests/Phrasea/Controller/Prod/MoveCollectionTest.php tests/Alchemy/Tests/Phrasea/Controller/Prod/PrinterTest.php tests/Alchemy/Tests/Phrasea/Controller/Prod/PushTest.php tests/Alchemy/Tests/Phrasea/Controller/Prod/RootTest.php tests/Alchemy/Tests/Phrasea/Controller/Prod/StoryTest.php tests/Alchemy/Tests/Phrasea/Controller/Prod/ToolsTest.php tests/Alchemy/Tests/Phrasea/Controller/Prod/TooltipTest.php tests/Alchemy/Tests/Phrasea/Controller/Prod/UploadTest.php tests/Alchemy/Tests/Phrasea/Controller/Prod/UsrListsTest.php tests/Alchemy/Tests/Phrasea/Controller/Prod/WorkZoneTest.php tests/Alchemy/Tests/Phrasea/Controller/Root/LoginTest.php tests/Alchemy/Tests/Phrasea/Controller/Root/RSSFeedTest.php tests/Alchemy/Tests/Phrasea/Controller/Utils/ConnectionTestTest.php tests/Alchemy/Tests/Phrasea/Controller/Utils/PathFileTestTest.php tests/Alchemy/Tests/Phrasea/Core/Provider/FTPServiceProviderTest.php tests/Alchemy/Tests/Phrasea/Core/Provider/LocaleServiceProviderTest.php tests/Alchemy/Tests/Phrasea/Core/Provider/PhraseanetServiceProviderTest.php tests/Alchemy/Tests/Phrasea/Core/Provider/RegistrationServiceProviderTest.php tests/Alchemy/Tests/Phrasea/Core/Provider/TaskManagerServiceProviderTest.php tests/Alchemy/Tests/Phrasea/Http/XSendFile/NginxModeTest.php tests/Alchemy/Tests/Phrasea/Metadata/Tag/TfEditDateTest.php tests/Alchemy/Tests/Phrasea/Metadata/Tag/TfMimeTypeTest.php tests/Alchemy/Tests/Phrasea/SearchEngine/SearchEngineAbstractTest.php tests/classes/Feed/Feed_AggregateTest.php tests/classes/PhraseanetPHPUnitAbstract.php tests/db-ref.sqlite
This commit is contained in:
722
lib/classes/Feed/Adapter.php
Normal file
722
lib/classes/Feed/Adapter.php
Normal file
@@ -0,0 +1,722 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Phraseanet
|
||||
*
|
||||
* (c) 2005-2014 Alchemy
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
use Alchemy\Phrasea\Application;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @package Feeds
|
||||
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
|
||||
* @link www.phraseanet.com
|
||||
*/
|
||||
class Feed_Adapter extends Feed_Abstract implements Feed_Interface, cache_cacheableInterface
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $id;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var collection
|
||||
*/
|
||||
protected $collection;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $publishers;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $public;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var Feed_Publisher_Adapter
|
||||
*/
|
||||
protected $owner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $icon_url;
|
||||
|
||||
const CACHE_ENTRY_NUMBER = 'entrynumber';
|
||||
const CACHE_USER_TOKEN = 'usr_token';
|
||||
const MAX_ENTRIES = 20;
|
||||
|
||||
public function __construct(Application $app, $id)
|
||||
{
|
||||
$this->app = $app;
|
||||
$this->id = (int) $id;
|
||||
|
||||
$this->load();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function load()
|
||||
{
|
||||
try {
|
||||
$datas = $this->get_data_from_cache();
|
||||
|
||||
$this->title = $datas['title'];
|
||||
$this->subtitle = $datas['subtitle'];
|
||||
$this->collection = $datas['base_id'] ? collection::get_from_base_id($this->app, $datas['base_id']) : null;
|
||||
$this->created_on = $datas['created_on'];
|
||||
$this->updated_on = $datas['updated_on'];
|
||||
$this->public = $datas['public'];
|
||||
|
||||
return $this;
|
||||
} catch (\Exception $e) {
|
||||
|
||||
}
|
||||
|
||||
$sql = 'SELECT id, title, subtitle, created_on, updated_on, base_id, public
|
||||
FROM feeds WHERE id = :feed_id';
|
||||
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
|
||||
$stmt->execute(array(':feed_id' => $this->id));
|
||||
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
$stmt->closeCursor();
|
||||
|
||||
if (!$row)
|
||||
throw new NotFoundHttpException('Feed not found');
|
||||
|
||||
$this->title = $row['title'];
|
||||
$this->subtitle = $row['subtitle'];
|
||||
if (!is_null($row['base_id']))
|
||||
$this->collection = collection::get_from_base_id($this->app, $row['base_id']);
|
||||
$this->created_on = new DateTime($row['created_on']);
|
||||
$this->updated_on = new DateTime($row['updated_on']);
|
||||
$this->public = !!$row['public'];
|
||||
|
||||
$base_id = $this->collection instanceof collection ? $this->collection->get_base_id() : null;
|
||||
|
||||
$datas = array(
|
||||
'title' => $this->title
|
||||
, 'subtitle' => $this->subtitle
|
||||
, 'base_id' => $base_id
|
||||
, 'created_on' => $this->created_on
|
||||
, 'updated_on' => $this->updated_on
|
||||
, 'public' => $this->public
|
||||
);
|
||||
|
||||
$this->set_data_to_cache($datas);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_icon_url()
|
||||
{
|
||||
if ($this->icon_url) {
|
||||
return $this->icon_url;
|
||||
}
|
||||
|
||||
$url = '/skins/icons/rss32.gif';
|
||||
|
||||
$file = $this->app['root.path']
|
||||
. '/www/custom/feed_' . $this->get_id() . '.jpg';
|
||||
|
||||
if (file_exists($file)) {
|
||||
$url = '/custom/feed_' . $this->get_id() . '.jpg';
|
||||
}
|
||||
|
||||
$this->icon_url = $url;
|
||||
|
||||
return $this->icon_url;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $file The path to the file
|
||||
* @return Feed_Adapter
|
||||
*/
|
||||
public function set_icon($file)
|
||||
{
|
||||
if (!file_exists($file)) {
|
||||
throw new \Alchemy\Phrasea\Exception\InvalidArgumentException('File does not exists');
|
||||
}
|
||||
|
||||
$config_file = $this->app['root.path'] . '/config/feed_' . $this->get_id() . '.jpg';
|
||||
$www_file = $this->app['root.path'] . '/www/custom/feed_' . $this->get_id() . '.jpg';
|
||||
|
||||
copy($file, $config_file);
|
||||
copy($file, $www_file);
|
||||
$this->icon_url = null;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function set_created_on(DateTime $created_on)
|
||||
{
|
||||
$sql = 'UPDATE feeds SET created_on = :created_on
|
||||
WHERE id = :feed_id';
|
||||
$params = array(
|
||||
':created_on' => $created_on->format(DATE_ISO8601)
|
||||
, ':feed_id' => $this->get_id()
|
||||
);
|
||||
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
$stmt->closeCursor();
|
||||
$this->created_on = $created_on;
|
||||
$this->delete_data_from_cache();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function reset_icon()
|
||||
{
|
||||
$config_file = $this->app['root.path']
|
||||
. '/config/feed_' . $this->get_id() . '.jpg';
|
||||
$www_file = $this->app['root.path']
|
||||
. '/www/custom/feed_' . $this->get_id() . '.jpg';
|
||||
|
||||
if (is_file($config_file))
|
||||
unlink($config_file);
|
||||
if (is_file($www_file))
|
||||
unlink($www_file);
|
||||
|
||||
$this->icon_url = null;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function is_aggregated()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param User_Adapter $user
|
||||
* @return boolean
|
||||
*/
|
||||
public function is_owner(User_Adapter $user)
|
||||
{
|
||||
$this->load_publishers();
|
||||
|
||||
if (!$this->owner) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->owner->get_user()->get_id() === $user->get_id();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param User_Adapter $user
|
||||
* @return boolean
|
||||
*/
|
||||
public function is_publisher(User_Adapter $user)
|
||||
{
|
||||
return in_array($user->get_id(), array_keys($this->get_publishers()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells if a user has access to the feed
|
||||
*
|
||||
* @param User_Adapter $user
|
||||
* @return type
|
||||
*/
|
||||
public function has_access(User_Adapter $user)
|
||||
{
|
||||
if ($this->get_collection() instanceof collection) {
|
||||
return $user->ACL()->has_access_to_base($this->collection->get_base_id());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function is_public()
|
||||
{
|
||||
if ($this->get_collection() instanceof collection) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->public;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_publishers()
|
||||
{
|
||||
return $this->load_publishers();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return collection
|
||||
*/
|
||||
public function get_collection()
|
||||
{
|
||||
return $this->collection;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param User_Adapter $user
|
||||
* @return Feed_Adapter
|
||||
*/
|
||||
public function add_publisher(User_Adapter $user)
|
||||
{
|
||||
if (in_array($user->get_id(), array_keys($this->get_publishers()))) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
Feed_Publisher_Adapter::create($this->app, $user, $this, false);
|
||||
$this->publishers = null;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function load_publishers()
|
||||
{
|
||||
if (is_array($this->publishers)) {
|
||||
return $this->publishers;
|
||||
}
|
||||
|
||||
$sql = 'SELECT id, usr_id, owner FROM feed_publishers
|
||||
WHERE feed_id = :feed_id';
|
||||
|
||||
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
|
||||
$stmt->execute(array(':feed_id' => $this->id));
|
||||
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
$stmt->closeCursor();
|
||||
|
||||
foreach ($rs as $row) {
|
||||
try {
|
||||
$publisher = new Feed_Publisher_Adapter($this->app, $row['id']);
|
||||
} catch (\Exception_Feed_PublisherNotFound $e) {
|
||||
continue;
|
||||
}
|
||||
$this->publishers[$row['usr_id']] = $publisher;
|
||||
if ($publisher->is_owner()) {
|
||||
$this->owner = $publisher;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->publishers;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function get_id()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param collection $collection
|
||||
* @return Feed_Adapter
|
||||
*/
|
||||
public function set_collection(collection $collection = null)
|
||||
{
|
||||
$base_id = null;
|
||||
if ($collection instanceof collection) {
|
||||
$base_id = $collection->get_base_id();
|
||||
}
|
||||
|
||||
$sql = 'UPDATE feeds SET base_id = :base_id, updated_on = NOW()
|
||||
WHERE id = :feed_id';
|
||||
$params = array(':base_id' => $base_id, ':feed_id' => $this->get_id());
|
||||
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
$stmt->closeCursor();
|
||||
$this->collection = $collection;
|
||||
$this->delete_data_from_cache();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param boolean $boolean
|
||||
* @return Feed_Adapter
|
||||
*/
|
||||
public function set_public($boolean)
|
||||
{
|
||||
$boolean = !!$boolean;
|
||||
$sql = 'UPDATE feeds SET public = :public, updated_on = NOW()
|
||||
WHERE id = :feed_id';
|
||||
|
||||
$params = array(
|
||||
':public' => $boolean ? '1' : '0',
|
||||
':feed_id' => $this->get_id()
|
||||
);
|
||||
|
||||
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
$stmt->closeCursor();
|
||||
$this->public = $boolean;
|
||||
$this->delete_data_from_cache();
|
||||
|
||||
$feed_collection = new Feed_Collection($this->app, array());
|
||||
$feed_collection->delete_data_from_cache(Feed_Collection::CACHE_PUBLIC);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $title
|
||||
* @return Feed_Adapter
|
||||
*/
|
||||
public function set_title($title)
|
||||
{
|
||||
$title = trim(strip_tags($title));
|
||||
|
||||
if ($title === '')
|
||||
throw new Exception_InvalidArgument();
|
||||
|
||||
$sql = 'UPDATE feeds SET title = :title, updated_on = NOW()
|
||||
WHERE id = :feed_id';
|
||||
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
|
||||
$stmt->execute(array(':title' => $title, ':feed_id' => $this->get_id()));
|
||||
$stmt->closeCursor();
|
||||
$this->title = $title;
|
||||
$this->delete_data_from_cache();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $subtitle
|
||||
* @return Feed_Adapter
|
||||
*/
|
||||
public function set_subtitle($subtitle)
|
||||
{
|
||||
$subtitle = strip_tags($subtitle);
|
||||
|
||||
$sql = 'UPDATE feeds SET subtitle = :subtitle, updated_on = NOW()
|
||||
WHERE id = :feed_id';
|
||||
$params = array(':subtitle' => $subtitle, ':feed_id' => $this->get_id());
|
||||
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
$stmt->closeCursor();
|
||||
$this->subtitle = $subtitle;
|
||||
$this->delete_data_from_cache();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public static function create(Application $app, User_Adapter $user, $title, $subtitle)
|
||||
{
|
||||
$sql = 'INSERT INTO feeds (id, title, subtitle, created_on, updated_on)
|
||||
VALUES (null, :title, :subtitle, NOW(), NOW())';
|
||||
$stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql);
|
||||
$stmt->execute(array(':title' => $title, ':subtitle' => $subtitle));
|
||||
$stmt->closeCursor();
|
||||
|
||||
$feed_id = $app['phraseanet.appbox']->get_connection()->lastInsertId();
|
||||
|
||||
$feed = new self($app, $feed_id);
|
||||
|
||||
Feed_Publisher_Adapter::create($app, $user, $feed, true);
|
||||
|
||||
return $feed;
|
||||
}
|
||||
|
||||
public static function load_with_user(Application $app, User_Adapter $user, $id)
|
||||
{
|
||||
$feed = new self($app, $id);
|
||||
$coll = $feed->get_collection();
|
||||
if (
|
||||
$feed->is_public()
|
||||
|| $coll === null
|
||||
|| in_array($coll->get_base_id(), array_keys($user->ACL()->get_granted_base()))
|
||||
) {
|
||||
return $feed;
|
||||
}
|
||||
|
||||
throw new NotFoundHttpException('Feed not found');
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function get_count_total_entries()
|
||||
{
|
||||
try {
|
||||
return $this->get_data_from_cache(self::CACHE_ENTRY_NUMBER);
|
||||
} catch (\Exception $e) {
|
||||
|
||||
}
|
||||
|
||||
$sql = 'SELECT count(id) as number
|
||||
FROM feed_entries WHERE feed_id = :feed_id';
|
||||
|
||||
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
|
||||
$stmt->execute(array(':feed_id' => $this->get_id()));
|
||||
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
$number = $row ? (int) $row['number'] : 0;
|
||||
$stmt->closeCursor();
|
||||
|
||||
$this->set_data_to_cache($number, self::CACHE_ENTRY_NUMBER);
|
||||
|
||||
return $number;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function delete()
|
||||
{
|
||||
$this->reset_icon();
|
||||
while ($this->get_count_total_entries() > 0) {
|
||||
$entries_coll = $this->get_entries(0, 10);
|
||||
foreach ($entries_coll->get_entries() as $entry) {
|
||||
$entry->delete();
|
||||
}
|
||||
unset($entries_coll);
|
||||
$this->delete_data_from_cache(self::CACHE_ENTRY_NUMBER);
|
||||
}
|
||||
|
||||
foreach ($this->get_publishers() as $publishers)
|
||||
$publishers->delete();
|
||||
|
||||
$sql = 'DELETE FROM feed_tokens WHERE feed_id = :feed_id';
|
||||
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
|
||||
$stmt->execute(array(':feed_id' => $this->get_id()));
|
||||
$stmt->closeCursor();
|
||||
|
||||
$sql = 'DELETE FROM feeds WHERE id = :feed_id';
|
||||
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
|
||||
$stmt->execute(array(':feed_id' => $this->get_id()));
|
||||
$stmt->closeCursor();
|
||||
|
||||
$this->delete_data_from_cache();
|
||||
|
||||
$feed_coll = new Feed_Collection($this->app, array());
|
||||
$feed_coll->delete_data_from_cache(Feed_Collection::CACHE_PUBLIC);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param int $offset_start
|
||||
* @param int $how_many
|
||||
* @return Feed_Entry_Collection
|
||||
*/
|
||||
public function get_entries($offset_start, $how_many)
|
||||
{
|
||||
$offset_start = (int) $offset_start;
|
||||
$how_many = $how_many > self::MAX_ENTRIES ? self::MAX_ENTRIES : (int) $how_many;
|
||||
|
||||
$sql = 'SELECT id
|
||||
FROM feed_entries
|
||||
WHERE feed_id = :feed_id
|
||||
ORDER BY id DESC
|
||||
LIMIT ' . $offset_start . ', ' . $how_many;
|
||||
|
||||
$params = array(
|
||||
':feed_id' => $this->get_id()
|
||||
);
|
||||
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
$stmt->closeCursor();
|
||||
|
||||
$result = new Feed_Entry_Collection();
|
||||
|
||||
foreach ($rs as $row) {
|
||||
$entry = new Feed_Entry_Adapter($this->app, $this, $row['id']);
|
||||
$result->add_entry($entry);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param registryInterface $registry
|
||||
* @param string $format
|
||||
* @param int $page
|
||||
* @return Feed_Link
|
||||
*/
|
||||
public function get_homepage_link(registryInterface $registry, $format, $page = null)
|
||||
{
|
||||
if (!$this->is_public()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
switch ($format) {
|
||||
case self::FORMAT_ATOM:
|
||||
return new Feed_Link(
|
||||
sprintf('%s/feeds/feed/%s/atom/%s'
|
||||
, rtrim($registry->get('GV_ServerName'), '/')
|
||||
, $this->get_id()
|
||||
, ($page ? '?page=' . $page : '')
|
||||
)
|
||||
, sprintf('%s - %s', $this->get_title(), 'Atom')
|
||||
, 'application/atom+xml'
|
||||
);
|
||||
break;
|
||||
case self::FORMAT_RSS:
|
||||
default:
|
||||
return new Feed_Link(
|
||||
sprintf('%s/feeds/feed/%s/rss/%s'
|
||||
, rtrim($registry->get('GV_ServerName'), '/')
|
||||
, $this->get_id()
|
||||
, ($page ? '?page=' . $page : '')
|
||||
)
|
||||
, sprintf('%s - %s', $this->get_title(), 'RSS')
|
||||
, 'application/rss+xml'
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param User_Adapter $user
|
||||
* @param boolean $renew
|
||||
* @return string
|
||||
*/
|
||||
protected function get_token(User_Adapter $user, $renew = false)
|
||||
{
|
||||
$cache_key = self::CACHE_USER_TOKEN . '_' . $user->get_id();
|
||||
try {
|
||||
if (!$renew) {
|
||||
return $this->get_data_from_cache($cache_key);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
|
||||
}
|
||||
|
||||
$sql = 'SELECT token FROM feed_tokens
|
||||
WHERE usr_id = :usr_id AND feed_id = :feed_id
|
||||
AND aggregated IS NULL';
|
||||
|
||||
$params = array(
|
||||
':usr_id' => $user->get_id(),
|
||||
':feed_id' => $this->get_id()
|
||||
);
|
||||
|
||||
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
$stmt->closeCursor();
|
||||
|
||||
if (!$row || $renew === true) {
|
||||
$token = random::generatePassword(12, random::LETTERS_AND_NUMBERS);
|
||||
$sql = 'REPLACE INTO feed_tokens (id, token, feed_id, usr_id, aggregated)
|
||||
VALUES (null, :token, :feed_id, :usr_id, :aggregated)';
|
||||
|
||||
$params = array(
|
||||
':token' => $token
|
||||
, ':feed_id' => $this->get_id()
|
||||
, ':usr_id' => $user->get_id()
|
||||
, ':aggregated' => null
|
||||
);
|
||||
|
||||
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
$this->delete_data_from_cache($cache_key);
|
||||
} else {
|
||||
$token = $row['token'];
|
||||
}
|
||||
|
||||
$this->set_data_to_cache($token, $cache_key);
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param registryInterface $registry
|
||||
* @param User_Adapter $user
|
||||
* @param string $format
|
||||
* @param int $page
|
||||
* @param boolean $renew_token
|
||||
* @return Feed_Link
|
||||
*/
|
||||
public function get_user_link(registryInterface $registry, User_Adapter $user, $format, $page = null, $renew_token = false)
|
||||
{
|
||||
switch ($format) {
|
||||
case self::FORMAT_ATOM:
|
||||
return new Feed_Link(
|
||||
sprintf('%s/feeds/userfeed/%s/%s/atom/'
|
||||
, rtrim($registry->get('GV_ServerName'), '/')
|
||||
, $this->get_token($user, $renew_token)
|
||||
, $this->get_id()
|
||||
, ($page ? '?page=' . $page : '')
|
||||
)
|
||||
, sprintf('%s - %s', $this->get_title(), 'Atom')
|
||||
, 'application/atom+xml'
|
||||
);
|
||||
break;
|
||||
case self::FORMAT_RSS:
|
||||
return new Feed_Link(
|
||||
sprintf('%s/feeds/userfeed/%s/%s/rss/%s'
|
||||
, rtrim($registry->get('GV_ServerName'), '/')
|
||||
, $this->get_token($user, $renew_token)
|
||||
, $this->get_id()
|
||||
, ($page ? '?page=' . $page : '')
|
||||
)
|
||||
, sprintf('%s - %s', $this->get_title(), 'RSS')
|
||||
, 'application/rss+xml'
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public function get_cache_key($option = null)
|
||||
{
|
||||
return 'feed_adapter_' . $this->get_id() . '_' . ($option ? '_' . $option : '');
|
||||
}
|
||||
|
||||
public function get_data_from_cache($option = null)
|
||||
{
|
||||
return $this->app['phraseanet.appbox']->get_data_from_cache($this->get_cache_key($option));
|
||||
}
|
||||
|
||||
public function set_data_to_cache($value, $option = null, $duration = 0)
|
||||
{
|
||||
return $this->app['phraseanet.appbox']->set_data_to_cache($value, $this->get_cache_key($option), $duration);
|
||||
}
|
||||
|
||||
public function delete_data_from_cache($option = null)
|
||||
{
|
||||
return $this->app['phraseanet.appbox']->delete_data_from_cache($this->get_cache_key($option));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user