PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/phpmyfaq/src/phpMyFAQ/Helper/FaqHelper.php

http://github.com/thorsten/phpMyFAQ
PHP | 238 lines | 126 code | 30 blank | 82 comment | 11 complexity | 7a8477e0232e0f14cb0b6e30c4c0a822 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * Helper class for phpMyFAQ FAQs.
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public License,
  6. * v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. * obtain one at http://mozilla.org/MPL/2.0/.
  8. *
  9. * @package phpMyFAQ\Helper
  10. * @author Thorsten Rinne <thorsten@phpmyfaq.de>
  11. * @copyright 2010-2021 phpMyFAQ Team
  12. * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
  13. * @link https://www.phpmyfaq.de
  14. * @since 2010-11-12
  15. */
  16. namespace phpMyFAQ\Helper;
  17. use Exception;
  18. use ParsedownExtra;
  19. use phpMyFAQ\Category;
  20. use phpMyFAQ\Configuration;
  21. use phpMyFAQ\Date;
  22. use phpMyFAQ\Faq;
  23. use phpMyFAQ\Helper;
  24. use phpMyFAQ\Link;
  25. use phpMyFAQ\Utils;
  26. /**
  27. * Class FaqHelper
  28. *
  29. * @package phpMyFAQ\Helper
  30. */
  31. class FaqHelper extends Helper
  32. {
  33. /**
  34. * Constructor.
  35. *
  36. * @param Configuration $config
  37. */
  38. public function __construct(Configuration $config)
  39. {
  40. $this->config = $config;
  41. }
  42. /**
  43. * Rewrites the CSS class generated by TinyMCE for HighlightJS.
  44. *
  45. * @param string $answer
  46. * @return string
  47. */
  48. public function renderMarkupContent(string $answer): string
  49. {
  50. return str_replace('class="language-markup"', 'class="language-html"', $answer);
  51. }
  52. /**
  53. * Extends URL fragments (e.g. <a href="#foo">) with the full default URL.
  54. * @param string $answer
  55. * @param string $currentUrl
  56. * @return string
  57. */
  58. public function rewriteUrlFragments(string $answer, string $currentUrl): string
  59. {
  60. return str_replace('href="#', 'href="' . $currentUrl . '#', $answer);
  61. }
  62. /**
  63. * Renders a Share on Twitter link.
  64. *
  65. * @param string $url
  66. * @return string
  67. */
  68. public function renderTwitterShareLink(string $url): string
  69. {
  70. if (empty($url) || $this->config->get('socialnetworks.disableAll')) {
  71. return '';
  72. }
  73. return sprintf(
  74. '<a rel="nofollow" href="%s" target="_blank"><i aria-hidden="true" class="fa fa-twitter"></i></a>',
  75. $url
  76. );
  77. }
  78. /**
  79. * Renders a "Send to friend" HTML snippet.
  80. *
  81. * @param string $url
  82. * @return string
  83. */
  84. public function renderSendToFriend(string $url): string
  85. {
  86. if (empty($url) || !$this->config->get('main.enableSendToFriend')) {
  87. return '';
  88. }
  89. return sprintf(
  90. '<a rel="nofollow" href="%s"><i aria-hidden="true" class="fa fa-envelope"></i></a>',
  91. $url
  92. );
  93. }
  94. /**
  95. * Renders a select box with all translations of a FAQ.
  96. *
  97. * @param Faq $faq
  98. * @param int $categoryId
  99. * @return string
  100. */
  101. public function renderChangeLanguageSelector(Faq $faq, int $categoryId): string
  102. {
  103. global $languageCodes;
  104. $html = '';
  105. $faqUrl = sprintf(
  106. '?action=faq&amp;cat=%d&amp;id=%d&amp;artlang=%%s',
  107. $categoryId,
  108. $faq->faqRecord['id']
  109. );
  110. $oLink = new Link($this->config->getDefaultUrl() . $faqUrl, $this->config);
  111. $oLink->itemTitle = $faq->faqRecord['title'];
  112. $availableLanguages = $this->config->getLanguage()->languageAvailable($faq->faqRecord['id']);
  113. if (count($availableLanguages) > 1) {
  114. $html = '<form method="post">';
  115. $html .= '<select name="language" onchange="top.location.href = this.options[this.selectedIndex].value;">';
  116. foreach ($availableLanguages as $language) {
  117. $html .= sprintf('<option value="%s"', sprintf($oLink->toString(), $language));
  118. $html .= ($faq->faqRecord['lang'] === $language ? ' selected' : '');
  119. $html .= sprintf('>%s</option>', $languageCodes[strtoupper($language)]);
  120. }
  121. $html .= '</select></form>';
  122. }
  123. return $html;
  124. }
  125. /**
  126. * Renders a preview of the answer.
  127. *
  128. * @param string $answer
  129. * @param int $numWords
  130. * @return string
  131. * @throws Exception
  132. */
  133. public function renderAnswerPreview(string $answer, int $numWords): string
  134. {
  135. if ($this->config->get('main.enableMarkdownEditor')) {
  136. $parseDown = new ParsedownExtra();
  137. return Utils::chopString(strip_tags($parseDown->text($answer)), $numWords);
  138. } else {
  139. return Utils::chopString(strip_tags($answer), $numWords);
  140. }
  141. }
  142. /**
  143. * Creates an overview with all categories with their FAQs.
  144. *
  145. * @param Category $category
  146. * @param Faq $faq
  147. * @param string $language
  148. * @return string
  149. * @throws Exception
  150. */
  151. public function createOverview(Category $category, Faq $faq, $language = ''): string
  152. {
  153. global $PMF_LANG;
  154. $output = '';
  155. // Initialize categories
  156. $category->transform(0);
  157. // Get all FAQs
  158. $faq->getAllRecords(FAQ_SORTING_TYPE_CATID_FAQID, ['lang' => $language]);
  159. $date = new Date($this->config);
  160. if (count($faq->faqRecords)) {
  161. $lastCategory = 0;
  162. foreach ($faq->faqRecords as $data) {
  163. if ($data['category_id'] !== $lastCategory) {
  164. $output .= sprintf('<h3>%s</h3>', $category->getPath($data['category_id'], ' &raquo; '));
  165. }
  166. $output .= sprintf('<h4>%s</h4>', strip_tags($data['title']));
  167. $output .= sprintf('<article>%s</article>', $data['content']);
  168. $output .= sprintf(
  169. '<p>%s: %s<br>%s',
  170. $PMF_LANG['msgAuthor'],
  171. $data['author'],
  172. $PMF_LANG['msgLastUpdateArticle'] . $date->format($data['updated'])
  173. );
  174. $output .= '<hr>';
  175. $lastCategory = $data['category_id'];
  176. }
  177. }
  178. return $output;
  179. }
  180. /**
  181. * Creates a list of links with available languages to edit a FAQ
  182. * in the admin backend.
  183. *
  184. * @param $faqId
  185. * @param $faqLang
  186. * @return string
  187. */
  188. public function createFaqTranslationLinkList(int $faqId, string $faqLang): string
  189. {
  190. global $languageCodes;
  191. $output = '';
  192. $availableLanguages = $this->config->getLanguage()->languageAvailable(0, 'faqcategories');
  193. foreach ($availableLanguages as $languageCode) {
  194. if ($languageCode !== $faqLang) {
  195. $output .= sprintf(
  196. '<a class="dropdown-item" href="?action=editentry&id=%d&translateTo=%s">%s %s</a>',
  197. $faqId,
  198. $languageCode,
  199. 'Translate to',
  200. $languageCodes[strtoupper($languageCode)]
  201. );
  202. } else {
  203. $output .= '<a class="dropdown-item">n/a</a>';
  204. }
  205. }
  206. return $output;
  207. }
  208. }