PageRenderTime 59ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/joomla/plugins/finder/content/content.php

https://gitlab.com/ricardosanchez/prueba
PHP | 380 lines | 162 code | 46 blank | 172 comment | 38 complexity | 519355691ff1bc8926c6b73e822e9f75 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Plugin
  4. * @subpackage Finder.Content
  5. *
  6. * @copyright Copyright (C) 2005 - 2015 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. require_once 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 an 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. // Initialise the item parameters.
  231. $registry = new Registry;
  232. $registry->loadString($item->params);
  233. $item->params = JComponentHelper::getParams('com_content', true);
  234. $item->params->merge($registry);
  235. $registry = new Registry;
  236. $registry->loadString($item->metadata);
  237. $item->metadata = $registry;
  238. // Trigger the onContentPrepare event.
  239. $item->summary = FinderIndexerHelper::prepareContent($item->summary, $item->params);
  240. $item->body = FinderIndexerHelper::prepareContent($item->body, $item->params);
  241. // Build the necessary route and path information.
  242. $item->url = $this->getUrl($item->id, $this->extension, $this->layout);
  243. $item->route = ContentHelperRoute::getArticleRoute($item->slug, $item->catid, $item->language);
  244. $item->path = FinderIndexerHelper::getContentPath($item->route);
  245. // Get the menu title if it exists.
  246. $title = $this->getItemMenuTitle($item->url);
  247. // Adjust the title if necessary.
  248. if (!empty($title) && $this->params->get('use_menu_title', true))
  249. {
  250. $item->title = $title;
  251. }
  252. // Add the meta-author.
  253. $item->metaauthor = $item->metadata->get('author');
  254. // Add the meta-data processing instructions.
  255. $item->addInstruction(FinderIndexer::META_CONTEXT, 'metakey');
  256. $item->addInstruction(FinderIndexer::META_CONTEXT, 'metadesc');
  257. $item->addInstruction(FinderIndexer::META_CONTEXT, 'metaauthor');
  258. $item->addInstruction(FinderIndexer::META_CONTEXT, 'author');
  259. $item->addInstruction(FinderIndexer::META_CONTEXT, 'created_by_alias');
  260. // Translate the state. Articles should only be published if the category is published.
  261. $item->state = $this->translateState($item->state, $item->cat_state);
  262. // Add the type taxonomy data.
  263. $item->addTaxonomy('Type', 'Article');
  264. // Add the author taxonomy data.
  265. if (!empty($item->author) || !empty($item->created_by_alias))
  266. {
  267. $item->addTaxonomy('Author', !empty($item->created_by_alias) ? $item->created_by_alias : $item->author);
  268. }
  269. // Add the category taxonomy data.
  270. $item->addTaxonomy('Category', $item->category, $item->cat_state, $item->cat_access);
  271. // Add the language taxonomy data.
  272. $item->addTaxonomy('Language', $item->language);
  273. // Get content extras.
  274. FinderIndexerHelper::getContentExtras($item);
  275. // Index the item.
  276. $this->indexer->index($item);
  277. }
  278. /**
  279. * Method to setup the indexer to be run.
  280. *
  281. * @return boolean True on success.
  282. *
  283. * @since 2.5
  284. */
  285. protected function setup()
  286. {
  287. // Load dependent classes.
  288. include_once JPATH_SITE . '/components/com_content/helpers/route.php';
  289. return true;
  290. }
  291. /**
  292. * Method to get the SQL query used to retrieve the list of content items.
  293. *
  294. * @param mixed $query A JDatabaseQuery object or null.
  295. *
  296. * @return JDatabaseQuery A database object.
  297. *
  298. * @since 2.5
  299. */
  300. protected function getListQuery($query = null)
  301. {
  302. $db = JFactory::getDbo();
  303. // Check if we can use the supplied SQL query.
  304. $query = $query instanceof JDatabaseQuery ? $query : $db->getQuery(true)
  305. ->select('a.id, a.title, a.alias, a.introtext AS summary, a.fulltext AS body')
  306. ->select('a.state, a.catid, a.created AS start_date, a.created_by')
  307. ->select('a.created_by_alias, a.modified, a.modified_by, a.attribs AS params')
  308. ->select('a.metakey, a.metadesc, a.metadata, a.language, a.access, a.version, a.ordering')
  309. ->select('a.publish_up AS publish_start_date, a.publish_down AS publish_end_date')
  310. ->select('c.title AS category, c.published AS cat_state, c.access AS cat_access');
  311. // Handle the alias CASE WHEN portion of the query
  312. $case_when_item_alias = ' CASE WHEN ';
  313. $case_when_item_alias .= $query->charLength('a.alias', '!=', '0');
  314. $case_when_item_alias .= ' THEN ';
  315. $a_id = $query->castAsChar('a.id');
  316. $case_when_item_alias .= $query->concatenate(array($a_id, 'a.alias'), ':');
  317. $case_when_item_alias .= ' ELSE ';
  318. $case_when_item_alias .= $a_id . ' END as slug';
  319. $query->select($case_when_item_alias);
  320. $case_when_category_alias = ' CASE WHEN ';
  321. $case_when_category_alias .= $query->charLength('c.alias', '!=', '0');
  322. $case_when_category_alias .= ' THEN ';
  323. $c_id = $query->castAsChar('c.id');
  324. $case_when_category_alias .= $query->concatenate(array($c_id, 'c.alias'), ':');
  325. $case_when_category_alias .= ' ELSE ';
  326. $case_when_category_alias .= $c_id . ' END as catslug';
  327. $query->select($case_when_category_alias)
  328. ->select('u.name AS author')
  329. ->from('#__content AS a')
  330. ->join('LEFT', '#__categories AS c ON c.id = a.catid')
  331. ->join('LEFT', '#__users AS u ON u.id = a.created_by');
  332. return $query;
  333. }
  334. }