HeartbeatController.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. namespace Drupal\heartbeat\Controller;
  3. use Drupal\block\BlockViewBuilder;
  4. use Drupal\Component\Utility\Xss;
  5. use Drupal\Core\Ajax\AppendCommand;
  6. use Drupal\Core\Block\BlockManager;
  7. use Drupal\Core\Controller\ControllerBase;
  8. use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
  9. use Drupal\Core\Render\Element\Ajax;
  10. use Drupal\Core\Render\Renderer;
  11. use Drupal\Core\Routing\RoutingEvents;
  12. use Drupal\Core\Url;
  13. use Drupal\Core\Ajax\AjaxResponse;
  14. use Drupal\heartbeat\Ajax\SubCommentCommand;
  15. use Drupal\heartbeat\Entity\HeartbeatInterface;
  16. use Symfony\Component\DependencyInjection\ContainerInterface;
  17. /**
  18. * Class HeartbeatController.
  19. *
  20. * Returns responses for Heartbeat routes.
  21. *
  22. * @package Drupal\heartbeat\Controller
  23. */
  24. class HeartbeatController extends ControllerBase {
  25. private $renderer;
  26. private $blockManager;
  27. /**
  28. * @param ContainerInterface $container
  29. * @return static
  30. */
  31. public static function create(ContainerInterface $container) {
  32. return new static(
  33. $container->get('renderer'),
  34. $container->get('plugin.manager.block')
  35. );
  36. }
  37. /**
  38. * HeartbeatController constructor.
  39. * @param Renderer $renderer
  40. * @param BlockManager $block_manager
  41. */
  42. public function __construct(Renderer $renderer, BlockManager $block_manager) {
  43. $this->renderer = $renderer;
  44. $this->blockManager = $block_manager;
  45. }
  46. /**
  47. * Displays a Heartbeat revision.
  48. *
  49. * @param int $heartbeat_revision
  50. * The Heartbeat revision ID.
  51. *
  52. * @return array
  53. * An array suitable for drupal_render().
  54. */
  55. public function revisionShow($heartbeat_revision) {
  56. $heartbeat = $this->entityManager()->getStorage('heartbeat')->loadRevision($heartbeat_revision);
  57. $view_builder = $this->entityManager()->getViewBuilder('heartbeat');
  58. return $view_builder->view($heartbeat);
  59. }
  60. /**
  61. * Page title callback for a Heartbeat revision.
  62. *
  63. * @param int $heartbeat_revision
  64. * The Heartbeat revision ID.
  65. *
  66. * @return string
  67. * The page title.
  68. */
  69. public function revisionPageTitle($heartbeat_revision) {
  70. $heartbeat = $this->entityManager()->getStorage('heartbeat')->loadRevision($heartbeat_revision);
  71. return $this->t('Revision of %title from %date', array('%title' => $heartbeat->label(), '%date' => format_date($heartbeat->getRevisionCreationTime())));
  72. }
  73. /**
  74. * Generates an overview table of older revisions of a Heartbeat .
  75. *
  76. * @param \Drupal\heartbeat\Entity\HeartbeatInterface $heartbeat
  77. * A Heartbeat object.
  78. *
  79. * @return array
  80. * An array as expected by drupal_render().
  81. */
  82. public function revisionOverview(HeartbeatInterface $heartbeat) {
  83. $account = $this->currentUser();
  84. $langcode = $heartbeat->language()->getId();
  85. $langname = $heartbeat->language()->getName();
  86. $languages = $heartbeat->getTranslationLanguages();
  87. $has_translations = (count($languages) > 1);
  88. $heartbeat_storage = $this->entityManager()->getStorage('heartbeat');
  89. $build['#title'] = $has_translations ? $this->t('@langname revisions for %title', ['@langname' => $langname, '%title' => $heartbeat->label()]) : $this->t('Revisions for %title', ['%title' => $heartbeat->label()]);
  90. $header = array($this->t('Revision'), $this->t('Operations'));
  91. $revert_permission = (($account->hasPermission("revert all heartbeat revisions") || $account->hasPermission('administer heartbeat entities')));
  92. $delete_permission = (($account->hasPermission("delete all heartbeat revisions") || $account->hasPermission('administer heartbeat entities')));
  93. $rows = array();
  94. $vids = $heartbeat_storage->revisionIds($heartbeat);
  95. $latest_revision = TRUE;
  96. foreach (array_reverse($vids) as $vid) {
  97. /** @var \Drupal\heartbeat\HeartbeatInterface $revision */
  98. $revision = $heartbeat_storage->loadRevision($vid);
  99. // Only show revisions that are affected by the language that is being
  100. // displayed.
  101. if ($revision->hasTranslation($langcode) && $revision->getTranslation($langcode)->isRevisionTranslationAffected()) {
  102. $username = [
  103. '#theme' => 'username',
  104. '#account' => $revision->getRevisionUser(),
  105. ];
  106. // Use revision link to link to revisions that are not active.
  107. $date = \Drupal::service('date.formatter')->format($revision->revision_timestamp->value, 'short');
  108. if ($vid != $heartbeat->getRevisionId()) {
  109. $link = $this->l($date, new Url('entity.heartbeat.revision', ['heartbeat' => $heartbeat->id(), 'heartbeat_revision' => $vid]));
  110. }
  111. else {
  112. $link = $heartbeat->link($date);
  113. }
  114. $row = [];
  115. $column = [
  116. 'data' => [
  117. '#type' => 'inline_template',
  118. '#template' => '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}',
  119. '#context' => [
  120. 'date' => $link,
  121. 'username' => \Drupal::service('renderer')->renderPlain($username),
  122. 'message' => ['#markup' => $revision->revision_log_message->value, '#allowed_tags' => Xss::getHtmlTagList()],
  123. ],
  124. ],
  125. ];
  126. $row[] = $column;
  127. if ($latest_revision) {
  128. $row[] = [
  129. 'data' => [
  130. '#prefix' => '<em>',
  131. '#markup' => $this->t('Current revision'),
  132. '#suffix' => '</em>',
  133. ],
  134. ];
  135. foreach ($row as &$current) {
  136. $current['class'] = ['revision-current'];
  137. }
  138. $latest_revision = FALSE;
  139. }
  140. else {
  141. $links = [];
  142. if ($revert_permission) {
  143. $links['revert'] = [
  144. 'title' => $this->t('Revert'),
  145. 'url' => $has_translations ?
  146. Url::fromRoute('entity.heartbeat.translation_revert', ['heartbeat' => $heartbeat->id(), 'heartbeat_revision' => $vid, 'langcode' => $langcode]) :
  147. Url::fromRoute('entity.heartbeat.revision_revert', ['heartbeat' => $heartbeat->id(), 'heartbeat_revision' => $vid]),
  148. ];
  149. }
  150. if ($delete_permission) {
  151. $links['delete'] = [
  152. 'title' => $this->t('Delete'),
  153. 'url' => Url::fromRoute('entity.heartbeat.revision_delete', ['heartbeat' => $heartbeat->id(), 'heartbeat_revision' => $vid]),
  154. ];
  155. }
  156. $row[] = [
  157. 'data' => [
  158. '#type' => 'operations',
  159. '#links' => $links,
  160. ],
  161. ];
  162. }
  163. $rows[] = $row;
  164. }
  165. }
  166. $build['heartbeat_revisions_table'] = array(
  167. '#theme' => 'table',
  168. '#rows' => $rows,
  169. '#header' => $header,
  170. );
  171. return $build;
  172. }
  173. public function renderFeed($arg) {
  174. $myConfig = \Drupal::service('config.factory')->getEditable('heartbeat_feed.settings');
  175. $myConfig->set('message', $arg)->save();
  176. \Drupal::logger('HeartbeatController')->debug('My argument is %arg', ['%arg' => $arg]);
  177. return BlockViewBuilder::lazyBuilder('heartbeatblock', 'full');
  178. }
  179. public function updateFeed($hid) {
  180. $myConfig = \Drupal::service('config.factory')->getEditable('heartbeat_more.settings');
  181. $myConfig->set('hid', $hid)->save();
  182. $block = $this->blockManager->createInstance('heartbeat_more_block')->build();
  183. return [
  184. '#type' => 'markup',
  185. '#markup' => $this->renderer->render($block)
  186. ];
  187. }
  188. public function filterFeed($tid) {
  189. $myConfig = \Drupal::service('config.factory')->getEditable('heartbeat_hashtag.settings');
  190. $myConfig->set('tid', $tid)->save();
  191. $block = $this->blockManager->createInstance('heartbeat_hash_block')->build();
  192. return [
  193. '#type' => 'markup',
  194. '#markup' => $this->renderer->render($block)
  195. ];
  196. }
  197. public function userFilterFeed($tid) {
  198. $myConfig = \Drupal::service('config.factory')->getEditable('heartbeat_username.settings');
  199. $myConfig->set('tid', $tid)->save();
  200. $block = $this->blockManager->createInstance('heartbeat_username_block')->build();
  201. return [
  202. '#type' => 'markup',
  203. '#markup' => $this->renderer->render($block)
  204. ];
  205. }
  206. public function commentConfigUpdate($entity_id) {
  207. $commentConfig = \Drupal::configFactory()->getEditable('heartbeat_comment.settings');
  208. $commentConfig->set('entity_id', $entity_id)->save();
  209. return [
  210. '#type' => 'markup',
  211. '#markup' => 'Success',
  212. ];
  213. }
  214. public function subCommentRequest($cid) {
  215. $subCommentConfig = \Drupal::configFactory()->getEditable('heartbeat_comment.settings');
  216. $subCommentConfig->set('cid', $cid)->save();
  217. $response = new AjaxResponse();
  218. $response->addCommand(new AppendCommand('#heartbeat-comment-' . $cid,
  219. BlockViewBuilder::lazyBuilder('heartbeatsubcommentblock', 'teaser')));
  220. $response->addCommand(new SubCommentCommand($cid));
  221. return $response;
  222. }
  223. public function subComment() {
  224. return BlockViewBuilder::lazyBuilder('heartbeatsubcommentblock', 'teaser');
  225. }
  226. public function friendInteract($uid) {
  227. $myConfig = \Drupal::service('config.factory')->getEditable('heartbeat_friend_interact.settings');
  228. $myConfig->set('uid', $uid)->save();
  229. // $block = BlockViewBuilder::lazyBuilder('friendinteractblock', 'full');
  230. $block = \Drupal::service('plugin.manager.block');
  231. $block = $block->createInstance('friend_interact_block')->build();
  232. $blockMarkup = \Drupal::service('renderer')->render($block);
  233. return ['#type' => 'markup', '#markup' => $blockMarkup];
  234. }
  235. }