heartbeat.module 11 KB

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