Version 1
[yaffs-website] / web / core / modules / history / src / Controller / HistoryController.php
1 <?php
2
3 namespace Drupal\history\Controller;
4
5 use Symfony\Component\HttpFoundation\Request;
6 use Symfony\Component\HttpFoundation\JsonResponse;
7 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
8 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9 use Drupal\Core\Controller\ControllerBase;
10 use Drupal\node\NodeInterface;
11
12 /**
13  * Returns responses for History module routes.
14  */
15 class HistoryController extends ControllerBase {
16
17   /**
18    * Returns a set of nodes' last read timestamps.
19    *
20    * @param \Symfony\Component\HttpFoundation\Request $request
21    *   The request of the page.
22    *
23    * @return Symfony\Component\HttpFoundation\JsonResponse
24    *   The JSON response.
25    */
26   public function getNodeReadTimestamps(Request $request) {
27     if ($this->currentUser()->isAnonymous()) {
28       throw new AccessDeniedHttpException();
29     }
30
31     $nids = $request->request->get('node_ids');
32     if (!isset($nids)) {
33       throw new NotFoundHttpException();
34     }
35     return new JsonResponse(history_read_multiple($nids));
36   }
37
38   /**
39    * Marks a node as read by the current user right now.
40    *
41    * @param \Symfony\Component\HttpFoundation\Request $request
42    *   The request of the page.
43    * @param \Drupal\node\NodeInterface $node
44    *   The node whose "last read" timestamp should be updated.
45    */
46   public function readNode(Request $request, NodeInterface $node) {
47     if ($this->currentUser()->isAnonymous()) {
48       throw new AccessDeniedHttpException();
49     }
50
51     // Update the history table, stating that this user viewed this node.
52     history_write($node->id());
53
54     return new JsonResponse((int)history_read($node->id()));
55   }
56
57 }