Heartbeat.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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\Core\Utility\Token;
  9. use Drupal\Core\Url;
  10. use Drupal\Core\Link;
  11. use Drupal\Core\Database\Database;
  12. use Drupal\taxonomy\Entity\Term;
  13. use Drupal\user\Entity\User;
  14. use Drupal\user\UserInterface;
  15. /**
  16. * Defines the Heartbeat entity.
  17. *
  18. * @ingroup heartbeat
  19. *
  20. * @ContentEntityType(
  21. * id = "heartbeat",
  22. * label = @Translation("Heartbeat"),
  23. * bundle_label = @Translation("Heartbeat type"),
  24. * handlers = {
  25. * "storage" = "Drupal\heartbeat\HeartbeatStorage",
  26. * "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
  27. * "list_builder" = "Drupal\heartbeat\HeartbeatListBuilder",
  28. * "views_data" = "Drupal\heartbeat\Entity\HeartbeatViewsData",
  29. * "translation" = "Drupal\heartbeat\HeartbeatTranslationHandler",
  30. *
  31. * "form" = {
  32. * "default" = "Drupal\heartbeat\Form\HeartbeatForm",
  33. * "add" = "Drupal\heartbeat\Form\HeartbeatForm",
  34. * "edit" = "Drupal\heartbeat\Form\HeartbeatForm",
  35. * "delete" = "Drupal\heartbeat\Form\HeartbeatDeleteForm",
  36. * },
  37. * "access" = "Drupal\heartbeat\HeartbeatAccessControlHandler",
  38. * "route_provider" = {
  39. * "html" = "Drupal\heartbeat\HeartbeatHtmlRouteProvider",
  40. * },
  41. * },
  42. * base_table = "heartbeat",
  43. * data_table = "heartbeat_field_data",
  44. * revision_table = "heartbeat_revision",
  45. * revision_data_table = "heartbeat_field_revision",
  46. * translatable = TRUE,
  47. * admin_permission = "administer heartbeat entities",
  48. * entity_keys = {
  49. * "id" = "id",
  50. * "revision" = "vid",
  51. * "bundle" = "type",
  52. * "label" = "name",
  53. * "uuid" = "uuid",
  54. * "uid" = "uid",
  55. * "nid" = "nid",
  56. * "langcode" = "langcode",
  57. * "status" = "status",
  58. * },
  59. * links = {
  60. * "canonical" = "/admin/structure/heartbeat/{heartbeat}",
  61. * "add-page" = "/admin/structure/heartbeat/add",
  62. * "add-form" = "/admin/structure/heartbeat/add/{heartbeat_type}",
  63. * "edit-form" = "/admin/structure/heartbeat/{heartbeat}/edit",
  64. * "delete-form" = "/admin/structure/heartbeat/{heartbeat}/delete",
  65. * "version-history" = "/admin/structure/heartbeat/{heartbeat}/revisions",
  66. * "revision" = "/admin/structure/heartbeat/{heartbeat}/revisions/{heartbeat_revision}/view",
  67. * "revision_revert" = "/admin/structure/heartbeat/{heartbeat}/revisions/{heartbeat_revision}/revert",
  68. * "translation_revert" = "/admin/structure/heartbeat/{heartbeat}/revisions/{heartbeat_revision}/revert/{langcode}",
  69. * "revision_delete" = "/admin/structure/heartbeat/{heartbeat}/revisions/{heartbeat_revision}/delete",
  70. * "collection" = "/admin/structure/heartbeat",
  71. * },
  72. * bundle_entity_type = "heartbeat_type",
  73. * field_ui_base_route = "entity.heartbeat_type.edit_form"
  74. * )
  75. */
  76. // Always block from display
  77. const HEARTBEAT_NONE = -1;
  78. // Display only activity messages that are mine or addressed to me
  79. const HEARTBEAT_PRIVATE = 0;
  80. // Only the person that is chosen by the actor, can see the message
  81. const HEARTBEAT_PUBLIC_TO_ADDRESSEE = 1;
  82. // Display activity message of all my user relations, described in contributed modules
  83. const HEARTBEAT_PUBLIC_TO_CONNECTED = 2;
  84. // Everyone can see this activity message, unless this type of message is set to private
  85. const HEARTBEAT_PUBLIC_TO_ALL = 4;
  86. //Group Types
  87. const HEARTBEAT_GROUP_NONE = 11;
  88. const HEARTBEAT_GROUP_SINGLE = 12;
  89. const HEARTBEAT_GROUP_SUMMARY = 13;
  90. const FILE_FIELD = 'Drupal\file\Plugin\Field\FieldType\FileFieldItemList';
  91. class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterface {
  92. use EntityChangedTrait;
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
  97. parent::preCreate($storage_controller, $values);
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function preSave(EntityStorageInterface $storage) {
  103. parent::preSave($storage);
  104. foreach (array_keys($this->getTranslationLanguages()) as $langcode) {
  105. $translation = $this->getTranslation($langcode);
  106. // If no owner has been set explicitly, make the anonymous user the owner.
  107. if (!$translation->getOwner()) {
  108. $translation->setOwnerId(0);
  109. }
  110. }
  111. // If no revision author has been set explicitly, make the heartbeat owner the
  112. // revision author.
  113. if (!$this->getRevisionUser()) {
  114. $this->setRevisionUserId($this->getOwnerId());
  115. }
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function getType() {
  121. return $this->bundle();
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function getName() {
  127. return $this->get('name')->value;
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function setName($name) {
  133. $this->set('name', $name);
  134. return $this;
  135. }
  136. /**
  137. * Gets the Heartbeat message.
  138. *
  139. * @return string
  140. * Message of the Heartbeat.
  141. */
  142. public function getMessage() {
  143. return $this->get('message');
  144. }
  145. /**
  146. * Sets the Heartbeat Message.
  147. *
  148. * @param $name
  149. * @return
  150. * @internal param string $message The Heartbeat Message
  151. */
  152. public function setMessage($message) {
  153. $this->set('message', $message);
  154. }
  155. /**
  156. * {@inheritdoc}
  157. */
  158. public function getCreatedTime() {
  159. return $this->get('created')->value;
  160. }
  161. /**
  162. * {@inheritdoc}
  163. */
  164. public function setCreatedTime($timestamp) {
  165. $this->set('created', $timestamp);
  166. return $this;
  167. }
  168. /**
  169. * {@inheritdoc}
  170. */
  171. public function getOwner() {
  172. return $this->get('uid')->entity;
  173. }
  174. /**
  175. * {@inheritdoc}
  176. */
  177. public function getOwnerId() {
  178. return $this->get('uid')->target_id;
  179. }
  180. /**
  181. * {@inheritdoc}
  182. */
  183. public function setOwnerId($uid) {
  184. $this->set('uid', $uid);
  185. return $this;
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. public function setOwner(UserInterface $account) {
  191. $this->set('uid', $account->id());
  192. return $this;
  193. }
  194. /**
  195. * {@inheritdoc}
  196. */
  197. public function isPublished() {
  198. return (bool) $this->getEntityKey('status');
  199. }
  200. /**
  201. * {@inheritdoc}
  202. */
  203. public function setPublished($published) {
  204. $this->set('status', $published ? TRUE : FALSE);
  205. return $this;
  206. }
  207. /**
  208. * {@inheritdoc}
  209. */
  210. public function getRevisionCreationTime() {
  211. return $this->get('revision_timestamp')->value;
  212. }
  213. /**
  214. * {@inheritdoc}
  215. */
  216. public function setRevisionCreationTime($timestamp) {
  217. $this->set('revision_timestamp', $timestamp);
  218. return $this;
  219. }
  220. /**
  221. * {@inheritdoc}
  222. */
  223. public function getRevisionUser() {
  224. return $this->get('revision_uid')->entity;
  225. }
  226. /**
  227. * {@inheritdoc}
  228. */
  229. public function setRevisionUserId($uid) {
  230. $this->set('revision_uid', $uid);
  231. return $this;
  232. }
  233. /**
  234. * {@inheritdoc}
  235. */
  236. public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  237. $fields = parent::baseFieldDefinitions($entity_type);
  238. $fields['uid'] = BaseFieldDefinition::create('entity_reference')
  239. ->setLabel(t('Authored by'))
  240. ->setDescription(t('The user ID of author of the Heartbeat entity.'))
  241. ->setRevisionable(TRUE)
  242. ->setSetting('target_type', 'user')
  243. ->setSetting('handler', 'default')
  244. ->setTranslatable(TRUE)
  245. ->setDisplayOptions('view', array(
  246. 'label' => 'hidden',
  247. 'type' => 'author',
  248. 'weight' => 0,
  249. ))
  250. ->setDisplayOptions('form', array(
  251. 'type' => 'entity_reference_autocomplete',
  252. 'weight' => 5,
  253. 'settings' => array(
  254. 'match_operator' => 'CONTAINS',
  255. 'size' => '60',
  256. 'autocomplete_type' => 'tags',
  257. 'placeholder' => '',
  258. ),
  259. ))
  260. ->setDisplayConfigurable('form', TRUE)
  261. ->setDisplayConfigurable('view', TRUE);
  262. $fields['nid'] = BaseFieldDefinition::create('entity_reference')
  263. ->setLabel(t('Node'))
  264. ->setDescription(t('The content associated with this Heartbeat'))
  265. ->setSetting('target_type', 'node')
  266. ->setSetting('handler', 'default')
  267. ->setDisplayOptions('view', array(
  268. 'label' => 'hidden',
  269. 'type' => 'content',
  270. 'weight' => 0,
  271. ))
  272. ->setDisplayConfigurable('view', TRUE)
  273. ->setRevisionable(TRUE);
  274. $fields['name'] = BaseFieldDefinition::create('string')
  275. ->setLabel(t('Name'))
  276. ->setDescription(t('The name of the Heartbeat entity.'))
  277. ->setRevisionable(TRUE)
  278. ->setSettings(array(
  279. 'max_length' => 50,
  280. 'text_processing' => 0,
  281. ))
  282. ->setDefaultValue('')
  283. ->setDisplayOptions('view', array(
  284. 'label' => 'above',
  285. 'type' => 'string',
  286. 'weight' => -4,
  287. ))
  288. ->setDisplayOptions('form', array(
  289. 'type' => 'string_textfield',
  290. 'weight' => -4,
  291. ))
  292. ->setDisplayConfigurable('form', TRUE)
  293. ->setDisplayConfigurable('view', TRUE);
  294. $fields['message'] = BaseFieldDefinition::create('string_long')
  295. ->setLabel(t('Message'))
  296. ->setDescription(t('The message of the Heartbeat entity.'))
  297. ->setRevisionable(TRUE)
  298. ->setDisplayOptions('view', array(
  299. 'label' => 'above',
  300. 'type' => 'full_html',
  301. 'weight' => -4,
  302. ))
  303. ->setDisplayConfigurable('view', TRUE);
  304. $fields['comments'] = BaseFieldDefinition::create('comment')
  305. ->setLabel(t('Kommentare'))
  306. ->setDescription(t('Kommentare.'))
  307. ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
  308. ->setSettings(
  309. array(
  310. 'default_mode'=> 1,
  311. 'per_page'=>50,
  312. 'anonymous'=> 0,
  313. 'form_location'=>1,
  314. 'preview'=> 1,
  315. 'comment_type'=>'heartbeat_comment',
  316. 'locked'=>false,
  317. ))
  318. ->setDefaultValue(
  319. array(
  320. 'status'=>2,
  321. 'cid'=>0,
  322. 'last_comment_timestamp'=> 0,
  323. 'last_comment_name'=> null,
  324. 'last_comment_uid'=> 0,
  325. 'comment_count'=> 0,
  326. )
  327. )
  328. ->setDisplayOptions('form', array(
  329. 'type' => 'comment_default',
  330. 'settings' => array(
  331. 'form_location' => 1,
  332. 'default_mode'=> 1,
  333. 'per_page'=>50,
  334. 'anonymous'=> 0,
  335. 'preview'=> 1,
  336. 'comment_type'=>'heartbeat_comment',
  337. 'locked'=>false,
  338. ),
  339. 'weight' => 1,
  340. ))
  341. ->setDisplayConfigurable('form', TRUE)
  342. ->setDisplayConfigurable('view', TRUE);
  343. $fields['status'] = BaseFieldDefinition::create('boolean')
  344. ->setLabel(t('Publishing status'))
  345. ->setDescription(t('A boolean indicating whether the Heartbeat is published.'))
  346. ->setRevisionable(TRUE)
  347. ->setDefaultValue(TRUE);
  348. $fields['created'] = BaseFieldDefinition::create('created')
  349. ->setLabel(t('Created'))
  350. ->setDescription(t('The time that the entity was created.'));
  351. $fields['changed'] = BaseFieldDefinition::create('changed')
  352. ->setLabel(t('Changed'))
  353. ->setDescription(t('The time that the entity was last edited.'));
  354. $fields['revision_timestamp'] = BaseFieldDefinition::create('created')
  355. ->setLabel(t('Revision timestamp'))
  356. ->setDescription(t('The time that the current revision was created.'))
  357. ->setQueryable(FALSE)
  358. ->setRevisionable(TRUE);
  359. $fields['revision_uid'] = BaseFieldDefinition::create('entity_reference')
  360. ->setLabel(t('Revision user ID'))
  361. ->setDescription(t('The user ID of the author of the current revision.'))
  362. ->setSetting('target_type', 'user')
  363. ->setQueryable(FALSE)
  364. ->setRevisionable(TRUE);
  365. $fields['revision_translation_affected'] = BaseFieldDefinition::create('boolean')
  366. ->setLabel(t('Revision translation affected'))
  367. ->setDescription(t('Indicates if the last edit of a translation belongs to current revision.'))
  368. ->setReadOnly(TRUE)
  369. ->setRevisionable(TRUE)
  370. ->setTranslatable(TRUE);
  371. return $fields;
  372. }
  373. /**
  374. * Returns the node type label for the passed node.
  375. *
  376. * @param \Drupal\heartbeat\Entity\HeartbeatInterface $heartbeat
  377. * A heartbeat entity to return the heartbeat type's label for.
  378. *
  379. * @return string|false
  380. * The heartbeat type label or FALSE if the heartbeat type is not found.
  381. *
  382. * @todo Add this as generic helper method for config entities representing
  383. * entity bundles.
  384. */
  385. public function heartbeat_get_type(HeartbeatInterface $heartbeat) {
  386. $type = HeartbeatType::load($heartbeat->bundle());
  387. return $type ? $type->label() : FALSE;
  388. }
  389. /**
  390. * Updates all heartbeat activities of one type to be of another type.
  391. *
  392. * @param string $old_id
  393. * The current heartbeat type of the activities.
  394. * @param string $new_id
  395. * The new heartbeat type of the activities.
  396. *
  397. * @return
  398. * The number of activities whose heartbeat type field was modified.
  399. */
  400. public function heartbeat_type_update_nodes($old_id, $new_id) {
  401. return \Drupal::entityManager()->getStorage('heartbeat')->updateType($old_id, $new_id);
  402. }
  403. /**
  404. * Builds a message template for a given HeartbeatType
  405. *
  406. * @param HeartbeatType $heartbeatType
  407. * @param null $mediaData
  408. * @return null|string
  409. */
  410. public static function buildMessage(Token $tokenService, $preparsedMessage, $entities = NULL, $entityType, $mediaData = NULL) {
  411. $arbitrarious = 'nothing at all';
  412. $naul = 'nullll';
  413. $preparsedMessage = self::wrapOwner($preparsedMessage, $entities);
  414. switch (true) {
  415. case $entityType === 'node':
  416. // $parsedMessage = $tokenService->replace($preparsedMessage . '<a class="heartbeat-node" href="/node/[node:nid]">', $entities);
  417. $parsedMessage = $tokenService->replace($preparsedMessage, $entities);
  418. if (strpos($parsedMessage, '#')) {
  419. self::parseHashtags($parsedMessage);
  420. }
  421. if (strpos($parsedMessage, '@')) {
  422. self::parseUsernames($parsedMessage);
  423. }
  424. /** @noinspection NestedTernaryOperatorInspection */
  425. $message = $parsedMessage;
  426. $message .= $mediaData ? self::buildMediaMarkup($mediaData) : '';
  427. // $message .= '</a>';
  428. return $message;
  429. break;
  430. case $entityType === 'status':
  431. $parsedMessage = $tokenService->replace($preparsedMessage . '<a class="status-post" href="/admin/structure/' . $entityType . '/[' . $entityType . ':id]">', $entities);
  432. /** @noinspection NestedTernaryOperatorInspection */
  433. $message = $parsedMessage;
  434. $message .= $mediaData ? self::buildMediaMarkup($mediaData) : 'Post';
  435. $message .= '</a>';
  436. return $message;
  437. break;
  438. case $entityType === 'user':
  439. break;
  440. case $entityType === 'flag':
  441. $returnMessage = self::handleMultipleEntities($tokenService, $preparsedMessage, $entities);
  442. return strlen($returnMessage) > 0 ? $returnMessage : "Error creating message";
  443. break;
  444. }
  445. }
  446. private static function buildMediaMarkup($mediaData) {
  447. $markup = '';
  448. foreach ($mediaData as $media) {
  449. $markup .= self::mediaTag($media->type, $media->path);
  450. }
  451. return $markup;
  452. }
  453. private static function mediaTag($type, $filePath) {
  454. //TODO put this into new method
  455. if ($type == 'image') {
  456. $type = 'img';
  457. return '<' . $type . ' src="' . str_replace('public://', '/sites/default/files/', $filePath) . '" class="heartbeat-image" / >';
  458. } else if ($type == 'youtube') {
  459. $filePath = str_replace('youtube://', 'https://www.youtube.com/embed/', $filePath);
  460. return '<iframe class="heartbeat-youtube" width="auto" height="auto" src="' . $filePath . '" frameborder="0"></iframe>';
  461. } else if ($type == 'video') {
  462. return '<' . $type . ' controls src="' . str_replace('public://', '/sites/default/files/', $filePath) . '" class="heartbeat-video"></' . $type . '>';
  463. }
  464. }
  465. protected static function handleMultipleEntities(Token $tokenService, $message, $entities) {
  466. $tokens = $tokenService->scan($message);
  467. foreach($tokens as $key => $token) {
  468. foreach ($token as $type) {
  469. if (substr_count($message, $type) > 1) {
  470. foreach ($entities as $entityKey => $entityValue) {
  471. if ($entityValue instanceof \stdClass && count($entityValue->entities) > 1) {
  472. if ($key == $entityValue->type) {
  473. $messageArray = explode($type, $message);
  474. $stringRebuild = array();
  475. $replacements = array();
  476. $i = 0;
  477. foreach ($entityValue->entities as $entity) {
  478. $stringRebuild[] = $tokenService->replace($message, array($key => $entity));
  479. foreach (self::getWordRepeats($stringRebuild[$i]) as $word => $num) {
  480. if ($num > 1 && !strpos($messageArray[1], $word)) {
  481. $replacements[] = $word;
  482. }
  483. }
  484. $i++;
  485. }
  486. if (count($replacements) == 2) {
  487. $uid = $entityValue->entities[0]->id();
  488. $uid_target = $entityValue->entities[1]->id();
  489. $query = Database::getConnection()->query('
  490. SELECT status
  491. FROM heartbeat_friendship
  492. WHERE uid = :uid AND uid_target = :uid_target', array(
  493. ':uid' => $uid,
  494. ':uid_target' => $uid_target
  495. )
  496. );
  497. if ($query->fetchCol()[0] < 1) {
  498. $messageArray[1] = ' has requested friendship with ';
  499. }
  500. $user1Link = Link::fromTextAndUrl($replacements[0], $entityValue->entities[0]->toUrl());
  501. $user2Link = Link::fromTextAndUrl($replacements[1], $entityValue->entities[1]->toUrl());
  502. $rebuiltMessage = $user1Link->toString() . $messageArray[1] . $user2Link->toString();
  503. return $rebuiltMessage;
  504. }
  505. }
  506. }
  507. }
  508. }
  509. }
  510. }
  511. return null;
  512. }
  513. public static function parseHashtags(&$message) {
  514. $lastRow = false;
  515. $tagsArray = explode('#', $message);
  516. $i = 0;
  517. $num = count($tagsArray);
  518. foreach ($tagsArray as $hashtag) {
  519. if ($i === $num - 1) {
  520. $lastTagArray = explode(' ', $hashtag);
  521. if (strlen($lastTagArray[1]) > 1) {
  522. $hashtag = trim($lastTagArray[0]);
  523. $lastRow = true;
  524. $remainder = '';
  525. $lastRowArgCount = count($lastTagArray);
  526. for ($x = 1; $x < $lastRowArgCount; $x++) {
  527. $remainder .= ' ' . $lastTagArray[$x];
  528. }
  529. }
  530. }
  531. $tid = \Drupal::entityQuery("taxonomy_term")
  532. ->condition("name", trim($hashtag))
  533. ->condition('vid', [
  534. 'twitter', 'tags', 'kekistan'
  535. ], 'IN')
  536. ->execute();
  537. if (count($tid) > 0) {
  538. $term = Term::load(array_values($tid)[0]);
  539. $link = Link::fromTextAndUrl('#' . $hashtag, $term->toUrl());
  540. $tagsArray[$i] = '<div class="heartbeat-hashtag">';
  541. $tagsArray[$i] .= !$lastRow ? $link->toString() . ' </div>' : $link->toString() . '</div>' . $remainder;
  542. }
  543. $i++;
  544. }
  545. $message = '';
  546. foreach ($tagsArray as $replacements) {
  547. $message .= $replacements;
  548. }
  549. }
  550. public static function parseUsernames(&$message) {
  551. $lastRow = false;
  552. $usersArray = explode('@', $message);
  553. $i = 0;
  554. $num = count($usersArray);
  555. foreach ($usersArray as $username) {
  556. if ($i === $num - 1) {
  557. $lastuserArray = explode(' ', $username);
  558. if (strlen($lastuserArray[1]) > 1) {
  559. $username = trim($lastuserArray[0]);
  560. $lastRow = true;
  561. $remainder = '';
  562. $lastRowArgCount = count($lastuserArray);
  563. for ($x = 1; $x < $lastRowArgCount; $x++) {
  564. $remainder .= ' ' . $lastuserArray[$x];
  565. }
  566. }
  567. }
  568. $tid = \Drupal::entityQuery("taxonomy_term")
  569. ->condition("name", trim($username))
  570. ->condition('vid', [
  571. 'twitter_user', 'usernames'
  572. ], 'IN'
  573. )
  574. ->execute();
  575. if (count($tid) > 0) {
  576. $term = Term::load(array_values($tid)[0]);
  577. $link = Link::fromTextAndUrl('@' . $username, $term->toUrl());
  578. $usersArray[$i] = '<div class="heartbeat-username">';
  579. $usersArray[$i] .= !$lastRow ? $link->toString() . ' </div>' : $link->toString() . '</div>' . $remainder;
  580. }
  581. $i++;
  582. }
  583. $message = '';
  584. foreach ($usersArray as $replacements) {
  585. $message .= $replacements;
  586. }
  587. }
  588. /**
  589. * Helper method to identify the number of times a word is repeated in a phrase
  590. *
  591. * @param $phrase
  592. * @return array
  593. */
  594. public static function getWordRepeats($phrase) {
  595. $counts = array();
  596. $words = explode(' ', $phrase);
  597. foreach ($words as $word) {
  598. if (!array_key_exists($word, $counts)) {
  599. $counts[$word] = 0;
  600. }
  601. $word = preg_replace("#[^a-zA-Z\-]#", "", $word);
  602. ++$counts[$word];
  603. }
  604. return $counts;
  605. }
  606. /**
  607. * Returns class of argument
  608. *
  609. * @param $field
  610. * @return string
  611. */
  612. public static function findClass($field) {
  613. return get_class($field);
  614. }
  615. /**
  616. * Returns an array of classes for array argument
  617. * @param $fields
  618. * @return array
  619. */
  620. public static function findAllMedia($fields) {
  621. return array_map(array(get_called_class(), 'findClass'), $fields);
  622. }
  623. /**
  624. * Returns all media types for an array of fields
  625. *
  626. * @param $fields
  627. * @return array
  628. * @throws \Drupal\Core\Entity\Exception\UndefinedLinkTemplateException
  629. * @throws \Drupal\Core\Entity\EntityMalformedException
  630. */
  631. public static function mediaFieldTypes($fields) {
  632. $types = array();
  633. foreach ($fields as $field) {
  634. if ($field instanceof \Drupal\file\Plugin\Field\FieldType\FileFieldItemList) {
  635. if ($field->getFieldDefinition()->getType() === 'image' ||
  636. $field->getFieldDefinition()->getType() === 'video' ||
  637. $field->getFieldDefinition()->getType() === 'audio') {
  638. $fieldValue = $field->getValue();
  639. foreach ($fieldValue as $value) {
  640. $file = \Drupal::entityTypeManager()->getStorage('file')->load($value['target_id']);
  641. if ($file !== NULL && is_object($file)) {
  642. $url = Url::fromUri($file->getFileUri());
  643. $posfind = strpos($url->getUri(), 'youtube://');
  644. if ($posfind !== 0 && $posfind === false) {
  645. $mediaObject = self::createHeartbeatMedia($field->getFieldDefinition()->getType(), $url->getUri());
  646. } else {
  647. $mediaObject = self::createHeartbeatMedia('youtube', $url->getUri());
  648. }
  649. $types[] = $mediaObject;
  650. } else {
  651. continue;
  652. }
  653. }
  654. }
  655. }
  656. }
  657. return $types;
  658. }
  659. /**
  660. * Parses a HeartbeatType message template and maps
  661. * variable values onto matching keywords
  662. *
  663. * @param $translatedMessage
  664. * @param $variables
  665. * @return string
  666. */
  667. public static function parseMessage($translatedMessage, $variables) {
  668. return strtr($translatedMessage, $variables);
  669. }
  670. public static function createHeartbeatMedia($type, $path) {
  671. $mediaObject = new \stdClass();
  672. $mediaObject->type = $type;
  673. $mediaObject->path = $path;
  674. return $mediaObject;
  675. }
  676. public static function getEntityNames($entityTypes) {
  677. $names = array();
  678. foreach ($entityTypes as $type) {
  679. if (($type->getBaseTable() === 'node') ||
  680. ($type->getBaseTable() === 'user') ||
  681. ($type->getBaseTable() === 'status')
  682. ||
  683. ($type->getStorageClass() !== NULL &&
  684. strpos($type->getStorageClass(), $type->getLabel()->getUntranslatedString())
  685. )
  686. ) {
  687. $names[] = $type->id();
  688. }
  689. }
  690. sort($names);
  691. return $names;
  692. }
  693. private static function wrapOwner($message, $entities) {
  694. foreach ($entities as $entity) {
  695. if ($entity instanceof User) {
  696. return str_replace(
  697. '[user:account-name]',
  698. '<a class="heartbeat-user" href="user/' . $entity->id() . '">[user:account-name]</a>', $message);
  699. }
  700. }
  701. return $message;
  702. }
  703. /**
  704. * Updates the friendship status of these two users
  705. *
  706. * @param $uid
  707. * @param $uid_target
  708. * @param $unixtime
  709. * @param $friendStatus
  710. * @return \Drupal\Core\Database\StatementInterface|int|null
  711. */
  712. public static function updateFriendship($uid, $uid_target, $unixtime, $friendStatus) {
  713. // $query = Database::getConnection()->upsert('heartbeat_friendship')
  714. // ->fields(array(
  715. // 'uid' => $uid,
  716. // 'uid_target' => $uid_target,
  717. // 'created' => $unixtime,
  718. // 'status' => $friendStatus,
  719. // ))
  720. // ->key('uid_relation');
  721. // return $query->execute();
  722. $update = Database::getConnection()->update('heartbeat_friendship')
  723. ->fields(['status' => $friendStatus])
  724. ->condition('uid', $uid, '=')
  725. ->condition('uid_target', $uid_target, '=');
  726. if (!$update->execute()) {
  727. $insert = Database::getConnection()->insert('heartbeat_friendship')
  728. ->fields([
  729. 'uid' => $uid,
  730. 'uid_target' => $uid_target,
  731. 'created' => $unixtime,
  732. 'status' => $friendStatus
  733. ]);
  734. if (!$insert->execute()) {
  735. \Drupal::logger('Heartbeat')->error('Unable to update friendship between %uid and %uid_target', array('%uid' => $uid, '%uid_target' => $uid_target));
  736. }
  737. }
  738. if ($friendStatus === 1) {
  739. $update2 = Database::getConnection()->update('heartbeat_friendship')
  740. ->fields(['status' => $friendStatus])
  741. ->condition('uid', $uid_target, '=')
  742. ->condition('uid_target', $uid, '=');
  743. if (!$update2->execute()) {
  744. $insert2 = Database::getConnection()->insert('heartbeat_friendship')
  745. ->fields([
  746. 'uid' => $uid_target,
  747. 'uid_target' => $uid,
  748. 'created' => $unixtime,
  749. 'status' => $friendStatus
  750. ]);
  751. if (!$insert2->execute()) {
  752. \Drupal::logger('Heartbeat')->error('Unable to update friendship between %uid and %uid_target', array('%uid' => $uid_target, '%uid_target' => $uid));
  753. }
  754. }
  755. }
  756. return null;
  757. }
  758. /**
  759. * @param $tid
  760. */
  761. public static function updateTermUsage($tid, $vid = null) {
  762. $update = Database::getConnection()->update('taxonomy_term__field_count')
  763. ->expression('field_count_value', 'field_count_value + 1')
  764. ->condition('entity_id', $tid);
  765. if (!$update->execute()) {
  766. $insert = Database::getConnection()->insert('taxonomy_term__field_count')
  767. ->fields([
  768. 'entity_id' => $tid,
  769. 'revision_id' => $tid,
  770. 'bundle' => $vid,
  771. 'langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId(),
  772. 'delta' => 0,
  773. 'field_count_value' => 1]);
  774. if (!$insert->execute()) {
  775. \Drupal::logger('Heartbeat')
  776. ->error('Unable to update term counts for Term with ID = %tid', array('%tid' => $tid));
  777. } else {
  778. self::newTermUsage($tid);
  779. }
  780. } else {
  781. self::newTermUsage($tid);
  782. }
  783. }
  784. /**
  785. * @param $tid
  786. */
  787. public static function newTermUsage($tid) {
  788. $insert = Database::getConnection()->insert('heartbeat_term_usage')
  789. ->fields(['tid' => $tid, 'timestamp' => time()]);
  790. if (!$insert->execute()) {
  791. \Drupal::logger('Heartbeat')->error('Unable to update term usage for Term with ID = %tid', array('%tid' => $tid));
  792. }
  793. }
  794. /**
  795. * Gets the Heartbeat user.
  796. *
  797. * @return int
  798. * The uid of the Heartbeat's user.
  799. */
  800. public function getUid()
  801. {
  802. // TODO: Implement getUid() method.
  803. }
  804. /**
  805. * Sets the Heartbeat user.
  806. *
  807. * @param int uid
  808. * The Heartbeat user.
  809. *
  810. */
  811. public function setUid($uid)
  812. {
  813. // TODO: Implement setUid() method.
  814. }
  815. /**
  816. * Gets the Heartbeat's associated node nid.
  817. *
  818. * @return int
  819. * The nid of the Heartbeat's associated node.
  820. */
  821. public function getNid() {
  822. return $this->get('nid');
  823. }
  824. /**
  825. * Sets the Heartbeat user.
  826. *
  827. * @param int uid
  828. * The Heartbeat user.
  829. *
  830. */
  831. public function setNid($nid) {
  832. $this->set('nid', $nid);
  833. }
  834. }