| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- namespace Tools\View\Helper;
- use Cake\Core\Configure;
- use Cake\View\Helper;
- /**
- * Common helper
- *
- * @author Mark Scherer
- * @property \Cake\View\Helper\HtmlHelper $Html
- * @property \Cake\View\Helper\UrlHelper $Url
- */
- class CommonHelper extends Helper {
- /**
- * @var array
- */
- protected array $helpers = ['Html', 'Url'];
- /**
- * Manual pluralizing a word using the Inflection class
- * //TODO: move to lib or bootstrap
- *
- * @param string $singular
- * @param string $plural
- * @param int $count
- * @param bool $autoTranslate
- * @return string result
- */
- public function sp(string $singular, string $plural, int $count, bool $autoTranslate = false): string {
- if ($count !== 1) {
- $result = $plural;
- } else {
- $result = $singular;
- }
- if ($autoTranslate) {
- $result = __($result);
- }
- return $result;
- }
- /**
- * Convenience method for clean ROBOTS allowance
- *
- * @param array<string>|string|null $type - private/public or array of index/follow/archtive,...
- * @return string HTML
- */
- public function metaRobots(array|string|null $type = null): string {
- $meta = Configure::read('Config.robots');
- if ($type === null && $meta !== null) {
- $type = $meta;
- }
- if ($type === null) {
- $type = ['noindex', 'nofollow', 'noarchive'];
- }
- if (is_array($type)) {
- $robots = $type;
- } elseif ($type === 'public') {
- $robots = ['index', 'follow', 'noarchive'];
- } else {
- $robots = ['noindex', 'nofollow', 'noarchive'];
- }
- $return = '<meta name="robots" content="' . implode(',', $robots) . '" />';
- return $return;
- }
- /**
- * Convenience method for clean meta name tags
- *
- * @param string|null $name Author, date, generator, revisit-after, language
- * @param array<string>|string|null $content If array, it will be separated by commas
- * @return string HTML Markup
- */
- public function metaName(?string $name = null, array|string|null $content = null): string {
- if (!$name || !$content) {
- return '';
- }
- $content = (array)$content;
- $return = '<meta name="' . $name . '" content="' . implode(', ', $content) . '">';
- return $return;
- }
- /**
- * Convenience method for meta description
- *
- * @param string $content
- * @param string|null $language (iso2: de, en-us, ...)
- * @param array<string, mixed> $options Additional options
- * @return string HTML Markup
- */
- public function metaDescription(string $content, ?string $language = null, array $options = []): string {
- if ($language) {
- $options['lang'] = mb_strtolower($language);
- } elseif ($language !== false) {
- $options['lang'] = Configure::read('Config.locale');
- }
- return $this->Html->meta('description', $content, $options);
- }
- /**
- * Convenience method to output meta keywords
- *
- * @param array|string|null $keywords
- * @param string|null $language (iso2: de, en-us, ...)
- * @param bool $escape
- * @return string HTML Markup
- */
- public function metaKeywords(array|string|null $keywords = null, ?string $language = null, bool $escape = true): string {
- if ($keywords === null) {
- $keywords = Configure::read('Config.keywords');
- }
- if (is_array($keywords)) {
- $keywords = implode(', ', $keywords);
- }
- if ($escape) {
- $keywords = h($keywords);
- }
- $options = [];
- if (!empty($language)) {
- $options['lang'] = mb_strtolower($language);
- } elseif ($language !== false) {
- $options['lang'] = Configure::read('Config.locale');
- }
- return $this->Html->meta('keywords', $keywords, $options);
- }
- /**
- * Convenience function for "canonical" SEO links
- *
- * @param array|string|null $url
- * @param bool $full
- * @return string HTML Markup
- */
- public function metaCanonical(array|string|null $url = null, bool $full = false): string {
- $canonical = $this->Url->build($url, ['fullBase' => $full]);
- $options = ['rel' => 'canonical', 'link' => $canonical];
- return $this->Html->meta($options);
- }
- /**
- * Convenience method for "alternate" SEO links
- *
- * @param array|string|null $url
- * @param array|string $lang (lang(iso2) or array of langs)
- * lang: language (in ISO 6391-1 format) + optionally the region (in ISO 3166-1 Alpha 2 format)
- * - de
- * - de-ch
- * etc
- * @param bool $full
- * @return string HTML Markup
- */
- public function metaAlternate(array|string|null $url, array|string $lang, bool $full = false): string {
- $url = $this->Url->build($url, ['fullBase' => $full]);
- $lang = (array)$lang;
- $res = [];
- foreach ($lang as $language => $countries) {
- if (is_numeric($language)) {
- $language = '';
- } else {
- $language .= '-';
- }
- $countries = (array)$countries;
- foreach ($countries as $country) {
- $l = $language . $country;
- $options = ['rel' => 'alternate', 'hreflang' => $l, 'link' => $url];
- $res[] = $this->Html->meta($options) . PHP_EOL;
- }
- }
- return implode('', $res);
- }
- /**
- * Convenience method for META Tags
- *
- * @param array|string|null $url
- * @param string|null $title
- * @return string HTML Markup
- */
- public function metaRss(array|string|null $url, ?string $title = null): string {
- $tags = [
- 'meta' => '<link rel="alternate" type="application/rss+xml" title="%s" href="%s">',
- ];
- if (!$title) {
- $title = __d('tools', 'Subscribe to this feed');
- } else {
- $title = h($title);
- }
- return sprintf($tags['meta'], $title, $this->Url->build($url));
- }
- /**
- * Convenience method for meta tags.
- *
- * @param string $type
- * @param string $value Content
- * @param bool $escape
- * @return string HTML Markup
- */
- public function metaEquiv(string $type, string $value, bool $escape = true): string {
- $tags = [
- 'meta' => '<meta http-equiv="%s"%s>',
- ];
- if ($escape) {
- $value = h($value);
- }
- return sprintf($tags['meta'], $type, ' content="' . $value . '"');
- }
- }
|