/administrator/components/com_finder/helpers/indexer/result.php

https://github.com/pjwiseman/joomla-cms · PHP · 429 lines · 123 code · 39 blank · 267 comment · 10 complexity · 453ddb54deaef87600c9348b15b914ad MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Administrator
  4. * @subpackage com_finder
  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
  8. */
  9. defined('_JEXEC') or die;
  10. JLoader::register('FinderIndexer', __DIR__ . '/indexer.php');
  11. /**
  12. * Result class for the Finder indexer package.
  13. *
  14. * This class uses magic __get() and __set() methods to prevent properties
  15. * being added that might confuse the system. All properties not explicitly
  16. * declared will be pushed into the elements array and can be accessed
  17. * explicitly using the getElement() method.
  18. *
  19. * @since 2.5
  20. */
  21. class FinderIndexerResult
  22. {
  23. /**
  24. * An array of extra result properties.
  25. *
  26. * @var array
  27. * @since 2.5
  28. */
  29. protected $elements = array();
  30. /**
  31. * This array tells the indexer which properties should be indexed and what
  32. * weights to use for those properties.
  33. *
  34. * @var array
  35. * @since 2.5
  36. */
  37. protected $instructions = array(
  38. FinderIndexer::TITLE_CONTEXT => array('title', 'subtitle', 'id'),
  39. FinderIndexer::TEXT_CONTEXT => array('summary', 'body'),
  40. FinderIndexer::META_CONTEXT => array('meta', 'list_price', 'sale_price'),
  41. FinderIndexer::PATH_CONTEXT => array('path', 'alias'),
  42. FinderIndexer::MISC_CONTEXT => array('comments')
  43. );
  44. /**
  45. * The indexer will use this data to create taxonomy mapping entries for
  46. * the item so that it can be filtered by type, label, category,
  47. * or whatever.
  48. *
  49. * @var array
  50. * @since 2.5
  51. */
  52. protected $taxonomy = array();
  53. /**
  54. * The content URL.
  55. *
  56. * @var string
  57. * @since 2.5
  58. */
  59. public $url;
  60. /**
  61. * The content route.
  62. *
  63. * @var string
  64. * @since 2.5
  65. */
  66. public $route;
  67. /**
  68. * The content title.
  69. *
  70. * @var string
  71. * @since 2.5
  72. */
  73. public $title;
  74. /**
  75. * The content description.
  76. *
  77. * @var string
  78. * @since 2.5
  79. */
  80. public $description;
  81. /**
  82. * The published state of the result.
  83. *
  84. * @var integer
  85. * @since 2.5
  86. */
  87. public $published;
  88. /**
  89. * The content published state.
  90. *
  91. * @var integer
  92. * @since 2.5
  93. */
  94. public $state;
  95. /**
  96. * The content access level.
  97. *
  98. * @var integer
  99. * @since 2.5
  100. */
  101. public $access;
  102. /**
  103. * The content language.
  104. *
  105. * @var string
  106. * @since 2.5
  107. */
  108. public $language = '*';
  109. /**
  110. * The publishing start date.
  111. *
  112. * @var string
  113. * @since 2.5
  114. */
  115. public $publish_start_date;
  116. /**
  117. * The publishing end date.
  118. *
  119. * @var string
  120. * @since 2.5
  121. */
  122. public $publish_end_date;
  123. /**
  124. * The generic start date.
  125. *
  126. * @var string
  127. * @since 2.5
  128. */
  129. public $start_date;
  130. /**
  131. * The generic end date.
  132. *
  133. * @var string
  134. * @since 2.5
  135. */
  136. public $end_date;
  137. /**
  138. * The item list price.
  139. *
  140. * @var mixed
  141. * @since 2.5
  142. */
  143. public $list_price;
  144. /**
  145. * The item sale price.
  146. *
  147. * @var mixed
  148. * @since 2.5
  149. */
  150. public $sale_price;
  151. /**
  152. * The content type id. This is set by the adapter.
  153. *
  154. * @var integer
  155. * @since 2.5
  156. */
  157. public $type_id;
  158. /**
  159. * The default language for content.
  160. *
  161. * @var string
  162. * @since 3.0.2
  163. */
  164. public $defaultLanguage;
  165. /**
  166. * Constructor
  167. *
  168. * @since 3.0.3
  169. */
  170. public function __construct()
  171. {
  172. $this->defaultLanguage = JComponentHelper::getParams('com_languages')->get('site', 'en-GB');
  173. }
  174. /**
  175. * The magic set method is used to push additional values into the elements
  176. * array in order to preserve the cleanliness of the object.
  177. *
  178. * @param string $name The name of the element.
  179. * @param mixed $value The value of the element.
  180. *
  181. * @return void
  182. *
  183. * @since 2.5
  184. */
  185. public function __set($name, $value)
  186. {
  187. $this->elements[$name] = $value;
  188. }
  189. /**
  190. * The magic get method is used to retrieve additional element values
  191. * from the elements array.
  192. *
  193. * @param string $name The name of the element.
  194. *
  195. * @return mixed The value of the element if set, null otherwise.
  196. *
  197. * @since 2.5
  198. */
  199. public function __get($name)
  200. {
  201. // Get the element value if set.
  202. if (array_key_exists($name, $this->elements))
  203. {
  204. return $this->elements[$name];
  205. }
  206. else
  207. {
  208. return null;
  209. }
  210. }
  211. /**
  212. * The magic isset method is used to check the state of additional element
  213. * values in the elements array.
  214. *
  215. * @param string $name The name of the element.
  216. *
  217. * @return boolean True if set, false otherwise.
  218. *
  219. * @since 2.5
  220. */
  221. public function __isset($name)
  222. {
  223. return isset($this->elements[$name]);
  224. }
  225. /**
  226. * The magic unset method is used to unset additional element values in the
  227. * elements array.
  228. *
  229. * @param string $name The name of the element.
  230. *
  231. * @return void
  232. *
  233. * @since 2.5
  234. */
  235. public function __unset($name)
  236. {
  237. unset($this->elements[$name]);
  238. }
  239. /**
  240. * Method to retrieve additional element values from the elements array.
  241. *
  242. * @param string $name The name of the element.
  243. *
  244. * @return mixed The value of the element if set, null otherwise.
  245. *
  246. * @since 2.5
  247. */
  248. public function getElement($name)
  249. {
  250. // Get the element value if set.
  251. if (array_key_exists($name, $this->elements))
  252. {
  253. return $this->elements[$name];
  254. }
  255. else
  256. {
  257. return null;
  258. }
  259. }
  260. /**
  261. * Method to set additional element values in the elements array.
  262. *
  263. * @param string $name The name of the element.
  264. * @param mixed $value The value of the element.
  265. *
  266. * @return void
  267. *
  268. * @since 2.5
  269. */
  270. public function setElement($name, $value)
  271. {
  272. $this->elements[$name] = $value;
  273. }
  274. /**
  275. * Method to get all processing instructions.
  276. *
  277. * @return array An array of processing instructions.
  278. *
  279. * @since 2.5
  280. */
  281. public function getInstructions()
  282. {
  283. return $this->instructions;
  284. }
  285. /**
  286. * Method to add a processing instruction for an item property.
  287. *
  288. * @param string $group The group to associate the property with.
  289. * @param string $property The property to process.
  290. *
  291. * @return void
  292. *
  293. * @since 2.5
  294. */
  295. public function addInstruction($group, $property)
  296. {
  297. // Check if the group exists. We can't add instructions for unknown groups.
  298. if (array_key_exists($group, $this->instructions))
  299. {
  300. // Check if the property exists in the group.
  301. if (!in_array($property, $this->instructions[$group]))
  302. {
  303. // Add the property to the group.
  304. $this->instructions[$group][] = $property;
  305. }
  306. }
  307. }
  308. /**
  309. * Method to remove a processing instruction for an item property.
  310. *
  311. * @param string $group The group to associate the property with.
  312. * @param string $property The property to process.
  313. *
  314. * @return void
  315. *
  316. * @since 2.5
  317. */
  318. public function removeInstruction($group, $property)
  319. {
  320. // Check if the group exists. We can't remove instructions for unknown groups.
  321. if (array_key_exists($group, $this->instructions))
  322. {
  323. // Search for the property in the group.
  324. $key = array_search($property, $this->instructions[$group]);
  325. // If the property was found, remove it.
  326. if ($key !== false)
  327. {
  328. unset($this->instructions[$group][$key]);
  329. }
  330. }
  331. }
  332. /**
  333. * Method to get the taxonomy maps for an item.
  334. *
  335. * @param string $branch The taxonomy branch to get. [optional]
  336. *
  337. * @return array An array of taxonomy maps.
  338. *
  339. * @since 2.5
  340. */
  341. public function getTaxonomy($branch = null)
  342. {
  343. // Get the taxonomy branch if available.
  344. if ($branch !== null && isset($this->taxonomy[$branch]))
  345. {
  346. // Filter the input.
  347. $branch = preg_replace('#[^\pL\pM\pN\p{Pi}\p{Pf}\'+-.,_]+#mui', ' ', $branch);
  348. return $this->taxonomy[$branch];
  349. }
  350. return $this->taxonomy;
  351. }
  352. /**
  353. * Method to add a taxonomy map for an item.
  354. *
  355. * @param string $branch The title of the taxonomy branch to add the node to.
  356. * @param string $title The title of the taxonomy node.
  357. * @param integer $state The published state of the taxonomy node. [optional]
  358. * @param integer $access The access level of the taxonomy node. [optional]
  359. *
  360. * @return void
  361. *
  362. * @since 2.5
  363. */
  364. public function addTaxonomy($branch, $title, $state = 1, $access = 1)
  365. {
  366. // Filter the input.
  367. $branch = preg_replace('#[^\pL\pM\pN\p{Pi}\p{Pf}\'+-.,_]+#mui', ' ', $branch);
  368. // Create the taxonomy node.
  369. $node = new JObject;
  370. $node->title = $title;
  371. $node->state = (int) $state;
  372. $node->access = (int) $access;
  373. // Add the node to the taxonomy branch.
  374. $this->taxonomy[$branch][$node->title] = $node;
  375. }
  376. /**
  377. * Method to set the item language
  378. *
  379. * @return void
  380. *
  381. * @since 3.0
  382. */
  383. public function setLanguage()
  384. {
  385. if ($this->language == '')
  386. {
  387. $this->language = $this->defaultLanguage;
  388. }
  389. }
  390. }