Heartbeat.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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" = "user_id",
  49. * "langcode" = "langcode",
  50. * "status" = "status",
  51. * },
  52. * links = {
  53. * "canonical" = "/admin/structure/heartbeat/{heartbeat}",
  54. * "add-page" = "/admin/structure/heartbeat/add",
  55. * "add-form" = "/admin/structure/heartbeat/add/{heartbeat_type}",
  56. * "edit-form" = "/admin/structure/heartbeat/{heartbeat}/edit",
  57. * "delete-form" = "/admin/structure/heartbeat/{heartbeat}/delete",
  58. * "version-history" = "/admin/structure/heartbeat/{heartbeat}/revisions",
  59. * "revision" = "/admin/structure/heartbeat/{heartbeat}/revisions/{heartbeat_revision}/view",
  60. * "revision_revert" = "/admin/structure/heartbeat/{heartbeat}/revisions/{heartbeat_revision}/revert",
  61. * "translation_revert" = "/admin/structure/heartbeat/{heartbeat}/revisions/{heartbeat_revision}/revert/{langcode}",
  62. * "revision_delete" = "/admin/structure/heartbeat/{heartbeat}/revisions/{heartbeat_revision}/delete",
  63. * "collection" = "/admin/structure/heartbeat",
  64. * },
  65. * bundle_entity_type = "heartbeat_type",
  66. * field_ui_base_route = "entity.heartbeat_type.edit_form"
  67. * )
  68. */
  69. // Always block from display
  70. const HEARTBEAT_NONE = -1;
  71. // Display only activity messages that are mine or addressed to me
  72. const HEARTBEAT_PRIVATE = 0;
  73. // Only the person that is chosen by the actor, can see the message
  74. const HEARTBEAT_PUBLIC_TO_ADDRESSEE = 1;
  75. // Display activity message of all my user relations, described in contributed modules
  76. const HEARTBEAT_PUBLIC_TO_CONNECTED = 2;
  77. // Everyone can see this activity message, unless this type of message is set to private
  78. const HEARTBEAT_PUBLIC_TO_ALL = 4;
  79. //Group Types
  80. const HEARTBEAT_GROUP_NONE = 11;
  81. const HEARTBEAT_GROUP_SINGLE = 12;
  82. const HEARTBEAT_GROUP_SUMMARY = 13;
  83. const FILE_FIELD = 'Drupal\file\Plugin\Field\FieldType\FileFieldItemList';
  84. class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterface {
  85. use EntityChangedTrait;
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
  90. parent::preCreate($storage_controller, $values);
  91. $values += array(
  92. 'user_id' => \Drupal::currentUser()->id(),
  93. );
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function preSave(EntityStorageInterface $storage) {
  99. parent::preSave($storage);
  100. foreach (array_keys($this->getTranslationLanguages()) as $langcode) {
  101. $translation = $this->getTranslation($langcode);
  102. // If no owner has been set explicitly, make the anonymous user the owner.
  103. if (!$translation->getOwner()) {
  104. $translation->setOwnerId(0);
  105. }
  106. }
  107. // If no revision author has been set explicitly, make the heartbeat owner the
  108. // revision author.
  109. if (!$this->getRevisionUser()) {
  110. $this->setRevisionUserId($this->getOwnerId());
  111. }
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function getType() {
  117. return $this->bundle();
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public function getName() {
  123. return $this->get('name')->value;
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function setName($name) {
  129. $this->set('name', $name);
  130. return $this;
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function getCreatedTime() {
  136. return $this->get('created')->value;
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function setCreatedTime($timestamp) {
  142. $this->set('created', $timestamp);
  143. return $this;
  144. }
  145. /**
  146. * {@inheritdoc}
  147. */
  148. public function getOwner() {
  149. return $this->get('user_id')->entity;
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function getOwnerId() {
  155. return $this->get('user_id')->target_id;
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. public function setOwnerId($uid) {
  161. $this->set('user_id', $uid);
  162. return $this;
  163. }
  164. /**
  165. * {@inheritdoc}
  166. */
  167. public function setOwner(UserInterface $account) {
  168. $this->set('user_id', $account->id());
  169. return $this;
  170. }
  171. /**
  172. * {@inheritdoc}
  173. */
  174. public function isPublished() {
  175. return (bool) $this->getEntityKey('status');
  176. }
  177. /**
  178. * {@inheritdoc}
  179. */
  180. public function setPublished($published) {
  181. $this->set('status', $published ? TRUE : FALSE);
  182. return $this;
  183. }
  184. /**
  185. * {@inheritdoc}
  186. */
  187. public function getRevisionCreationTime() {
  188. return $this->get('revision_timestamp')->value;
  189. }
  190. /**
  191. * {@inheritdoc}
  192. */
  193. public function setRevisionCreationTime($timestamp) {
  194. $this->set('revision_timestamp', $timestamp);
  195. return $this;
  196. }
  197. /**
  198. * {@inheritdoc}
  199. */
  200. public function getRevisionUser() {
  201. return $this->get('revision_uid')->entity;
  202. }
  203. /**
  204. * {@inheritdoc}
  205. */
  206. public function setRevisionUserId($uid) {
  207. $this->set('revision_uid', $uid);
  208. return $this;
  209. }
  210. /**
  211. * {@inheritdoc}
  212. */
  213. public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  214. $fields = parent::baseFieldDefinitions($entity_type);
  215. $fields['user_id'] = BaseFieldDefinition::create('entity_reference')
  216. ->setLabel(t('Authored by'))
  217. ->setDescription(t('The user ID of author of the Heartbeat entity.'))
  218. ->setRevisionable(TRUE)
  219. ->setSetting('target_type', 'user')
  220. ->setSetting('handler', 'default')
  221. ->setTranslatable(TRUE)
  222. ->setDisplayOptions('view', array(
  223. 'label' => 'hidden',
  224. 'type' => 'author',
  225. 'weight' => 0,
  226. ))
  227. ->setDisplayOptions('form', array(
  228. 'type' => 'entity_reference_autocomplete',
  229. 'weight' => 5,
  230. 'settings' => array(
  231. 'match_operator' => 'CONTAINS',
  232. 'size' => '60',
  233. 'autocomplete_type' => 'tags',
  234. 'placeholder' => '',
  235. ),
  236. ))
  237. ->setDisplayConfigurable('form', TRUE)
  238. ->setDisplayConfigurable('view', TRUE);
  239. $fields['name'] = BaseFieldDefinition::create('string')
  240. ->setLabel(t('Name'))
  241. ->setDescription(t('The name of the Heartbeat entity.'))
  242. ->setRevisionable(TRUE)
  243. ->setSettings(array(
  244. 'max_length' => 50,
  245. 'text_processing' => 0,
  246. ))
  247. ->setDefaultValue('')
  248. ->setDisplayOptions('view', array(
  249. 'label' => 'above',
  250. 'type' => 'string',
  251. 'weight' => -4,
  252. ))
  253. ->setDisplayOptions('form', array(
  254. 'type' => 'string_textfield',
  255. 'weight' => -4,
  256. ))
  257. ->setDisplayConfigurable('form', TRUE)
  258. ->setDisplayConfigurable('view', TRUE);
  259. $fields['status'] = BaseFieldDefinition::create('boolean')
  260. ->setLabel(t('Publishing status'))
  261. ->setDescription(t('A boolean indicating whether the Heartbeat is published.'))
  262. ->setRevisionable(TRUE)
  263. ->setDefaultValue(TRUE);
  264. $fields['created'] = BaseFieldDefinition::create('created')
  265. ->setLabel(t('Created'))
  266. ->setDescription(t('The time that the entity was created.'));
  267. $fields['changed'] = BaseFieldDefinition::create('changed')
  268. ->setLabel(t('Changed'))
  269. ->setDescription(t('The time that the entity was last edited.'));
  270. $fields['revision_timestamp'] = BaseFieldDefinition::create('created')
  271. ->setLabel(t('Revision timestamp'))
  272. ->setDescription(t('The time that the current revision was created.'))
  273. ->setQueryable(FALSE)
  274. ->setRevisionable(TRUE);
  275. $fields['revision_uid'] = BaseFieldDefinition::create('entity_reference')
  276. ->setLabel(t('Revision user ID'))
  277. ->setDescription(t('The user ID of the author of the current revision.'))
  278. ->setSetting('target_type', 'user')
  279. ->setQueryable(FALSE)
  280. ->setRevisionable(TRUE);
  281. $fields['revision_translation_affected'] = BaseFieldDefinition::create('boolean')
  282. ->setLabel(t('Revision translation affected'))
  283. ->setDescription(t('Indicates if the last edit of a translation belongs to current revision.'))
  284. ->setReadOnly(TRUE)
  285. ->setRevisionable(TRUE)
  286. ->setTranslatable(TRUE);
  287. return $fields;
  288. }
  289. /**
  290. * Returns the node type label for the passed node.
  291. *
  292. * @param \Drupal\heartbeat\Entity\HeartbeatInterface $heartbeat
  293. * A heartbeat entity to return the heartbeat type's label for.
  294. *
  295. * @return string|false
  296. * The heartbeat type label or FALSE if the heartbeat type is not found.
  297. *
  298. * @todo Add this as generic helper method for config entities representing
  299. * entity bundles.
  300. */
  301. public function heartbeat_get_type(HeartbeatInterface $heartbeat) {
  302. $type = HeartbeatType::load($heartbeat->bundle());
  303. return $type ? $type->label() : FALSE;
  304. }
  305. /**
  306. * Updates all heartbeat activities of one type to be of another type.
  307. *
  308. * @param string $old_id
  309. * The current heartbeat type of the activities.
  310. * @param string $new_id
  311. * The new heartbeat type of the activities.
  312. *
  313. * @return
  314. * The number of activities whose heartbeat type field was modified.
  315. */
  316. public function heartbeat_type_update_nodes($old_id, $new_id) {
  317. return \Drupal::entityManager()->getStorage('heartbeat')->updateType($old_id, $new_id);
  318. }
  319. /**
  320. * Builds a message template for a given HeartbeatType
  321. *
  322. * @param HeartbeatType $heartbeatType
  323. * @param null $mediaData
  324. * @return null|string
  325. */
  326. public static function buildMessage(HeartbeatType $heartbeatType, $mediaData = NULL) {
  327. //!username has added !node_type !node_title. <a href="/node/"><img src="/sites/default/files/!node_image"/></a>
  328. /** @noinspection NestedTernaryOperatorInspection */
  329. $message = $heartbeatType->get('message') . '<a href="/node/!nid">';
  330. $message .= $mediaData ? self::buildMediaMarkup($mediaData) : '';
  331. $message .= '</a>';
  332. return $message;
  333. }
  334. private static function buildMediaMarkup($mediaData) {
  335. $markup = '';
  336. foreach ($mediaData as $media) {
  337. $markup .= self::mediaTag($media->type, $media->path);
  338. }
  339. return $markup;
  340. }
  341. private static function mediaTag($type, $filePath) {
  342. return '<'. $type . ' src="' . $filePath . '" / >';
  343. }
  344. /**
  345. * Returns class of argument
  346. *
  347. * @param $field
  348. * @return string
  349. */
  350. public static function findClass($field) {
  351. return get_class($field);
  352. }
  353. /**
  354. * Returns an array of classes for array argument
  355. * @param $fields
  356. * @return array
  357. */
  358. public static function findAllMedia($fields) {
  359. return array_map(array(get_called_class(), 'findClass'), $fields);
  360. }
  361. /**
  362. * Returns all media types for an array of fields
  363. *
  364. * @param $fields
  365. * @return array
  366. */
  367. public static function mediaFieldTypes($fields) {
  368. $types = array();
  369. foreach ($fields as $field) {
  370. if ($field instanceof \Drupal\file\Plugin\Field\FieldType\FileFieldItemList) {
  371. if ($field->getFieldDefinition()->getType() === 'image' ||
  372. $field->getFieldDefinition()->getType() === 'video' ||
  373. $field->getFieldDefinition()->getType() === 'audio') {
  374. $fieldValue = $field->getValue();
  375. $fileId = $fieldValue[0]['target_id'];
  376. $file = \Drupal::entityTypeManager()->getStorage('file')->load($fileId);
  377. if ($file !== NULL && is_object($file)) {
  378. $mediaObject = self::createHeartbeatMedia($field->getFieldDefinition()->getType(), $file->url());
  379. $types[] = $mediaObject;
  380. } else {
  381. continue;
  382. }
  383. }
  384. }
  385. }
  386. return $types;
  387. }
  388. /**
  389. * Parses a HeartbeatType message template and maps
  390. * variable values onto matching keywords
  391. *
  392. * @param $translatedMessage
  393. * @param $variables
  394. * @return string
  395. */
  396. public static function parseMessage($translatedMessage, $variables) {
  397. return strtr($translatedMessage, $variables);
  398. }
  399. public static function createHeartbeatMedia($type, $path) {
  400. $mediaObject = new \stdClass();
  401. $mediaObject->type = $type;
  402. $mediaObject->path = $path;
  403. return $mediaObject;
  404. }
  405. public static function getEntityNames($entityTypes) {
  406. $names = array();
  407. foreach ($entityTypes as $type) {
  408. if (($type->getBaseTable() === 'node') ||
  409. ($type->getBaseTable() === 'user')
  410. ||
  411. ($type->getStorageClass() !== NULL &&
  412. strpos($type->getStorageClass(), $type->getLabel()->getUntranslatedString())
  413. )
  414. ) {
  415. $names[] = $type->id();
  416. }
  417. }
  418. sort($names);
  419. return $names;
  420. }
  421. }