/
editChemReg-exec.php
413 lines (362 loc) · 19.8 KB
/
editChemReg-exec.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?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-2013 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');
//Array to store validation errors
$msg_arr = array();
//Validation error flag
$errflag = false;
// CHECKS
// ID
if(is_pos_int($_POST['regid']) || $_POST['regid'] == 0){
$regid = $_POST['regid'];
} else {
$regid='';
$msg_arr[] = 'The id parameter is not valid !';
$errflag = true;
}
if(is_pos_int($_POST['cpdid'])){
$cpdid = $_POST['cpdid'];
} else {
$cpdid='';
}
$name = check_title($_POST['name']);
$iupacname = check_title($_POST['iupac_name']);
if(isset($_POST['validated'])) {
if($_POST['validated'] === 'true') {
$validated = 1;
} else {
$validated = 0;
}
} else {
$validated = 0;
}
if($_POST['regno'] === 'unassigned') {
$registered = false;
} else {
$registered = true;
}
$parentregid = null;
$isSalt = 0;
if(isset($_POST['saltCheck'])) {
if ($_POST['saltCheck'] === 'true') {
$isSalt = 1;
if(isset($_POST['cpdParentRegNum'])) {
$sql = "SELECT id FROM compound_registry WHERE regno = :regno";
$req = $bdd->prepare($sql);
$result = $req->execute(array('regno' => check_title($_POST['cpdParentRegNum'])));
$ids = $req->fetch();
if (count($ids) === 2) {
$parentregid = $ids[0];
} else {
$msg_arr[] = 'The parent registration ID for the salt is not valid!';
$errflag = true;
}
} else {
$msg_arr[] = 'The parent registration ID for the salt is not valid!';
$errflag = true;
}
}
}
$mol = check_rxn($_POST['mol']);
$cas_number = (isset($_POST['cas_number']) && $_POST['cas_number'] !== '') ? $_POST['cas_number'] : null;
$pubchem_id = (isset($_POST['pubchem_id']) && $_POST['pubchem_id'] !== '') ? $_POST['pubchem_id'] : null;
$chemspider_id = (isset($_POST['chemspider_id']) && $_POST['chemspider_id'] !== '') ? $_POST['chemspider_id'] : null;
$mwt = (isset($_POST['mwt'])) ? ((floatval($_POST['mwt'])) ? floatval($_POST['mwt']) : null) : null;
$exact_mass = (isset($_POST['exact_mass'])) ? ((floatval($_POST['exact_mass'])) ? floatval($_POST['exact_mass']) : null) : null;
$density = (isset($_POST['density'])) ? ((floatval($_POST['density'])) ? floatval($_POST['density']) : null) : null;
$notes = check_body($_POST['notes']);
$formula = (isset($_POST['formula'])) ? $_POST['formula'] : null;
// If input errors, redirect back to the experiment form
if($errflag) {
$_SESSION['errors'] = $msg_arr;
session_write_close();
header("location: compounds.php?mode=view®id=$regid");
exit();
}
// // if nothing is changed, we can just return.
// if(!$rxnChanged && !$gridChanged && !$prodGridChanged && !$bodyChanged && !$titleChanged && !$gridColumnsChanged && !$prodGridColumnsChanged) {
// unset($_SESSION['new_title']);
// unset($_SESSION['new_date']);
// unset($_SESSION['status']);
// unset($_SESSION['errors']);
// header("location: experiments.php?mode=view&id=$id");
// }
// SQL for editChemReg
$bdd->beginTransaction();
if($mol !== '' && $cpdid !== '') {
// if we have a structure and a compound ID, we can fire away and add all of our data in one shot
// for data integrity, we will not change mwt, exact_mass or formula.
if($regid == 0) {
$sql = "INSERT INTO compound_registry (cpd_id, userid_entrant, validated, no_structure, is_salt) VALUES (:cpd_id, :userid_entrant, :validated, :no_structure, :is_salt)";
$req = $bdd->prepare($sql);
$result = $req->execute(array(
'cpd_id' => $cpdid,
'userid_entrant' => $_SESSION['userid'],
'validated' => 0,
'no_structure' => 0,
'is_salt' => 0));
$regid = $bdd->lastInsertId();
} else {
$sql = "UPDATE compound_registry SET cpd_id = :cpdid WHERE id = :regid";
$req = $bdd->prepare($sql);
$result = $req->execute(array(
'cpdid' => $cpdid,
'regid' => $regid));
}
$sql = "UPDATE compounds AS c JOIN compound_properties AS cp
ON c.id = cp.compound_id JOIN compound_registry AS creg ON c.id = creg.cpd_id
SET c.name = :name, c.iupac_name = :iupac_name, c.cas_number = :cas_number, c.pubchem_id = :pubchem_id,
c.chemspider_id = :chemspider_id, cp.density = :density, creg.is_salt = :saltCheck,
creg.parent_regid = :parentregid, c.notes = :notes, creg.no_structure = 0
WHERE creg.id = :regid";
$req = $bdd->prepare($sql, array(PDO::ATTR_EMULATE_PREPARES => false));
$result = $req->execute(array(
'name' => $name,
'iupac_name' => $iupacname,
'cas_number' => $cas_number,
'pubchem_id' => $pubchem_id,
'chemspider_id' => $chemspider_id,
'density' => $density,
'saltCheck' => $isSalt,
'parentregid' => $parentregid,
'notes' => $notes,
'regid' => $regid));
} else if($mol !== '') {
// if we have a structure but no compound ID, let's first register the structure as a compound and calculate properties
// and then insert data
$sql = "INSERT INTO compounds (name, iupac_name, cas_number, pubchem_id, chemspider_id, user_id_entrant, created, notes)
VALUES (:name, :iupac_name, :cas_number, :pubchem_id, :chemspider_id, :user_id, NOW(), :notes)";
$req = $bdd->prepare($sql, array(PDO::ATTR_EMULATE_PREPARES => false));
$result = $req->execute(array(
'name' => $name,
'iupac_name' => $iupacname,
'cas_number' => $cas_number,
'pubchem_id' => $pubchem_id,
'chemspider_id' => $chemspider_id,
'user_id' => $_SESSION['userid'],
'notes' => $notes));
$cpdid = $bdd->lastInsertId();
$sql = "INSERT INTO 3D_structures (compound_id, molfile) VALUES ($cpdid, :mol)";
$req = $bdd->prepare($sql);
$result = $req->execute(array('mol' => $mol));
$sql = "INSERT INTO `1D_structures` (`compound_id`,`inchi`,`smiles`) SELECT `compound_id`,
MOLECULE_TO_INCHI(`molfile`), MOLECULE_TO_SMILES(`molfile`) FROM `3D_structures` WHERE `3D_structures`.compound_id = $cpdid";
$req = $bdd->prepare($sql);
$result = $req->execute();
$sql = "INSERT INTO `bin_structures` (`compound_id`,`fp2`,`obserialized`) SELECT `compound_id`, FINGERPRINT2(`molfile`),
MOLECULE_TO_SERIALIZEDOBMOL(`molfile`) FROM `3D_structures` WHERE `3D_structures`.compound_id = $cpdid";
$req = $bdd->prepare($sql);
$result = $req->execute();
$sql = "INSERT INTO compound_properties (compound_id, mwt, exact_mass, clogp, formula, num_donor, num_acceptor, num_heavyat,
num_rings, num_rot_bond, tot_charge, num_atom, num_bond, mol_psa, is_chiral, density)
SELECT compound_id, MOLWEIGHT(molfile), EXACTMASS(molfile), MOLLOGP(ADD_HYDROGENS(molfile)), molformula(molfile),
NUMBER_OF_DONORS(ADD_HYDROGENS(molfile)), NUMBER_OF_ACCEPTORS(ADD_HYDROGENS(molfile)), NUMBER_OF_HEAVY_ATOMS(molfile),
NUMBER_OF_RINGS(molfile), NUMBER_OF_ROTABLE_BONDS(molfile), TOTAL_CHARGE(molfile), NUMBER_OF_ATOMS(ADD_HYDROGENS(molfile)),
NUMBER_OF_BONDS(ADD_HYDROGENS(molfile)), MOLPSA(ADD_HYDROGENS(molfile)), IS_CHIRAL(molfile), :density
FROM 3D_structures WHERE `3D_structures`.compound_id = $cpdid";
$req = $bdd->prepare($sql, array(PDO::ATTR_EMULATE_PREPARES => false));
$result = $req->execute(array('density' => $density));
if($regid == 0) {
$sql = "INSERT INTO compound_registry (cpd_id, userid_entrant, validated, no_structure, is_salt, parent_regid) VALUES (:cpd_id, :userid_entrant, :validated, :no_structure, :is_salt, :parent_regid)";
$req = $bdd->prepare($sql);
$result = $req->execute(array(
'cpd_id' => $cpdid,
'userid_entrant' => $_SESSION['userid'],
'validated' => 0,
'no_structure' => 0,
'is_salt' => $isSalt,
'parent_regid' => $parentregid));
$regid = $bdd->lastInsertId();
} else {
$sql = "UPDATE compound_registry SET cpd_id = $cpdid, is_salt = :issalt, parent_regid = :parent_regid, no_structure = 0 WHERE id = :regid";
$req = $bdd->prepare($sql, array(PDO::ATTR_EMULATE_PREPARES => false));
$result = $req->execute(array(
'issalt' => $isSalt,
'parent_regid' => $parentregid,
'regid' => $regid));
}
} else if($cpdid !== '') {
// if we have no structure but we have a compound ID, we can update all compound properties (incl. mwt, exact_mass, formula)
if($regid == 0) {
$sql = "INSERT INTO compound_registry (cpd_id, userid_entrant, validated, no_structure, is_salt) VALUES (:cpd_id, :userid_entrant, :validated, :no_structure, :is_salt)";
$req = $bdd->prepare($sql);
$result = $req->execute(array(
'cpd_id' => $cpdid,
'userid_entrant' => $_SESSION['userid'],
'validated' => 0,
'no_structure' => 1,
'is_salt' => 0));
$regid = $bdd->lastInsertId();
} else {
$sql = "UPDATE compound_registry SET cpd_id = :cpdid WHERE id = :regid";
$req = $bdd->prepare($sql);
$result = $req->execute(array(
'cpdid' => $cpdid,
'regid' => $regid));
}
$sql = "UPDATE compounds AS c JOIN compound_properties AS cp
ON c.id = cp.compound_id JOIN compound_registry AS creg ON c.id = creg.cpd_id
SET c.name = :name, c.iupac_name = :iupac_name, c.cas_number = :cas_number, c.pubchem_id = :pubchem_id,
c.chemspider_id = :chemspider_id, cp.mwt = :mwt, cp.exact_mass = :exact_mass, cp.formula = :formula,
cp.density = :density, creg.no_structure = 1, creg.is_salt = :saltCheck,
creg.parent_regid = :parentregid, c.notes = :notes
WHERE creg.id = :regid";
$req = $bdd->prepare($sql, array(PDO::ATTR_EMULATE_PREPARES => false));
$result = $req->execute(array(
'name' => $name,
'iupac_name' => $iupacname,
'cas_number' => $cas_number,
'pubchem_id' => $pubchem_id,
'chemspider_id' => $chemspider_id,
'mwt' => $mwt,
'exact_mass' => $exact_mass,
'formula' => $formula,
'density' => $density,
'saltCheck' => $isSalt,
'parentregid' => $parentregid,
'notes' => $notes,
'regid' => $regid));
} else {
// if we have no structure and no compound ID, we need to first make a compound with the properties stated by the user,
// then update the entry in the registry to refer to the new cpd_id.
$sql = "INSERT INTO compounds (name, iupac_name, cas_number, pubchem_id, chemspider_id, user_id_entrant, created, notes)
VALUES (:name, :iupac_name, :cas_number, :pubchem_id, :chemspider_id, :user_id_entrant, NOW(), :notes)";
$req = $bdd->prepare($sql, array(PDO::ATTR_EMULATE_PREPARES => false));
$result = $req->execute(array(
'name' => $name,
'iupac_name' => $iupacname,
'cas_number' => $cas_number,
'pubchem_id' => $pubchem_id,
'chemspider_id' => $chemspider_id,
'user_id' => $_SESSION['userid'],
'notes' => $notes));
$cpdid = $bdd->lastInsertId();
$sql = "INSERT INTO compound_properties (compound_id, mwt, exact_mass, formula, density)
VALUES($cpdid, :mwt, :exact_mass, :formula, :density)";
$req = $bdd->prepare($sql, array(PDO::ATTR_EMULATE_PREPARES => false));
$result = $req->execute(array(
'mwt' => $mwt,
'exact_mass' => $exact_mass,
'formula' => $formula,
'density' => $density));
if($regid == 0) {
$sql = "INSERT INTO compound_registry (cpd_id, userid_entrant, validated, no_structure, is_salt, parent_regid) VALUES (:cpd_id, :userid_entrant, :validated, :no_structure, :is_salt, :parent_regid)";
$req = $bdd->prepare($sql);
$result = $req->execute(array(
'cpd_id' => $cpdid,
'userid_entrant' => $_SESSION['userid'],
'validated' => 0,
'no_structure' => 1,
'is_salt' => $isSalt,
'parent_regid' => $parentregid));
$regid = $bdd->lastInsertId();
} else {
$sql = "UPDATE compound_registry SET cpd_id = $cpdid, is_salt = :issalt, parent_regid = :parent_regid, no_structure=1 WHERE id = :regid";
$req = $bdd->prepare($sql, array(PDO::ATTR_EMULATE_PREPARES => false));
$result = $req->execute(array(
'issalt' => $isSalt,
'parent_regid' => $parentregid,
'regid' => $regid));
}
}
if($isSalt) {
// let's look up the parent regno, see how many salts we already have (count(regno) - 1) then select a
// $saltSuffice accordingly
$sql = "SELECT count(regno) FROM compound_registry WHERE regno LIKE :regno";
$req = $bdd->prepare($sql);
$result = $req->execute(array('regno' => $_POST['cpdParentRegNum'] . '%'));
$count = $req->fetch();
$saltSuffix = 'A';
for($i = 0; $i < $count[0]-1; $i++) {
$saltSuffix = ++$saltSuffix;
}
}
if(!$registered && $validated) {
if(!$isSalt) {
$sql = "SELECT regno FROM compound_registry ORDER BY regno DESC LIMIT 1";
$req = $bdd->prepare($sql);
$result = $req->execute();
$latestRegNo = $req->fetch();
$prefix = CPD_PREFIX;
if(count($latestRegNo) >= 1) {
if(ctype_alpha(substr($latestRegNo[0], strlen($latestRegNo[0])-1))) {
$number = substr($latestRegNo[0], strlen($prefix), strlen($latestRegNo[0]) - strlen($prefix) - 1);
} else {
$number = substr($latestRegNo[0], strlen($prefix), strlen($latestRegNo[0]) - strlen($prefix));
}
} else {
$number = 0;
}
$newRegNo = $prefix . strval($number+1);
} else {
$newRegNo = check_title($_POST['cpdParentRegNum']) . $saltSuffix;
}
$sql = "UPDATE compound_registry SET cpd_id = :cpdid, validated = :validated, regno = :regno WHERE id = :regid";
$req = $bdd->prepare($sql);
$result = $req->execute(array(
'cpdid' => $cpdid,
'validated' => $validated,
'regno' => $newRegNo,
'regid' => $regid));
} else if (!$validated) {
$sql = "UPDATE compound_registry SET cpd_id = :cpdid, validated = :validated WHERE id = :regid";
$req = $bdd->prepare($sql);
$result = $req->execute(array(
'cpdid' => $cpdid,
'validated' => $validated,
'regid' => $regid));
}
$result = $bdd->commit();
// Check if insertion is successful
if($result) {
unset($_SESSION['new_title']);
unset($_SESSION['new_date']);
unset($_SESSION['status']);
unset($_SESSION['errors']);
header("location: compounds.php?mode=edit®id=$regid");
} else {
die('Something went wrong in the database query. Check the flux capacitor.');
}