CommonHelper.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace Tools\View\Helper;
  3. use Cake\Core\Configure;
  4. use Cake\View\Helper;
  5. /**
  6. * Common helper
  7. *
  8. * @author Mark Scherer
  9. * @property \Cake\View\Helper\HtmlHelper $Html
  10. * @property \Cake\View\Helper\UrlHelper $Url
  11. */
  12. class CommonHelper extends Helper {
  13. /**
  14. * @var array
  15. */
  16. protected array $helpers = ['Html', 'Url'];
  17. /**
  18. * Manual pluralizing a word using the Inflection class
  19. * //TODO: move to lib or bootstrap
  20. *
  21. * @param string $singular
  22. * @param string $plural
  23. * @param int $count
  24. * @param bool $autoTranslate
  25. * @return string result
  26. */
  27. public function sp(string $singular, string $plural, int $count, bool $autoTranslate = false): string {
  28. if ($count !== 1) {
  29. $result = $plural;
  30. } else {
  31. $result = $singular;
  32. }
  33. if ($autoTranslate) {
  34. $result = __($result);
  35. }
  36. return $result;
  37. }
  38. /**
  39. * Convenience method for clean ROBOTS allowance
  40. *
  41. * @param array<string>|string|null $type - private/public or array of index/follow/archtive,...
  42. * @return string HTML
  43. */
  44. public function metaRobots(array|string|null $type = null): string {
  45. $meta = Configure::read('Config.robots');
  46. if ($type === null && $meta !== null) {
  47. $type = $meta;
  48. }
  49. if ($type === null) {
  50. $type = ['noindex', 'nofollow', 'noarchive'];
  51. }
  52. if (is_array($type)) {
  53. $robots = $type;
  54. } elseif ($type === 'public') {
  55. $robots = ['index', 'follow', 'noarchive'];
  56. } else {
  57. $robots = ['noindex', 'nofollow', 'noarchive'];
  58. }
  59. $return = '<meta name="robots" content="' . implode(',', $robots) . '" />';
  60. return $return;
  61. }
  62. /**
  63. * Convenience method for clean meta name tags
  64. *
  65. * @param string|null $name Author, date, generator, revisit-after, language
  66. * @param array<string>|string|null $content If array, it will be separated by commas
  67. * @return string HTML Markup
  68. */
  69. public function metaName(?string $name = null, array|string|null $content = null): string {
  70. if (!$name || !$content) {
  71. return '';
  72. }
  73. $content = (array)$content;
  74. $return = '<meta name="' . $name . '" content="' . implode(', ', $content) . '">';
  75. return $return;
  76. }
  77. /**
  78. * Convenience method for meta description
  79. *
  80. * @param string $content
  81. * @param string|null $language (iso2: de, en-us, ...)
  82. * @param array<string, mixed> $options Additional options
  83. * @return string HTML Markup
  84. */
  85. public function metaDescription(string $content, ?string $language = null, array $options = []): string {
  86. if ($language) {
  87. $options['lang'] = mb_strtolower($language);
  88. } elseif ($language !== false) {
  89. $options['lang'] = Configure::read('Config.locale');
  90. }
  91. return $this->Html->meta('description', $content, $options);
  92. }
  93. /**
  94. * Convenience method to output meta keywords
  95. *
  96. * @param array|string|null $keywords
  97. * @param string|null $language (iso2: de, en-us, ...)
  98. * @param bool $escape
  99. * @return string HTML Markup
  100. */
  101. public function metaKeywords(array|string|null $keywords = null, ?string $language = null, bool $escape = true): string {
  102. if ($keywords === null) {
  103. $keywords = Configure::read('Config.keywords');
  104. }
  105. if (is_array($keywords)) {
  106. $keywords = implode(', ', $keywords);
  107. }
  108. if ($escape) {
  109. $keywords = h($keywords);
  110. }
  111. $options = [];
  112. if (!empty($language)) {
  113. $options['lang'] = mb_strtolower($language);
  114. } elseif ($language !== false) {
  115. $options['lang'] = Configure::read('Config.locale');
  116. }
  117. return $this->Html->meta('keywords', $keywords, $options);
  118. }
  119. /**
  120. * Convenience function for "canonical" SEO links
  121. *
  122. * @param array|string|null $url
  123. * @param bool $full
  124. * @return string HTML Markup
  125. */
  126. public function metaCanonical(array|string|null $url = null, bool $full = false): string {
  127. $canonical = $this->Url->build($url, ['fullBase' => $full]);
  128. $options = ['rel' => 'canonical', 'link' => $canonical];
  129. return $this->Html->meta($options);
  130. }
  131. /**
  132. * Convenience method for "alternate" SEO links
  133. *
  134. * @param array|string|null $url
  135. * @param array|string $lang (lang(iso2) or array of langs)
  136. * lang: language (in ISO 6391-1 format) + optionally the region (in ISO 3166-1 Alpha 2 format)
  137. * - de
  138. * - de-ch
  139. * etc
  140. * @param bool $full
  141. * @return string HTML Markup
  142. */
  143. public function metaAlternate(array|string|null $url, array|string $lang, bool $full = false): string {
  144. $url = $this->Url->build($url, ['fullBase' => $full]);
  145. $lang = (array)$lang;
  146. $res = [];
  147. foreach ($lang as $language => $countries) {
  148. if (is_numeric($language)) {
  149. $language = '';
  150. } else {
  151. $language .= '-';
  152. }
  153. $countries = (array)$countries;
  154. foreach ($countries as $country) {
  155. $l = $language . $country;
  156. $options = ['rel' => 'alternate', 'hreflang' => $l, 'link' => $url];
  157. $res[] = $this->Html->meta($options) . PHP_EOL;
  158. }
  159. }
  160. return implode('', $res);
  161. }
  162. /**
  163. * Convenience method for META Tags
  164. *
  165. * @param array|string|null $url
  166. * @param string|null $title
  167. * @return string HTML Markup
  168. */
  169. public function metaRss(array|string|null $url, ?string $title = null): string {
  170. $tags = [
  171. 'meta' => '<link rel="alternate" type="application/rss+xml" title="%s" href="%s">',
  172. ];
  173. if (!$title) {
  174. $title = __d('tools', 'Subscribe to this feed');
  175. } else {
  176. $title = h($title);
  177. }
  178. return sprintf($tags['meta'], $title, $this->Url->build($url));
  179. }
  180. /**
  181. * Convenience method for meta tags.
  182. *
  183. * @param string $type
  184. * @param string $value Content
  185. * @param bool $escape
  186. * @return string HTML Markup
  187. */
  188. public function metaEquiv(string $type, string $value, bool $escape = true): string {
  189. $tags = [
  190. 'meta' => '<meta http-equiv="%s"%s>',
  191. ];
  192. if ($escape) {
  193. $value = h($value);
  194. }
  195. return sprintf($tags['meta'], $type, ' content="' . $value . '"');
  196. }
  197. }