HtmlTrait.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace Tools\View\Helper;
  3. trait HtmlTrait {
  4. /**
  5. * Display image tag from blob content.
  6. * Enhancement for HtmlHelper. Defaults to png image
  7. *
  8. * Options:
  9. * - type: png, gif, jpg, ...
  10. *
  11. * @param string $content Data in binary form
  12. * @param array<string, mixed> $options Attributes
  13. * @return string HTML image tag
  14. */
  15. public function imageFromBlob($content, array $options = []) {
  16. $options += ['type' => 'png'];
  17. $mimeType = 'image/' . $options['type'];
  18. $text = 'data:' . $mimeType . ';base64,' . base64_encode($content);
  19. return $this->formatTemplate('image', [
  20. 'url' => $text,
  21. 'attrs' => $this->templater()->formatAttributes($options, ['block', 'link']),
  22. ]);
  23. }
  24. /**
  25. * Creates a reset HTML link.
  26. * The prefix and plugin params are resetting to default false.
  27. *
  28. * ### Options
  29. *
  30. * - `escape` Set to false to disable escaping of title and attributes.
  31. * - `escapeTitle` Set to false to disable escaping of title. Takes precedence
  32. * over value of `escape`)
  33. * - `confirm` JavaScript confirmation message.
  34. *
  35. * @param string $title The content to be wrapped by <a> tags.
  36. * @param array|string|null $url URL or array of URL parameters, or
  37. * external URL (starts with http://)
  38. * @param array<string, mixed> $options Array of options and HTML attributes.
  39. * @return string An `a` HTML element.
  40. */
  41. public function linkReset($title, $url = null, array $options = []) {
  42. if (is_array($url)) {
  43. $url += ['prefix' => false, 'plugin' => false];
  44. }
  45. return parent::link($title, $url, $options);
  46. }
  47. /**
  48. * Keep query string params for pagination/filter for this link,
  49. * e.g. after edit action.
  50. *
  51. * ### Options
  52. *
  53. * - `escape` Set to false to disable escaping of title and attributes.
  54. * - `escapeTitle` Set to false to disable escaping of title. Takes precedence
  55. * over value of `escape`)
  56. * - `confirm` JavaScript confirmation message.
  57. *
  58. * @param string $title The content to be wrapped by <a> tags.
  59. * @param array|string|null $url URL or array of URL parameters, or
  60. * external URL (starts with http://)
  61. * @param array<string, mixed> $options Array of options and HTML attributes.
  62. * @return string An `a` HTML element.
  63. */
  64. public function linkComplete($title, $url = null, array $options = []) {
  65. if (is_array($url)) {
  66. // Add query strings
  67. if (!isset($url['?'])) {
  68. $url['?'] = [];
  69. }
  70. $url['?'] += $this->_View->getRequest()->getQuery();
  71. $pass = $this->_View->getRequest()->getParam('pass');
  72. $url = array_merge($url, $pass);
  73. }
  74. return parent::link($title, $url, $options);
  75. }
  76. }