PageRenderTime 58ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

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