/
create_item.php
191 lines (172 loc) · 8 KB
/
create_item.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/********************************************************************************
* This file is part of eLabChem (http://github.com/martinp23/elabchem) *
* Copyright (c) 2013 Martin Peeks (martinp23@googlemail.com) *
* *
* eLabChem is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version. *
* *
* eLabChem is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR *
* PURPOSE. See the GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public *
* License along with eLabChem. If not, see <http://www.gnu.org/licenses/>. *
* *
* eLabChem is a fork of elabFTW. This file incorporates work covered by the *
* copyright notice below. *
* *
********************************************************************************/
/********************************************************************************
* *
* Copyright 2012 Nicolas CARPi (nicolas.carpi@gmail.com) *
* http://www.elabftw.net/ *
* *
********************************************************************************/
/********************************************************************************
* This file is part of eLabFTW. *
* *
* eLabFTW is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version. *
* *
* eLabFTW is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR *
* PURPOSE. See the GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public *
* License along with eLabFTW. If not, see <http://www.gnu.org/licenses/>. *
* *
********************************************************************************/
require_once('inc/common.php');
$msg_arr = array();
// What do we create ?
if (isset($_GET['type']) && !empty($_GET['type']) && is_pos_int($_GET['type'])) {
// $type is int here
$type = $_GET['type'];
} elseif (isset($_GET['type']) && !empty($_GET['type']) && ($_GET['type'] === 'exp')){
$type = 'experiments';
if (isset($_GET['exp_type']) && !empty($_GET['exp_type'])) {
switch ($_GET['exp_type']) {
case 'chemsingle':
$exp_type = 'chemsingle';
break;
case 'chemparallel':
$exp_type = 'chemparallel';
break;
default:
$exp_type = 'generic';
}
} else {
$exp_type = 'generic';
}
} 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 body FROM experiments_templates WHERE id = :id";
$get_tpl = $bdd->prepare($sql);
$get_tpl->execute(array(
'id' => $_GET['tpl']
));
$get_tpl_body = $get_tpl->fetch();
$body = $get_tpl_body['body'];
} else {
$body = '';
}
// SQL for create experiments
$sql = "INSERT INTO experiments(date, status, elabid, userid_creator, visibility, type) VALUES(NOW(), :status, :elabid, :userid, :visibility, :type)";
$req = $bdd->prepare($sql);
$result = $req->execute(array(
'status' => 'running',
'elabid' => $elabid,
'visibility' => 'team',
'userid' => $_SESSION['userid'],
'type' => $exp_type
));
// Get what is the experiment id we just created
$sql = "SELECT LAST_INSERT_ID();";
$req = $bdd->prepare($sql);
$req->execute();
$data = $req->fetch();
$newid = $data['LAST_INSERT_ID()'];
// if($exp_type === 'chemsimple' || $exp_type === 'chemparallel') {
// $sql = "INSERT INTO reactions(user_id, structure) VALUES(:userid, ' ')";
// $req = $bdd->prepare($sql);
// $req->execute();
// $data = $req->fetch();
// $rxnid = $data['LAST_INSERT_ID()'];
// }
// now insert 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' => 'Untitled',
'expid' => $newid,
'notes' => "New experiment.",
'body' => $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();
} else { // create item for DB
// SQL to get template
$sql = "SELECT template FROM items_types WHERE id = :id";
$get_tpl = $bdd->prepare($sql);
$get_tpl->execute(array(
'id' => $type
));
$get_tpl_body = $get_tpl->fetch();
// SQL for create DB item
$sql = "INSERT INTO items(title, date, body, userid, type) VALUES(:title, NOW(), :body, :userid, :type)";
$req = $bdd->prepare($sql);
$result = $req->execute(array(
'title' => 'Untitled',
'body' => $get_tpl_body['template'],
'userid' => $_SESSION['userid'],
'type' => $type
));
// Get what is the item id we just created
$sql = "SELECT LAST_INSERT_ID();";
$req = $bdd->prepare($sql);
$req->execute();
$data = $req->fetch();
$newid = $data['LAST_INSERT_ID()'];
}
// // Get what is the item id we just created
// if ($type === 'experiments') {
// $sql = "SELECT id FROM experiments WHERE userid_creator = :userid ORDER BY id DESC LIMIT 0,1";
// } else {
// $sql = "SELECT id FROM items WHERE userid = :userid ORDER BY id DESC LIMIT 0,1";
// }
// $req = $bdd->prepare($sql);
// $req->bindParam(':userid', $_SESSION['userid']);
// $req->execute();
// $data = $req->fetch();
// $newid = $data['id'];
// Check if insertion is successful and redirect to the newly created experiment in edit mode
if($result) {
// info box
$msg_arr[] = 'New item successfully created.';
$_SESSION['infos'] = $msg_arr;
if ($type === 'experiments') {
header('location: experiments.php?mode=edit&id='.$newid.'');
} else {
header('location: database.php?mode=edit&id='.$newid.'');
}
} else {
die();
}