CommonHelper.php 5.9 KB

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