require dirname(__FILE__) . '/includes/bootstrap.php';
//Getting Current User ID
if (!($userID = buckys_is_logged_in())) {
    buckys_redirect('/index.php', MSG_NOT_LOGGED_IN_USER, MSG_TYPE_ERROR);
}
$albumID = isset($_REQUEST['albumID']) ? $_REQUEST['albumID'] : '';
if (!$albumID || !BuckysAlbum::checkAlbumOwner($albumID, $userID)) {
    buckys_redirect("/photo_albums.php", MSG_INVALID_REQUEST, MSG_TYPE_ERROR);
}
//Getting UserData from Id
$userData = BuckysUser::getUserData($profileID);
//Getting Album
$album = BuckysAlbum::getAlbum($albumID);
//Getting Photos
$myphotos = BuckysPost::getPhotosByUserID($userID, $userID, BuckysPost::INDEPENDENT_POST_PAGE_ID, true);
$albumPhotos = BuckysAlbum::getPhotos($albumID);
//Getting Album Photos
if (isset($_POST['action'])) {
    //Create New Album
    if ($_POST['action'] == 'save-album') {
        //If the album title is empty, throw error
        //If the album title is empty, throw error
        if (trim($_POST['album_name']) == '') {
            buckys_redirect('/photo_album_edit.php?albumID=' . $_POST['albumID'], MSG_ALBUM_TITLE_EMPTY, MSG_TYPE_ERROR);
        }
        BuckysAlbum::updateAlbum($_POST['albumID'], trim($_POST['album_name']), $_POST['visibility'], $_POST['photos']);
        buckys_redirect("/photo_album_edit.php?albumID=" . $_POST['albumID'], MSG_ALBUM_UPDATED);
    } else {
        if ($_POST['action'] == 'remove-from-album' || $_POST['action'] == 'add-to-album') {
            $photoID = $_POST['photoID'];
            $photo = BuckysPost::getPostById($photoID);
 /**
  * Get photos
  *
  * @param int $userID
  * @param int $loggedUserID
  * @param int $pageID
  * @param boolean $canViewPrivate
  * @param int $postID
  * @param int $albumID
  * @param int $limit
  * @param string $lastDate
  * @return Indexed
  */
 public static function getPhotosByUserID($userID, $loggedUserID = null, $pageID = BuckysPost::INDEPENDENT_POST_PAGE_ID, $canViewPrivate = false, $postID = null, $albumID = null, $limit = null, $lastDate = null)
 {
     global $db;
     $userID = intval($userID);
     //Getting Page Parameter
     if (isset($_GET['page']) && buckys_not_null($_GET['page'])) {
         $page = intval($_GET['page']);
     } else {
         $page = 1;
     }
     //Page Limit Query
     if ($limit) {
         $limit_query = ' LIMIT ' . ($page - 1) * $limit . ", " . $limit;
     }
     if (buckys_not_null($loggedUserID) && $canViewPrivate) {
         //Get All posts that were posted by $userID
         $query = 'SELECT p.*, CONCAT(u.firstName, " ", u.lastName) AS posterFullName, pl.likeID, pa.album_id FROM ' . TABLE_POSTS . ' AS p
                             LEFT JOIN ' . TABLE_USERS . ' AS u ON p.poster = u.userID
                             LEFT JOIN ' . TABLE_ALBUMS_PHOTOS . ' AS pa ON pa.post_id = p.postID
                             LEFT JOIN ' . TABLE_POSTS_LIKES . ' AS pl ON pl.postID = p.postID AND pl.userID = ' . $userID . '
                             WHERE p.poster= ' . $userID . ' AND p.pageID=' . $pageID;
     } else {
         //Get Only Public Posts
         $query = 'SELECT p.*, CONCAT(u.firstName, " ", u.lastName) AS posterFullName, pl.likeID, pa.album_id FROM ' . TABLE_POSTS . ' AS p
                             LEFT JOIN ' . TABLE_USERS . ' AS u ON p.poster = u.userID
                             LEFT JOIN ' . TABLE_ALBUMS_PHOTOS . ' AS pa ON pa.post_id = p.postID
                             LEFT JOIN ' . TABLE_POSTS_LIKES . ' AS pl ON pl.postID = p.postID AND pl.userID = ' . $userID . '
                             WHERE p.poster= ' . $userID . ' AND p.visibility=1 ' . ' AND p.pageID=' . $pageID;
     }
     $query .= ' AND p.type="image" ';
     //If postID is set, get only one post
     if ($postID != null) {
         $query .= $db->prepare(' AND p.postID=%d', $postID);
     }
     //AlbumID Query
     if ($albumID != null) {
         $aPhotos = BuckysAlbum::getPhotos($albumID);
         $apIds = [0];
         foreach ($aPhotos as $a) {
             $apIds[] = $a['postID'];
         }
         $query .= ' AND p.postID in (' . implode(', ', $apIds) . ')';
     }
     if ($lastDate != null) {
         $lastDate = date('Y-m-d H:i:s', strtotime($lastDate));
         $query .= ' AND p.post_date < "' . $lastDate . '"';
     }
     $query .= ' ORDER BY p.post_date DESC ' . $limit_query;
     $rows = $db->getResultsArray($query);
     return $rows;
 }