From 235ce5716edc2f7cbe80fdccf26eac7269587839 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Mon, 08 Jun 2026 04:38:18 +0000
Subject: [PATCH] =FavouritesManager.php and FavouritesRoutes.php fixes. Moving all logic to FavouritesManager.php. Still some left to do

---
 inc/managers/FavouritesManager.php |  109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 109 insertions(+), 0 deletions(-)

diff --git a/inc/managers/FavouritesManager.php b/inc/managers/FavouritesManager.php
index 0ea911c..125c75a 100644
--- a/inc/managers/FavouritesManager.php
+++ b/inc/managers/FavouritesManager.php
@@ -461,6 +461,115 @@
 				'status'	=> $accept ? 'accepted' : 'rejected',
 			], $args);
 		}
+
+		public function getFavourites(array $args = []):array
+		{
+			return $this->favourites->getMany($args, false);
+		}
+		public function getLists(array $args = []):array
+		{
+			return $this->lists->getMany($args, false);
+		}
+			public function getAvailableLists(array $args = [], bool $include_shared = true):array
+			{
+				$lists = [];
+				$owned = $this->lists->getMany($args, false);
+				foreach ($owned['items'] as &$list) {
+					$list['item_count'] = $this->listItems->count(['list_id' => $list['id']]);
+				}
+
+				$lists['owned'] = $owned;
+
+				if ($include_shared) {
+					$args['where']['status'] = 'accepted';
+					$sharedLists = $this->listShares->getMany($args);
+					$shared = [];
+
+					foreach ($sharedLists as $share) {
+						$sharedList = $this->lists->get(['id' => $share->list_id]);
+						if ($sharedList) {
+							$sharedList['owner_name'] = jvbGetUsername($sharedList['user_id']);
+							$sharedList['item_count'] = $this->listItems->count(['list_id' => $sharedList['id']]);
+							$sharedList['permission_type'] = $sharedList->permission_type;
+							$shared[] = $sharedList;
+						}
+					}
+					$lists['shared'] = $shared;
+				}
+				return $lists;
+			}
+
+			public function userOwnsList(int $list_id, int $user_id):bool
+			{
+				return $this->lists->count(['id' => $list_id, 'user_id' => $user_id]) > 0;
+			}
+
+			public function getListDetails(int $list_id, int $user_id, int $page = 1):array
+			{
+				$list = JVB()->favourites()->getLists(['id' => $list_id, 'user_id' => $user_id]);
+				if (!$list) {
+					return [];
+				}
+				$items = $this->listItems->getMany([
+					'where' 	=> ['list_id' => $list_id],
+					'order_by'	=> 'added_at',
+					'order'		=> 'DESC',
+					'per_page'	=> 25,
+					'page'		=> $page,
+				]);
+
+				$formatted = [];
+
+				foreach($items as $item) {
+					//See if we can get the actual favourite record first
+					if ($item->favourite_id) {
+						$fav = $this->favourites->get(['id' => $item->favourite_id]);
+						if ($fav) {
+							$formatted[] = $fav;
+							continue;
+						}
+					}
+
+					//Create a dummy favourite object otherwise
+					$formatted[] = (object) [
+						'type'		=> $item->item_type,
+						'target_id'	=> $item->item_id,
+						'created_at'=> $item->added_at
+					];
+				}
+
+				$sharedWith = [];
+				$is_owner = $this->userOwnsList($list_id, $user_id);
+				if ($is_owner) {
+					$shares = $this->listShares->getMany([
+						'where' => ['list_id' => $list_id],
+						'order_by' => 'created_at',
+						'order'	=> 'DESC'
+					]);
+					foreach ($shares as $share_item) {
+						$user = [
+							'email'		=> $share_item->email,
+							'status'	=> $share_item->status,
+							'date_added'=> $share_item->created_at,
+						];
+						if ($share_item->status === 'accepted' && $share_item->user_id) {
+							$user['name'] = jvbGetUsername($share_item->user_id);
+							$user['permission_type'] = $share_item->permission_type;
+						}
+						$sharedWith[] = $user;
+					}
+				}
+
+				return [
+					'id'	=> (int)$list['id'],
+					'name'	=> $list['name'],
+					'description'	=> $list['description']??'',
+					'created_at'	=> $list['created_at'],
+					'is_owner'		=> $is_owner,
+					'items'			=> $formatted,
+					'shared_users'	=> $sharedWith
+				];
+			}
 	/***************************************************************
 	 * UTILITY METHODS
 	 **************************************************************/

--
Gitblit v1.10.0