PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/finder/weblinks/weblinks.php

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