PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/com_finder/admin/helpers/indexer/result.php

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