PageRenderTime 118ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/plg_finder_newsfeeds/newsfeeds.php

https://github.com/vietvh/Finder
PHP | 426 lines | 182 code | 52 blank | 192 comment | 26 complexity | b658fe42c08f5a9d5d50b38f86923093 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Plugin
  4. * @subpackage Finder.Newsfeeds
  5. *
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_BASE') or die;
  10. jimport('joomla.application.component.helper');
  11. // Load the base adapter.
  12. require_once JPATH_ADMINISTRATOR . '/components/com_finder/helpers/indexer/adapter.php';
  13. /**
  14. * Finder adapter for Joomla Newsfeeds.
  15. *
  16. * @package Joomla.Plugin
  17. * @subpackage Finder.Newsfeeds
  18. * @since 2.5
  19. */
  20. class PlgFinderNewsfeeds extends FinderIndexerAdapter
  21. {
  22. /**
  23. * The plugin identifier.
  24. *
  25. * @var string
  26. * @since 2.5
  27. */
  28. protected $context = 'Newsfeeds';
  29. /**
  30. * The extension name.
  31. *
  32. * @var string
  33. * @since 2.5
  34. */
  35. protected $extension = 'com_newsfeeds';
  36. /**
  37. * The sublayout to use when rendering the results.
  38. *
  39. * @var string
  40. * @since 2.5
  41. */
  42. protected $layout = 'newsfeed';
  43. /**
  44. * The type of content that the adapter indexes.
  45. *
  46. * @var string
  47. * @since 2.5
  48. */
  49. protected $type_title = 'News Feed';
  50. /**
  51. * Constructor
  52. *
  53. * @param object &$subject The object to observe
  54. * @param array $config An array that holds the plugin configuration
  55. *
  56. * @since 2.5
  57. */
  58. public function __construct(&$subject, $config)
  59. {
  60. parent::__construct($subject, $config);
  61. $this->loadLanguage();
  62. }
  63. /**
  64. * Method to update the item link information when the item category is
  65. * changed. This is fired when the item category is published or unpublished
  66. * from the list view.
  67. *
  68. * @param string $extension The extension whose category has been updated.
  69. * @param array $pks A list of primary key ids of the content that has changed state.
  70. * @param integer $value The value of the state that the content has been changed to.
  71. *
  72. * @return void
  73. *
  74. * @since 2.5
  75. */
  76. public function onCategoryChangeState($extension, $pks, $value)
  77. {
  78. // Make sure we're handling com_newsfeeds categories
  79. if ($extension != 'com_newsfeeds')
  80. {
  81. return;
  82. }
  83. // The news feed published state is tied to the category
  84. // published state so we need to look up all published states
  85. // before we change anything.
  86. foreach ($pks as $pk)
  87. {
  88. $sql = clone($this->_getStateQuery());
  89. $sql->where('c.id = ' . (int) $pk);
  90. // Get the published states.
  91. $this->db->setQuery($sql);
  92. $items = $this->db->loadObjectList();
  93. // Adjust the state for each item within the category.
  94. foreach ($items as $item)
  95. {
  96. // Translate the state.
  97. $temp = $this->translateState($item->state, $value);
  98. // Update the item.
  99. $this->change($item->id, 'state', $temp);
  100. // Queue the item to be reindexed.
  101. FinderIndexerQueue::add('com_newsfeeds.newsfeed', $item->id, JFactory::getDate()->toMySQL());
  102. }
  103. }
  104. }
  105. /**
  106. * Method to remove the link information for items that have been deleted.
  107. *
  108. * @param string $context The context of the action being performed.
  109. * @param JTable $table A JTable object containing the record to be deleted
  110. *
  111. * @return boolean True on success.
  112. *
  113. * @since 2.5
  114. * @throws Exception on database error.
  115. */
  116. public function onContentAfterDelete($context, $table)
  117. {
  118. if ($context == 'com_newsfeeds.newsfeed')
  119. {
  120. $id = $table->id;
  121. }
  122. elseif ($context == 'com_finder.index')
  123. {
  124. $id = $table->link_id;
  125. }
  126. else
  127. {
  128. return true;
  129. }
  130. // Remove the items.
  131. return $this->remove($id);
  132. }
  133. /**
  134. * Method to determine if the access level of an item changed.
  135. *
  136. * @param string $context The context of the content passed to the plugin.
  137. * @param JTable &$row A JTable object
  138. * @param boolean $isNew If the content is just about to be created
  139. *
  140. * @return boolean True on success.
  141. *
  142. * @since 2.5
  143. * @throws Exception on database error.
  144. */
  145. public function onContentAfterSave($context, &$row, $isNew)
  146. {
  147. // We only want to handle news feeds here
  148. if ($context == 'com_newsfeeds.newsfeed')
  149. {
  150. // Check if the access levels are different
  151. if (!$isNew && $this->old_access != $row->access)
  152. {
  153. $sql = clone($this->_getStateQuery());
  154. $sql->where('a.id = ' . (int) $row->id);
  155. // Get the access level.
  156. $this->db->setQuery($sql);
  157. $item = $this->db->loadObject();
  158. // Set the access level.
  159. $temp = max($row->access, $item->cat_access);
  160. // Update the item.
  161. $this->change((int) $row->id, 'access', $temp);
  162. }
  163. // Queue the item to be reindexed.
  164. FinderIndexerQueue::add($context, $row->id, JFactory::getDate()->toMySQL());
  165. }
  166. // Check for access changes in the category
  167. if ($context == 'com_categories.category')
  168. {
  169. // Check if the access levels are different
  170. if (!$isNew && $this->old_cataccess != $row->access)
  171. {
  172. $sql = clone($this->_getStateQuery());
  173. $sql->where('c.id = ' . (int) $row->id);
  174. // Get the access level.
  175. $this->db->setQuery($sql);
  176. $items = $this->db->loadObjectList();
  177. // Adjust the access level for each item within the category.
  178. foreach ($items as $item)
  179. {
  180. // Set the access level.
  181. $temp = max($item->access, $row->access);
  182. // Update the item.
  183. $this->change((int) $item->id, 'access', $temp);
  184. // Queue the item to be reindexed.
  185. FinderIndexerQueue::add('com_newsfeeds.newsfeed', $row->id, JFactory::getDate()->toMySQL());
  186. }
  187. }
  188. }
  189. return true;
  190. }
  191. /**
  192. * Method to reindex the link information for an item that has been saved.
  193. * This event is fired before the data is actually saved so we are going
  194. * to queue the item to be indexed later.
  195. *
  196. * @param string $context The context of the content passed to the plugin.
  197. * @param JTable &$row A JTable object
  198. * @param boolean $isNew If the content is just about to be created
  199. *
  200. * @return boolean True on success.
  201. *
  202. * @since 2.5
  203. * @throws Exception on database error.
  204. */
  205. public function onContentBeforeSave($context, &$row, $isNew)
  206. {
  207. // We only want to handle news feeds here
  208. if ($context == 'com_newsfeeds.newsfeed')
  209. {
  210. // Query the database for the old access level if the item isn't new
  211. if (!$isNew)
  212. {
  213. $query = $this->db->getQuery(true);
  214. $query->select($this->db->quoteName('access'));
  215. $query->from($this->db->quoteName('#__newsfeeds'));
  216. $query->where($this->db->quoteName('id') . ' = ' . $row->id);
  217. $this->db->setQuery($query);
  218. // Store the access level to determine if it changes
  219. $this->old_access = $this->db->loadResult();
  220. }
  221. }
  222. // Check for access levels from the category
  223. if ($context == 'com_categories.category')
  224. {
  225. // Query the database for the old access level if the item isn't new
  226. if (!$isNew)
  227. {
  228. $query = $this->db->getQuery(true);
  229. $query->select($this->db->quoteName('access'));
  230. $query->from($this->db->quoteName('#__categories'));
  231. $query->where($this->db->quoteName('id') . ' = ' . $row->id);
  232. $this->db->setQuery($query);
  233. // Store the access level to determine if it changes
  234. $this->old_cataccess = $this->db->loadResult();
  235. }
  236. }
  237. return true;
  238. }
  239. /**
  240. * Method to update the link information for items that have been changed
  241. * from outside the edit screen. This is fired when the item is published,
  242. * unpublished, archived, or unarchived from the list view.
  243. *
  244. * @param string $context The context for the content passed to the plugin.
  245. * @param array $pks A list of primary key ids of the content that has changed state.
  246. * @param integer $value The value of the state that the content has been changed to.
  247. *
  248. * @return void
  249. *
  250. * @since 2.5
  251. */
  252. public function onContentChangeState($context, $pks, $value)
  253. {
  254. // We only want to handle news feeds here
  255. if ($context != 'com_newsfeeds.newsfeed')
  256. {
  257. return;
  258. }
  259. // The news feed published state is tied to the category
  260. // published state so we need to look up all published states
  261. // before we change anything.
  262. foreach ($pks as $pk)
  263. {
  264. $sql = clone($this->_getStateQuery());
  265. $sql->where('a.id = ' . (int) $pk);
  266. // Get the published states.
  267. $this->db->setQuery($sql);
  268. $item = $this->db->loadObject();
  269. // Translate the state.
  270. $temp = $this->translateState($value, $item->cat_state);
  271. // Update the item.
  272. $this->change($pk, 'state', $temp);
  273. // Queue the item to be reindexed.
  274. FinderIndexerQueue::add($context, $pk, JFactory::getDate()->toMySQL());
  275. }
  276. }
  277. /**
  278. * Method to index an item. The item must be a FinderIndexerResult object.
  279. *
  280. * @param FinderIndexerResult $item The item to index as an FinderIndexerResult object.
  281. *
  282. * @return void
  283. *
  284. * @since 2.5
  285. * @throws Exception on database error.
  286. */
  287. protected function index(FinderIndexerResult $item)
  288. {
  289. // Check if the extension is enabled
  290. if (JComponentHelper::isEnabled($this->extension) == false)
  291. {
  292. return;
  293. }
  294. // Initialize the item parameters.
  295. $registry = new JRegistry;
  296. $registry->loadString($item->params);
  297. $item->params = $registry;
  298. // Build the necessary route and path information.
  299. $item->url = $this->getURL($item->id, $this->extension, $this->layout);
  300. $item->route = NewsfeedsHelperRoute::getNewsfeedRoute($item->slug, $item->catslug);
  301. $item->path = FinderIndexerHelper::getContentPath($item->route);
  302. // Handle the link to the meta-data.
  303. $item->addInstruction(FinderIndexer::META_CONTEXT, 'link');
  304. // Set the language.
  305. $item->language = FinderIndexerHelper::getDefaultLanguage();
  306. // Add the type taxonomy data.
  307. $item->addTaxonomy('Type', 'News Feed');
  308. // Add the category taxonomy data.
  309. if (!empty($item->category))
  310. {
  311. $item->addTaxonomy('Category', $item->category, $item->cat_state, $item->cat_access);
  312. }
  313. // Get content extras.
  314. FinderIndexerHelper::getContentExtras($item);
  315. // Index the item.
  316. FinderIndexer::index($item);
  317. }
  318. /**
  319. * Method to setup the indexer to be run.
  320. *
  321. * @return boolean True on success.
  322. *
  323. * @since 2.5
  324. */
  325. protected function setup()
  326. {
  327. // Load dependent classes.
  328. require_once JPATH_SITE . '/includes/application.php';
  329. require_once JPATH_SITE . '/components/com_newsfeeds/helpers/route.php';
  330. return true;
  331. }
  332. /**
  333. * Method to get the SQL query used to retrieve the list of content items.
  334. *
  335. * @param mixed $sql A JDatabaseQuery object or null.
  336. *
  337. * @return JDatabaseQuery A database object.
  338. *
  339. * @since 2.5
  340. */
  341. protected function getListQuery($sql = null)
  342. {
  343. $db = JFactory::getDbo();
  344. // Check if we can use the supplied SQL query.
  345. $sql = is_a($sql, 'JDatabaseQuery') ? $sql : $db->getQuery(true);
  346. $sql->select('a.id, a.catid, a.name AS title, a.alias, a.link AS link');
  347. $sql->select('a.published AS state, a.ordering, a.created AS start_date, a.params, a.access');
  348. $sql->select('a.publish_up AS publish_start_date, a.publish_down AS publish_end_date');
  349. $sql->select('c.title AS category, c.published AS cat_state, c.access AS cat_access');
  350. $sql->select('CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug');
  351. $sql->select('CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(":", c.id, c.alias) ELSE c.id END as catslug');
  352. $sql->from('#__newsfeeds AS a');
  353. $sql->join('LEFT', '#__categories AS c ON c.id = a.catid');
  354. return $sql;
  355. }
  356. /**
  357. * Method to get a SQL query to load the published and access states for
  358. * a news feed and category.
  359. *
  360. * @return JDatabaseQuery A database object.
  361. *
  362. * @since 2.5
  363. */
  364. private function _getStateQuery()
  365. {
  366. $sql = $this->db->getQuery(true);
  367. $sql->select('a.id');
  368. $sql->select('a.published AS state, c.published AS cat_state');
  369. $sql->select('a.access AS access, c.access AS cat_access');
  370. $sql->from('#__newsfeeds AS a');
  371. $sql->join('LEFT', '#__categories AS c ON c.id = a.catid');
  372. return $sql;
  373. }
  374. }