PageRenderTime 73ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/finder/content/content.php

https://bitbucket.org/asosso/joomla25
PHP | 382 lines | 165 code | 42 blank | 175 comment | 38 complexity | f29a86b6e1348c61e9221d62df4774ad MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * @package Joomla.Plugin
  4. * @subpackage Finder.Content
  5. *
  6. * @copyright Copyright (C) 2005 - 2013 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 com_content.
  15. *
  16. * @package Joomla.Plugin
  17. * @subpackage Finder.Content
  18. * @since 2.5
  19. */
  20. class plgFinderContent extends FinderIndexerAdapter
  21. {
  22. /**
  23. * The plugin identifier.
  24. *
  25. * @var string
  26. * @since 2.5
  27. */
  28. protected $context = 'Content';
  29. /**
  30. * The extension name.
  31. *
  32. * @var string
  33. * @since 2.5
  34. */
  35. protected $extension = 'com_content';
  36. /**
  37. * The sublayout to use when rendering the results.
  38. *
  39. * @var string
  40. * @since 2.5
  41. */
  42. protected $layout = 'article';
  43. /**
  44. * The type of content that the adapter indexes.
  45. *
  46. * @var string
  47. * @since 2.5
  48. */
  49. protected $type_title = 'Article';
  50. /**
  51. * The table name.
  52. *
  53. * @var string
  54. * @since 2.5
  55. */
  56. protected $table = '#__content';
  57. /**
  58. * Constructor
  59. *
  60. * @param object &$subject The object to observe
  61. * @param array $config An array that holds the plugin configuration
  62. *
  63. * @since 2.5
  64. */
  65. public function __construct(&$subject, $config)
  66. {
  67. parent::__construct($subject, $config);
  68. $this->loadLanguage();
  69. }
  70. /**
  71. * Method to update the item link information when the item category is
  72. * changed. This is fired when the item category is published or unpublished
  73. * from the list view.
  74. *
  75. * @param string $extension The extension whose category has been updated.
  76. * @param array $pks A list of primary key ids of the content that has changed state.
  77. * @param integer $value The value of the state that the content has been changed to.
  78. *
  79. * @return void
  80. *
  81. * @since 2.5
  82. */
  83. public function onFinderCategoryChangeState($extension, $pks, $value)
  84. {
  85. // Make sure we're handling com_content categories
  86. if ($extension == 'com_content')
  87. {
  88. $this->categoryStateChange($pks, $value);
  89. }
  90. }
  91. /**
  92. * Method to remove the link information for items that have been deleted.
  93. *
  94. * @param string $context The context of the action being performed.
  95. * @param JTable $table A JTable object containing the record to be deleted
  96. *
  97. * @return boolean True on success.
  98. *
  99. * @since 2.5
  100. * @throws Exception on database error.
  101. */
  102. public function onFinderAfterDelete($context, $table)
  103. {
  104. if ($context == 'com_content.article')
  105. {
  106. $id = $table->id;
  107. }
  108. elseif ($context == 'com_finder.index')
  109. {
  110. $id = $table->link_id;
  111. }
  112. else
  113. {
  114. return true;
  115. }
  116. // Remove the items.
  117. return $this->remove($id);
  118. }
  119. /**
  120. * Method to determine if the access level of an item changed.
  121. *
  122. * @param string $context The context of the content passed to the plugin.
  123. * @param JTable $row A JTable object
  124. * @param boolean $isNew If the content has just been created
  125. *
  126. * @return boolean True on success.
  127. *
  128. * @since 2.5
  129. * @throws Exception on database error.
  130. */
  131. public function onFinderAfterSave($context, $row, $isNew)
  132. {
  133. // We only want to handle articles here
  134. if ($context == 'com_content.article' || $context == 'com_content.form')
  135. {
  136. // Check if the access levels are different
  137. if (!$isNew && $this->old_access != $row->access)
  138. {
  139. // Process the change.
  140. $this->itemAccessChange($row);
  141. }
  142. // Reindex the item
  143. $this->reindex($row->id);
  144. }
  145. // Check for access changes in the category
  146. if ($context == 'com_categories.category')
  147. {
  148. // Check if the access levels are different
  149. if (!$isNew && $this->old_cataccess != $row->access)
  150. {
  151. $this->categoryAccessChange($row);
  152. }
  153. }
  154. return true;
  155. }
  156. /**
  157. * Method to reindex the link information for an item that has been saved.
  158. * This event is fired before the data is actually saved so we are going
  159. * to queue the item to be indexed later.
  160. *
  161. * @param string $context The context of the content passed to the plugin.
  162. * @param JTable $row A JTable object
  163. * @param boolean $isNew If the content is just about to be created
  164. *
  165. * @return boolean True on success.
  166. *
  167. * @since 2.5
  168. * @throws Exception on database error.
  169. */
  170. public function onFinderBeforeSave($context, $row, $isNew)
  171. {
  172. // We only want to handle articles here
  173. if ($context == 'com_content.article' || $context == 'com_content.form')
  174. {
  175. // Query the database for the old access level if the item isn't new
  176. if (!$isNew)
  177. {
  178. $this->checkItemAccess($row);
  179. }
  180. }
  181. // Check for access levels from the category
  182. if ($context == 'com_categories.category')
  183. {
  184. // Query the database for the old access level if the item isn't new
  185. if (!$isNew)
  186. {
  187. $this->checkCategoryAccess($row);
  188. }
  189. }
  190. return true;
  191. }
  192. /**
  193. * Method to update the link information for items that have been changed
  194. * from outside the edit screen. This is fired when the item is published,
  195. * unpublished, archived, or unarchived from the list view.
  196. *
  197. * @param string $context The context for the content passed to the plugin.
  198. * @param array $pks A list of primary key ids of the content that has changed state.
  199. * @param integer $value The value of the state that the content has been changed to.
  200. *
  201. * @return void
  202. *
  203. * @since 2.5
  204. */
  205. public function onFinderChangeState($context, $pks, $value)
  206. {
  207. // We only want to handle articles here
  208. if ($context == 'com_content.article' || $context == 'com_content.form')
  209. {
  210. $this->itemStateChange($pks, $value);
  211. }
  212. // Handle when the plugin is disabled
  213. if ($context == 'com_plugins.plugin' && $value === 0)
  214. {
  215. $this->pluginDisable($pks);
  216. }
  217. }
  218. /**
  219. * Method to index an item. The item must be a FinderIndexerResult object.
  220. *
  221. * @param FinderIndexerResult $item The item to index as an FinderIndexerResult object.
  222. * @param string $format The item format
  223. *
  224. * @return void
  225. *
  226. * @since 2.5
  227. * @throws Exception on database error.
  228. */
  229. protected function index(FinderIndexerResult $item, $format = 'html')
  230. {
  231. // Check if the extension is enabled
  232. if (JComponentHelper::isEnabled($this->extension) == false)
  233. {
  234. return;
  235. }
  236. // Initialize the item parameters.
  237. $registry = new JRegistry;
  238. $registry->loadString($item->params);
  239. $item->params = JComponentHelper::getParams('com_content', true);
  240. $item->params->merge($registry);
  241. $registry = new JRegistry;
  242. $registry->loadString($item->metadata);
  243. $item->metadata = $registry;
  244. // Trigger the onContentPrepare event.
  245. $item->summary = FinderIndexerHelper::prepareContent($item->summary, $item->params);
  246. $item->body = FinderIndexerHelper::prepareContent($item->body, $item->params);
  247. // Build the necessary route and path information.
  248. $item->url = $this->getURL($item->id, $this->extension, $this->layout);
  249. $item->route = ContentHelperRoute::getArticleRoute($item->slug, $item->catslug);
  250. $item->path = FinderIndexerHelper::getContentPath($item->route);
  251. // Get the menu title if it exists.
  252. $title = $this->getItemMenuTitle($item->url);
  253. // Adjust the title if necessary.
  254. if (!empty($title) && $this->params->get('use_menu_title', true))
  255. {
  256. $item->title = $title;
  257. }
  258. // Add the meta-author.
  259. $item->metaauthor = $item->metadata->get('author');
  260. // Add the meta-data processing instructions.
  261. $item->addInstruction(FinderIndexer::META_CONTEXT, 'metakey');
  262. $item->addInstruction(FinderIndexer::META_CONTEXT, 'metadesc');
  263. $item->addInstruction(FinderIndexer::META_CONTEXT, 'metaauthor');
  264. $item->addInstruction(FinderIndexer::META_CONTEXT, 'author');
  265. $item->addInstruction(FinderIndexer::META_CONTEXT, 'created_by_alias');
  266. // Translate the state. Articles should only be published if the category is published.
  267. $item->state = $this->translateState($item->state, $item->cat_state);
  268. // Add the type taxonomy data.
  269. $item->addTaxonomy('Type', 'Article');
  270. // Add the author taxonomy data.
  271. if (!empty($item->author) || !empty($item->created_by_alias))
  272. {
  273. $item->addTaxonomy('Author', !empty($item->created_by_alias) ? $item->created_by_alias : $item->author);
  274. }
  275. // Add the category taxonomy data.
  276. $item->addTaxonomy('Category', $item->category, $item->cat_state, $item->cat_access);
  277. // Add the language taxonomy data.
  278. $item->addTaxonomy('Language', $item->language);
  279. // Get content extras.
  280. FinderIndexerHelper::getContentExtras($item);
  281. // Index the item.
  282. FinderIndexer::index($item);
  283. }
  284. /**
  285. * Method to setup the indexer to be run.
  286. *
  287. * @return boolean True on success.
  288. *
  289. * @since 2.5
  290. */
  291. protected function setup()
  292. {
  293. // Load dependent classes.
  294. include_once JPATH_SITE . '/components/com_content/helpers/route.php';
  295. return true;
  296. }
  297. /**
  298. * Method to get the SQL query used to retrieve the list of content items.
  299. *
  300. * @param mixed $sql A JDatabaseQuery object or null.
  301. *
  302. * @return JDatabaseQuery A database object.
  303. *
  304. * @since 2.5
  305. */
  306. protected function getListQuery($sql = null)
  307. {
  308. $db = JFactory::getDbo();
  309. // Check if we can use the supplied SQL query.
  310. $sql = $sql instanceof JDatabaseQuery ? $sql : $db->getQuery(true);
  311. $sql->select('a.id, a.title, a.alias, a.introtext AS summary, a.fulltext AS body');
  312. $sql->select('a.state, a.catid, a.created AS start_date, a.created_by');
  313. $sql->select('a.created_by_alias, a.modified, a.modified_by, a.attribs AS params');
  314. $sql->select('a.metakey, a.metadesc, a.metadata, a.language, a.access, a.version, a.ordering');
  315. $sql->select('a.publish_up AS publish_start_date, a.publish_down AS publish_end_date');
  316. $sql->select('c.title AS category, c.published AS cat_state, c.access AS cat_access');
  317. // Handle the alias CASE WHEN portion of the query
  318. $case_when_item_alias = ' CASE WHEN ';
  319. $case_when_item_alias .= $sql->charLength('a.alias');
  320. $case_when_item_alias .= ' THEN ';
  321. $a_id = $sql->castAsChar('a.id');
  322. $case_when_item_alias .= $sql->concatenate(array($a_id, 'a.alias'), ':');
  323. $case_when_item_alias .= ' ELSE ';
  324. $case_when_item_alias .= $a_id.' END as slug';
  325. $sql->select($case_when_item_alias);
  326. $case_when_category_alias = ' CASE WHEN ';
  327. $case_when_category_alias .= $sql->charLength('c.alias');
  328. $case_when_category_alias .= ' THEN ';
  329. $c_id = $sql->castAsChar('c.id');
  330. $case_when_category_alias .= $sql->concatenate(array($c_id, 'c.alias'), ':');
  331. $case_when_category_alias .= ' ELSE ';
  332. $case_when_category_alias .= $c_id.' END as catslug';
  333. $sql->select($case_when_category_alias);
  334. $sql->select('u.name AS author');
  335. $sql->from('#__content AS a');
  336. $sql->join('LEFT', '#__categories AS c ON c.id = a.catid');
  337. $sql->join('LEFT', '#__users AS u ON u.id = a.created_by');
  338. return $sql;
  339. }
  340. }