public function init() { $options = array('table' => 'categories', 'order' => 'sort_order', 'columnNameMaps' => array('id' => 'categories_id', 'parentId' => 'parent_id')); $this->tree = Tree::setupMemory('DBsimple', DSN, $options); $this->tree->setup(); $this->assign_by_ref('categoryTree', $this->tree); $this->cPath = array(); if ($cPath = $this->app->GP('cPath')) { $this->cPath = explode('_', $cPath); } elseif ($productId = $this->app->GP('pId')) { $prodToCat = DB_DataObject::factory('products_to_categories'); $prodToCat->get($productId); $path = $this->tree->getPath($prodToCat->categories_id); foreach ($path as $idx => $cat) { $this->cPath = $cat['id']; } } $this->currentCategory = $this->cPath[count($this->cPath) - 1]; }
it reads out the entire tree upon calling the method 'setup', then you can work on the tree in whichever way you want, just have a look at the examples there are different ways to achieve things, i will try to demonstrate (all of) them */ require_once 'Tree/Tree.php'; // define the DB-table where the data shall be read from $options = array('table' => 'MemoryNestedTree', 'whereAddOn' => "comment=''"); // calling 'setupMemory' means to retreive a class, which works on trees, // that are temporarily stored in the memory, in an array // this means the entire tree is available at all time !!! // consider the resource usage and it's not to suggested to work // on huge trees (upto 1000 elements it should be ok, depending on your environment and requirements) // using 'setupMemory' $tree = Tree::setupMemory('DBnested', 'mysql://root@localhost/test', $options); // pass the options we had assigned up there // add a new root element in the tree $rootId = $tree->add(array('name' => 'myElement')); // add an element under the new element we added $id = $tree->add(array('name' => 'subElement'), $rootId); // add another element under the parent element we added $id = $tree->add(array('name' => 'anotherSubElement'), $rootId, $id); // call 'setup', to build the inner array, so we can work on the structure using the // given methods $tree->setup(); dumpAllNicely('dump all after creation'); // get the path of the last inserted element dumpHelper('$tree->getPath( ' . $id . ' )', 'dump the path from "myElement/anotherSubElement"'); $id = $tree->getIdByPath('myElement/subElement'); dumpHelper('$tree->getParent(' . $id . ')', 'dump the parent of "myElement/subElement"', true);
/** * applies the given xml config file * * @access public * @version 01/12/14 * @author Wolfram Kriesing <*****@*****.**> */ function _setOptionsByXmlConfig($configFileOrString, $isString = false) { //print '_setOptionsByXmlConfig ... '.nl2br(htmlentities($configFileOrString))." , $isString<br>"; // include this so i can get the xml file prepared in the tree shape // and i can use the tree methods to retreive the options i need to set :-) if (!@(include_once 'Tree/Tree.php')) { return $this->_error("xml-config could not be parsed, because the class 'Tree/Tree.php' could not be included<br>" . '1. pleace be sure to have the latest PEAR::Tree package installed ' . '(<a href="http://pear.php.net/package-info.php?pacid=104">you get it from here</a>)', PEAR_ERROR_DIE); } if ($isString) { $config = Tree::setupMemory('XML'); $config->setupByRawData($configFileOrString); } else { $config = Tree::setupMemory('XML', $configFileOrString); $config->setup(); } // // add the filters set in the xml config files // // TODO check for prefilter defines in xml config if ($id = $config->getIdByPath('html_template_xipe/prefilter')) { $this->_applyFiltersFromXMLConfig($config, $id, true); } // // apply xml given options to this class, do this after applying the filters // if ($id = $config->getIdByPath('html_template_xipe/options')) { $delimiter = $config->getElementByPath('delimiter', $id); if ($delimiter) { $begin = $delimiter['attributes']['begin']; $end = $delimiter['attributes']['end']; if ($begin && $end) { $setOptions['delimiter'] = array(trim($begin), trim($end)); } } if ($autoBraces = $config->getIdByPath('autobraces', $id)) { $setOptions['autoBraces'] = false; if (strtolower(trim($config->data[$autoBraces]['attributes']['value'])) == 'true') { $setOptions['autoBraces'] = true; } } if ($localeId = $config->getIdByPath('locale', $id)) { $locale = trim($config->data[$localeId]['attributes']['value']); if ($locale) { $setOptions['locale'] = $locale; } } // // find the cache tag // <cache> // <time value="10" unit="minutes"/> // <depends value="$_GET $_POST"/> // </cache> // if ($cacheId = $config->getIdByPath('cache', $id)) { if (@$config->data[$cacheId]['attributes']['dontcache'] == 'true') { $setOptions['cache']['time'] = false; $this->_log('XMLConfig: dont cache this file!'); } else { if ($timeId = $config->getIdByPath('time', $cacheId)) { $time = $config->data[$timeId]['attributes']['value']; if ($unit = $config->data[$timeId]['attributes']['unit']) { switch (strtolower($unit)) { case 'week': case 'weeks': $time = $time * 7; case 'day': case 'days': $time = $time * 24; case 'hour': case 'hours': $time = $time * 60; case 'minute': case 'minutes': $time = $time * 60; case 'second': break; } } //print "XML: cache this page for $time seconds<br><br>"; // if a valid time was found parse also the other tags, valid is also 0, that's why this strange check // accept only integers if ($time == (int) $time) { $cacheOption['time'] = (int) $time; if (($id = $config->getIdByPath('depends', $cacheId)) && ($depends = $config->data[$id]['attributes']['value'])) { $cacheOption['depends'] = $depends; } $setOptions['cache'] = $cacheOption; } else { $this->_log("ERROR in your xml config, caching-time: {$time}, is not valid, in: " . $isString ? 'tpl-file-embedded config' : $configFileOrString); } } } } // apply the options to this class $this->setOptions($setOptions); $this->_applyOptionsToFilterClasses($setOptions); } }
function process($srcDir, $destDir) { $this->_srcDir = $srcDir; $packageFile = $this->_srcDir . '/package.xml'; $cli =& Horde_CLI::singleton(); if (!is_file($packageFile)) { $cli->message('No package.xml in ' . $this->_srcDir, 'cli.warning'); return false; } $tree = Tree::setupMemory('XML', $packageFile); $tree->setup(); // read package name $packageName = trim($tree->getElementContent('/package/name', 'cdata')); $cli->writeln("Processing package {$packageName}."); // look for filelist in '/package/release/filelist' $filelist = $tree->getElementByPath('/package/release/filelist'); if ($filelist) { // do this better, make the tree class work case insensitive $baseInstallDir = $filelist['child']['attributes']['baseinstalldir']; $this->_baseInstallDir = $destDir; if ($baseInstallDir != '/') { $this->_baseInstallDir .= '/' . $baseInstallDir; } if (!is_dir($this->_baseInstallDir)) { require_once 'System.php'; System::mkdir('-p ' . $this->_baseInstallDir); } $this->_handleFilelistTag($filelist); } else { $cli->message('No filelist tag found inside: ' . $packageFile, 'cli.warning'); } }
i will try to demonstrate (all of) them NOTE: for referening the XML-Nodes currently everything has to be lower case, SimpleTemplate/preFilter should be simpletemplate/prefilter */ require_once 'Tree/Tree.php'; // calling 'setupMemory' means to retreive a class, which works on trees, // that are temporarily stored in the memory, in an array // this means the entire tree is available at all time // consider the resource usage and it's not to suggested to work // on huge trees (upto 1000 elements it should be ok, depending on your environment and requirements) // using 'setupMemory' $tree = Tree::setupMemory('XML', 'config.xml'); // methods 'add' 'remove' and so on are not implemented yet, you can only read the tree for now // and navigate inside of it // call 'setup', to build the inner array, so we can work on the structure using the // given methods $tree->setup(); dumpAllNicely('dump all after "$tree->setup"'); // get the path of the last inserted element print 'id=' . ($id = $tree->getIdByPath('simpletemplate/options/delimiter')); dumpHelper($tree->getPath($id), 'dump the path from "simpletemplate/options/delimiter"'); $id = $tree->getIdByPath('simpletemplate/options'); dumpHelper(array($tree->getParent($id)), 'dump the parent of "simpletemplate/options"'); // you can also use: $tree->data[$id]['parent'] $id = $tree->getIdByPath('simpletemplate'); dumpHelper(array($tree->getChild($id)), 'dump the child of "simpletemplate"'); // you can also use: $tree->data[$id]['child']