function copy_to($id, $parent_id, $tproject_id, $user_id, $options = null)
 {
     $debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;
     $op = array('status_ok' => 1, 'msg' => 'ok', 'id' => -1, 'mappings' => null);
     $my['options'] = array('copy_also' => null);
     $my['options'] = array_merge($my['options'], (array) $options);
     $field_size = config_get('field_size');
     $item_info = $this->get_by_id($id);
     $target_doc = $this->generateDocID($id, $tproject_id);
     $new_item = $this->create($tproject_id, $parent_id, $target_doc, $item_info['title'], $item_info['scope'], $item_info['total_req'], $item_info['author_id'], $item_info['type'], $item_info['node_order']);
     $op = $new_item;
     if ($new_item['status_ok']) {
         $op['mappings'][$id] = $new_item['id'];
         $this->copy_cfields($id, $new_item['id']);
         // Now loop to copy all items inside it
         $my['filters'] = null;
         $subtree = $this->tree_mgr->get_subtree($id, $my['filters']);
         if (!is_null($subtree)) {
             $reqMgr = new requirement_mgr($this->db);
             $parent_decode = array();
             $parent_decode[$id] = $new_item['id'];
             foreach ($subtree as $the_key => $elem) {
                 // 20100311 - franciscom
                 $the_parent_id = isset($parent_decode[$elem['parent_id']]) ? $parent_decode[$elem['parent_id']] : null;
                 switch ($elem['node_type_id']) {
                     case $this->node_types_descr_id['requirement']:
                         $ret = $reqMgr->copy_to($elem['id'], $the_parent_id, $user_id, $tproject_id, $my['options']['copy_also']);
                         $op['status_ok'] = $ret['status_ok'];
                         $op['mappings'] += $ret['mappings'];
                         break;
                     case $this->node_types_descr_id['requirement_spec']:
                         $item_info = $this->get_by_id($elem['id']);
                         $target_doc = $this->generateDocID($elem['id'], $tproject_id);
                         $ret = $this->create($tproject_id, $the_parent_id, $target_doc, $item_info['title'], $item_info['scope'], $item_info['total_req'], $item_info['author_id'], $item_info['type'], $item_info['node_order']);
                         $parent_decode[$elem['id']] = $ret['id'];
                         $op['mappings'][$elem['id']] = $ret['id'];
                         if ($op['status_ok'] = $ret['status_ok']) {
                             $this->copy_cfields($elem['id'], $ret['id']);
                         }
                         break;
                 }
                 if ($op['status_ok'] == 0) {
                     break;
                 }
             }
         }
     }
     return $op;
 }
 function copy_to($id, $parent_id, $tproject_id, $user_id, $options = null)
 {
     static $get_tree_nt2exclude;
     if (!$get_tree_nt2exclude) {
         $get_tree_nt2exclude = array('req_version' => 'exclude_me', 'req_revision' => 'exclude_me', 'requirement_spec_revision' => 'exclude_me');
     }
     $debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;
     $op = array('status_ok' => 1, 'msg' => 'ok', 'id' => -1, 'mappings' => null);
     $my['options'] = array('copy_also' => null);
     $my['options'] = array_merge($my['options'], (array) $options);
     $item_info = $this->get_by_id($id);
     $target_doc = $this->generateDocID($id, $tproject_id);
     $new_item = $this->create($tproject_id, $parent_id, $target_doc, $item_info['title'], $item_info['scope'], $item_info['total_req'], $item_info['author_id'], $item_info['type'], $item_info['node_order']);
     $op = $new_item;
     if ($new_item['status_ok']) {
         $op['mappings'][$id] = $new_item['id'];
         $op['mappings']['req_spec'] = array();
         $op['mappings']['req'] = array();
         $op['mappings']['req_version'] = array();
         $idCard = array('parent_id' => $id, 'tproject_id' => $tproject_id);
         $this->copy_cfields($idCard, $new_item['id']);
         $this->copy_attachments($id, $new_item['id']);
         // Now loop to copy all items inside it
         // null is OK, because $id is a req spec, there is no risk
         // to copy/traverse wrong node types.
         // Hmmm may be req_revi ???
         $my['filters']['exclude_node_types'] = $get_tree_nt2exclude;
         $subtree = $this->tree_mgr->get_subtree($id, $my['filters'], array('output' => 'essential'));
         if (!is_null($subtree)) {
             $reqMgr = new requirement_mgr($this->db);
             $parent_decode = array();
             $parent_decode[$id] = $new_item['id'];
             // using reference has to avoid duplicate => memory consumption
             // (at least this is info found on Internet)
             // Few test indicates that it's true, but that using a counter
             // is still better.
             //
             $loop2do = count($subtree);
             for ($sdx = 0; $sdx <= $loop2do; $sdx++) {
                 $elem =& $subtree[$sdx];
                 $the_parent_id = isset($parent_decode[$elem['parent_id']]) ? $parent_decode[$elem['parent_id']] : null;
                 switch ($elem['node_type_id']) {
                     case $this->node_types_descr_id['requirement']:
                         $ret = $reqMgr->copy_to($elem['id'], $the_parent_id, $user_id, $tproject_id, $my['options']);
                         $op['status_ok'] = $ret['status_ok'];
                         $op['mappings']['req'] += $ret['mappings']['req'];
                         $op['mappings']['req_version'] += $ret['mappings']['req_version'];
                         break;
                     case $this->node_types_descr_id['requirement_spec']:
                         $item_info = $this->get_by_id($elem['id']);
                         // hmm, when copy_to() is called because we are duplicating
                         // a test project, call to generateDocID(), can be avoided.
                         // we have IMHO an absolute inexistent risk.
                         $target_doc = $this->generateDocID($elem['id'], $tproject_id);
                         $ret = $this->create($tproject_id, $the_parent_id, $target_doc, $item_info['title'], $item_info['scope'], $item_info['total_req'], $item_info['author_id'], $item_info['type'], $item_info['node_order']);
                         $parent_decode[$elem['id']] = $ret['id'];
                         $op['mappings']['req_spec'][$elem['id']] = $ret['id'];
                         if ($op['status_ok'] = $ret['status_ok']) {
                             // try to reduce memory usage
                             // $idCard = array('parent_id' => $elem['id'], 'tproject_id' => $tproject_id);
                             $this->copy_cfields(array('parent_id' => $elem['id'], 'tproject_id' => $tproject_id), $ret['id']);
                         }
                         break;
                 }
                 if ($op['status_ok'] == 0) {
                     break;
                 }
             }
         }
     }
     return $op;
 }