/plugins/SaitoHelp/src/View/Helper/SaitoHelpHelper.php

https://github.com/Schlaefer/Saito · PHP · 112 lines · 59 code · 17 blank · 36 comment · 5 complexity · 68e24843c3034e3ea95f93e4aaa8d273 MD5 · raw file

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Saito - The Threaded Web Forum
  5. *
  6. * @copyright Copyright (c) the Saito Project Developers
  7. * @link https://github.com/Schlaefer/Saito
  8. * @license http://opensource.org/licenses/MIT
  9. */
  10. namespace SaitoHelp\View\Helper;
  11. use App\View\Helper\LayoutHelper;
  12. use Cake\View\Helper;
  13. use Cake\View\Helper\HtmlHelper;
  14. use Cake\View\Helper\UrlHelper;
  15. use Commonmark\View\Helper\CommonmarkHelper;
  16. use Saito\User\CurrentUser\CurrentUserInterface;
  17. /**
  18. * Helper for Saito-help
  19. *
  20. * @property CommonmarkHelper $Commonmark
  21. * @property HtmlHelper $Html
  22. * @property LayoutHelper $Layout
  23. * @property UrlHelper $Url
  24. */
  25. class SaitoHelpHelper extends Helper
  26. {
  27. public $helpers = ['Commonmark.Commonmark', 'Html', 'Layout', 'Url'];
  28. /**
  29. * Create a help icon linking to a help page
  30. *
  31. * @param string $id help page id
  32. * @param array $options options
  33. * @return mixed
  34. */
  35. public function icon($id, array $options = [])
  36. {
  37. $options += ['label' => '', 'target' => '_blank'];
  38. $options = ['class' => 'shp-icon', 'escape' => false] + $options;
  39. if ($options['label'] === true) {
  40. $options['label'] = __('Help');
  41. }
  42. if (!empty($options['label'])) {
  43. $options['label'] = h($options['label']);
  44. }
  45. $title = $this->Layout->textWithIcon(
  46. $options['label'],
  47. 'question-circle'
  48. );
  49. unset($options['label']);
  50. return $this->Html->link($title, "/help/$id", $options);
  51. }
  52. /**
  53. * Parse text
  54. *
  55. * @param string $text text to parse
  56. * @param CurrentUserInterface $CurrentUser current user
  57. * @return string
  58. */
  59. public function parse($text, CurrentUserInterface $CurrentUser)
  60. {
  61. $text = $this->_replaceUrl($text, $CurrentUser);
  62. return $this->Commonmark->parse($text);
  63. }
  64. /**
  65. * Allow linking within the Saito app
  66. *
  67. * @param string $text text to parse with link markup
  68. * @param CurrentUserInterface $CurrentUser current user
  69. * @return string text with links replaced
  70. */
  71. private function _replaceUrl($text, $CurrentUser)
  72. {
  73. $webroot = $this->Url->build('/', true);
  74. $text = preg_replace_callback(
  75. '/\[(?P<text>.*?)\]\((?P<url>.*?)\)/',
  76. function ($matches) use ($CurrentUser, $webroot) {
  77. $text = $matches['text'];
  78. $url = $matches['url'];
  79. if (strpos($matches['url'], ':uid')) {
  80. if (!$CurrentUser->isLoggedIn()) {
  81. return $text;
  82. }
  83. $uid = $CurrentUser->getId();
  84. $url = str_replace(':uid', $uid, $url);
  85. }
  86. if (strpos($url, 'webroot:') === 0) {
  87. $url = str_replace('webroot:', $webroot, $url);
  88. }
  89. return "[$text]($url)";
  90. },
  91. $text
  92. );
  93. return $text;
  94. }
  95. }