/** * Load les infos depuis un objet Service_JSON * * @access public * @param Service_JSON $service L'objet Service_JSON * @return array Les TafelTreeBranch créées */ function &loadServiceJSON($service) { $branch =& new TafelTreeBranch(); // On check toutes les propriétés de branche foreach ($service as $property => $value) { if ($property != 'items') { $branch->setParam($property, $value); } } // On check les enfants if (isset($service) && isset($service->items)) { $branch->items = array(); foreach ($service->items as $b) { $branch->items[] =& TafelTreeBranch::loadServiceJSON($b); } } return $branch; }
/** * Ajoute une sous-branche à l'arbre * * @access public * @param string $id L'id de la sous-branche * @param string $txt Le texte de la sous-branche * @param array $options Les informations complémentaires * @return TafelTreeBranch La sous-branche */ public function addBranch($id, $txt, $options = array()) { $branch = new TafelTreeBranch(); $branch->setId($id); $branch->setText($txt); foreach ($options as $property => $value) { if ($property != 'items') { $branch->setParam($property, $value); } } if (isset($options['items'])) { foreach ($options['items'] as $opt) { $branch->addBranch(null, null, $opt); } } if (!isset($this->items)) { $this->items = array(); } $this->items[] = $branch; return $branch; }
/** * Load les infos depuis un objet Service_JSON * * @access public * @param Service_JSON $service L'objet Service_JSON * @return array Les TafelTreeBranch créées */ function &loadServiceJSON($service) { $branches = array(); foreach ($service as $branch) { $branches[] =& TafelTreeBranch::loadServiceJSON($branch); } return $branches; }
<?php include_once 'php4/TafelTree.class.php'; /// // CASE 1 : load structure from a JSON string // /// // Check the JSON string $json = "[{id:'r1',txt:'root',items:[{id:'b1',txt:'branch 1',myPos:{x:1,y:9}},{id:'b2',txt:'branch 2'}]}]"; // Load the JSON into an object (or array of objects) $branches = TafelTreeBranch::loadJSON($json); // Manipulate the object (or array of objects) $first = $branches[0]; echo '<h3>CASE 1 : load structure from a JSON string</h3>'; echo 'First JSON branch : ' . $first->getText() . ' (' . $first->getId() . ')'; echo '<ul>'; foreach ($first->items as $next) { echo '<li>Next JSON branch : ' . $next->getText() . ' (' . $next->getId() . ')</li>'; } echo '</ul>'; /// // CASE 2 : create a loader TafelTree from an object // /// // Create the tree (same options as javascript). In this sample, the file // drop.php doesn't exist. It's just to show how to manage ajax declarations $tree = new TafelTree('divTree', '../imgs/', null, null, array('generate' => true, 'onMouseOver' => 'myMouseover', 'onMouseOut' => 'myMouseout', 'defaultImg' => 'page.gif', 'lineStyle' => 'full', 'onDropAjax' => array('funcDrop', 'drop.php'))); // Add a root branch $b1 = $tree->addBranch('r1', 'root'); // Add two branches into the root $b1->addBranch('b1', 'branch 1', array('onclick' => 'testclick', 'thing' => 1));