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

/framework/DataTree/lib/Horde/DataTree/Null.php

https://github.com/sgtcarneiro/horde
PHP | 412 lines | 150 code | 34 blank | 228 comment | 18 complexity | d9181d92d3c91ca2837f33834f80a197 MD5 | raw file
  1. <?php
  2. /**
  3. * The Horde_DataTree_null class provides a dummy implementation of the
  4. * Horde_DataTree API; no data will last beyond a single page request.
  5. *
  6. * Copyright 1999-2011 The Horde Project (http://www.horde.org/)
  7. *
  8. * See the enclosed file COPYING for license information (LGPL). If you
  9. * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  10. *
  11. * @author Stephane Huther <shuther1@free.fr>
  12. * @author Chuck Hagenbuch <chuck@horde.org>
  13. * @package DataTree
  14. */
  15. class Horde_DataTree_Null extends Horde_DataTree {
  16. /**
  17. * Cache of attributes for any objects created during this page request.
  18. *
  19. * @var array
  20. */
  21. var $_attributeCache = array();
  22. /**
  23. * Cache of data for any objects created during this page request.
  24. *
  25. * @var array
  26. */
  27. var $_dataCache = array();
  28. /**
  29. * Load (a subset of) the datatree into the $_data array. Part of the
  30. * Horde_DataTree API that must be overridden by subclasses.
  31. *
  32. * @param string $root Which portion of the tree to load. Defaults to
  33. * all of it.
  34. * @param boolean $reload Re-load already loaded values?
  35. *
  36. * @return mixed True on success or a PEAR_Error on failure.
  37. *
  38. * @access private
  39. */
  40. function _load($root = null, $reload = false)
  41. {
  42. }
  43. /**
  44. * Load a specific object identified by its unique ID ($id), and
  45. * its parents, into the $_data array.
  46. *
  47. * @param integer $cid The unique ID of the object to load.
  48. *
  49. * @return mixed True on success or a PEAR_Error on failure.
  50. *
  51. * @access private
  52. */
  53. function _loadById($cid)
  54. {
  55. }
  56. /**
  57. * Check for existance of an object in a backend-specific manner.
  58. *
  59. * @param string $object_name Object name to check for.
  60. *
  61. * @return boolean True if the object exists, false otherwise.
  62. */
  63. function _exists($object_name)
  64. {
  65. return false;
  66. }
  67. /**
  68. * Look up a datatree id by name.
  69. *
  70. * @param string $name
  71. *
  72. * @return integer Horde_DataTree id
  73. */
  74. function _getId($name)
  75. {
  76. return null;
  77. }
  78. /**
  79. * Look up a datatree name by id.
  80. *
  81. * @param integer $id
  82. *
  83. * @return string Horde_DataTree name
  84. */
  85. function _getName($id)
  86. {
  87. return null;
  88. }
  89. /**
  90. * Get a tree sorted by the specified attribute name and/or key.
  91. *
  92. * @param string $root Which portion of the tree to sort.
  93. * Defaults to all of it.
  94. * @param boolean $loadTree Sort the tree starting at $root, or just the
  95. * requested level and direct parents?
  96. * Defaults to single level.
  97. * @param array $sortby_name Attribute name to use for sorting.
  98. * @param array $sortby_key Attribute key to use for sorting.
  99. * @param array $direction Sort direction:
  100. * 0 - ascending
  101. * 1 - descending
  102. *
  103. * @return array TODO
  104. */
  105. function getSortedTree($root, $loadTree = false, $sortby_name = null, $sortby_key = null, $direction = 0)
  106. {
  107. return array();
  108. }
  109. /**
  110. * Add an object. Part of the Horde_DataTree API that must be
  111. * overridden by subclasses.
  112. *
  113. * @param mixed $fullname The object to add (string or Horde_DataTreeObject).
  114. */
  115. function add($object)
  116. {
  117. if (is_a($object, 'Horde_DataTreeObject')) {
  118. $fullname = $object->getName();
  119. $order = $object->order;
  120. } else {
  121. $fullname = $object;
  122. $order = null;
  123. }
  124. $id = md5(mt_rand());
  125. if (strpos($fullname, ':') !== false) {
  126. $parts = explode(':', $fullname);
  127. $name = array_pop($parts);
  128. $parent = implode(':', $parts);
  129. $pid = $this->getId($parent);
  130. if (is_a($pid, 'PEAR_Error')) {
  131. $this->add($parent);
  132. }
  133. } else {
  134. $pid = DATATREE_ROOT;
  135. }
  136. if (parent::exists($fullname)) {
  137. return PEAR::raiseError('Already exists');
  138. }
  139. $added = parent::_add($fullname, $id, $pid, $order);
  140. if (is_a($added, 'PEAR_Error')) {
  141. return $added;
  142. }
  143. return $this->updateData($object);
  144. }
  145. /**
  146. * Change order of the children of an object.
  147. *
  148. * @param string $parents The parent id string path.
  149. * @param mixed $order A specific new order position or an array
  150. * containing the new positions for the given
  151. * $parents object.
  152. * @param integer $cid If provided indicates insertion of a new child
  153. * to the object, and will be used to avoid
  154. * incrementing it when shifting up all other
  155. * children's order. If not provided indicates
  156. * deletion, hence shift all other positions down
  157. * one.
  158. */
  159. function reorder($parents, $order = null, $cid = null)
  160. {
  161. if (is_array($order) && !empty($order)) {
  162. // Multi update.
  163. $this->_reorder($pid, $order);
  164. }
  165. }
  166. /**
  167. * Explicitly set the order for a datatree object.
  168. *
  169. * @param integer $id The datatree object id to change.
  170. * @param integer $order The new order.
  171. */
  172. function setOrder($id, $order)
  173. {
  174. }
  175. /**
  176. * Removes an object.
  177. *
  178. * @param mixed $object The object to remove.
  179. * @param boolean $force Force removal of every child object?
  180. */
  181. function remove($object, $force = false)
  182. {
  183. }
  184. /**
  185. * Remove one or more objects by id. This function does *not* do
  186. * the validation, reordering, etc. that remove() does. If you
  187. * need to check for children, re-do ordering, etc., then you must
  188. * remove() objects one-by-one. This is for code that knows it's
  189. * dealing with single (non-parented) objects and needs to delete
  190. * a batch of them quickly.
  191. *
  192. * @param array $ids The objects to remove.
  193. */
  194. function removeByIds($ids)
  195. {
  196. }
  197. /**
  198. * Remove one or more objects by name. This function does *not* do
  199. * the validation, reordering, etc. that remove() does. If you
  200. * need to check for children, re-do ordering, etc., then you must
  201. * remove() objects one-by-one. This is for code that knows it's
  202. * dealing with single (non-parented) objects and needs to delete
  203. * a batch of them quickly.
  204. *
  205. * @param array $names The objects to remove.
  206. */
  207. function removeByNames($names)
  208. {
  209. }
  210. /**
  211. * Move an object to a new parent.
  212. *
  213. * @param mixed $object The object to move.
  214. * @param string $newparent The new parent object. Defaults to the root.
  215. */
  216. function move($object, $newparent = null)
  217. {
  218. }
  219. /**
  220. * Change an object's name.
  221. *
  222. * @param mixed $old_object The old object.
  223. * @param string $new_object_name The new object name.
  224. */
  225. function rename($old_object, $new_object_name)
  226. {
  227. }
  228. /**
  229. * Retrieve data for an object from the datatree_data field.
  230. *
  231. * @param integer $cid The object id to fetch, or an array of object ids.
  232. */
  233. function getData($cid)
  234. {
  235. return isset($this->_dataCache[$cid]) ?
  236. $this->_dataCache[$cid] :
  237. array();
  238. }
  239. /**
  240. * Retrieve data for an object.
  241. *
  242. * @param integer $cid The object id to fetch.
  243. */
  244. function getAttributes($cid)
  245. {
  246. if (is_array($cid)) {
  247. $data = array();
  248. foreach ($cid as $id) {
  249. if (isset($this->_attributeCache[$id])) {
  250. $data[$id] = $this->_attributeCache[$id];
  251. }
  252. }
  253. return $data;
  254. } else {
  255. return isset($this->_attributeCache[$cid]) ?
  256. $this->_attributeCache[$cid] :
  257. array();
  258. }
  259. }
  260. /**
  261. * Returns the number of objects matching a set of attribute
  262. * criteria.
  263. *
  264. * @see buildAttributeQuery()
  265. *
  266. * @param array $criteria The array of criteria.
  267. * @param string $parent The parent node to start searching from.
  268. * @param boolean $allLevels Return all levels, or just the direct
  269. * children of $parent? Defaults to all levels.
  270. * @param string $restrict Only return attributes with the same
  271. * attribute_name or attribute_id.
  272. */
  273. function countByAttributes($criteria, $parent = DATATREE_ROOT, $allLevels = true, $restrict = 'name')
  274. {
  275. if (!count($criteria)) {
  276. return 0;
  277. }
  278. return count($this->_attributeCache);
  279. }
  280. /**
  281. * Returns a set of object ids based on a set of attribute criteria.
  282. *
  283. * @see buildAttributeQuery()
  284. *
  285. * @param array $criteria The array of criteria.
  286. * @param string $parent The parent node to start searching from.
  287. * @param boolean $allLevels Return all levels, or just the direct
  288. * children of $parent? Defaults to all levels.
  289. * @param string $restrict Only return attributes with the same
  290. * attribute_name or attribute_id.
  291. * @param integer $from The object to start to fetching
  292. * @param integer $count The number of objects to fetch
  293. * @param string $sortby_name Attribute name to use for sorting.
  294. * @param string $sortby_key Attribute key to use for sorting.
  295. * @param integer $direction Sort direction:
  296. * 0 - ascending
  297. * 1 - descending
  298. */
  299. function getByAttributes($criteria, $parent = DATATREE_ROOT, $allLevels = true, $restrict = 'name', $from = 0, $count = 0,
  300. $sortby_name = null, $sortby_key = null, $direction = 0)
  301. {
  302. if (!count($criteria)) {
  303. return PEAR::raiseError('no criteria');
  304. }
  305. $cids = array();
  306. foreach (array_keys($this->_attributeCache) as $cid) {
  307. $cids[$cid] = null;
  308. }
  309. return $cids;
  310. }
  311. /**
  312. * Sorts IDs by attribute values. IDs without attributes will be
  313. * added to the end of the sorted list.
  314. *
  315. * @param array $unordered_ids Array of ids to sort.
  316. * @param array $sortby_name Attribute name to use for sorting.
  317. * @param array $sortby_key Attribute key to use for sorting.
  318. * @param array $direction Sort direction:
  319. * 0 - ascending
  320. * 1 - descending
  321. *
  322. * @return array Sorted ids.
  323. */
  324. function sortByAttributes($unordered_ids, $sortby_name = null, $sortby_key = null, $direction = 0)
  325. {
  326. return $unordered_ids;
  327. }
  328. /**
  329. * Returns a list of all of the available values of the given
  330. * attribute name/key combination. Either attribute_name or
  331. * attribute_key MUST be supplied, and both MAY be supplied.
  332. *
  333. * @param string $attribute_name The name of the attribute.
  334. * @param string $attribute_key The key value of the attribute.
  335. * @param string $parent The parent node to start searching from.
  336. * @param boolean $allLevels Return all levels, or just the direct
  337. * children of $parent?
  338. *
  339. * @return array An array of all of the available values.
  340. */
  341. function getAttributeValues($attribute_name = null, $attribute_key = null, $parent = DATATREE_ROOT, $allLevels = true)
  342. {
  343. return array();
  344. }
  345. /**
  346. * Update the data in an object. Does not change the object's
  347. * parent or name, just serialized data.
  348. *
  349. * @param string $object The object.
  350. */
  351. function updateData($object)
  352. {
  353. if (!is_a($object, 'Horde_DataTreeObject')) {
  354. return true;
  355. }
  356. $cid = $this->getId($object->getName());
  357. if (is_a($cid, 'PEAR_Error')) {
  358. return $cid;
  359. }
  360. // We handle data differently if we can map it to
  361. // attributes.
  362. if (method_exists($object, '_toAttributes')) {
  363. $this->_attributeCache[$cid] = $object->_toAttributes();
  364. } else {
  365. $this->_dataCache[$cid] = $object->getData();
  366. }
  367. return true;
  368. }
  369. /**
  370. * Init the object.
  371. *
  372. * @return boolean True.
  373. */
  374. function _init()
  375. {
  376. return true;
  377. }
  378. }