/**
  * Allows user to bookmark or unbookmark a discussion.
  *
  * If the discussion isn't bookmarked by the user, this bookmarks it.
  * If it is already bookmarked, this unbookmarks it.
  *
  * @since 2.0.0
  * @access public
  *
  * @param int $DiscussionID Unique discussion ID.
  */
 public function Bookmark($DiscussionID)
 {
     // Make sure we are posting back.
     if (!$this->Request->IsAuthenticatedPostBack()) {
         throw PermissionException('Javascript');
     }
     $Session = Gdn::Session();
     if ($Session->UserID == 0) {
         throw PermissionException('SignedIn');
     }
     $Discussion = $this->DiscussionModel->GetID($DiscussionID);
     if (!$Discussion) {
         throw NotFoundException('Discussion');
     }
     $State = $this->DiscussionModel->BookmarkDiscussion($DiscussionID, $Session->UserID, $Discussion);
     // Update the user's bookmark count
     $CountBookmarks = $this->DiscussionModel->SetUserBookmarkCount($Session->UserID);
     $this->JsonTarget('.User-CountBookmarks', (string) $CountBookmarks);
     // Return the appropriate bookmark.
     require_once $this->FetchViewLocation('helper_functions', 'Discussions');
     $Html = BookmarkButton($Discussion);
     //      $this->JsonTarget(".Section-DiscussionList #Discussion_$DiscussionID .Bookmark,.Section-Discussion .PageTitle .Bookmark", $Html, 'ReplaceWith');
     $this->JsonTarget("!element", $Html, 'ReplaceWith');
     // Add the bookmark to the bookmarks module.
     if ($State) {
         // Grab the individual bookmark and send it to the client.
         $Bookmarks = new BookmarkedModule($this);
         if ($CountBookmarks == 1) {
             // When there is only one bookmark we have to get the whole module.
             $Target = '#Panel';
             $Type = 'Append';
             $Bookmarks->GetData();
             $Data = $Bookmarks->ToString();
         } else {
             $Target = '#Bookmark_List';
             $Type = 'Prepend';
             $Loc = $Bookmarks->FetchViewLocation('discussion');
             ob_start();
             include $Loc;
             $Data = ob_get_clean();
         }
         $this->JsonTarget($Target, $Data, $Type);
     } else {
         // Send command to remove bookmark html.
         if ($CountBookmarks == 0) {
             $this->JsonTarget('#Bookmarks', NULL, 'Remove');
         } else {
             $this->JsonTarget('#Bookmark_' . $DiscussionID, NULL, 'Remove');
         }
     }
     $this->Render('Blank', 'Utility', 'Dashboard');
 }
 /**
  * Allows user to bookmark or unbookmark a discussion.
  *
  * If the discussion isn't bookmarked by the user, this bookmarks it.
  * If it is already bookmarked, this unbookmarks it.
  * 
  * @since 2.0.0
  * @access public
  *
  * @param int $DiscussionID Unique discussion ID.
  * @param string $TransientKey Single-use hash to prove intent.
  */
 public function Bookmark($DiscussionID = '', $TransientKey = '')
 {
     $Session = Gdn::Session();
     $State = FALSE;
     if (is_numeric($DiscussionID) && $DiscussionID > 0 && $Session->UserID > 0 && $Session->ValidateTransientKey($TransientKey)) {
         $Discussion = NULL;
     }
     $State = $this->DiscussionModel->BookmarkDiscussion($DiscussionID, $Session->UserID, $Discussion);
     // Update the user's bookmark count
     $CountBookmarks = $this->DiscussionModel->SetUserBookmarkCount($Session->UserID);
     // Redirect back where the user came from if necessary
     if ($this->_DeliveryType != DELIVERY_TYPE_BOOL) {
         $Target = GetIncomingValue('Target', 'discussions/bookmarked');
         Redirect($Target);
     }
     $this->SetJson('State', $State);
     $this->SetJson('CountBookmarks', $CountBookmarks);
     $this->SetJson('CountDiscussionBookmarks', GetValue('CountDiscussionBookmarks', $this->DiscussionModel));
     $this->SetJson('ButtonLink', T($State ? 'Unbookmark this Discussion' : 'Bookmark this Discussion'));
     $this->SetJson('AnchorTitle', T($State ? 'Unbookmark' : 'Bookmark'));
     $this->SetJson('MenuText', T('My Bookmarks'));
     $Targets = array();
     if ($State) {
         // Grab the individual bookmark and send it to the client.
         $Bookmarks = new BookmarkedModule($this);
         if ($CountBookmarks == 1) {
             // When there is only one bookmark we have to get the whole module.
             $Target = '#Panel';
             $Type = 'Append';
             $Bookmarks->GetData();
             $Data = $Bookmarks->ToString();
         } else {
             $Target = '#Bookmark_List';
             $Type = 'Prepend';
             $Loc = $Bookmarks->FetchViewLocation('discussion');
             ob_start();
             include $Loc;
             $Data = ob_get_clean();
         }
         $Targets[] = array('Target' => $Target, 'Type' => $Type, 'Data' => $Data);
     } else {
         // Send command to remove bookmark html.
         if ($CountBookmarks == 0) {
             $Targets[] = array('Target' => '#Bookmarks', 'Type' => 'Remove');
         } else {
             $Targets[] = array('Target' => '#Bookmark_' . $DiscussionID, 'Type' => 'Remove');
         }
     }
     $this->SetJson('Targets', $Targets);
     $this->Render();
 }
 /**
  * Allows user to bookmark or unbookmark a discussion.
  *
  * If the discussion isn't bookmarked by the user, this bookmarks it.
  * If it is already bookmarked, this unbookmarks it.
  *
  * @since 2.0.0
  * @access public
  *
  * @param int $DiscussionID Unique discussion ID.
  */
 public function Bookmark($DiscussionID = NULL)
 {
     // Make sure we are posting back.
     if (!$this->Request->IsPostBack()) {
         throw PermissionException('Javascript');
     }
     $Session = Gdn::Session();
     if (!$Session->UserID) {
         throw PermissionException('SignedIn');
     }
     // Check the form to see if the data was posted.
     $Form = new Gdn_Form();
     $DiscussionID = $Form->GetFormValue('DiscussionID', $DiscussionID);
     $Bookmark = $Form->GetFormValue('Bookmark', NULL);
     $UserID = $Form->GetFormValue('UserID', $Session->UserID);
     // Check the permission on the user.
     if ($UserID != $Session->UserID) {
         $this->Permission('Garden.Moderation.Manage');
     }
     $Discussion = $this->DiscussionModel->GetID($DiscussionID);
     if (!$Discussion) {
         throw NotFoundException('Discussion');
     }
     $Bookmark = $this->DiscussionModel->Bookmark($DiscussionID, $UserID, $Bookmark);
     // Set the new value for api calls and json targets.
     $this->SetData(array('UserID' => $UserID, 'DiscussionID' => $DiscussionID, 'Bookmarked' => (bool) $Bookmark));
     SetValue('Bookmarked', $Discussion, (int) $Bookmark);
     // Update the user's bookmark count
     $CountBookmarks = $this->DiscussionModel->SetUserBookmarkCount($UserID);
     $this->JsonTarget('.User-CountBookmarks', (string) $CountBookmarks);
     //  Short circuit if this is an api call.
     if ($this->DeliveryType() === DELIVERY_TYPE_DATA) {
         $this->Render('Blank', 'Utility', 'Dashboard');
         return;
     }
     // Return the appropriate bookmark.
     require_once $this->FetchViewLocation('helper_functions', 'Discussions');
     $Html = BookmarkButton($Discussion);
     //      $this->JsonTarget(".Section-DiscussionList #Discussion_$DiscussionID .Bookmark,.Section-Discussion .PageTitle .Bookmark", $Html, 'ReplaceWith');
     $this->JsonTarget("!element", $Html, 'ReplaceWith');
     // Add the bookmark to the bookmarks module.
     if ($Bookmark) {
         // Grab the individual bookmark and send it to the client.
         $Bookmarks = new BookmarkedModule($this);
         if ($CountBookmarks == 1) {
             // When there is only one bookmark we have to get the whole module.
             $Target = '#Panel';
             $Type = 'Append';
             $Bookmarks->GetData();
             $Data = $Bookmarks->ToString();
         } else {
             $Target = '#Bookmark_List';
             $Type = 'Prepend';
             $Loc = $Bookmarks->FetchViewLocation('discussion');
             ob_start();
             include $Loc;
             $Data = ob_get_clean();
         }
         $this->JsonTarget($Target, $Data, $Type);
     } else {
         // Send command to remove bookmark html.
         if ($CountBookmarks == 0) {
             $this->JsonTarget('#Bookmarks', NULL, 'Remove');
         } else {
             $this->JsonTarget('#Bookmark_' . $DiscussionID, NULL, 'Remove');
         }
     }
     $this->Render('Blank', 'Utility', 'Dashboard');
 }