| 1 |
joko |
1.1 |
<?php |
| 2 |
|
|
// |
| 3 |
|
|
// Id: Memory_XML.php,v 1.2 2003/01/30 17:43:41 cain Exp |
| 4 |
|
|
// $Id: Memory_XML.php,v 1.2 2003/01/30 17:43:41 cain Exp $ |
| 5 |
|
|
// |
| 6 |
|
|
|
| 7 |
|
|
//ini_set('include_path',realpath(dirname(__FILE__).'/../../').':'.realpath(dirname(__FILE__).'/../../../includes').':'.ini_get('include_path')); |
| 8 |
|
|
//ini_set('error_reporting',E_ALL); |
| 9 |
|
|
/** |
| 10 |
|
|
* this is a helper function, so i dont have to write so many prints :-) |
| 11 |
|
|
* @param array $para the result returned by some method, that will be dumped |
| 12 |
|
|
* @param string $string the explaining string |
| 13 |
|
|
*/ |
| 14 |
|
|
function dumpHelper( $para , $string='' ) |
| 15 |
|
|
{ |
| 16 |
|
|
global $tree; |
| 17 |
|
|
|
| 18 |
|
|
print '<i><u><font color="#008000">'.$string.'</font></u></i><br>'; |
| 19 |
|
|
// this method dumps to the screen, since print_r or var_dump dont |
| 20 |
|
|
// work too good here, because the inner array is recursive |
| 21 |
|
|
// well, it looks ugly but one can see what is meant :-) |
| 22 |
|
|
$tree->varDump($para); |
| 23 |
|
|
print '<br>'; |
| 24 |
|
|
|
| 25 |
|
|
} |
| 26 |
|
|
|
| 27 |
|
|
/** |
| 28 |
|
|
* dumps the entire structure nicely |
| 29 |
|
|
* @param string $string the explaining string |
| 30 |
|
|
*/ |
| 31 |
|
|
function dumpAllNicely( $string='' ) |
| 32 |
|
|
{ |
| 33 |
|
|
global $tree; |
| 34 |
|
|
|
| 35 |
|
|
print '<i><u><font color="#008000">'.$string.'</font></u></i><br>'; |
| 36 |
|
|
$all = $tree->getNode(); // get the entire structure sorted as the tree is, so we can simply foreach through it and show it |
| 37 |
|
|
foreach( $all as $aElement ) |
| 38 |
|
|
{ |
| 39 |
|
|
for( $i=0 ; $i<$aElement['level'] ; $i++) |
| 40 |
|
|
print ' '; |
| 41 |
|
|
print '<b>'.$aElement['name'].'</b> ===> '; |
| 42 |
|
|
|
| 43 |
|
|
// you can also show all the content, using this |
| 44 |
|
|
// $tree->varDump(array($aElement)); |
| 45 |
|
|
// i just didnt, since it takes up more then the entire line, and its unreadable :-) |
| 46 |
|
|
|
| 47 |
|
|
print 'attributes - '; |
| 48 |
|
|
print_r($aElement['attributes']); |
| 49 |
|
|
print '<br>'; |
| 50 |
|
|
|
| 51 |
|
|
} |
| 52 |
|
|
print '<br>'; |
| 53 |
|
|
|
| 54 |
|
|
} |
| 55 |
|
|
|
| 56 |
|
|
|
| 57 |
|
|
/* |
| 58 |
|
|
|
| 59 |
|
|
This example demonstrates how to manage trees |
| 60 |
|
|
that are saved in an XML-file |
| 61 |
|
|
|
| 62 |
|
|
it reads out the entire file upon calling the method |
| 63 |
|
|
'setup', then you can work on the tree in whichever way |
| 64 |
|
|
you want, just have a look at the examples |
| 65 |
|
|
there are different ways to achieve things, |
| 66 |
|
|
i will try to demonstrate (all of) them |
| 67 |
|
|
|
| 68 |
|
|
NOTE: for referening the XML-Nodes currently everything has to |
| 69 |
|
|
be lower case, |
| 70 |
|
|
SimpleTemplate/preFilter |
| 71 |
|
|
should be |
| 72 |
|
|
simpletemplate/prefilter |
| 73 |
|
|
|
| 74 |
|
|
*/ |
| 75 |
|
|
|
| 76 |
|
|
require_once('Tree/Tree.php'); |
| 77 |
|
|
|
| 78 |
|
|
// calling 'setupMemory' means to retreive a class, which works on trees, |
| 79 |
|
|
// that are temporarily stored in the memory, in an array |
| 80 |
|
|
// this means the entire tree is available at all time |
| 81 |
|
|
// consider the resource usage and it's not to suggested to work |
| 82 |
|
|
// on huge trees (upto 1000 elements it should be ok, depending on your environment and requirements) |
| 83 |
|
|
// using 'setupMemory' |
| 84 |
|
|
$tree = Tree::setupMemory( 'XML', // use the XML class to read an xml file |
| 85 |
|
|
'config.xml' // the DSN |
| 86 |
|
|
); |
| 87 |
|
|
|
| 88 |
|
|
// methods 'add' 'remove' and so on are not implemented yet, you can only read the tree for now |
| 89 |
|
|
// and navigate inside of it |
| 90 |
|
|
|
| 91 |
|
|
// call 'setup', to build the inner array, so we can work on the structure using the |
| 92 |
|
|
// given methods |
| 93 |
|
|
$tree->setup(); |
| 94 |
|
|
|
| 95 |
|
|
dumpAllNicely( 'dump all after "$tree->setup"' ); |
| 96 |
|
|
|
| 97 |
|
|
// get the path of the last inserted element |
| 98 |
|
|
print 'id='.$id = $tree->getIdByPath('simpletemplate/options/delimiter'); |
| 99 |
|
|
dumpHelper( $tree->getPath( $id ) , 'dump the path from "simpletemplate/options/delimiter"' ); |
| 100 |
|
|
|
| 101 |
|
|
$id = $tree->getIdByPath('simpletemplate/options'); |
| 102 |
|
|
dumpHelper( array($tree->getParent($id)) , 'dump the parent of "simpletemplate/options"' ); |
| 103 |
|
|
// you can also use: $tree->data[$id]['parent'] |
| 104 |
|
|
|
| 105 |
|
|
$id = $tree->getIdByPath('simpletemplate'); |
| 106 |
|
|
dumpHelper( array($tree->getChild($id)) , 'dump the child of "simpletemplate"' ); |
| 107 |
|
|
// you can also use: $tree->data[$id]['child'] |
| 108 |
|
|
|
| 109 |
|
|
$id = $tree->getIdByPath('simpletemplate/prefilter'); |
| 110 |
|
|
dumpHelper( $tree->getChildren($id) , 'dump the children of "simpletemplate/prefilter"' ); |
| 111 |
|
|
// you can also use: $tree->data[$id]['children'] |
| 112 |
|
|
|
| 113 |
|
|
$id = $tree->getIdByPath('simpletemplate/options'); |
| 114 |
|
|
dumpHelper( array($tree->getNext($id)) , 'dump the "next" of "simpletemplate/options"' ); |
| 115 |
|
|
// you can also use: $tree->data[$id]['next'] |
| 116 |
|
|
|
| 117 |
|
|
$id = $tree->getIdByPath('simpletemplate/prefilter'); |
| 118 |
|
|
dumpHelper( array($tree->getPrevious($id)) , 'dump the "previous" of "simpletemplate/prefilter"' ); |
| 119 |
|
|
// you can also use: $tree->data[$id]['previous'] |
| 120 |
|
|
|
| 121 |
|
|
|
| 122 |
|
|
$id = $tree->getIdByPath('simpletemplate/preFilter'); |
| 123 |
|
|
$element = $tree->data[$id]['child']['next']['next']; // refer to the third child of 'SimpleTemplate/preFilter/register' |
| 124 |
|
|
dumpHelper( $element['id'] , 'demo of using the internal array, for referencing tree-nodes' ); |
| 125 |
|
|
|
| 126 |
|
|
/* |
| 127 |
|
|
NOT IMPLEMENTED YET |
| 128 |
|
|
|
| 129 |
|
|
$id = $tree->getIdByPath('myElement/anotherSubElement'); |
| 130 |
|
|
$tree->move( $id , 0 ); |
| 131 |
|
|
$tree->setup(); // rebuild the structure again, since we had changed it |
| 132 |
|
|
dumpAllNicely( 'dump all, after "myElement/anotherSubElement" was moved under the root' ); |
| 133 |
|
|
|
| 134 |
|
|
$moveId = $tree->getIdByPath('myElement'); |
| 135 |
|
|
$id = $tree->getIdByPath('anotherSubElement'); |
| 136 |
|
|
$tree->move( $moveId , $id ); |
| 137 |
|
|
$tree->setup(); // rebuild the structure again, since we had changed it |
| 138 |
|
|
dumpAllNicely( 'dump all, after "myElement" was moved under the "anotherSubElement"' ); |
| 139 |
|
|
|
| 140 |
|
|
|
| 141 |
|
|
$tree->setRemoveRecursively(true); |
| 142 |
|
|
$tree->remove(0); |
| 143 |
|
|
print '<font color="red">ALL ELEMENTS HAVE BEEN REMOVED (uncomment this part to keep them in the DB after running this test script)</font>'; |
| 144 |
|
|
*/ |
| 145 |
|
|
?> |