public function build() { if (parent::build()) { // the table wasn't existing and has been built $result = $this->db->query("\n ALTER TABLE {$this->forest_name}\n ADD COLUMN name VARCHAR(50) NOT NULL\n "); if (!$result) { throw new sp_MySQL_Error($this->db); } } }
public function testImportExport() { /* ### test empty tree ### */ $this->assertEquals(array(), json_decode(Baobab::export(self::$db, self::$forest_name), TRUE)); Baobab::import(self::$db, self::$forest_name, '[]'); $this->assertEquals(NULL, $this->baobab->getTree()); $empty_json_tree = '[{"fields":["id","lft","rgt","label"],"values":null}]'; Baobab::import(self::$db, self::$forest_name, $empty_json_tree); $this->assertEquals(NULL, $this->baobab->getTree()); /* ### test import/export not empty tree with non numeric values ### */ $json_tree = '[{ "fields":["id","lft","label","rgt"], "values": [5,1,"AAA",14,[ [3,2,"BBB",7,[ [4,3,"CCC",4,[]], [6,5,"DDD",6,[]] ]], [1,8,"EEE",13,[ [2,9,"FFF",10,[]], [7,11,"GGG",12,[]] ]] ]] }]'; Baobab::import(self::$db, self::$forest_name, $json_tree); $this->assertEquals(array(array("tree_id" => 1, "fields" => array("id", "label", "rgt"), "values" => array(5, "AAA", 14, array(array(3, "BBB", 7, array(array(4, "CCC", 4, array()), array(6, "DDD", 6, array()))), array(1, "EEE", 13, array(array(2, "FFF", 10, array()), array(7, "GGG", 12, array()))))))), json_decode(Baobab::export(self::$db, self::$forest_name, array("id", "label", "rgt")), TRUE)); }
function _fillAnyIdTree($tree_id) { $t = new Baobab(self::$db, self::$forest_name, $tree_id); $t->clean(); Baobab::import(self::$db, self::$forest_name, '[{' . ($tree_id ? '"tree_id":' . $tree_id . ',' : '') . ' "fields":["id","lft","rgt"], "values": [100001,1,14,[ [100002,2,7,[ [100003,3,4,[]], [100004,5,6,[]] ]], [100005,8,13,[ [100006,9,10,[]], [100007,11,12,[]] ]] ]] }]'); }
public static function export($db, $forest_name, $fields = NULL, $tree_id = NULL) { // check if the table exists before doing anything else $sql_utils = new sp_SQLUtils($db); if (!$sql_utils->table_exists($forest_name)) { throw new sp_Error("Table `Baobab_{$forest_name}` does not exist"); } // get all the fields to export or check if the passed fields are valid $tree = new Baobab($db, $forest_name, NULL); // use a unexistent tree in the right table if ($fields !== NULL) { $tree->_sql_check_fields($fields); } else { $fields = array_keys($tree->_get_fields()); } // remove tree_id from $fields to avoid writing it n times // ( we give him a single property in the json format ) $idx_treeId_field = array_search("tree_id", $fields); if (FALSE !== $idx_treeId_field) { unset($fields[$idx_treeId_field]); $fields = array_values($fields); // I want to mantain a correct index sequence } // get the ids of the trees to export $ar_tree_id = array(); if ($tree_id) { if (!is_array($tree_id)) { $tree_id = array($tree_id); } // ensure $tree_id contains numbers foreach ($tree_id as $tmp_id) { $ar_tree_id[] = intval($tmp_id); } } else { $query = "SELECT DISTINCT tree_id FROM {$forest_name}"; $result = $db->query($query, MYSQLI_STORE_RESULT); if (!$result) { throw new sp_MySQL_Error($db); } while ($row = $result->fetch_row()) { $ar_tree_id[] = intval($row[0]); } $result->close(); } // get the type of the columns mainly to write numbers as ... numbers $fieldsFlags = array(); // each index will have the field flag, to know his type $result = $db->query("SELECT " . join(",", $fields) . " FROM {$forest_name} LIMIT 1", MYSQLI_STORE_RESULT); if (!$result) { throw new sp_MySQL_Error($db); } // retrieve the column names and their types while ($finfo = $result->fetch_field()) { $fieldsFlags[] = $finfo->flags; } $result->close(); // parse each tree and build an array to jsonize later $ar_out = array(); foreach ($ar_tree_id as $tree_id) { $tmp_ar = array("tree_id" => $tree_id, "fields" => $fields, "values" => null); // retrieve the data $tree = new Baobab($db, $forest_name, $tree_id); $root = $tree->getTree(); if ($root !== NULL) { $data = array(array()); // the inner array emulate a node to gain root as child self::_traverse_tree_to_export_data($root, $data, $fieldsFlags, $tmp_ar["fields"]); if (!empty($data[0][0])) { $tmp_ar["values"] =& $data[0][0]; } $ar_out[] = $tmp_ar; } } return json_encode($ar_out); }