PageRenderTime 74ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/plugins/finder/content/content.php

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