echo "</pre>";
$tproject_mgr = new testproject($db);
new dBug($tproject_mgr);
$item = new stdClass();
$item->name = 'CRASH';
$item->notes = " Created doing test ";
$item->color = '';
$item->options = new stdClass();
//$item->options->requirement_mgmt = 1;
//$item->options->priority_mgmt = 1;
//$item->options->automated_execution = 1;
$item->active = 1;
$item->is_public = 1;
$item->prefix = 'TPX :: ';
try {
    $id = $tproject_mgr->create($item, array('doChecks' => true));
} catch (Exception $e) {
    echo 'Caught exception: ', $e->getMessage(), "\n";
}
die;
// new dBug($_SESSION);
$xx = $tproject_mgr->get_accessible_for_user(1, array('output' => 'map', 'field_set' => 'id', 'format' => 'simple'));
new dBug($xx);
die;
// create()
// function create($name,$color,$options,$notes,$active=1,$tcasePrefix='',$is_public=1)
$notes = " Created doing test ";
$color = '';
$options = new stdClass();
$options->requirement_mgmt = 1;
$options->priority_mgmt = 1;
function migrate_cc_specs(&$source_db, &$target_db, &$items, &$old_new)
{
    $mgtcom_keys = array('intro' => 'introduction', 'scope' => 'scope', 'ref' => 'references', 'method' => 'methodology', 'lim' => 'limitations');
    $mgtcat_keys = array('objective' => 'objective', 'config' => 'configuration', 'data' => 'data', 'tools' => 'tools');
    $tproject_mgr = new testproject($target_db);
    $ts_mgr = new testsuite($target_db);
    $tree_mgr = new tree($target_db);
    foreach ($items as $prod_id => $pd) {
        $old_new['product'][$prod_id] = @$tproject_mgr->create($pd['name'], $pd['color'], $pd['option_reqs'], EMPTY_NOTES, $pd['active']);
        echo "<pre><font color='blue'>Product {$pd['name']} has became a test project!</font></pre>";
        flush();
        $tproject_id = $old_new['product'][$prod_id];
        $sql = "SELECT * FROM mgtcomponent WHERE prodid={$prod_id}";
        $comp = $source_db->fetchRowsIntoMap($sql, 'id');
        // for change_order_bulk($hash_node_id, $hash_node_order)
        // $hash_node_id=array(10=>10, 23=>23, 30=>30);
        // $hash_node_order=array(10=>3, 23=>1, 30=>2);
        // $hash_node_id=array();
        // $hash_node_order=array();
        // 20071021 - francisco.mancardi@gruppotesi.com
        //
        // $hash_order_node_order=array(3=>10, 1=>23, 2=>30);
        // means:  node_id: 10 order:3
        //         node_id: 23 order:1
        //         node_id: 30 order:2
        $hash_order_node_id = array();
        if (count($comp) > 0) {
            foreach ($comp as $coid => $cod) {
                $details = '';
                foreach ($mgtcom_keys as $key => $val) {
                    $details .= $val . ": <br>" . $cod[$key] . "<p>";
                }
                $ret = $ts_mgr->create($tproject_id, $cod['name'], $details);
                if ($ret['status_ok']) {
                    echo "<pre>Component " . htmlentities($cod['name']) . " Migrated<br></pre>";
                    flush();
                    $mgtcomp_id = $ret['id'];
                    $old_new['mgtcomp'][$coid] = $mgtcomp_id;
                }
                // ----------------------------------------------------------------------------------
                $sql = "SELECT * FROM mgtcategory WHERE compid={$coid}";
                $cat = $source_db->fetchRowsIntoMap($sql, 'id');
                if (count($cat) > 0) {
                    foreach ($cat as $caid => $cad) {
                        // ----------------------------------------------------------------------------------
                        $details = '';
                        foreach ($mgtcat_keys as $key => $val) {
                            $details .= $val . ": <br>" . $cad[$key] . "<p>";
                        }
                        // ----------------------------------------------------------------------------------
                        $ret = $ts_mgr->create($mgtcomp_id, $cad['name'], $details);
                        if ($ret['status_ok']) {
                            echo "<pre>    Category " . htmlentities($cad['name']) . " Migrated<br></pre>";
                            flush();
                            $mgtcat_id = $ret['id'];
                            $old_new['mgtcat'][$caid] = $mgtcat_id;
                            if ($cad['CATorder'] != 0) {
                                // 20071021 - franciscom
                                // $hash_node_id[$mgtcat_id]=$mgtcat_id;
                                // $hash_node_order[$mgtcat_id]=$cad['CATorder'];
                                $node_id = $mgtcat_id;
                                $node_order = $cad['CATorder'];
                                $hash_order_node_id[$node_order] = $node_id;
                            }
                        }
                        // ----------------------------------------------------------------------------------
                    }
                }
            }
            // 20060725 - franciscom
            // $tree_mgr->change_order_bulk($hash_node_id, $hash_node_order) ;
            $tree_mgr->change_order_bulk($hash_order_node_id);
        }
    }
}
$target_testproject->options = new stdClass();
$target_testproject->options->requirement_mgmt = 1;
$target_testproject->options->priority_mgmt = 1;
$target_testproject->options->automated_execution = 1;
$target_testproject->notes = 'Created to run testplan unit tests on ';
$target_testproject->active = 1;
$target_testproject->tcasePrefix = 'TPlanUnitTest';
// Create a test project that will be Test plan parent
$tproject_mgr = new testproject($db);
$info = $tproject_mgr->get_by_name($target_testproject->name);
if (!is_null($info)) {
    $name = $info[0]['name'];
    echo "TestProject with name <b><i>{$name}</i></b> exists!<br>Will be deleted and re-created";
    $tproject_mgr->delete($info[0]['id']);
}
$testproject_id = $tproject_mgr->create($target_testproject->name, $target_testproject->color, $target_testproject->options, $target_testproject->notes, $target_testproject->active, $target_testproject->tcasePrefix);
$testplan = new stdClass();
$testplan->name = 'Test Plan Code Testing';
$testplan->notes = 'Test Plan created running Code Testing code by TestLink Development Team';
echo "<pre> {$object_class} - create(\$name,\$notes,\$testproject_id)";
echo "</pre>";
echo "<pre> {$object_class} - create('{$testplan->name}','{$testplan->notes}',{$testproject_id})";
echo "</pre>";
$testplan->id = $obj_mgr->create($testplan->name, $testplan->notes, $testproject_id);
$info = $obj_mgr->get_by_id($testplan->id);
new dBug($info);
// ---------------------------------------------------------------------------------------------------------
// Build Manager
// ---------------------------------------------------------------------------------------------------------
// Support Object
$tplan_mgr = new testplan($db);