Common.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Wolfram Kriesing <wolfram@kriesing.de> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Common.php,v 1.21 2003/03/04 18:59:21 cain Exp $
  20. require_once('Tree/OptionsDB.php');
  21. define("TREE_ERROR", -1);
  22. define("TREE_ERROR_INVALID_PARENT", -2);
  23. /**
  24. * common tree class, implements common functionality
  25. *
  26. * this class extends Tree_OptionsDB so every class that extends this oe can
  27. * connect to a db and set options
  28. *
  29. * @access public
  30. * @author Wolfram Kriesing <wolfram@kriesing.de>
  31. * @version 2001/06/27
  32. * @package Tree
  33. */
  34. class Tree_Common extends Tree_OptionsDB
  35. {
  36. /**
  37. * put proper value-keys are given in each class, depending on the implementation
  38. * only some options are needed or allowed, see the classes which extend this one
  39. *
  40. * @access public
  41. * @var array saves the options passed to the constructor
  42. */
  43. var $options = array();
  44. /**
  45. *
  46. *
  47. * @version 2002/01/18
  48. * @access public
  49. * @author Wolfram Kriesing <wolfram@kriesing.de>
  50. */
  51. function getChildId( $id )
  52. {
  53. $child = $this->getChild( $id );
  54. return $child['id'];
  55. }
  56. /**
  57. * get the ids of the children of the given element
  58. *
  59. * @version 2002/02/06
  60. * @access public
  61. * @author Wolfram Kriesing <wolfram@kriesing.de>
  62. * @param integer $id ID of the element that the children shall be retreived for
  63. * @param integer how many levels deep into the tree
  64. * @return mixed an array of all the ids of the children of the element with id=$id,
  65. * or false if there are no children
  66. */
  67. function getChildrenIds($id,$levels=1)
  68. {
  69. if (!($children = $this->getChildren($id,$levels))) { // returns false if no children exist
  70. return array(); // return an empty array, if you want to know if there are children, use hasChildren
  71. }
  72. if ($children && sizeof($children)) {
  73. foreach ($children as $aChild) {
  74. $childrenIds[] = $aChild['id'];
  75. }
  76. }
  77. return $childrenIds;
  78. }
  79. /**
  80. * gets all the children and grand children etc.
  81. *
  82. * @version 2002/09/30
  83. * @access public
  84. * @author Wolfram Kriesing <wolfram@kriesing.de>
  85. * @param integer $id ID of the element that the children shall be retreived for
  86. * @return mixed an array of all the children of the element with id=$id,
  87. * or false if there are no children
  88. */
  89. // FIXXXME remove this method and replace it by getChildren($id,0)
  90. function getAllChildren($id)
  91. {
  92. $retChildren = false;
  93. if ($children = $this->hasChildren($id)) {
  94. $retChildren = $this->_getAllChildren( $id );
  95. }
  96. return $retChildren;
  97. }
  98. /**
  99. * this method gets all the children recursively
  100. *
  101. * @see getAllChildren()
  102. * @version 2002/09/30
  103. * @access public
  104. * @author Wolfram Kriesing <wolfram@kriesing.de>
  105. * @param integer $id ID of the element that the children shall be retreived for
  106. * @return mixed an array of all the ids of the children of the element with id=$id,
  107. * or false if there are no children
  108. */
  109. function &_getAllChildren($id)
  110. {
  111. $retChildren = array();
  112. if ($children = $this->getChildren($id)) {
  113. foreach ($children as $key=>$aChild) {
  114. $retChildren[] = &$children[$key];
  115. $retChildren = array_merge($retChildren,$this->_getAllChildren( $aChild['id'] ));
  116. }
  117. }
  118. return $retChildren;
  119. }
  120. /**
  121. * gets all the children-ids and grand children-ids
  122. *
  123. * @version 2002/09/30
  124. * @access public
  125. * @author Wolfram Kriesing <wolfram@kriesing.de>
  126. * @param integer $id ID of the element that the children shall be retreived for
  127. * @return mixed an array of all the ids of the children of the element with id=$id,
  128. * or false if there are no children
  129. */
  130. function getAllChildrenIds( $id )
  131. {
  132. $childrenIds = array();
  133. if( $allChildren = $this->getAllChildren($id) )
  134. {
  135. $childrenIds = array();
  136. foreach( $allChildren as $aNode )
  137. $childrenIds[] = $aNode['id'];
  138. }
  139. return $childrenIds;
  140. }
  141. /**
  142. * get the id of the parent for the given element
  143. *
  144. * @version 2002/01/18
  145. * @access public
  146. * @param integer $id the id of the element for which the parentId shall be retreived
  147. * @author Wolfram Kriesing <wolfram@kriesing.de>
  148. */
  149. function getParentId( $id )
  150. {
  151. $parent = $this->getParent( $id );
  152. return $parent['id'];
  153. }
  154. /**
  155. * this gets all the preceeding nodes, the parent and it's parent and so on
  156. *
  157. * @version 2002/08/19
  158. * @access public
  159. * @author Wolfram Kriesing <wolfram@kriesing.de>
  160. * @param integer $id the id of the element for which the parentId shall be retreived
  161. * @return array of the parent nodes including the node with id $id
  162. */
  163. function getParents( $id )
  164. {
  165. $path = $this->getPath($id);
  166. $parents = array();
  167. if( sizeof($path) )
  168. foreach( $path as $aNode )
  169. $parents[] = $aNode;
  170. return $parents;
  171. }
  172. /**
  173. * get the ids of the parents and all it's parents and so on
  174. * it simply returns the ids of the elements returned by getParents()
  175. * @see getParents()
  176. * @version 2002/08/19
  177. * @access public
  178. * @author Wolfram Kriesing <wolfram@kriesing.de>
  179. * @param integer $id the id of the element for which the parentId shall be retreived
  180. * @return array of the ids
  181. */
  182. function getParentsIds( $id )
  183. {
  184. $parents = $this->getParents($id);
  185. $parentsIds = array();
  186. if( sizeof($parents) )
  187. foreach( $parents as $aNode )
  188. $parentsIds[] = $aNode['id'];
  189. return $parentsIds;
  190. }
  191. /**
  192. *
  193. *
  194. * @version 2002/01/18
  195. * @access public
  196. * @author Wolfram Kriesing <wolfram@kriesing.de>
  197. */
  198. function getNextId( $id )
  199. {
  200. $next = $this->getNext( $id );
  201. return $next['id'];
  202. }
  203. /**
  204. *
  205. *
  206. * @version 2002/01/18
  207. * @access public
  208. * @author Wolfram Kriesing <wolfram@kriesing.de>
  209. */
  210. function getPreviousId( $id )
  211. {
  212. $previous = $this->getPrevious( $id );
  213. return $previous['id'];
  214. }
  215. /**
  216. *
  217. *
  218. * @version 2002/01/18
  219. * @access public
  220. * @author Wolfram Kriesing <wolfram@kriesing.de>
  221. */
  222. function getLeftId( $id )
  223. {
  224. $left = $this->getLeft( $id );
  225. return $left['id'];
  226. }
  227. /**
  228. *
  229. *
  230. * @version 2002/01/18
  231. * @access public
  232. * @author Wolfram Kriesing <wolfram@kriesing.de>
  233. */
  234. function getRightId( $id )
  235. {
  236. $right = $this->getRight( $id );
  237. return $right['id'];
  238. }
  239. /**
  240. *
  241. *
  242. * @version 2002/01/18
  243. * @access public
  244. * @author Wolfram Kriesing <wolfram@kriesing.de>
  245. */
  246. function getFirstRootId()
  247. {
  248. $firstRoot = $this->getFirstRoot();
  249. return $firstRoot['id'];
  250. }
  251. /**
  252. *
  253. *
  254. * @version 2002/04/16
  255. * @access public
  256. * @author Wolfram Kriesing <wolfram@kriesing.de>
  257. */
  258. function getRootId()
  259. {
  260. $firstRoot = $this->getRoot();
  261. return $firstRoot['id'];
  262. }
  263. /**
  264. * returns the path as a string
  265. *
  266. * @access public
  267. * @version 2002/03/28
  268. * @access public
  269. * @author Wolfram Kriesing <wolfram@kriesing.de>
  270. * @param mixed $id the id of the node to get the path for
  271. * @param integer If offset is positive, the sequence will
  272. * start at that offset in the array . If
  273. * offset is negative, the sequence will start that far from the end of the array .
  274. * @param integer If length is given and is positive, then
  275. * the sequence will have that many elements in it. If
  276. * length is given and is negative then the
  277. * sequence will stop that many elements from the end of the
  278. * array. If it is omitted, then the sequence will have everything
  279. * from offset up until the end of the array.
  280. * @return array this array contains all elements from the root to the element given by the id
  281. *
  282. */
  283. function getPathAsString( $id , $seperator='/' , $offset=0 , $length=0 )
  284. {
  285. $path = $this->getPath($id);
  286. foreach ($path as $aNode) {
  287. $pathArray[] = $aNode['name'];
  288. }
  289. if ($offset) {
  290. if ($length) {
  291. $pathArray = array_slice($pathArray,$offset,$length);
  292. } else {
  293. $pathArray = array_slice($pathArray,$offset);
  294. }
  295. }
  296. $pathString = '';
  297. if( sizeof($pathArray) )
  298. $pathString = implode($seperator,$pathArray);
  299. return $pathString;
  300. } // end of function
  301. //
  302. // abstract methods, those should be overwritten by the implementing class
  303. //
  304. /**
  305. * gets the path to the element given by its id
  306. *
  307. * @abstract
  308. * @version 2001/10/10
  309. * @access public
  310. * @author Wolfram Kriesing <wolfram@kriesing.de>
  311. * @param mixed $id the id of the node to get the path for
  312. * @return array this array contains all elements from the root to the element given by the id
  313. *
  314. */
  315. function getPath( $id )
  316. {
  317. return $this->_throwError( 'not implemented, at least not overwritten the abstract declaration' , __LINE__ );
  318. } // end of function
  319. /**
  320. * get the level, which is how far below the root the element with the given id is
  321. *
  322. * @abstract
  323. * @version 2001/11/25
  324. * @access public
  325. * @author Wolfram Kriesing <wolfram@kriesing.de>
  326. * @param mixed $id the id of the node to get the level for
  327. *
  328. */
  329. function getLevel( $id )
  330. {
  331. return $this->_throwError( 'not implemented, at least not overwritten the abstract declaration' , __LINE__ );
  332. } // end of function
  333. /**
  334. * returns if $childId is a child of $id
  335. *
  336. * @abstract
  337. * @version 2002/04/29
  338. * @access public
  339. * @author Wolfram Kriesing <wolfram@kriesing.de>
  340. * @param int id of the element
  341. * @param int id of the element to check if it is a child
  342. * @param boolean if this is true the entire tree below is checked
  343. * @return boolean true if it is a child
  344. */
  345. function isChildOf( $id , $childId , $checkAll=true )
  346. {
  347. return $this->_throwError( 'not implemented, at least not overwritten the abstract declaration' , __LINE__ );
  348. } // end of function
  349. /**
  350. * return the maximum depth of the tree
  351. *
  352. * @version 2003/02/25
  353. * @access public
  354. * @author Wolfram Kriesing <wolfram@kriesing.de>
  355. * @return int the depth of the tree
  356. */
  357. function getDepth()
  358. {
  359. return $this->_treeDepth;
  360. } // end of function
  361. //
  362. // PRIVATE METHODS
  363. //
  364. /**
  365. * prepare multiple results
  366. *
  367. * @see _prepareResult()
  368. * @access private
  369. * @version 2002/03/03
  370. * @author Wolfram Kriesing <wolfram@kriesing.de>
  371. * @param
  372. * @return
  373. */
  374. function _prepareResults( $results )
  375. {
  376. $newResults = array();
  377. foreach( $results as $key=>$aResult )
  378. $newResults[$key] = $this->_prepareResult($aResult);
  379. return $newResults;
  380. }
  381. /**
  382. * map back the index names to get what is expected
  383. *
  384. * @access private
  385. * @version 2002/03/03
  386. * @author Wolfram Kriesing <wolfram@kriesing.de>
  387. * @param
  388. * @return
  389. */
  390. function _prepareResult( $result )
  391. {
  392. $map = $this->getOption('columnNameMaps');
  393. if( $map )
  394. foreach( $map as $key=>$columnName )
  395. {
  396. $result[$key] = $result[$columnName];
  397. unset($result[$columnName]);
  398. }
  399. return $result;
  400. }
  401. /**
  402. * this method retreives the real column name, as used in the DB
  403. * since the internal names are fixed, to be portable between different
  404. * DB-column namings, we map the internal name to the real column name here
  405. *
  406. * @access private
  407. * @version 2002/03/02
  408. * @author Wolfram Kriesing <wolfram@kriesing.de>
  409. * @param
  410. * @return
  411. */
  412. function _getColName( $internalName )
  413. {
  414. if( $map = $this->getOption( 'columnNameMaps' ) )
  415. {
  416. if( isset($map[$internalName]) )
  417. return $map[$internalName];
  418. }
  419. return $internalName;
  420. }
  421. /**
  422. *
  423. *
  424. * @access private
  425. * @version 2002/03/02
  426. * @author Wolfram Kriesing <wolfram@kriesing.de>
  427. * @param
  428. * @return
  429. */
  430. function _throwError( $msg , $line , $mode=null )
  431. {
  432. if( $mode===null && $this->debug>0 )
  433. $mode = PEAR_ERROR_PRINT;
  434. return new Tree_Error( $msg , $line , __FILE__ , $mode , $this->dbh->last_query );
  435. }
  436. }
  437. ?>