/** * Duplicate an item. * * @param int $id The id of the item to duplicate * @param string $type Can be 'experiments' or 'item' * @return int Will return the ID of the new item or 0 if error */ function duplicate_item($id, $type) { global $pdo; $result = false; $result_tags = false; if ($type === 'experiments') { $elabid = generate_elabid(); // what will be the status ? // go pick what is the default status upon creating experiment // there should be only one because upon making a status default, // all the others are made not default $sql = 'SELECT id FROM status WHERE is_default = true AND team = :team LIMIT 1'; $req = $pdo->prepare($sql); $req->bindParam(':team', $_SESSION['team_id']); $req->execute(); $status = $req->fetchColumn(); // if there is no is_default status // we take the first status that come if (!$status) { $sql = 'SELECT id FROM status WHERE team = :team LIMIT 1'; $req = $pdo->prepare($sql); $req->bindParam(':team', $_SESSION['team_id']); $req->execute(); $status = $req->fetchColumn(); } // SQL to get data from the experiment we duplicate $sql = "SELECT title, body, visibility FROM experiments WHERE id = " . $id; $req = $pdo->prepare($sql); $req->execute(); $data = $req->fetch(); // let's add something at the end of the title to show it's a duplicate // capital i looks good enough $title = $data['title'] . ' I'; // SQL for duplicateXP $sql = "INSERT INTO experiments(team, title, date, body, status, elabid, visibility, userid) VALUES(:team, :title, :date, :body, :status, :elabid, :visibility, :userid)"; $req = $pdo->prepare($sql); $result = $req->execute(array('team' => $_SESSION['team_id'], 'title' => $title, 'date' => kdate(), 'body' => $data['body'], 'status' => $status, 'elabid' => $elabid, 'visibility' => $data['visibility'], 'userid' => $_SESSION['userid'])); // END SQL main } if ($type === 'items') { // SQL to get data from the item we duplicate $sql = "SELECT * FROM items WHERE id = " . $id; $req = $pdo->prepare($sql); $req->execute(); $data = $req->fetch(); // SQL for duplicateDB $sql = "INSERT INTO items(team, title, date, body, userid, type) VALUES(:team, :title, :date, :body, :userid, :type)"; $req = $pdo->prepare($sql); $result = $req->execute(array('team' => $data['team'], 'title' => $data['title'], 'date' => kdate(), 'body' => $data['body'], 'userid' => $_SESSION['userid'], 'type' => $data['type'])); // END SQL main } // Get what is the id we just created $newid = $pdo->lastInsertId(); if ($type === 'experiments') { // TAGS $sql = "SELECT tag FROM experiments_tags WHERE item_id = :id"; $req = $pdo->prepare($sql); $req->execute(array('id' => $id)); $tag_number = $req->rowCount(); if ($tag_number > 0) { while ($tags = $req->fetch()) { // Put them in the new one. here $newid is the new exp created $sql = "INSERT INTO experiments_tags(tag, item_id, userid) VALUES(:tag, :item_id, :userid)"; $reqtag = $pdo->prepare($sql); $result_tags = $reqtag->execute(array('tag' => $tags['tag'], 'item_id' => $newid, 'userid' => $_SESSION['userid'])); } } else { //no tag $result_tags = true; } // LINKS $linksql = "SELECT link_id FROM experiments_links WHERE item_id = :id"; $linkreq = $pdo->prepare($linksql); $result_links = $linkreq->execute(array('id' => $id)); while ($links = $linkreq->fetch()) { $sql = "INSERT INTO experiments_links (link_id, item_id) VALUES(:link_id, :item_id)"; $req = $pdo->prepare($sql); $result_links = $req->execute(array('link_id' => $links['link_id'], 'item_id' => $newid)); } if ($result && $result_tags && $result_links) { return $newid; } return 0; } else { // DB // TAGS $sql = "SELECT tag FROM items_tags WHERE item_id = " . $id; $req = $pdo->prepare($sql); $req->execute(); $tag_number = $req->rowCount(); // we initilize $result_tags here in case there is now tag to duplicate $result_tags = true; if ($tag_number > 0) { while ($tags = $req->fetch()) { // Put them in the new one. here $newid is the new exp created $sql = "INSERT INTO items_tags(tag, item_id) VALUES(:tag, :item_id)"; $reqtag = $pdo->prepare($sql); $result_tags = $reqtag->execute(array('tag' => $tags['tag'], 'item_id' => $newid)); } } if ($result && $result_tags) { return $newid; } return false; } }
require_once ELAB_ROOT . 'inc/locale.php'; $msg_arr = array(); // What do we create ? if (isset($_GET['type']) && !empty($_GET['type']) && is_pos_int($_GET['type'])) { // $type is int for DB items $type = $_GET['type']; } elseif (isset($_GET['type']) && !empty($_GET['type']) && $_GET['type'] === 'exp') { $type = 'experiments'; } else { $msg_arr[] = _('Wrong item type!'); $_SESSION['infos'] = $msg_arr; header('location: ../index.php'); exit; } if ($type === 'experiments') { $elabid = generate_elabid(); // do we want template ? if (isset($_GET['tpl']) && is_pos_int($_GET['tpl'])) { // SQL to get template $sql = "SELECT name, body FROM experiments_templates WHERE id = :id"; $get_tpl = $pdo->prepare($sql); $get_tpl->execute(array('id' => $_GET['tpl'])); $get_tpl_info = $get_tpl->fetch(); // the title is the name of the template $title = $get_tpl_info['name']; $body = $get_tpl_info['body']; } else { // if there is no template, title is 'Untitled' and the body is the default exp_tpl $title = _('Untitled'); // SQL to get body $sql = "SELECT body FROM experiments_templates WHERE userid = 0 AND team = :team";
function duplicate_item($id, $type) { global $bdd; if ($type === 'experiments') { $elabid = generate_elabid(); // SQL to get latest revision from the experiment we duplicate $sql = "SELECT rev.rev_id, rev.rev_title, rev.rev_body, exp.visibility FROM revisions as rev JOIN experiments as exp ON rev.experiment_id = exp.id WHERE exp.id = :id"; $req = $bdd->prepare($sql); $req->execute(array('id' => $id)); $data = $req->fetch(); //now get content of latest revision and // SQL for duplicateXP $sql = "INSERT INTO experiments(date, status, elabid, visibility, userid_creator) VALUES(:date, :status, :elabid, :visibility, :userid)"; $req = $bdd->prepare($sql); $result = $req->execute(array('date' => kdate(), 'status' => 'running', 'elabid' => $elabid, 'visibility' => $data['visibility'], 'userid' => $_SESSION['userid'])); // END SQL main // Get what is the experiment id we just created // Get what is the experiment id we just created $sql = "SELECT LAST_INSERT_ID();"; $req = $bdd->prepare($sql); $req->execute(); $data1 = $req->fetch(); $newid = $data1['LAST_INSERT_ID()']; // now copy the text for the new page into the revisions table $sql = "INSERT INTO revisions(user_id, experiment_id, rev_notes, rev_body, rev_title) VALUES(:userid, :expid, :notes, :body, :title)"; $req = $bdd->prepare($sql); $result = $req->execute(array('title' => $data['rev_title'], 'expid' => $newid, 'notes' => "Duplication of experiment {$id}.", 'body' => $data['rev_body'], 'userid' => $_SESSION['userid'])); // now populate rev-id for expt $sql = "UPDATE experiments SET rev_id=LAST_INSERT_ID() WHERE id = " . $newid; $req = $bdd->prepare($sql); $result = $req->execute(); } if ($type === 'items') { // SQL to get data from the item we duplicate $sql = "SELECT * FROM items WHERE id = :id"; $req = $bdd->prepare($sql); $req->execute(array('id' => $id)); $data = $req->fetch(); // SQL for duplicateDB $sql = "INSERT INTO items(title, date, body, userid, type) VALUES(:title, :date, :body, :userid, :type)"; $req = $bdd->prepare($sql); $result = $req->execute(array('title' => $data['title'], 'date' => kdate(), 'body' => $data['body'], 'userid' => $_SESSION['userid'], 'type' => $data['type'])); // END SQL main // Get what is the item id we just created $sql = "SELECT LAST_INSERT_ID();"; $req = $bdd->prepare($sql); $req->execute(); $data1 = $req->fetch(); $newid = $data1['LAST_INSERT_ID()']; } if ($type === 'experiments') { // TAGS $sql = "SELECT tag FROM experiments_tags WHERE item_id = :id"; $req = $bdd->prepare($sql); $req->execute(array('id' => $id)); $tag_number = $req->rowCount(); if ($tag_number > 1) { while ($tags = $req->fetch()) { // Put them in the new one. here $newid is the new exp created $sql = "INSERT INTO experiments_tags(tag, item_id, userid) VALUES(:tag, :item_id, :userid)"; $reqtag = $bdd->prepare($sql); $result_tags = $reqtag->execute(array('tag' => $tags['tag'], 'item_id' => $newid, 'userid' => $_SESSION['userid'])); } } else { //no tag $result_tags = true; } // LINKS $linksql = "SELECT link_id FROM experiments_links WHERE item_id = :id"; $linkreq = $bdd->prepare($linksql); $result_links = $linkreq->execute(array('id' => $id)); while ($links = $linkreq->fetch()) { $sql = "INSERT INTO experiments_links (link_id, item_id) VALUES(:link_id, :item_id)"; $req = $bdd->prepare($sql); $result_links = $req->execute(array('link_id' => $links['link_id'], 'item_id' => $newid)); } if ($result && $result_tags && $result_links) { return $newid; } return false; } else { // DB // TAGS $sql = "SELECT tag FROM items_tags WHERE item_id = :id"; $req = $bdd->prepare($sql); $req->execute(array('id' => $id)); while ($tags = $req->fetch()) { // Put them in the new one. here $newid is the new exp created $sql = "INSERT INTO items_tags(tag, item_id) VALUES(:tag, :item_id)"; $reqtag = $bdd->prepare($sql); $result_tags = $reqtag->execute(array('tag' => $tags['tag'], 'item_id' => $newid)); } if ($result && $result_tags) { return $newid; } return false; } }