HeartbeatType.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. namespace Drupal\heartbeat\Entity;
  3. use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
  4. use Drupal\Core\Entity\EntityStorageInterface;
  5. use Drupal\heartbeat\HeartbeatTypeListBuilder;
  6. /**
  7. * Defines the Heartbeat type entity.
  8. *
  9. * @ConfigEntityType(
  10. * id = "heartbeat_type",
  11. * label = @Translation("Heartbeat type"),
  12. * handlers = {
  13. * "list_builder" = "Drupal\heartbeat\HeartbeatTypeListBuilder",
  14. * "form" = {
  15. * "add" = "Drupal\heartbeat\Form\HeartbeatTypeForm",
  16. * "edit" = "Drupal\heartbeat\Form\HeartbeatTypeForm",
  17. * "delete" = "Drupal\heartbeat\Form\HeartbeatTypeDeleteForm"
  18. * },
  19. * "route_provider" = {
  20. * "html" = "Drupal\heartbeat\HeartbeatTypeHtmlRouteProvider",
  21. * },
  22. * },
  23. * config_prefix = "heartbeat_type",
  24. * admin_permission = "administer site configuration",
  25. * bundle_of = "heartbeat",
  26. * entity_keys = {
  27. * "id" = "id",
  28. * "label" = "label",
  29. * "uuid" = "uuid"
  30. * },
  31. * links = {
  32. * "canonical" = "/admin/structure/heartbeat_type/{heartbeat_type}",
  33. * "add-form" = "/admin/structure/heartbeat_type/add",
  34. * "edit-form" = "/admin/structure/heartbeat_type/{heartbeat_type}/edit",
  35. * "delete-form" = "/admin/structure/heartbeat_type/{heartbeat_type}/delete",
  36. * "collection" = "/admin/structure/heartbeat_type"
  37. * }
  38. * )
  39. */
  40. class HeartbeatType extends ConfigEntityBundleBase implements HeartbeatTypeInterface {
  41. /**
  42. * The Heartbeat Stream ID.
  43. *
  44. * @var string
  45. */
  46. protected $id;
  47. protected $messageId;
  48. protected $hid;
  49. protected $description;
  50. protected $perms;
  51. protected $messageConcat;
  52. protected $arguments;
  53. protected $message;
  54. protected $variables;
  55. protected $attachments;
  56. protected $groupType;
  57. protected $mainentity;
  58. protected $entityManager;
  59. /**
  60. * The Heartbeat Stream label.
  61. *
  62. * @var string
  63. */
  64. protected $label;
  65. public static function getHeartbeatTypeEntity($messageId) {
  66. $entity_manager = \Drupal::entityTypeManager();
  67. }
  68. public function setMessageId($messageId) {
  69. $this->messageId = $messageId;
  70. }
  71. public function getMessageId() {
  72. return $this->messageId;
  73. }
  74. /**
  75. * Sets the description of the stream
  76. *
  77. * @param string $description
  78. * Describing streams of this type
  79. */
  80. public function setDescription($description) {
  81. $this->description = $description;
  82. }
  83. /**
  84. * Gets the description of the stream
  85. *
  86. * @return string
  87. * The Stream's description
  88. */
  89. public function getDescription() {
  90. return $this->description;
  91. }
  92. /**
  93. * Sets the translatable message
  94. * This message creates the structure of each message
  95. *
  96. * @param string $message
  97. * The template message serving as the foundation of each message structure of this stream type
  98. */
  99. public function setMessage($message) {
  100. $this->set('message', $message);
  101. }
  102. /**
  103. * Gets the translatable message of the stream
  104. *
  105. * @return string
  106. * The Stream's message
  107. */
  108. public function getMessage() {
  109. return $this->get('message');
  110. }
  111. // /**
  112. // * Sets the translatable concatenated message
  113. // *
  114. // * @param string $messageConcat
  115. // *
  116. // */
  117. // public function setMessageConcat($messageConcat) {
  118. // $this->messageConcat = $messageConcat;
  119. // }
  120. //
  121. // /**
  122. // * Gets the concatenated message of the stream
  123. // *
  124. // * @return string
  125. // * The Stream's concatenated message
  126. // */
  127. // public function getMessageConcat() {
  128. // return $this->messageConcat;
  129. // }
  130. /**
  131. * Sets the Permissions for this message stream
  132. *
  133. * @param int $perms
  134. *
  135. */
  136. public function setPerms($perms) {
  137. $this->perms = $perms;
  138. }
  139. /**
  140. * Gets the Permissions of this message stream
  141. *
  142. * @return int
  143. * The stream's permissions
  144. */
  145. public function getPerms() {
  146. return $this->perms;
  147. }
  148. /**
  149. * Sets the Group Type for this message stream
  150. *
  151. * @param string $groupType
  152. *
  153. */
  154. public function setGroupType($groupType) {
  155. $this->groupType = $groupType;
  156. }
  157. /**
  158. * Gets the Group Type of this message stream
  159. *
  160. * @return string
  161. * The stream's Group Type
  162. */
  163. public function getGroupType() {
  164. return $this->groupType;
  165. }
  166. /**
  167. * Sets the arguments for the concatenated message
  168. *
  169. * @param string $arguments
  170. *
  171. */
  172. public function setArguments($arguments) {
  173. $this->set('arguments', $arguments);
  174. // $this->arguments = $arguments;
  175. }
  176. /**
  177. * Gets the arguments for the concatenated message
  178. *
  179. * @return string
  180. * The stream's arguments for the concatenated message
  181. */
  182. public function getArguments() {
  183. return $this->get('arguments');
  184. // return $this->arguments;
  185. }
  186. /**
  187. * Sets the variables for this message stream
  188. *
  189. * @param string $variables
  190. *
  191. */
  192. public function setVariables($variables) {
  193. $this->variables = $variables;
  194. }
  195. /**
  196. * Gets the variables of this message stream
  197. *
  198. * @return string
  199. * The stream's variables
  200. */
  201. public function getVariables() {
  202. return $this->variables;
  203. }
  204. /**
  205. * @inheritDoc
  206. */
  207. public function __construct(array $values, $entity_type)
  208. {
  209. parent::__construct($values, $entity_type);
  210. $this->entityManager = \Drupal::entityManager();
  211. $this->entityTypeManager = \Drupal::entityTypeManager();
  212. }
  213. /**
  214. * @inheritDoc
  215. */
  216. protected function entityManager(){
  217. return parent::entityManager();
  218. }
  219. /**
  220. * @inheritDoc
  221. */
  222. protected function entityTypeManager() {
  223. return parent::entityTypeManager();
  224. }
  225. /**
  226. * {@inheritdoc}
  227. */
  228. public function isLocked() {
  229. $locked = \Drupal::state()->get('heartbeat.type.locked');
  230. return isset($locked[$this->id()]) ? $locked[$this->id()] : FALSE;
  231. }
  232. /**
  233. *
  234. */
  235. public function loadHeartbeatType() {
  236. $this->entityTypeManager->getDefinitions();
  237. }
  238. /**
  239. * {@inheritdoc}
  240. */
  241. public function postSave(EntityStorageInterface $storage, $update = TRUE) {
  242. parent::postSave($storage, $update);
  243. if ($update && $this->getOriginalId() != $this->id()) {
  244. $update_count = heartbeat_type_update_heartbeats($this->getOriginalId(), $this->id());
  245. if ($update_count) {
  246. drupal_set_message(\Drupal::translation()->formatPlural($update_count,
  247. 'Changed the heartbeat type of 1 activity from %old-type to %type.',
  248. 'Changed the heartbeat type of @count activities from %old-type to %type.',
  249. [
  250. '%old-type' => $this->getOriginalId(),
  251. '%type' => $this->id(),
  252. ]));
  253. }
  254. }
  255. if ($update) {
  256. // Clear the cached field definitions as some settings affect the field
  257. // definitions.
  258. $this->entityManager()->clearCachedFieldDefinitions();
  259. }
  260. }
  261. /**
  262. * {@inheritdoc}
  263. */
  264. public static function postDelete(EntityStorageInterface $storage, array $entities) {
  265. parent::postDelete($storage, $entities);
  266. // Clear the heartbeat type cache to reflect the removal.
  267. $storage->resetCache(array_keys($entities));
  268. }
  269. /**
  270. * Sets the main Entity Type of the Heartbeat Type
  271. *
  272. * @param string $mainentity
  273. * Describing entity type used in this Heartbeat Type
  274. */
  275. public function setMainEntity($mainentity) {
  276. $this->set('mainentity', $mainentity);
  277. }
  278. /**
  279. * @return mixed|null
  280. *
  281. */
  282. public function getMainEntity() {
  283. return $this->get('mainentity');
  284. }
  285. /**
  286. * Sets the bundle targeted for this Heartbeat type
  287. *
  288. * @param string $variables
  289. *
  290. */
  291. public function setBundle($bundle) {
  292. $this->set('bundle', $bundle);
  293. }
  294. /**
  295. * Gets the bundle of this Heartbeat type
  296. *
  297. * @return string
  298. * The stream's variables
  299. */
  300. public function getBundle() {
  301. return $this->get('bundle');
  302. }
  303. /**
  304. * @return mixed
  305. */
  306. public function setWeight($weight) {
  307. $this->set('weight', $weight);
  308. }
  309. /**
  310. * @param $weight
  311. * @return mixed
  312. */
  313. public function getWeight() {
  314. return $this->get('weight');
  315. }
  316. }