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