PageRenderTime 26ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/components/com_newsfeeds/views/newsfeed/view.html.php

https://gitlab.com/lankerd/paGO---Testing-Site
PHP | 343 lines | 215 code | 59 blank | 69 comment | 42 complexity | d222f7ad383715d401e129b90fd5514b MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Site
  4. * @subpackage com_newsfeeds
  5. *
  6. * @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('_JEXEC') or die;
  10. use Joomla\Registry\Registry;
  11. /**
  12. * HTML View class for the Newsfeeds component
  13. *
  14. * @since 1.0
  15. */
  16. class NewsfeedsViewNewsfeed extends JViewLegacy
  17. {
  18. /**
  19. * @var object
  20. * @since 1.6
  21. */
  22. protected $state;
  23. /**
  24. * @var object
  25. * @since 1.6
  26. */
  27. protected $item;
  28. /**
  29. * @var boolean
  30. * @since 1.6
  31. */
  32. protected $print;
  33. /**
  34. * Execute and display a template script.
  35. *
  36. * @param string $tpl The name of the template file to parse; automatically searches through the template paths.
  37. *
  38. * @return mixed A string if successful, otherwise a Error object.
  39. *
  40. * @since 1.6
  41. */
  42. public function display($tpl = null)
  43. {
  44. $app = JFactory::getApplication();
  45. $user = JFactory::getUser();
  46. // Get view related request variables.
  47. $print = $app->input->getBool('print');
  48. // Get model data.
  49. $state = $this->get('State');
  50. $item = $this->get('Item');
  51. if ($item)
  52. {
  53. // Get Category Model data
  54. $categoryModel = JModelLegacy::getInstance('Category', 'NewsfeedsModel', array('ignore_request' => true));
  55. $categoryModel->setState('category.id', $item->catid);
  56. $categoryModel->setState('list.ordering', 'a.name');
  57. $categoryModel->setState('list.direction', 'asc');
  58. // @TODO: $items is not used. Remove this line?
  59. $items = $categoryModel->getItems();
  60. }
  61. // Check for errors.
  62. // @TODO: Maybe this could go into JComponentHelper::raiseErrors($this->get('Errors'))
  63. if (count($errors = $this->get('Errors')))
  64. {
  65. JError::raiseWarning(500, implode("\n", $errors));
  66. return false;
  67. }
  68. // Add router helpers.
  69. $item->slug = $item->alias ? ($item->id . ':' . $item->alias) : $item->id;
  70. $item->catslug = $item->category_alias ? ($item->catid . ':' . $item->category_alias) : $item->catid;
  71. $item->parent_slug = $item->category_alias ? ($item->parent_id . ':' . $item->parent_alias) : $item->parent_id;
  72. // Check if cache directory is writeable
  73. $cacheDir = JPATH_CACHE . '/';
  74. if (!is_writable($cacheDir))
  75. {
  76. JError::raiseNotice('0', JText::_('COM_NEWSFEEDS_CACHE_DIRECTORY_UNWRITABLE'));
  77. return;
  78. }
  79. // Merge newsfeed params. If this is single-newsfeed view, menu params override newsfeed params
  80. // Otherwise, newsfeed params override menu item params
  81. $params = $state->get('params');
  82. $newsfeed_params = clone $item->params;
  83. $active = $app->getMenu()->getActive();
  84. $temp = clone $params;
  85. // Check to see which parameters should take priority
  86. if ($active)
  87. {
  88. $currentLink = $active->link;
  89. // If the current view is the active item and an newsfeed view for this feed, then the menu item params take priority
  90. if (strpos($currentLink, 'view=newsfeed') && (strpos($currentLink, '&id=' . (string) $item->id)))
  91. {
  92. // $item->params are the newsfeed params, $temp are the menu item params
  93. // Merge so that the menu item params take priority
  94. $newsfeed_params->merge($temp);
  95. $item->params = $newsfeed_params;
  96. // Load layout from active query (in case it is an alternative menu item)
  97. if (isset($active->query['layout']))
  98. {
  99. $this->setLayout($active->query['layout']);
  100. }
  101. }
  102. else
  103. {
  104. // Current view is not a single newsfeed, so the newsfeed params take priority here
  105. // Merge the menu item params with the newsfeed params so that the newsfeed params take priority
  106. $temp->merge($newsfeed_params);
  107. $item->params = $temp;
  108. // Check for alternative layouts (since we are not in a single-newsfeed menu item)
  109. if ($layout = $item->params->get('newsfeed_layout'))
  110. {
  111. $this->setLayout($layout);
  112. }
  113. }
  114. }
  115. else
  116. {
  117. // Merge so that newsfeed params take priority
  118. $temp->merge($newsfeed_params);
  119. $item->params = $temp;
  120. // Check for alternative layouts (since we are not in a single-newsfeed menu item)
  121. if ($layout = $item->params->get('newsfeed_layout'))
  122. {
  123. $this->setLayout($layout);
  124. }
  125. }
  126. // Check the access to the newsfeed
  127. $levels = $user->getAuthorisedViewLevels();
  128. if (!in_array($item->access, $levels) or ((in_array($item->access, $levels) and (!in_array($item->category_access, $levels)))))
  129. {
  130. $app->enqueueMessage(JText::_('JERROR_ALERTNOAUTHOR'), 'error');
  131. $app->setHeader('status', 403, true);
  132. return;
  133. }
  134. // Get the current menu item
  135. $params = $app->getParams();
  136. // Get the newsfeed
  137. $newsfeed = $item;
  138. $temp = new Registry;
  139. $temp->loadString($item->params);
  140. $params->merge($temp);
  141. try
  142. {
  143. $feed = new JFeedFactory;
  144. $this->rssDoc = $feed->getFeed($newsfeed->link);
  145. }
  146. catch (InvalidArgumentException $e)
  147. {
  148. $msg = JText::_('COM_NEWSFEEDS_ERRORS_FEED_NOT_RETRIEVED');
  149. }
  150. catch (RunTimeException $e)
  151. {
  152. $msg = JText::_('COM_NEWSFEEDS_ERRORS_FEED_NOT_RETRIEVED');
  153. }
  154. if (empty($this->rssDoc))
  155. {
  156. $msg = JText::_('COM_NEWSFEEDS_ERRORS_FEED_NOT_RETRIEVED');
  157. }
  158. $feed_display_order = $params->get('feed_display_order', 'des');
  159. if ($feed_display_order == 'asc')
  160. {
  161. $this->rssDoc->reverseItems();
  162. }
  163. // Escape strings for HTML output
  164. $this->pageclass_sfx = htmlspecialchars($params->get('pageclass_sfx'));
  165. $this->assignRef('params', $params);
  166. $this->assignRef('newsfeed', $newsfeed);
  167. $this->assignRef('state', $state);
  168. $this->assignRef('item', $item);
  169. $this->assignRef('user', $user);
  170. if (!empty($msg))
  171. {
  172. $this->assignRef('msg', $msg);
  173. }
  174. $this->print = $print;
  175. $item->tags = new JHelperTags;
  176. $item->tags->getItemTags('com_newsfeeds.newsfeed', $item->id);
  177. // Increment the hit counter of the newsfeed.
  178. $model = $this->getModel();
  179. $model->hit();
  180. $this->_prepareDocument();
  181. return parent::display($tpl);
  182. }
  183. /**
  184. * Prepares the document
  185. *
  186. * @return void
  187. *
  188. * @since 1.6
  189. */
  190. protected function _prepareDocument()
  191. {
  192. $app = JFactory::getApplication();
  193. $menus = $app->getMenu();
  194. $pathway = $app->getPathway();
  195. $title = null;
  196. // Because the application sets a default page title,
  197. // we need to get it from the menu item itself
  198. $menu = $menus->getActive();
  199. if ($menu)
  200. {
  201. $this->params->def('page_heading', $this->params->get('page_title', $menu->title));
  202. }
  203. else
  204. {
  205. $this->params->def('page_heading', JText::_('COM_NEWSFEEDS_DEFAULT_PAGE_TITLE'));
  206. }
  207. $title = $this->params->get('page_title', '');
  208. $id = (int) @$menu->query['id'];
  209. // If the menu item does not concern this newsfeed
  210. if ($menu && ($menu->query['option'] != 'com_newsfeeds' || $menu->query['view'] != 'newsfeed' || $id != $this->item->id))
  211. {
  212. // If this is not a single newsfeed menu item, set the page title to the newsfeed title
  213. if ($this->item->name)
  214. {
  215. $title = $this->item->name;
  216. }
  217. $path = array(array('title' => $this->item->name, 'link' => ''));
  218. $category = JCategories::getInstance('Newsfeeds')->get($this->item->catid);
  219. while (($menu->query['option'] != 'com_newsfeeds' || $menu->query['view'] == 'newsfeed' || $id != $category->id) && $category->id > 1)
  220. {
  221. $path[] = array('title' => $category->title, 'link' => NewsfeedsHelperRoute::getCategoryRoute($category->id));
  222. $category = $category->getParent();
  223. }
  224. $path = array_reverse($path);
  225. foreach ($path as $item)
  226. {
  227. $pathway->addItem($item['title'], $item['link']);
  228. }
  229. }
  230. if (empty($title))
  231. {
  232. $title = $app->get('sitename');
  233. }
  234. elseif ($app->get('sitename_pagetitles', 0) == 1)
  235. {
  236. $title = JText::sprintf('JPAGETITLE', $app->get('sitename'), $title);
  237. }
  238. elseif ($app->get('sitename_pagetitles', 0) == 2)
  239. {
  240. $title = JText::sprintf('JPAGETITLE', $title, $app->get('sitename'));
  241. }
  242. if (empty($title))
  243. {
  244. $title = $this->item->name;
  245. }
  246. $this->document->setTitle($title);
  247. if ($this->item->metadesc)
  248. {
  249. $this->document->setDescription($this->item->metadesc);
  250. }
  251. elseif ($this->params->get('menu-meta_description'))
  252. {
  253. $this->document->setDescription($this->params->get('menu-meta_description'));
  254. }
  255. if ($this->item->metakey)
  256. {
  257. $this->document->setMetadata('keywords', $this->item->metakey);
  258. }
  259. elseif ($this->params->get('menu-meta_keywords'))
  260. {
  261. $this->document->setMetadata('keywords', $this->params->get('menu-meta_keywords'));
  262. }
  263. if ($this->params->get('robots'))
  264. {
  265. $this->document->setMetadata('robots', $this->params->get('robots'));
  266. }
  267. if ($app->get('MetaTitle') == '1')
  268. {
  269. $this->document->setMetaData('title', $this->item->name);
  270. }
  271. if ($app->get('MetaAuthor') == '1')
  272. {
  273. $this->document->setMetaData('author', $this->item->author);
  274. }
  275. $mdata = $this->item->metadata->toArray();
  276. foreach ($mdata as $k => $v)
  277. {
  278. if ($v)
  279. {
  280. $this->document->setMetadata($k, $v);
  281. }
  282. }
  283. }
  284. }