Heartbeat.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. <?php
  2. namespace Drupal\heartbeat\Entity;
  3. use Drupal\Core\Entity\EntityStorageInterface;
  4. use Drupal\Core\Field\BaseFieldDefinition;
  5. use Drupal\Core\Entity\RevisionableContentEntityBase;
  6. use Drupal\Core\Entity\EntityChangedTrait;
  7. use Drupal\Core\Entity\EntityTypeInterface;
  8. use Drupal\user\UserInterface;
  9. /**
  10. * Defines the Heartbeat entity.
  11. *
  12. * @ingroup heartbeat
  13. *
  14. * @ContentEntityType(
  15. * id = "heartbeat",
  16. * label = @Translation("Heartbeat"),
  17. * bundle_label = @Translation("Heartbeat type"),
  18. * handlers = {
  19. * "storage" = "Drupal\heartbeat\HeartbeatStorage",
  20. * "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
  21. * "list_builder" = "Drupal\heartbeat\HeartbeatListBuilder",
  22. * "views_data" = "Drupal\heartbeat\Entity\HeartbeatViewsData",
  23. * "translation" = "Drupal\heartbeat\HeartbeatTranslationHandler",
  24. *
  25. * "form" = {
  26. * "default" = "Drupal\heartbeat\Form\HeartbeatForm",
  27. * "add" = "Drupal\heartbeat\Form\HeartbeatForm",
  28. * "edit" = "Drupal\heartbeat\Form\HeartbeatForm",
  29. * "delete" = "Drupal\heartbeat\Form\HeartbeatDeleteForm",
  30. * },
  31. * "access" = "Drupal\heartbeat\HeartbeatAccessControlHandler",
  32. * "route_provider" = {
  33. * "html" = "Drupal\heartbeat\HeartbeatHtmlRouteProvider",
  34. * },
  35. * },
  36. * base_table = "heartbeat",
  37. * data_table = "heartbeat_field_data",
  38. * revision_table = "heartbeat_revision",
  39. * revision_data_table = "heartbeat_field_revision",
  40. * translatable = TRUE,
  41. * admin_permission = "administer heartbeat entities",
  42. * entity_keys = {
  43. * "id" = "id",
  44. * "revision" = "vid",
  45. * "bundle" = "type",
  46. * "label" = "name",
  47. * "uuid" = "uuid",
  48. * "uid" = "uid",
  49. * "nid" = "nid",
  50. * "langcode" = "langcode",
  51. * "status" = "status",
  52. * },
  53. * links = {
  54. * "canonical" = "/admin/structure/heartbeat/{heartbeat}",
  55. * "add-page" = "/admin/structure/heartbeat/add",
  56. * "add-form" = "/admin/structure/heartbeat/add/{heartbeat_type}",
  57. * "edit-form" = "/admin/structure/heartbeat/{heartbeat}/edit",
  58. * "delete-form" = "/admin/structure/heartbeat/{heartbeat}/delete",
  59. * "version-history" = "/admin/structure/heartbeat/{heartbeat}/revisions",
  60. * "revision" = "/admin/structure/heartbeat/{heartbeat}/revisions/{heartbeat_revision}/view",
  61. * "revision_revert" = "/admin/structure/heartbeat/{heartbeat}/revisions/{heartbeat_revision}/revert",
  62. * "translation_revert" = "/admin/structure/heartbeat/{heartbeat}/revisions/{heartbeat_revision}/revert/{langcode}",
  63. * "revision_delete" = "/admin/structure/heartbeat/{heartbeat}/revisions/{heartbeat_revision}/delete",
  64. * "collection" = "/admin/structure/heartbeat",
  65. * },
  66. * bundle_entity_type = "heartbeat_type",
  67. * field_ui_base_route = "entity.heartbeat_type.edit_form"
  68. * )
  69. */
  70. // Always block from display
  71. const HEARTBEAT_NONE = -1;
  72. // Display only activity messages that are mine or addressed to me
  73. const HEARTBEAT_PRIVATE = 0;
  74. // Only the person that is chosen by the actor, can see the message
  75. const HEARTBEAT_PUBLIC_TO_ADDRESSEE = 1;
  76. // Display activity message of all my user relations, described in contributed modules
  77. const HEARTBEAT_PUBLIC_TO_CONNECTED = 2;
  78. // Everyone can see this activity message, unless this type of message is set to private
  79. const HEARTBEAT_PUBLIC_TO_ALL = 4;
  80. //Group Types
  81. const HEARTBEAT_GROUP_NONE = 11;
  82. const HEARTBEAT_GROUP_SINGLE = 12;
  83. const HEARTBEAT_GROUP_SUMMARY = 13;
  84. const FILE_FIELD = 'Drupal\file\Plugin\Field\FieldType\FileFieldItemList';
  85. class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterface {
  86. use EntityChangedTrait;
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
  91. parent::preCreate($storage_controller, $values);
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function preSave(EntityStorageInterface $storage) {
  97. parent::preSave($storage);
  98. foreach (array_keys($this->getTranslationLanguages()) as $langcode) {
  99. $translation = $this->getTranslation($langcode);
  100. // If no owner has been set explicitly, make the anonymous user the owner.
  101. if (!$translation->getOwner()) {
  102. $translation->setOwnerId(0);
  103. }
  104. }
  105. // If no revision author has been set explicitly, make the heartbeat owner the
  106. // revision author.
  107. if (!$this->getRevisionUser()) {
  108. $this->setRevisionUserId($this->getOwnerId());
  109. }
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function getType() {
  115. return $this->bundle();
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function getName() {
  121. return $this->get('name')->value;
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function setName($name) {
  127. $this->set('name', $name);
  128. return $this;
  129. }
  130. /**
  131. * Gets the Heartbeat message.
  132. *
  133. * @return string
  134. * Message of the Heartbeat.
  135. */
  136. public function getMessage() {
  137. return $this->get('message');
  138. }
  139. /**
  140. * Sets the Heartbeat Message.
  141. *
  142. * @param $name
  143. * @return
  144. * @internal param string $message The Heartbeat Message
  145. */
  146. public function setMessage($message) {
  147. $this->set('message', $message);
  148. }
  149. /**
  150. * {@inheritdoc}
  151. */
  152. public function getCreatedTime() {
  153. return $this->get('created')->value;
  154. }
  155. /**
  156. * {@inheritdoc}
  157. */
  158. public function setCreatedTime($timestamp) {
  159. $this->set('created', $timestamp);
  160. return $this;
  161. }
  162. /**
  163. * {@inheritdoc}
  164. */
  165. public function getOwner() {
  166. return $this->get('uid')->entity;
  167. }
  168. /**
  169. * {@inheritdoc}
  170. */
  171. public function getOwnerId() {
  172. return $this->get('uid')->target_id;
  173. }
  174. /**
  175. * {@inheritdoc}
  176. */
  177. public function setOwnerId($uid) {
  178. $this->set('uid', $uid);
  179. return $this;
  180. }
  181. /**
  182. * {@inheritdoc}
  183. */
  184. public function setOwner(UserInterface $account) {
  185. $this->set('uid', $account->id());
  186. return $this;
  187. }
  188. /**
  189. * {@inheritdoc}
  190. */
  191. public function isPublished() {
  192. return (bool) $this->getEntityKey('status');
  193. }
  194. /**
  195. * {@inheritdoc}
  196. */
  197. public function setPublished($published) {
  198. $this->set('status', $published ? TRUE : FALSE);
  199. return $this;
  200. }
  201. /**
  202. * {@inheritdoc}
  203. */
  204. public function getRevisionCreationTime() {
  205. return $this->get('revision_timestamp')->value;
  206. }
  207. /**
  208. * {@inheritdoc}
  209. */
  210. public function setRevisionCreationTime($timestamp) {
  211. $this->set('revision_timestamp', $timestamp);
  212. return $this;
  213. }
  214. /**
  215. * {@inheritdoc}
  216. */
  217. public function getRevisionUser() {
  218. return $this->get('revision_uid')->entity;
  219. }
  220. /**
  221. * {@inheritdoc}
  222. */
  223. public function setRevisionUserId($uid) {
  224. $this->set('revision_uid', $uid);
  225. return $this;
  226. }
  227. /**
  228. * {@inheritdoc}
  229. */
  230. public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  231. $fields = parent::baseFieldDefinitions($entity_type);
  232. $fields['uid'] = BaseFieldDefinition::create('entity_reference')
  233. ->setLabel(t('Authored by'))
  234. ->setDescription(t('The user ID of author of the Heartbeat entity.'))
  235. ->setRevisionable(TRUE)
  236. ->setSetting('target_type', 'user')
  237. ->setSetting('handler', 'default')
  238. ->setTranslatable(TRUE)
  239. ->setDisplayOptions('view', array(
  240. 'label' => 'hidden',
  241. 'type' => 'author',
  242. 'weight' => 0,
  243. ))
  244. ->setDisplayOptions('form', array(
  245. 'type' => 'entity_reference_autocomplete',
  246. 'weight' => 5,
  247. 'settings' => array(
  248. 'match_operator' => 'CONTAINS',
  249. 'size' => '60',
  250. 'autocomplete_type' => 'tags',
  251. 'placeholder' => '',
  252. ),
  253. ))
  254. ->setDisplayConfigurable('form', TRUE)
  255. ->setDisplayConfigurable('view', TRUE);
  256. $fields['nid'] = BaseFieldDefinition::create('entity_reference')
  257. ->setLabel(t('Node'))
  258. ->setDescription(t('The content associated with this Heartbeat'))
  259. ->setSetting('target_type', 'node')
  260. ->setSetting('handler', 'default')
  261. ->setRevisionable(TRUE);
  262. $fields['name'] = BaseFieldDefinition::create('string')
  263. ->setLabel(t('Name'))
  264. ->setDescription(t('The name of the Heartbeat entity.'))
  265. ->setRevisionable(TRUE)
  266. ->setSettings(array(
  267. 'max_length' => 50,
  268. 'text_processing' => 0,
  269. ))
  270. ->setDefaultValue('')
  271. ->setDisplayOptions('view', array(
  272. 'label' => 'above',
  273. 'type' => 'string',
  274. 'weight' => -4,
  275. ))
  276. ->setDisplayOptions('form', array(
  277. 'type' => 'string_textfield',
  278. 'weight' => -4,
  279. ))
  280. ->setDisplayConfigurable('form', TRUE)
  281. ->setDisplayConfigurable('view', TRUE);
  282. $fields['message'] = BaseFieldDefinition::create('string_long')
  283. ->setLabel(t('Message'))
  284. ->setDescription(t('The message of the Heartbeat entity.'))
  285. ->setRevisionable(TRUE);
  286. $fields['status'] = BaseFieldDefinition::create('boolean')
  287. ->setLabel(t('Publishing status'))
  288. ->setDescription(t('A boolean indicating whether the Heartbeat is published.'))
  289. ->setRevisionable(TRUE)
  290. ->setDefaultValue(TRUE);
  291. $fields['created'] = BaseFieldDefinition::create('created')
  292. ->setLabel(t('Created'))
  293. ->setDescription(t('The time that the entity was created.'));
  294. $fields['changed'] = BaseFieldDefinition::create('changed')
  295. ->setLabel(t('Changed'))
  296. ->setDescription(t('The time that the entity was last edited.'));
  297. $fields['revision_timestamp'] = BaseFieldDefinition::create('created')
  298. ->setLabel(t('Revision timestamp'))
  299. ->setDescription(t('The time that the current revision was created.'))
  300. ->setQueryable(FALSE)
  301. ->setRevisionable(TRUE);
  302. $fields['revision_uid'] = BaseFieldDefinition::create('entity_reference')
  303. ->setLabel(t('Revision user ID'))
  304. ->setDescription(t('The user ID of the author of the current revision.'))
  305. ->setSetting('target_type', 'user')
  306. ->setQueryable(FALSE)
  307. ->setRevisionable(TRUE);
  308. $fields['revision_translation_affected'] = BaseFieldDefinition::create('boolean')
  309. ->setLabel(t('Revision translation affected'))
  310. ->setDescription(t('Indicates if the last edit of a translation belongs to current revision.'))
  311. ->setReadOnly(TRUE)
  312. ->setRevisionable(TRUE)
  313. ->setTranslatable(TRUE);
  314. return $fields;
  315. }
  316. /**
  317. * Returns the node type label for the passed node.
  318. *
  319. * @param \Drupal\heartbeat\Entity\HeartbeatInterface $heartbeat
  320. * A heartbeat entity to return the heartbeat type's label for.
  321. *
  322. * @return string|false
  323. * The heartbeat type label or FALSE if the heartbeat type is not found.
  324. *
  325. * @todo Add this as generic helper method for config entities representing
  326. * entity bundles.
  327. */
  328. public function heartbeat_get_type(HeartbeatInterface $heartbeat) {
  329. $type = HeartbeatType::load($heartbeat->bundle());
  330. return $type ? $type->label() : FALSE;
  331. }
  332. /**
  333. * Updates all heartbeat activities of one type to be of another type.
  334. *
  335. * @param string $old_id
  336. * The current heartbeat type of the activities.
  337. * @param string $new_id
  338. * The new heartbeat type of the activities.
  339. *
  340. * @return
  341. * The number of activities whose heartbeat type field was modified.
  342. */
  343. public function heartbeat_type_update_nodes($old_id, $new_id) {
  344. return \Drupal::entityManager()->getStorage('heartbeat')->updateType($old_id, $new_id);
  345. }
  346. /**
  347. * Builds a message template for a given HeartbeatType
  348. *
  349. * @param HeartbeatType $heartbeatType
  350. * @param null $mediaData
  351. * @return null|string
  352. */
  353. public static function buildMessage(\Drupal\token\Token $tokenService, $preparsedMessage, $entities = NULL, $mediaData = NULL) {
  354. $parsedMessage = $tokenService->replace($preparsedMessage . '<a href="/node/[node:nid]">', $entities);
  355. /** @noinspection NestedTernaryOperatorInspection */
  356. $message = $parsedMessage;
  357. $message .= $mediaData ? self::buildMediaMarkup($mediaData) : '';
  358. $message .= '</a>';
  359. return $message;
  360. }
  361. private static function buildMediaMarkup($mediaData) {
  362. $markup = '';
  363. foreach ($mediaData as $media) {
  364. $markup .= self::mediaTag($media->type, $media->path);
  365. }
  366. return $markup;
  367. }
  368. private static function mediaTag($type, $filePath) {
  369. //TODO put this into new method
  370. if ($type == 'image') { $type = 'img';}
  371. return '<'. $type . ' src="' . str_replace('public://', '/sites/default/files/', $filePath) . '" / >';
  372. }
  373. /**
  374. * Returns class of argument
  375. *
  376. * @param $field
  377. * @return string
  378. */
  379. public static function findClass($field) {
  380. return get_class($field);
  381. }
  382. /**
  383. * Returns an array of classes for array argument
  384. * @param $fields
  385. * @return array
  386. */
  387. public static function findAllMedia($fields) {
  388. return array_map(array(get_called_class(), 'findClass'), $fields);
  389. }
  390. /**
  391. * Returns all media types for an array of fields
  392. *
  393. * @param $fields
  394. * @return array
  395. * @throws \Drupal\Core\Entity\Exception\UndefinedLinkTemplateException
  396. * @throws \Drupal\Core\Entity\EntityMalformedException
  397. */
  398. public static function mediaFieldTypes($fields) {
  399. $types = array();
  400. foreach ($fields as $field) {
  401. if ($field instanceof \Drupal\file\Plugin\Field\FieldType\FileFieldItemList) {
  402. if ($field->getFieldDefinition()->getType() === 'image' ||
  403. $field->getFieldDefinition()->getType() === 'video' ||
  404. $field->getFieldDefinition()->getType() === 'audio') {
  405. $fieldValue = $field->getValue();
  406. $fileId = $fieldValue[0]['target_id'];
  407. $file = \Drupal::entityTypeManager()->getStorage('file')->load($fileId);
  408. if ($file !== NULL && is_object($file)) {
  409. $url = \Drupal\Core\Url::fromUri($file->getFileUri());
  410. $mediaObject = self::createHeartbeatMedia($field->getFieldDefinition()->getType(), $url->getUri());
  411. $types[] = $mediaObject;
  412. } else {
  413. continue;
  414. }
  415. }
  416. }
  417. }
  418. return $types;
  419. }
  420. /**
  421. * Parses a HeartbeatType message template and maps
  422. * variable values onto matching keywords
  423. *
  424. * @param $translatedMessage
  425. * @param $variables
  426. * @return string
  427. */
  428. public static function parseMessage($translatedMessage, $variables) {
  429. return strtr($translatedMessage, $variables);
  430. }
  431. public static function createHeartbeatMedia($type, $path) {
  432. $mediaObject = new \stdClass();
  433. $mediaObject->type = $type;
  434. $mediaObject->path = $path;
  435. return $mediaObject;
  436. }
  437. public static function getEntityNames($entityTypes) {
  438. $names = array();
  439. foreach ($entityTypes as $type) {
  440. if (($type->getBaseTable() === 'node') ||
  441. ($type->getBaseTable() === 'user')
  442. ||
  443. ($type->getStorageClass() !== NULL &&
  444. strpos($type->getStorageClass(), $type->getLabel()->getUntranslatedString())
  445. )
  446. ) {
  447. $names[] = $type->id();
  448. }
  449. }
  450. sort($names);
  451. return $names;
  452. }
  453. /**
  454. * Gets the Heartbeat user.
  455. *
  456. * @return int
  457. * The uid of the Heartbeat's user.
  458. */
  459. public function getUid()
  460. {
  461. // TODO: Implement getUid() method.
  462. }
  463. /**
  464. * Sets the Heartbeat user.
  465. *
  466. * @param int uid
  467. * The Heartbeat user.
  468. *
  469. */
  470. public function setUid($uid)
  471. {
  472. // TODO: Implement setUid() method.
  473. }
  474. /**
  475. * Gets the Heartbeat's associated node nid.
  476. *
  477. * @return int
  478. * The nid of the Heartbeat's associated node.
  479. */
  480. public function getNid() {
  481. return $this->get('nid');
  482. }
  483. /**
  484. * Sets the Heartbeat user.
  485. *
  486. * @param int uid
  487. * The Heartbeat user.
  488. *
  489. */
  490. public function setNid($nid) {
  491. $this->set('nid', $nid);
  492. }
  493. }