heartbeat.module 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. /**
  3. * @file
  4. * Contains heartbeat.module.
  5. */
  6. use Drupal\Core\Routing\RouteMatchInterface;
  7. use Drupal\Core\Entity\EntityInterface;
  8. use Drupal\Core\Form\FormState;
  9. use Drupal\node\NodeInterface;
  10. use Drupal\Core\Session\AccountInterface;
  11. use Drupal\Core\Entity;
  12. use Drupal\heartbeat\Entity\FILE_FIELD;
  13. use Drupal\heartbeat\Entity\Heartbeat;
  14. use GuzzleHttp\Exception\RequestException;
  15. use Drupal\heartbeat\Entity\HeartbeatType;
  16. use Drupal\heartbeat\Ajax\UpdateFeedCommand;
  17. /**
  18. * Implements hook_help().
  19. */
  20. function heartbeat_help($route_name, RouteMatchInterface $route_match) {
  21. switch ($route_name) {
  22. // Main module help for the heartbeat module.
  23. case 'help.page.heartbeat':
  24. $output = '';
  25. $output .= '<h3>' . t('About') . '</h3>';
  26. $output .= '<p>' . t('Heartbeat for Drupal 8') . '</p>';
  27. return $output;
  28. default:
  29. }
  30. }
  31. /**
  32. * Implements hook_theme().
  33. */
  34. function heartbeat_theme() {
  35. $theme = [];
  36. $theme['heartbeat'] = array(
  37. 'render element' => 'elements',
  38. 'file' => 'heartbeat.page.inc',
  39. 'template' => 'heartbeat',
  40. );
  41. $theme['heartbeat_content_add_list'] = [
  42. 'render element' => 'content',
  43. 'variables' => ['content' => NULL],
  44. 'file' => 'heartbeat.page.inc',
  45. ];
  46. $theme['heartbeat_stream'] = [
  47. 'variables' => array(
  48. 'messages' => NULL,
  49. 'zilla' => 2,
  50. )
  51. ];
  52. return $theme;
  53. }
  54. /**
  55. * Implements hook_theme_suggestions_HOOK().
  56. */
  57. function heartbeat_theme_suggestions_heartbeat(array $variables) {
  58. $suggestions = array();
  59. $entity = $variables['elements']['#heartbeat'];
  60. $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
  61. $suggestions[] = 'heartbeat__' . $sanitized_view_mode;
  62. $suggestions[] = 'heartbeat__' . $entity->bundle();
  63. $suggestions[] = 'heartbeat__' . $entity->bundle() . '__' . $sanitized_view_mode;
  64. $suggestions[] = 'heartbeat__' . $entity->id();
  65. $suggestions[] = 'heartbeat__' . $entity->id() . '__' . $sanitized_view_mode;
  66. return $suggestions;
  67. }
  68. /**
  69. * Implements hook_entity_insert().
  70. */
  71. function heartbeat_entity_insert(EntityInterface $entity) {
  72. $bundleSaved = false;
  73. switch (true) {
  74. case $entity instanceof \Drupal\node\Entity\Node:
  75. $user = $entity->getOwner();
  76. $media = HeartBeat::mediaFieldTypes($entity->getFields());
  77. if ($entity->id() && $user->isAuthenticated()) {
  78. $heartbeatTypeService = \Drupal::service('heartbeat.heartbeattype');
  79. $tokenService = \Drupal::service('token');
  80. foreach ($heartbeatTypeService->getTypes() as $type) {
  81. $heartbeatTypeEntity = $heartbeatTypeService->load($type);
  82. if ($heartbeatTypeEntity->getMainEntity() == 'node') {
  83. $arguments = json_decode($heartbeatTypeEntity->getArguments());
  84. foreach ($arguments as $key => $argument) {
  85. $variables[$key] = $argument;
  86. }
  87. $preparsedMessageString = strtr($heartbeatTypeEntity->getMessage(), $variables);
  88. $entities = array(
  89. 'node' => $entity,
  90. 'user' => $user,
  91. );
  92. if ($entity->bundle() === $heartbeatTypeEntity->getBundle()
  93. &&
  94. array_key_exists($heartbeatTypeEntity->getBundle(), $heartbeatTypeService->getEntityBundles($entity->getEntityType()))
  95. ) {
  96. $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity->getEntityTypeId(), $media);
  97. $heartbeatActivity = Heartbeat::create([
  98. 'type' => $heartbeatTypeEntity->id(),
  99. 'uid' => $user->id(),
  100. 'nid' => $entity->id(),
  101. 'name' => 'Dev Test',
  102. ]);
  103. $heartbeatActivity->setMessage($heartbeatMessage);
  104. if ($heartbeatActivity->save()) {
  105. $bundleSaved = true;
  106. }
  107. continue;
  108. } else if ($heartbeatTypeEntity->getBundle() === null || trim($heartbeatTypeEntity->getBundle()) === '') {
  109. $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity->getEntityTypeId(), $media);
  110. // $translatedMessage = t($messageTemplate);
  111. $heartbeatActivity = Heartbeat::create([
  112. 'type' => $heartbeatTypeEntity->id(),
  113. 'uid' => $user->id(),
  114. 'nid' => $entity->id(),
  115. 'name' => 'Dev Test',
  116. 'status' => !$bundleSaved ? 1 : 0,
  117. ]);
  118. $heartbeatActivity->setMessage($heartbeatMessage);
  119. $heartbeatActivity->save();
  120. } else {
  121. continue;
  122. }
  123. }
  124. }
  125. updateFeeds();
  126. }
  127. break;
  128. case $entity instanceof \Drupal\user\Entity\User:
  129. // $userService = \Drupal\User\Entity\user
  130. break;
  131. case $entity instanceof \Drupal\flag\Entity\Flag:
  132. break;
  133. case $entity instanceof \Drupal\statusmessage\Entity\Status:
  134. $user = \Drupal\user\Entity\User::load($entity->get('uid')->getValue()[0]['target_id']);
  135. $heartbeatTypeService = \Drupal::service('heartbeat.heartbeattype');
  136. $tokenService = \Drupal::service('token');
  137. heartbeat_handle_entity($entity, $tokenService, $heartbeatTypeService, $user, 'status');
  138. break;
  139. }
  140. }
  141. function heartbeat_handle_entity($entity, $tokenService, $heartbeatTypeService, $user, $mainEntity) {
  142. $bundleSaved = false;
  143. if ($entity->id() && $user->isAuthenticated()) {
  144. $media = HeartBeat::mediaFieldTypes($entity->getFields());
  145. foreach ($heartbeatTypeService->getTypes() as $type) {
  146. $heartbeatTypeEntity = $heartbeatTypeService->load($type);
  147. if ($heartbeatTypeEntity->getMainEntity() == $mainEntity) {
  148. $arguments = json_decode($heartbeatTypeEntity->getArguments());
  149. foreach ($arguments as $key => $argument) {
  150. $variables[$key] = $argument;
  151. }
  152. $preparsedMessageString = strtr($heartbeatTypeEntity->getMessage(), $variables);
  153. $entities = array(
  154. $mainEntity => $entity,
  155. 'user' => $user,
  156. );
  157. if ($entity->bundle() === $heartbeatTypeEntity->getBundle()
  158. &&
  159. array_key_exists($heartbeatTypeEntity->getBundle(), $heartbeatTypeService->getEntityBundles($entity->getEntityType()))
  160. ) {
  161. $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity->getEntityTypeId(), $media);
  162. $heartbeatActivity = Heartbeat::create([
  163. 'type' => $heartbeatTypeEntity->id(),
  164. 'uid' => $user->id(),
  165. 'nid' => $entity->id(),
  166. 'name' => 'Dev Test',
  167. ]);
  168. $heartbeatActivity->setMessage($heartbeatMessage);
  169. if ($heartbeatActivity->save()) {
  170. $bundleSaved = true;
  171. }
  172. continue;
  173. } else if ($heartbeatTypeEntity->getBundle() === null || trim($heartbeatTypeEntity->getBundle()) === '') {
  174. $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity->getEntityTypeId(), $media);
  175. // $translatedMessage = t($messageTemplate);
  176. $heartbeatActivity = Heartbeat::create([
  177. 'type' => $heartbeatTypeEntity->id(),
  178. 'uid' => $user->id(),
  179. 'nid' => $entity->id(),
  180. 'name' => 'Dev Test',
  181. 'status' => !$bundleSaved ? 1 : 0,
  182. ]);
  183. $heartbeatActivity->setMessage($heartbeatMessage);
  184. $heartbeatActivity->save();
  185. } else {
  186. continue;
  187. }
  188. }
  189. }
  190. }
  191. updateFeeds();
  192. }
  193. function updateFeeds() {
  194. $configFactory = \Drupal::service('config.factory');
  195. $updateFeedConfig = $configFactory->getEditable('heartbeat_update_feed.settings');
  196. $feedConfig = $configFactory->get('heartbeat_feed.settings');
  197. // $form = \Drupal::formBuilder()->getForm('\Drupal\heartbeat\Form\HeartbeatUpdateFeedForm');
  198. // $form_state = new FormState();
  199. // $form_state->set('timestamp', $updateFeedConfig->get('timestamp'));
  200. // \Drupal::formBuilder()->submitForm('\Drupal\heartbeat\Form\HeartbeatUpdateFeedForm', $form_state);
  201. // $errors = $form_state->getErrors();
  202. // $updateFeedConfig->set('update', true)->save();
  203. // $feedUpdateConfig = new stdClass();
  204. // $feedUpdateConfig->feed = $feedConfig->get('message');
  205. // $feedUpdateConfig->update = true;
  206. // $feedUpdateConfig->timestamp = $updateFeedConfig->get('timestamp');
  207. // $curlQuery = '/heartbeat/update_feed/' . $updateFeedConfig->get('timestamp');
  208. //Set options for the curl request
  209. // try {
  210. // $response = \Drupal::httpClient()->get($curlQuery, array('headers' => array('Accept' => 'text/plain')));
  211. // $data = (string) $response->getBody();
  212. // if (empty($data)) {
  213. // $stophere = null;
  214. //
  215. // }
  216. // }
  217. // catch (RequestException $e) {
  218. // return FALSE;
  219. // }
  220. // $curlOptions = array(
  221. // 'headers' => array(
  222. // 'X-DataSource-Auth' => 'a',
  223. // 'method' => 'POST',
  224. // ),
  225. // );
  226. // //Perform the request and decode the results
  227. // $result = chr_curl_http_request($curlQuery, $curlOptions);
  228. }
  229. //public function saveHeartbeatMessage($entity, $tokenService, $preparsedMessageString, $entities, $entityTypeId, $media) {
  230. // $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity->getEntityTypeId(), $media);
  231. //
  232. //
  233. // $heartbeatActivity = Heartbeat::create([
  234. // 'type' => $heartbeatTypeEntity->id(),
  235. // 'uid' => $user->id(),
  236. // 'nid' => $entity->id(),
  237. // 'name' => 'Dev Test',
  238. // ]);
  239. //
  240. // $heartbeatActivity->setMessage($heartbeatMessage);
  241. // $heartbeatActivity->save();
  242. //}
  243. /**
  244. * Implements hook_ajax_render_alter().
  245. */
  246. function heartbeat_ajax_render_alter(array &$data) {
  247. }
  248. /**
  249. * Implements hook_entity_bundle_create().
  250. */
  251. function heartbeat_entity_bundle_create($entity_type_id, $bundle)
  252. {
  253. }
  254. //TODO Add heartbeat language to Javascript
  255. //TODO Determine necessity of polling
  256. //Add
  257. //TODO implement polling in JS using Drupal Settings
  258. //$variables['#attached']['drupalSettings']['heartbeatData'] = $mydata;
  259. //^ will become available in JS as:
  260. //settings.heartbeatData, data