PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/site/src/View/Misc/HtmlView.php

https://github.com/Kunena/Kunena-Forum
PHP | 432 lines | 228 code | 77 blank | 127 comment | 22 complexity | fefdd2bfc8062793c08d96c179b31e1a MD5 | raw file
  1. <?php
  2. /**
  3. * Kunena Component
  4. *
  5. * @package Kunena.Site
  6. * @subpackage Views
  7. *
  8. * @copyright Copyright (C) 2008 - 2022 Kunena Team. All rights reserved.
  9. * @license https://www.gnu.org/copyleft/gpl.html GNU/GPL
  10. * @link https://www.kunena.org
  11. **/
  12. namespace Kunena\Forum\Site\View\Misc;
  13. \defined('_JEXEC') or die();
  14. use Exception;
  15. use Joomla\CMS\Factory;
  16. use Joomla\CMS\Language\Text;
  17. use Joomla\CMS\Plugin\PluginHelper;
  18. use Joomla\Registry\Registry;
  19. use Kunena\Forum\Libraries\Date\KunenaDate;
  20. use Kunena\Forum\Libraries\Factory\KunenaFactory;
  21. use Kunena\Forum\Libraries\Route\KunenaRoute;
  22. use Kunena\Forum\Libraries\View\KunenaView;
  23. /**
  24. * Topics View
  25. *
  26. * @since Kunena 6.0
  27. */
  28. class HtmlView extends KunenaView
  29. {
  30. public $me;
  31. public $state;
  32. public $embedded;
  33. public $config;
  34. public $topic;
  35. public $position;
  36. /**
  37. * @var false|float
  38. * @since version
  39. */
  40. public $pages;
  41. /**
  42. * @var integer
  43. * @since version
  44. */
  45. public $message_position;
  46. public $module;
  47. public $firstUserName;
  48. public $firstPostTime;
  49. public $firstPostAuthor;
  50. /**
  51. * @var string
  52. * @since version
  53. */
  54. public $categoryLink;
  55. /**
  56. * @var boolean
  57. * @since version
  58. */
  59. public $cache;
  60. public $category;
  61. public $topics;
  62. public $messages;
  63. public $layout;
  64. /**
  65. * @var string
  66. * @since version
  67. */
  68. public $moreUri;
  69. /**
  70. * @var boolean|string
  71. * @since version
  72. */
  73. public $URL;
  74. public $message_ordering;
  75. /**
  76. * @var false
  77. * @since version
  78. */
  79. public $actionMove;
  80. /**
  81. * @var mixed
  82. * @since version
  83. */
  84. public $total;
  85. public $params;
  86. /**
  87. * @var mixed
  88. * @since version
  89. */
  90. public $topicActions;
  91. /**
  92. * @param null $tpl tpl
  93. *
  94. * @return void
  95. *
  96. * @since Kunena 6.0
  97. *
  98. * @throws Exception
  99. * @throws null
  100. */
  101. public function displayDefault($tpl = null)
  102. {
  103. $this->layout = 'default';
  104. $this->params = $this->state->get('params');
  105. $this->topics = $this->get('Topics');
  106. $this->total = $this->get('Total');
  107. $this->topicActions = $this->get('TopicActions');
  108. $this->actionMove = $this->get('ActionMove');
  109. $this->message_ordering = $this->me->getMessageOrdering();
  110. $this->URL = KunenaRoute::_();
  111. if ($this->embedded)
  112. {
  113. $this->moreUri = 'index.php?option=com_kunena&view=topics&layout=default&mode=' . $this->state->get('list.mode');
  114. $userid = $this->state->get('user');
  115. if ($userid)
  116. {
  117. $this->moreUri .= "&userid={$userid}";
  118. }
  119. }
  120. $this->_prepareDocument();
  121. $this->render('Topic/List', $tpl);
  122. }
  123. /**
  124. * @return void
  125. *
  126. * @since Kunena 6.0
  127. */
  128. protected function _prepareDocument()
  129. {
  130. }
  131. /**
  132. * @param null $tpl tpl
  133. *
  134. * @return void
  135. *
  136. * @since Kunena 6.0
  137. *
  138. * @throws Exception
  139. * @throws null
  140. */
  141. public function displayUser($tpl = null)
  142. {
  143. $this->layout = 'user';
  144. $this->params = $this->state->get('params');
  145. $this->topics = $this->get('Topics');
  146. $this->total = $this->get('Total');
  147. $this->topicActions = $this->get('TopicActions');
  148. $this->actionMove = $this->get('ActionMove');
  149. $this->message_ordering = $this->me->getMessageOrdering();
  150. $this->URL = KunenaRoute::_();
  151. if ($this->embedded)
  152. {
  153. $this->moreUri = 'index.php?option=com_kunena&view=topics&layout=user&mode=' . $this->state->get('list.mode');
  154. $userid = $this->state->get('user');
  155. if ($userid)
  156. {
  157. $this->moreUri .= "&userid={$userid}";
  158. }
  159. }
  160. $this->_prepareDocument();
  161. $this->render('Topic/List', $tpl);
  162. }
  163. /**
  164. * @param null $tpl tpl
  165. *
  166. * @return void
  167. *
  168. * @since Kunena 6.0
  169. *
  170. * @throws Exception
  171. * @throws null
  172. */
  173. public function displayPosts($tpl = null)
  174. {
  175. $this->layout = 'posts';
  176. $this->params = $this->state->get('params');
  177. $this->messages = $this->get('Messages');
  178. $this->topics = $this->get('Topics');
  179. $this->total = $this->get('Total');
  180. $this->actionMove = false;
  181. $this->message_ordering = $this->me->getMessageOrdering();
  182. $this->URL = KunenaRoute::_();
  183. if ($this->embedded)
  184. {
  185. $this->moreUri = 'index.php?option=com_kunena&view=topics&layout=posts&mode=' . $this->state->get('list.mode');
  186. $userid = $this->state->get('user');
  187. if ($userid)
  188. {
  189. $this->moreUri .= "&userid={$userid}";
  190. }
  191. }
  192. $this->_prepareDocument();
  193. $this->render('Message/List', $tpl);
  194. }
  195. /**
  196. * @return void
  197. *
  198. * @since Kunena 6.0
  199. *
  200. * @throws Exception
  201. */
  202. public function displayRows()
  203. {
  204. if ($this->layout == 'posts')
  205. {
  206. $this->displayPostRows();
  207. }
  208. else
  209. {
  210. $this->displayTopicRows();
  211. }
  212. }
  213. /**
  214. * @return void
  215. *
  216. * @since Kunena 6.0
  217. *
  218. * @throws Exception
  219. */
  220. public function displayPostRows()
  221. {
  222. $this->position = 0;
  223. // Run events
  224. $params = new Registry;
  225. $params->set('ksource', 'kunena');
  226. $params->set('kunena_view', 'user');
  227. $params->set('kunena_layout', 'posts');
  228. PluginHelper::importPlugin('kunena');
  229. Factory::getApplication()->triggerEvent('onKunenaPrepare', ['kunena.messages', &$this->messages, &$params, 0]);
  230. foreach ($this->messages as $message)
  231. {
  232. $this->position++;
  233. $this->topic = $message->getTopic();
  234. $this->category = $this->topic->getCategory();
  235. $usertype = $this->me->getType($this->category->id, true);
  236. // TODO: add context (options, template) to caching
  237. $this->cache = true;
  238. $cache = Factory::getCache('com_kunena', 'output');
  239. $cachekey = "{$this->getTemplateMD5()}.{$usertype}.t{$this->topic->id}.p{$message->id}";
  240. $cachegroup = 'com_kunena.posts';
  241. // FIXME: enable caching after fixing the issues
  242. $contents = false; // $cache->get($cachekey, $cachegroup);
  243. if (!$contents)
  244. {
  245. $this->categoryLink = $this->getCategoryLink($this->category->getParent()) . ' / ' . $this->getCategoryLink($this->category);
  246. $this->firstPostAuthor = $this->topic->getfirstPostAuthor();
  247. $this->firstPostTime = $this->topic->first_post_time;
  248. $this->firstUserName = $this->topic->first_post_guest_name;
  249. $this->module = $this->getModulePosition('kunena_topic_' . $this->position);
  250. $this->message_position = $this->topic->posts - ($this->topic->unread ? $this->topic->unread - 1 : 0);
  251. $this->pages = ceil($this->topic->getTotal() / $this->config->messagesPerPage);
  252. if ($this->config->avatarOnCategory)
  253. {
  254. $this->topic->avatar = KunenaFactory::getUser($this->topic->last_post_userid)->getAvatarImage('klist-avatar', 'list');
  255. }
  256. $contents = $this->loadTemplateFile('row');
  257. if ($usertype == 'guest')
  258. {
  259. $contents = preg_replace_callback('|\[K=(\w+)(?:\:([\w_-]+))?\]|', [$this, 'fillTopicInfo'], $contents);
  260. }
  261. // FIXME: enable caching after fixing the issues
  262. // if ($this->cache) $cache->store($contents, $cachekey, $cachegroup);
  263. }
  264. if ($usertype != 'guest')
  265. {
  266. $contents = preg_replace_callback('|\[K=(\w+)(?:\:([\w_-]+))?\]|', [$this, 'fillTopicInfo'], $contents);
  267. }
  268. echo $contents;
  269. $lasttopic = $this->topic;
  270. }
  271. }
  272. /**
  273. * @return void
  274. *
  275. * @since Kunena 6.0
  276. *
  277. * @throws Exception
  278. */
  279. public function displayTopicRows()
  280. {
  281. $lasttopic = null;
  282. $this->position = 0;
  283. // Run events
  284. $params = new Registry;
  285. $params->set('ksource', 'kunena');
  286. $params->set('kunena_view', 'user');
  287. $params->set('kunena_layout', 'topics');
  288. PluginHelper::importPlugin('kunena');
  289. Factory::getApplication()->triggerEvent('onKunenaPrepare', ['kunena.topics', &$this->topics, &$params, 0]);
  290. foreach ($this->topics as $this->topic)
  291. {
  292. $this->position++;
  293. $this->category = $this->topic->getCategory();
  294. $usertype = $this->me->getType($this->category->id, true);
  295. // TODO: add context (options, template) to caching
  296. $this->cache = true;
  297. $cache = Factory::getCache('com_kunena', 'output');
  298. $cachekey = "{$this->getTemplateMD5()}.{$usertype}.t{$this->topic->id}.p{$this->topic->last_post_id}";
  299. $cachegroup = 'com_kunena.topics';
  300. // FIXME: enable caching after fixing the issues
  301. $contents = false; // $cache->get($cachekey, $cachegroup);
  302. if (!$contents)
  303. {
  304. $this->categoryLink = $this->getCategoryLink($this->category->getParent()) . ' / ' . $this->getCategoryLink($this->category);
  305. $this->firstPostAuthor = $this->topic->getfirstPostAuthor();
  306. $this->firstPostTime = $this->topic->first_post_time;
  307. $this->firstUserName = $this->topic->first_post_guest_name;
  308. $this->module = $this->getModulePosition('kunena_topic_' . $this->position);
  309. $this->message_position = $this->topic->posts - ($this->topic->unread ? $this->topic->unread - 1 : 0);
  310. $this->pages = ceil($this->topic->getTotal() / $this->config->messagesPerPage);
  311. if ($this->config->avatarOnCategory)
  312. {
  313. $this->topic->avatar = KunenaFactory::getUser($this->topic->last_post_userid)->getAvatarImage('klist-avatar', 'list');
  314. }
  315. $contents = $this->loadTemplateFile('row');
  316. if ($usertype == 'guest')
  317. {
  318. $contents = preg_replace_callback('|\[K=(\w+)(?:\:([\w_-]+))?\]|', [$this, 'fillTopicInfo'], $contents);
  319. }
  320. // FIXME: enable caching after fixing the issues
  321. // if ($this->cache) $cache->store($contents, $cachekey, $cachegroup);
  322. }
  323. if ($usertype != 'guest')
  324. {
  325. $contents = preg_replace_callback('|\[K=(\w+)(?:\:([\w_-]+))?\]|', [$this, 'fillTopicInfo'], $contents);
  326. }
  327. echo $contents;
  328. }
  329. }
  330. /**
  331. * @param array $matches matches
  332. *
  333. * @return mixed|string|void
  334. *
  335. * @since Kunena 6.0
  336. *
  337. * @throws Exception
  338. * @throws null
  339. */
  340. public function fillTopicInfo($matches)
  341. {
  342. switch ($matches[1])
  343. {
  344. case 'ROW':
  345. return $matches[2] . ($this->position && 1 ? 'odd' : 'even') . ($this->topic->ordering ? " {$matches[2]}sticky" : '');
  346. case 'TOPIC_ICON':
  347. return $this->topic->getIcon();
  348. case 'TOPIC_NEW_COUNT':
  349. return $this->topic->unread ? $this->getTopicLink($this->topic, 'unread', '<sup class="kindicator-new">(' . $this->topic->unread . ' ' . Text::_('COM_KUNENA_A_GEN_NEWCHAR') . ')</sup>') : '';
  350. case 'DATE':
  351. $date = new KunenaDate($matches[2]);
  352. return $date->toSpan('config_postDateFormat', 'config_postDateFormatHover');
  353. }
  354. }
  355. }