use Exception;
use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
+use Shaarli\Bookmark\Exception\DatastoreNotInitializedException;
use Shaarli\Bookmark\Exception\EmptyDataStoreException;
use Shaarli\Config\ConfigManager;
use Shaarli\Formatter\BookmarkMarkdownFormatter;
/** @var bool true for logged in users. Default value to retrieve private bookmarks. */
protected $isLoggedIn;
- /** @var bool Allow datastore alteration from not logged in users. */
- protected $anonymousPermission = false;
-
/**
* @inheritDoc
*/
} else {
try {
$this->bookmarks = $this->bookmarksIO->read();
- } catch (EmptyDataStoreException $e) {
+ } catch (EmptyDataStoreException|DatastoreNotInitializedException $e) {
$this->bookmarks = new BookmarkArray();
+
if ($this->isLoggedIn) {
- $this->save();
+ // Datastore file does not exists, we initialize it with default bookmarks.
+ if ($e instanceof DatastoreNotInitializedException) {
+ $this->initialize();
+ } else {
+ $this->save();
+ }
}
}
/**
* @inheritDoc
*/
- public function search($request = [], $visibility = null, $caseSensitive = false, $untaggedOnly = false)
- {
+ public function search(
+ $request = [],
+ $visibility = null,
+ $caseSensitive = false,
+ $untaggedOnly = false,
+ bool $ignoreSticky = false
+ ) {
if ($visibility === null) {
$visibility = $this->isLoggedIn ? BookmarkFilter::$ALL : BookmarkFilter::$PUBLIC;
}
$searchtags = isset($request['searchtags']) ? $request['searchtags'] : '';
$searchterm = isset($request['searchterm']) ? $request['searchterm'] : '';
+ if ($ignoreSticky) {
+ $this->bookmarks->reorder('DESC', true);
+ }
+
return $this->bookmarkFilter->filter(
BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT,
[$searchtags, $searchterm],
*/
public function set($bookmark, $save = true)
{
- if (true !== $this->isLoggedIn && true !== $this->anonymousPermission) {
+ if (true !== $this->isLoggedIn) {
throw new Exception(t('You\'re not authorized to alter the datastore'));
}
if (! $bookmark instanceof Bookmark) {
*/
public function add($bookmark, $save = true)
{
- if (true !== $this->isLoggedIn && true !== $this->anonymousPermission) {
+ if (true !== $this->isLoggedIn) {
throw new Exception(t('You\'re not authorized to alter the datastore'));
}
if (! $bookmark instanceof Bookmark) {
*/
public function addOrSet($bookmark, $save = true)
{
- if (true !== $this->isLoggedIn && true !== $this->anonymousPermission) {
+ if (true !== $this->isLoggedIn) {
throw new Exception(t('You\'re not authorized to alter the datastore'));
}
if (! $bookmark instanceof Bookmark) {
*/
public function remove($bookmark, $save = true)
{
- if (true !== $this->isLoggedIn && true !== $this->anonymousPermission) {
+ if (true !== $this->isLoggedIn) {
throw new Exception(t('You\'re not authorized to alter the datastore'));
}
if (! $bookmark instanceof Bookmark) {
*/
public function save()
{
- if (true !== $this->isLoggedIn && true !== $this->anonymousPermission) {
+ if (true !== $this->isLoggedIn) {
// TODO: raise an Exception instead
die('You are not authorized to change the database.');
}
{
$initializer = new BookmarkInitializer($this);
$initializer->initialize();
- }
- public function enableAnonymousPermission(): void
- {
- $this->anonymousPermission = true;
- }
-
- public function disableAnonymousPermission(): void
- {
- $this->anonymousPermission = false;
+ if (true === $this->isLoggedIn) {
+ $this->save();
+ }
}
/**