class.tx_nG6_db.php 39.8 KB
Newer Older
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
<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2009 PF bioinformatique de Toulouse <>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script 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 General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

require_once(t3lib_extMgm::extPath('nG6').'/lib/class.tx_nG6_utils.php');

/**
 * Class_nG6_db' for the 'nG6' extension.
 * This class is in charge of all communication with the database
 *
 * @author	PF bioinformatique de Toulouse <>
 */
class tx_nG6_db {

	/*
	 * Project functions
	 *------------------------------------------------------------*/

	/**
	 * Select all project for the current user
41
	 * 
42
43
44
	 * @param	string	$user_group	the user group
	 * @return	table with all projects
	 */
Jerome Mariette's avatar
Jerome Mariette committed
45
	function select_all_user_projects($user_group, $orderby='', $limit='') {
46
		
47
48
		$projects = array();
		// If the user is not logged on display demonstration project
49
		if (!$GLOBALS['TSFE']->loginUser) { 
50
			$where = 'public=0';
51
		} else {
52
			$where = 'fe_group IN ('.tx_nG6_db::get_user_groups($user_group).')';	
53
54
		}
		//First, get the analyzis project-scale
55
		$queryParts = array( 
56
57
58
59
			'SELECT' => 'tx_nG6_project.uid AS project_id, '.
						'tx_nG6_project.name AS project_name, '.
						'tx_nG6_project.description AS project_description, '.
						'tx_nG6_project.fe_group AS project_fe_group, '.
60
						'tx_nG6_project.public AS project_public, '.
61
62
63
64
						'tx_nG6_project.hidden AS project_hidden',
			'FROM' => 'tx_nG6_project',
			'WHERE' => $where,
			'GROUPBY' => '',
Jerome Mariette's avatar
Jerome Mariette committed
65
66
			'ORDERBY' => $orderby,
			'LIMIT' => $limit
67
68
69
70
71
		);
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		$user_groups = t3lib_div::intExplode(',',tx_nG6_db::get_user_groups($user_group));
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			$project_id = $row['project_id'];
72
			
73
74
75
76
77
78
79
80
81
82
83
			// Is the user allowed to see this project
			$user_allowed = false;
			if ($row['project_hidden'] == 0) {
				$user_allowed = true;
			} else {
			    foreach(tx_nG6_db::get_super_groups_of($row['project_fe_group']) AS $valeur) {
					if (in_array($valeur, $user_groups)) {
						$user_allowed = true;
					}
			    }
			}
84
85
			
			if (!isset ($projects['project_'.$project_id]) and $user_allowed) {		
86
				$projects['project_'.$project_id] = array(
87
					'id'			=> $project_id,				
88
					'name'			=> $row['project_name'],
Jerome Mariette's avatar
Jerome Mariette committed
89
					'hidden'		=> $row['project_hidden'],
90
					'public'		=> $row['project_public'],
Jerome Mariette's avatar
Jerome Mariette committed
91
					'group'			=> $row['project_fe_group'],
92
93
					'description'	=> $row['project_description']);
			}
94
						
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
		}
		return $projects;
	}

	/**
	 * Select a run from the database
	 *
	 * @param	string	$project_id	the project id to return
	 * @return	hash table with all project information
	 */
	function select_project($project_id) {
		$queryParts = array(
			'SELECT' => '*',
			'FROM' => 'tx_nG6_project',
			'WHERE' => 'tx_nG6_project.uid = '.$project_id,
			'GROUPBY' => '',
			'ORDERBY' => '',
			'LIMIT' => ''
		);
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
Jerome Mariette's avatar
Jerome Mariette committed
115
116
117
118
119
120
121
		$vals = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
		return array('id'			=> $vals['uid'],				
					 'name'			=> $vals['name'],
					 'hidden'		=> $vals['hidden'],
					 'public'		=> $vals['public'],
					 'group'		=> $vals['fe_group'],
					 'description'	=> $vals['description']);
122
123
124
125
126
127
128
129
130
	}

	/**
	 * Select all runs linked to the specified project
	 *
	 * @param	string	$user_group	the user group
	 * @param	string	$project_id	the project id
	 * @return	hash table with all runs information
	 */
Jerome Mariette's avatar
Jerome Mariette committed
131
    function get_project_runs($user_group, $project_id, $orderby='', $limit='') {
132
		// First select all runs from the database
133
		$queryParts = Array( 
134
135
136
137
			'SELECT' => 'tx_nG6_run.uid AS run_id,'.
					 	'tx_nG6_run.directory AS run_directory,'.
					 	'tx_nG6_run.species AS run_species,'.
						'tx_nG6_run.description AS run_description, '.
Jerome Mariette's avatar
Jerome Mariette committed
138
139
140
						'tx_nG6_run.data_nature AS run_data_nature, '.
						'tx_nG6_run.sequencer AS run_sequencer, '.
						'tx_nG6_run.type AS run_type, '.
141
						'tx_nG6_run.name AS run_name, '.
Jerome Mariette's avatar
Jerome Mariette committed
142
143
144
						'tx_nG6_project.uid AS project_id, '.
						'tx_nG6_run.nb_sequences AS run_nb_sequences, '.
						'tx_nG6_run.full_seq_size AS run_full_seq_size, '.
145
146
						'tx_nG6_run.date AS run_date, '.
						'tx_nG6_run.hidden AS run_hidden, '.
Jerome Mariette's avatar
Jerome Mariette committed
147
						'tx_nG6_project.name AS project_name, '.
148
149
150
						'tx_nG6_project.fe_group AS project_fe_group',
			'FROM' => 'tx_nG6_project INNER JOIN ( tx_nG6_project_run INNER JOIN tx_nG6_run ON tx_nG6_project_run.run_id=tx_nG6_run.uid '.
					  ' ) ON tx_nG6_project.uid=tx_nG6_project_run.project_id',
151
			'WHERE' => 'tx_nG6_project.uid='.$project_id,
152
			'GROUPBY' => '',
Jerome Mariette's avatar
Jerome Mariette committed
153
154
			'ORDERBY' => $orderby,
			'LIMIT' 	=> $limit
155
		); 
156
157
158
159
160
161
		// Then create the result hash table
		$results = array();
		$user_groups = t3lib_div::intExplode(',',tx_nG6_db::get_user_groups($user_group));
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			$run_id = $row['run_id'];
162
			
163
164
165
166
167
168
169
170
171
172
173
			// Is the user allowed to see this run
			$user_allowed = false;
			if ($row['run_hidden'] == 0) {
				$user_allowed = true;
			} else {
			    foreach(tx_nG6_db::get_super_groups_of($row['project_fe_group']) AS $valeur) {
					if (in_array($valeur, $user_groups)) {
						$user_allowed = true;
					}
			    }
			}
174
175
176
					
			if (!isset($results['run_'.$run_id]) and $user_allowed) {				
				$results['run_'.$run_id] = array( 
Jerome Mariette's avatar
Jerome Mariette committed
177
					'id'			=> $run_id,
178
					'directory'		=> $row['run_directory'], 
179
					'name' 			=> $row['run_name'],
Jerome Mariette's avatar
Jerome Mariette committed
180
181
182
					'project_name' 	=> $row['project_name'],
					'fe_group' 		=> $row['project_fe_group'],
					'hidden'		=> $row['run_hidden'],
183
					'species' 		=> $row['run_species'],
Jerome Mariette's avatar
Jerome Mariette committed
184
185
186
					'nb_sequences' 	=> $row['run_nb_sequences'],
					'full_seq_size' => $row['run_full_seq_size'],
					'project_id' 	=> $row['project_id'],
187
					'date'			=> $row['run_date'],
Jerome Mariette's avatar
Jerome Mariette committed
188
189
190
					'data_nature'	=> $row['run_data_nature'],
					'sequencer'		=> $row['run_sequencer'],
					'type'			=> $row['run_type'],
191
					'description' 	=> $row['run_description']				
192
				);
193
			}						
194
195
196
197
198
199
200
201
202
203
204
		}
		return $results;
    }

	/**
	 * Select all analysis linked to the specified project
	 *
	 * @param	string	$user_group	the user group
	 * @param	string	$project_id	the project id
	 * @return	hash table with all analysis information
	 */
Jerome Mariette's avatar
Jerome Mariette committed
205
    function get_project_analysis($user_group, $project_id, $orderby='', $limit='') {
206
		// First select all analysis from the database
207
208
209
		$queryParts = array( 
			'SELECT' => 'tx_nG6_analyze.uid AS analyze_id,'. 
						'tx_nG6_analyze.directory AS analyze_directory,'. 
210
						'tx_nG6_analyze.name AS analyze_name, '.
211
						'tx_nG6_analyze.class AS analyze_class, '.
212
						'tx_nG6_analyze.date AS analyze_date, '.
Jerome Mariette's avatar
Jerome Mariette committed
213
214
						'tx_nG6_analyze.software AS analyze_software, '.
						'tx_nG6_analyze.version AS analyze_version, '.
215
						'tx_nG6_analyze.params AS analyze_params, '.
216
217
218
219
220
221
222
						'tx_nG6_analyze.description AS analyze_description, '.
						'tx_nG6_analyze.hidden AS analyze_hidden, '.
						'tx_nG6_project.fe_group AS project_fe_group',
			'FROM' => 'tx_nG6_project INNER JOIN (tx_nG6_project_analyze INNER JOIN tx_nG6_analyze ON ' .
					  'tx_nG6_analyze.uid=tx_nG6_project_analyze.analyze_id) ON tx_nG6_project.uid = tx_nG6_project_analyze.project_id ',
			'WHERE' => 'tx_nG6_project_analyze.project_id='.$project_id,
			'GROUPBY' => '',
Jerome Mariette's avatar
Jerome Mariette committed
223
224
			'ORDERBY' => $orderby,
			'LIMIT' => $limit
225
226
227
228
229
230
231
		);
		// Then create the result hash table
		$results = array();
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		$user_groups = t3lib_div::intExplode(',',tx_nG6_db::get_user_groups($user_group));
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			$analyze_id = $row['analyze_id'];
232
			
233
234
235
236
237
238
239
240
241
242
243
			// Is the user allowed to see this analyse
			$user_allowed = false;
			if ($row['analyze_hidden'] == 0) {
				$user_allowed = true;
			} else {
			    foreach(tx_nG6_db::get_super_groups_of($row['project_fe_group']) AS $valeur) {
					if (in_array($valeur, $user_groups)) {
						$user_allowed = true;
					}
			    }
			}
244
245
246
247
			
			if (!isset ($results['analyse_'.$analyze_id]) and $user_allowed) {				
				$results['analyse_'.$analyze_id] = array( 
					'directory'		=> $row['analyze_directory'], 
248
					'name' 			=> $row['analyze_name'],
249
					'class'			=> $row['analyze_class'],
Jerome Mariette's avatar
Jerome Mariette committed
250
251
					'id' 			=> $row['analyze_id'],
					'hidden' 		=> $row['analyze_hidden'],
252
					'params' 		=> $row['analyze_params'],
Jerome Mariette's avatar
Jerome Mariette committed
253
254
					'software' 		=> $row['analyze_software'],
					'version' 		=> $row['analyze_version'],
255
					'date'			=> $row['analyze_date'],
256
					'description' 	=> $row['analyze_description']				
257
				);
258
			}						
259
260
261
262
		}
		return $results;
    }

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
    /**
     * Hide a project and sublevels (runs & analyzes) datas from database project
     * 
     * @param	string	$p_id	the project id to hide
     * @param	string	$user_group	the group of the current user
     */
	function hide_project($p_id, $user_group) {
		
		// First change all project runs
		$project_runs = tx_nG6_db::get_project_runs($user_group, $p_id);
		foreach($project_runs as $run_id => $run_values) {
			tx_nG6_db::hide_run($run_values['id'], $user_group);
		}

		// Then all project analysis
    	$project_analysis = tx_nG6_db::get_project_analysis($user_group, $p_id);
		foreach($project_analysis as $analysis_id => $analysis_values) {  
			tx_nG6_db::hide_analysis($analysis_values['id']);
		}
		
		// Finaly change the project itself
		$GLOBALS['TYPO3_DB']-> exec_UPDATEquery ('tx_nG6_project', 'uid='.$p_id, array('hidden' => '1'));
	}

    /**
     * Unhide a project and sublevels (runs & analyzes) datas
     * 
     * @param	string	$p_id	the project id to hide
     * @param	string	$user_group	the group of the current user
     */
	function unhide_project($p_id, $user_group) {
		
		// First change all project runs
		$project_runs = tx_nG6_db::get_project_runs($user_group, $p_id);
		foreach($project_runs as $run_id => $run_values) {
			tx_nG6_db::unhide_run($run_values['id'], $user_group);
		}

		// Then all project analysis
    	$project_analysis = tx_nG6_db::get_project_analysis($user_group, $p_id);
		foreach($project_analysis as $analysis_id => $analysis_values) {  
			tx_nG6_db::unhide_analysis($analysis_values['id']);
		}
		
		// Finaly change the project itself
		$GLOBALS['TYPO3_DB']-> exec_UPDATEquery ('tx_nG6_project', 'uid='.$p_id, array('hidden' => '0'));
	}

    /**
     * Publish the project
     * 
     * @param	string	$p_id	the project id to publish
     */
	function publish_project($p_id, $user_group) {
		// First unhide the project
		tx_nG6_db::unhide_project($p_id, $user_group);
		// The set the project as visible
		$GLOBALS['TYPO3_DB']-> exec_UPDATEquery ('tx_nG6_project', 'uid='.$p_id, array('public' => '0'));
	}

    /**
     * Unpublish the project
     * 
     * @param	string	$p_id	the project id to publish
     */
	function unpublish_project($p_id) {
		$GLOBALS['TYPO3_DB']-> exec_UPDATEquery ('tx_nG6_project', 'uid='.$p_id, array('public' => '1'));
	}

    /**
     * Delete a project and sublevels (runs & analyzes) datas from database project
     * 
     * @param string $user_group	the user group
     * @param string $p_id			the project id
     * @param string $user_login	the user login
     * @param string $user_pwd		the user password
     * @param string $data_folder	the data folder
     */
	function delete_project($user_group, $p_id, $user_login, $user_pwd, $data_folder) {
		
		$res = 0;
		// All runs
		$project_runs = tx_nG6_db::get_project_runs($user_group, $p_id);
		foreach($project_runs as $run_id => $run_values) {
			if ($run_values['id'] != 'undefined' && $run_values['id'] != '') {
				//delete the run sublevels
				$res = tx_nG6_db::delete_run($user_group, $run_values['id'], $user_login, $user_pwd, $data_folder);
				if ($res != 0) { break; }				
			}
		}
		
		// All project_analysis
		if ($res == 0) {
			$project_analysis = tx_nG6_db::get_project_analysis($user_group, $p_id);
			foreach($project_analysis as $analyse_id => $analyze_values) {  
				if ($analyze_values['id'] != 'undefined' && $analyze_values['id'] != '') {
					// delete table 'analyze' entries
					$res = tx_nG6_db::delete_analysis($analyze_values['id'], $user_login, $user_pwd, $data_folder);
					if ($res != 0) { break; }
				}
			}
		}
		
		// Delete DB project entry
		if ($res == 0) {
			$table='tx_nG6_project';
			$where='tx_nG6_project.uid='.$p_id;
			$res1 = $GLOBALS['TYPO3_DB']-> exec_DELETEquery ($table, $where);
			if ($res1 != 1) {$res = 1;}
		}
		
		return $res;
	}

377
378
379
380
381

	/*
	 * Run functions
	 *------------------------------------------------------------*/

Jerome Mariette's avatar
Jerome Mariette committed
382
383
	/**
	 * Select all run for the specified user
384
	 * 
Jerome Mariette's avatar
Jerome Mariette committed
385
386
387
388
	 * @param	string	$user_group	the user group
	 * @return	table with all projects
	 */
	function select_all_user_runs($user_group, $orderby='', $limit='') {
389

Jerome Mariette's avatar
Jerome Mariette committed
390
		// If the user is not logged on display demonstration project
391
		if (!$GLOBALS['TSFE']->loginUser) { 
392
			$where = 'public=0';
Jerome Mariette's avatar
Jerome Mariette committed
393
		} else {
394
			$where = 'fe_group IN ('.tx_nG6_db::get_user_groups($user_group).')';	
Jerome Mariette's avatar
Jerome Mariette committed
395
396
		}
     	// Execute the request
397
		$queryParts = Array( 
Jerome Mariette's avatar
Jerome Mariette committed
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
			'SELECT' => 'tx_nG6_run.uid AS run_id,'.
					 	'tx_nG6_run.directory AS run_directory,'.
					 	'tx_nG6_run.species AS run_species,'.
						'tx_nG6_run.description AS run_description, '.
						'tx_nG6_run.data_nature AS run_data_nature, '.
						'tx_nG6_run.sequencer AS run_sequencer, '.
						'tx_nG6_run.type AS run_type, '.
						'tx_nG6_run.name AS run_name, '.
						'tx_nG6_run.nb_sequences AS run_nb_sequences, '.
						'tx_nG6_run.full_seq_size AS run_full_seq_size, '.
						'tx_nG6_run.date AS run_date, '.
						'tx_nG6_run.hidden AS run_hidden, '.
						'tx_nG6_project.uid AS project_id, '.
						'tx_nG6_project.name AS project_name, '.
						'tx_nG6_project.fe_group AS project_fe_group',
			'FROM' => 'tx_nG6_project INNER JOIN ( tx_nG6_project_run INNER JOIN tx_nG6_run ON tx_nG6_project_run.run_id=tx_nG6_run.uid '.
					  ' ) ON tx_nG6_project.uid=tx_nG6_project_run.project_id',
			'WHERE' => $where,
			'GROUPBY' => '',
			'ORDERBY' => $orderby,
			'LIMIT' 	=> $limit
419
		); 
Jerome Mariette's avatar
Jerome Mariette committed
420
421
422
423
424
425
		// Then create the result hash table
		$results = array();
		$user_groups = t3lib_div::intExplode(',',tx_nG6_db::get_user_groups($user_group));
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			$run_id = $row['run_id'];
426
			
Jerome Mariette's avatar
Jerome Mariette committed
427
428
429
430
431
432
433
434
435
436
437
			// Is the user allowed to see this run
			$user_allowed = false;
			if ($row['run_hidden'] == 0) {
				$user_allowed = true;
			} else {
			    foreach(tx_nG6_db::get_super_groups_of($row['project_fe_group']) AS $valeur) {
					if (in_array($valeur, $user_groups)) {
						$user_allowed = true;
					}
			    }
			}
438
439
					
			if (!isset($results['run_'.$run_id]) and $user_allowed) {				
Jerome Mariette's avatar
Jerome Mariette committed
440
				$results['run_'.$run_id] = array(
441
442
					'id'			=> $run_id,  
					'directory'		=> $row['run_directory'], 
Jerome Mariette's avatar
Jerome Mariette committed
443
444
445
446
447
448
449
450
451
452
453
454
					'name' 			=> $row['run_name'],
					'project_name' 	=> $row['project_name'],
					'fe_group' 		=> $row['project_fe_group'],
					'project_id' 	=> $row['project_id'],
					'hidden'		=> $row['run_hidden'],
					'species' 		=> $row['run_species'],
					'nb_sequences' 	=> $row['run_nb_sequences'],
					'full_seq_size' => $row['run_full_seq_size'],
					'date'			=> $row['run_date'],
					'data_nature'	=> $row['run_data_nature'],
					'sequencer'		=> $row['run_sequencer'],
					'type'			=> $row['run_type'],
455
					'description' 	=> $row['run_description']				
Jerome Mariette's avatar
Jerome Mariette committed
456
				);
457
			}						
Jerome Mariette's avatar
Jerome Mariette committed
458
459
460
461
462
		}
		return $results;

	}

463
464
465
466
467
468
	/**
	 * Select all information for the specified run
	 *
	 * @param	string	$run_id	the run id
	 * @return	hash table with all the run information
	 */
Jerome Mariette's avatar
Jerome Mariette committed
469
	function select_run($run_id) {
470
471
		
		
472
		// First select the run from the database
473
		$queryParts = Array( 
474
475
476
477
478
			'SELECT' => 'tx_nG6_project.uid AS project_id,'.
						'tx_nG6_project.name AS project_name,'.
						'tx_nG6_run.uid AS run_id,'.
					 	'tx_nG6_run.directory AS run_directory,'.
						'tx_nG6_run.description AS run_description, '.
Jerome Mariette's avatar
Jerome Mariette committed
479
						'tx_nG6_run.species AS run_species, '.
480
						'tx_nG6_run.name AS run_name, '.
Jerome Mariette's avatar
Jerome Mariette committed
481
482
483
484
						'tx_nG6_run.data_nature AS run_data_nature, '.
						'tx_nG6_run.sequencer AS run_sequencer, '.
						'tx_nG6_run.type AS run_type, '.
						'tx_nG6_run.full_seq_size, '.
Jerome Mariette's avatar
Jerome Mariette committed
485
						'tx_nG6_run.hidden AS run_hidden, '.
486
487
488
489
490
491
492
493
						'tx_nG6_run.nb_sequences, '.
						'tx_nG6_run.date AS run_date',
			'FROM' => 'tx_nG6_project INNER JOIN ( tx_nG6_project_run INNER JOIN tx_nG6_run ON tx_nG6_project_run.run_id=tx_nG6_run.uid '.
					  ' ) ON tx_nG6_project.uid=tx_nG6_project_run.project_id',
			'WHERE' => 'tx_nG6_run.uid='.$run_id,
			'GROUPBY' => '',
			'ORDERBY' => 'tx_nG6_run.date DESC',
			'LIMIT' 	=> ''
494
		); 
495
496
497
		// Then create the result hash table
		$result = array();
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
498
499
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {			
			$result = array( 
Jerome Mariette's avatar
Jerome Mariette committed
500
				'id'			=> $row['run_id'],  
501
502
503
				'project_id'	=> $row['project_id'], 
				'project_name'	=> $row['project_name'], 
				'directory'		=> $row['run_directory'], 
Jerome Mariette's avatar
Jerome Mariette committed
504
				'name' 			=> $row['run_name'],
Jerome Mariette's avatar
Jerome Mariette committed
505
				'hidden'		=> $row['run_hidden'],
Jerome Mariette's avatar
Jerome Mariette committed
506
507
				'species' 		=> $row['run_species'],
				'nb_sequences' 	=> $row['nb_sequences'],
Jerome Mariette's avatar
Jerome Mariette committed
508
				'full_seq_size' => $row['full_seq_size'],
Jerome Mariette's avatar
Jerome Mariette committed
509
				'date'			=> $row['run_date'],
Jerome Mariette's avatar
Jerome Mariette committed
510
511
512
				'data_nature' 	=> $row['run_data_nature'],
				'sequencer' 	=> $row['run_sequencer'],
				'type' 			=> $row['run_type'],
513
514
				'description' 	=> $row['run_description']				
			);					
515
516
517
		}
		return $result;
    }
518

519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
	/**
	 * Select all mids linked to the specified run
	 *
	 * @param	string	$run_id	the run id
	 * @return	hash table with all mids description
	 */
	function select_mid_descriptions($run_id) {
		
		// First select all analysis from the database
		$queryParts = array( 
			'SELECT' => 'mid, description ',
			'FROM' => 'tx_nG6_sample ',
			'WHERE' => 'run_id='.$run_id,
			'GROUPBY' => '',
			'ORDERBY' => '',
			'LIMIT' => ''
		);
		
		$result = array();
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			$result[$row["mid"]] = $row["description"];
		}
		return $result;
		
	}	

546
547
548
549
550
551
552
	/**
	 * Select all analysis linked to the specified run
	 *
	 * @param	string	$user_group	the user group
	 * @param	string	$run_id	the run id
	 * @return	hash table with all analysis information
	 */
Jerome Mariette's avatar
Jerome Mariette committed
553
    function get_run_analysis($user_group, $run_id, $orderby='', $limit='') {
554
		// First select all analysis from the database
555
556
557
		$queryParts = array( 
			'SELECT' => 'tx_nG6_analyze.uid AS analyze_id,'. 
						'tx_nG6_analyze.directory AS analyze_directory,'. 
558
						'tx_nG6_analyze.name AS analyze_name, '.
559
560
						'tx_nG6_analyze.params AS analyze_params, '.
						'tx_nG6_analyze.class AS analyze_class, '.
561
						'tx_nG6_analyze.date AS analyze_date, '.
Jerome Mariette's avatar
Jerome Mariette committed
562
563
						'tx_nG6_analyze.software AS analyze_software, '.
						'tx_nG6_analyze.version AS analyze_version, '.
564
565
566
567
568
569
570
571
						'tx_nG6_analyze.hidden AS analyze_hidden, '.
						'tx_nG6_analyze.description AS analyze_description, '.
						'tx_nG6_project.fe_group AS project_fe_group',
			'FROM' => 'tx_nG6_project INNER JOIN ( tx_nG6_project_run INNER JOIN ( tx_nG6_run INNER JOIN ( tx_nG6_run_analyze INNER JOIN '.
					  'tx_nG6_analyze ON tx_nG6_analyze.uid = tx_nG6_run_analyze.analyze_id) ON tx_nG6_run_analyze.run_id = tx_nG6_run.uid '.
					  ') ON tx_nG6_run.uid = tx_nG6_project_run.run_id) ON tx_nG6_project_run.project_id = tx_nG6_project.uid ',
			'WHERE' => 'tx_nG6_run_analyze.run_id='.$run_id,
			'GROUPBY' => '',
Jerome Mariette's avatar
Jerome Mariette committed
572
573
			'ORDERBY' => $orderby,
			'LIMIT' => $limit
574
575
576
577
578
579
580
		);
		// Then create the result hash table
		$results = array();
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		$user_groups = t3lib_div::intExplode(',',tx_nG6_db::get_user_groups($user_group));
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			$analyze_id = $row['analyze_id'];
581
			
582
583
584
585
586
587
588
589
590
591
592
			// Is the user allowed to see this analyse
			$user_allowed = false;
			if ($row['analyze_hidden'] == 0) {
				$user_allowed = true;
			} else {
			    foreach(tx_nG6_db::get_super_groups_of($row['project_fe_group']) AS $valeur) {
					if (in_array($valeur, $user_groups)) {
						$user_allowed = true;
					}
			    }
			}
593
594
595
596
			
			if (!isset ($results['analyse_'.$analyze_id]) and $user_allowed) {				
				$results['analyse_'.$analyze_id] = array( 
					'directory'		=> $row['analyze_directory'], 
Jerome Mariette's avatar
Jerome Mariette committed
597
					'name' 			=> $row['analyze_name'],
598
599
					'params' 		=> $row['analyze_params'],
					'class' 		=> $row['analyze_class'],
Jerome Mariette's avatar
Jerome Mariette committed
600
601
602
603
604
					'id' 			=> $row['analyze_id'],
					'hidden' 		=> $row['analyze_hidden'],
					'software' 		=> $row['analyze_software'],
					'version' 		=> $row['analyze_version'],
					'date'			=> $row['analyze_date'],
605
					'description' 	=> $row['analyze_description']				
606
				);
607
			}						
608
609
610
611
		}
		return $results;
    }

612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
	/**
	 * Delete a run, sample and sublevel (analyzes) datas from database project
	 * 
	 * @param string $user_group	the user group
	 * @param string $r_id			the run id
	 * @param string $user_login	the user login
	 * @param string $user_pwd		the user password
	 * @param string $data_folder	the data folder
	 */
	function delete_run($user_group, $r_id, $user_login, $user_pwd, $data_folder) {		

		$res = 0;		
		$run_analysis = tx_nG6_db::get_run_analysis($user_group, $r_id);
		foreach($run_analysis as $analyse_id => $analyze_values) {
			if ($analyze_values['id'] != 'undefined' && $analyze_values['id'] != '') {				
				// delete table 'analyze' entries
				$res = tx_nG6_db::delete_analysis($analyze_values['id'], $user_login, $user_pwd, $data_folder);
				if ($res != 0) { break; }
			}
		}
		
		// First select the run
		$my_run = tx_nG6_db::select_run($r_id);
		
		// delete run
		if ($res == 0) {
			$table='tx_nG6_project_run';
			$where='run_id='.$r_id;
			$GLOBALS['TYPO3_DB']-> exec_DELETEquery ($table, $where);
			
			$table='tx_nG6_run';
			$where='tx_nG6_run.uid='.$r_id;
			$res1 = $GLOBALS['TYPO3_DB']-> exec_DELETEquery ($table, $where);
			if ($res1 != 1) {$res = 1;}
		}
			
		// delete run samples
		if ($res == 0) {
			$table='tx_nG6_sample';
			$where='tx_nG6_sample.run_id='.$r_id;
			$GLOBALS['TYPO3_DB']-> exec_DELETEquery ($table, $where);
		}
		
		if ($res == 0) {
			// Delete the run directory
			$res = tx_nG6_utils::delete_directory($user_login, $user_pwd, $data_folder.$my_run['directory']);
		}
		
		return $res;
	}

    /**
     * Hide a run and sublevels (analysis)
     * 
     * @param	string	$r_id	the run id to hide
     * @param	string	$user_group	the group of the current user
     */
	function hide_run($r_id, $user_group) {

		// First select all run analysis
    	$run_analysis = tx_nG6_db::get_run_analysis($user_group, $r_id);
		foreach($run_analysis as $analysis_id => $analysis_values) {  
			tx_nG6_db::hide_analysis($analysis_values['id']);
		}
		
		// Finaly change the project itself
		$GLOBALS['TYPO3_DB']-> exec_UPDATEquery ('tx_nG6_run', 'uid='.$r_id, array('hidden' => '1'));
	}

    /**
     * Unhide a run and sublevels (analysis)
     * 
     * @param	string	$r_id	the run id to hide
     * @param	string	$user_group	the group of the current user
     */
	function unhide_run($r_id, $user_group) {

		// Then all run analysis
    	$run_analysis = tx_nG6_db::get_run_analysis($user_group, $r_id);
		foreach($run_analysis as $analysis_id => $analysis_values) {  
			tx_nG6_db::unhide_analysis($analysis_values['id']);
		}
		
		// Finaly change the run itself
		$GLOBALS['TYPO3_DB']-> exec_UPDATEquery ('tx_nG6_run', 'uid='.$r_id, array('hidden' => '0'));
	}
698
699

	/*
700
	 * Analysis functions
701
	 *------------------------------------------------------------*/
702
	 
703
704
705
706
707
708
	/**
	 * Select all information on an analyse
	 *
	 * @param	string	$analyse_id	the analyse id
	 * @return	hash table with all analyse information
	 */
Jerome Mariette's avatar
Jerome Mariette committed
709
710
    function select_analyse($analyse_id) {

711
		// First select all analyse information
712
713
714
715
716
		$queryParts = array( 
			'SELECT' => 'tx_nG6_project.uid AS project_id,'. 
						'tx_nG6_project.name AS project_name,'. 
						'tx_nG6_analyze.uid AS analyze_id,'. 
						'tx_nG6_analyze.directory AS analyze_directory,'. 
717
						'tx_nG6_analyze.name AS analyze_name, '.
718
719
						'tx_nG6_analyze.params AS analyze_params, '.
						'tx_nG6_analyze.class AS analyze_class, '.
720
721
722
723
724
725
726
727
728
						'tx_nG6_analyze.date AS analyze_date, '.
						'tx_nG6_analyze.description AS analyze_description ',
			'FROM' => 'tx_nG6_project INNER JOIN (tx_nG6_project_analyze INNER JOIN tx_nG6_analyze ON ' .
					  'tx_nG6_analyze.uid=tx_nG6_project_analyze.analyze_id) ON tx_nG6_project.uid = tx_nG6_project_analyze.project_id ',
			'WHERE' => 'tx_nG6_analyze.uid='.$analyse_id,
			'GROUPBY' => '',
			'ORDERBY' => '',
			'LIMIT' => ''
		);
729
		
730
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
731
		
732
733
734
		// If it's a project analyse, add project information
		if ($GLOBALS['TYPO3_DB']->sql_num_rows($res) > 0) {
			$result = array();
735
736
737
738
739
740
741
742
743
744
745
746
747
			while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {	
				$result = array( 
					'project_name'	=> $row['project_name'], 
					'project_id'	=> $row['project_id'],
					'run_name'		=> 'None', 
					'run_id'		=> 'None',					 
					'directory'		=> $row['analyze_directory'], 
					'name' 			=> $row['analyze_name'],
					'class'			=> $row['analyze_class'],
					'params'		=> $row['analyze_params'],
					'date'			=> $row['analyze_date'],
					'description' 	=> $row['analyze_description']				
				);				
748
749
750
			}
			return $result;
		} else { // If it's a run analyse, add run information
751
			$queryParts = array( 
752
753
				'SELECT' => 'tx_nG6_project.name AS project_name,'.
							'tx_nG6_project.uid AS project_id,'.
754
							'tx_nG6_run.uid AS run_id,'. 
755
							'tx_nG6_run.name AS run_name,'.
756
757
							'tx_nG6_analyze.uid AS analyze_id,'. 
							'tx_nG6_analyze.directory AS analyze_directory,'. 
758
							'tx_nG6_analyze.name AS analyze_name, '.
759
760
							'tx_nG6_analyze.params AS analyze_params, '.
							'tx_nG6_analyze.class AS analyze_class, '.
761
762
763
764
765
766
767
768
769
770
771
772
							'tx_nG6_analyze.date AS analyze_date, '.
							'tx_nG6_analyze.description AS analyze_description',
				'FROM' => 'tx_nG6_project INNER JOIN ( tx_nG6_project_run INNER JOIN ( tx_nG6_run INNER JOIN ( tx_nG6_run_analyze INNER JOIN '.
						  'tx_nG6_analyze ON tx_nG6_analyze.uid = tx_nG6_run_analyze.analyze_id) ON tx_nG6_run_analyze.run_id = tx_nG6_run.uid '.
						  ') ON tx_nG6_run.uid = tx_nG6_project_run.run_id) ON tx_nG6_project_run.project_id = tx_nG6_project.uid ',
				'WHERE' => 'tx_nG6_analyze.uid='.$analyse_id,
				'GROUPBY' => '',
				'ORDERBY' => 'tx_nG6_analyze.name',
				'LIMIT' => ''
			);
			$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
			$result = array();
773
774
775
776
777
778
779
780
781
782
783
784
785
			while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {	
				$result = array( 
					'project_name'	=> $row['project_name'], 
					'project_id'	=> $row['project_id'],
					'run_name'		=> $row['run_name'], 
					'run_id'		=> $row['run_id'], 			 
					'directory'		=> $row['analyze_directory'], 
					'name' 			=> $row['analyze_name'],
					'params'		=> $row['analyze_params'],
					'class' 		=> $row['analyze_class'],
					'date'			=> $row['analyze_date'],
					'description' 	=> $row['analyze_description']				
				);				
786
787
788
789
			}
			return $result;
		}
    }
790

791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
	/**
	 * Select all results of an analyse
	 *
	 * @param	string	$analyse_id	the analyse id
	 * @return	hash table with all analyse results
	 */
    function select_analyse_results($analyse_id) {

		// First select all analyse information
		$queryParts = array( 
			'SELECT' => 'uid,file,rkey,rvalue,rgroup ',
			'FROM' => 'tx_nG6_result ',
			'WHERE' => 'analyze_id='.$analyse_id,
			'GROUPBY' => '',
			'ORDERBY' => '',
			'LIMIT' => ''
		);
		
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		$result = array();
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			if (array_key_exists($row['file'], $result)) {
				if (array_key_exists($row['rgroup'], $result[$row['file']])) {
					$result[$row['file']][$row['rgroup']][$row['rkey']] = $row['rvalue'];
				} else {
					$result[$row['file']][$row['rgroup']] = array($row['rkey'] => $row['rvalue']);
				}
			} else {
				$result[$row['file']] = array($row['rgroup'] => array($row['rkey'] => $row['rvalue']));
			}			
		}
		return $result;
    }
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888

    /**
     * Hide an analysis
     * 
     * @param	string	$a_id	the analysis id to hide
     */
	function hide_analysis($a_id) {
		// Change the analysis itself
		$GLOBALS['TYPO3_DB']-> exec_UPDATEquery ('tx_nG6_analyze', 'uid='.$a_id, array('hidden' => '1'));
	}

    /**
     * Unhide an analysis
     * 
     * @param	string	$a_id	the analysis id to hide
     * @param	string	$user_group	the group of the current user
     */
	function unhide_analysis($a_id) {
		// Change the analysis itself
		$GLOBALS['TYPO3_DB']-> exec_UPDATEquery ('tx_nG6_analyze', 'uid='.$a_id, array('hidden' => '0'));
	}

	/**
	 * Delete an analyze and results from database project
	 * 
	 * @param string $a_id			the analyze id
	 * @param string $user_login	the user login
	 * @param string $user_pwd		the user password
	 * @param string $data_folder	the data folder
	 * @return	0=>everything ok, 1=>user right problem, 2=>wrong authentification, 3=>connection error
	 */
	function delete_analysis($a_id, $user_login, $user_pwd, $data_folder) {

		$res = 0;

		// First select the analyse
		$my_analysis = tx_nG6_db::select_analyse($a_id);
		
		// Delete the analysis results from the database
		$table='tx_nG6_result';
		$where='tx_nG6_result.analyze_id='.$a_id;
		$GLOBALS['TYPO3_DB']-> exec_DELETEquery ($table, $where);

		// Intermediate tables
		$table='tx_nG6_project_analyze';
		$where='analyze_id='.$a_id;
		$GLOBALS['TYPO3_DB']-> exec_DELETEquery ($table, $where);
		$table='tx_nG6_run_analyze';
		$where='analyze_id='.$a_id;
		$GLOBALS['TYPO3_DB']-> exec_DELETEquery ($table, $where);
		
		// Finaly the analysis itself
		$table='tx_nG6_analyze';
		$where='tx_nG6_analyze.uid='.$a_id;
		$res1 = $GLOBALS['TYPO3_DB']-> exec_DELETEquery ($table, $where);
		if ($res1 != 1) { $res = 1; } 
		
		if ($res == 0) {
			// Delete the analyse directory
			$res = tx_nG6_utils::delete_directory($user_login, $user_pwd, $data_folder.$my_analysis['directory']);
		}

		return $res;
	}

889
890
891
892
893
894
895

	/*
	 * User functions
	 *------------------------------------------------------------*/

    /**
    * Return the supergroups list of the specified group
896
    * 
897
898
    * @param string	$group	the group
    * @return array
899
    */ 
900
901
902
903
904
905
906
907
908
909
910
911
912
	function get_super_groups_of ($group) {
		// First get the super group of the given group
		$queryParts = array(
			'SELECT' => 'uid, subgroup',
			'FROM' => 'fe_groups',
			'WHERE' => '',
			'GROUPBY' => '',
			'ORDERBY' => '',
			'LIMIT' => ''
		);
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		$reslist = array();
		$i = 0;
Jerome Mariette's avatar
Jerome Mariette committed
913
914
		
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
915
916
917
918
919
			if (in_array($group, t3lib_div::intExplode(',',$row['subgroup']))) {
				$reslist[$i] = $row['uid'];
				$i++;
			}
		}
Jerome Mariette's avatar
Jerome Mariette committed
920
		
921
922
		return $reslist;
	}
923
    
Jerome Mariette's avatar
Jerome Mariette committed
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
	/**
     * Return the recursive supergroups list of the specified group
     *
     * @param string $group the group id
     * @param string $level the search depth, can be 'all' or 'first' (default)
     * @return array
     */
    function get_all_super_groups_of ($group, $level = 'first') {
        
        if ($level == 'all') {
            // return the recursive list of all $group super groups
            $first_list = tx_nG6_db::get_super_groups_of($group);
            $super_list = $first_list;
        
            foreach ($first_list as $first_list_group_id) {
                $next_list = tx_nG6_db::get_all_super_groups_of($first_list_group_id, $level);
                $super_list = array_merge($super_list,$next_list);
            }
	        return $super_list;
        } else {
            return tx_nG6_db::get_super_groups_of($group);
        }
    }
    
948
949
	/**
	 * Returns a string with user groups separated by comas
950
	 * 
951
952
953
954
955
956
957
958
     * @param	string	$user_group	the user group
	 * @return	string	list of user groups splited by a coma
	 */
	function get_user_groups($user_group) {
		$tab_groups = array();
		tx_nG6_db::get_sub_groups($user_group, '', $tab_groups);
		$groups = '';
		foreach($tab_groups as $valeur) { $groups .= $valeur.','; }
959
		return substr($groups, 0, -1);	
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
	}

	/**
	 * Fetches subgroups of groups. Function is called recursively for each subgroup.
	 * Function was previously copied from t3lib_userAuthGroup->fetchGroups and has been slightly modified.
	 *
	 * @param	string	$grList	commalist of fe_groups uid numbers
	 * @param	string	$idList	list of already processed fe_groups-uids so the function will not fall into a eternal recursion.
	 * @return	array
	 */
	function get_sub_groups($grList, $idList='', &$groups)	{

		// Fetching records of the groups in $grList (which are not blocked by lockedToDomain either):
		$lockToDomain_SQL = ' AND (lockToDomain=\'\' OR lockToDomain IS NULL)';
		$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,subgroup', 'fe_groups', 'deleted=0 AND hidden=0 AND uid IN ('.$grList.')'.$lockToDomain_SQL);

		$groupRows = array();	// Internal group record storage
		// The groups array is filled
		while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))	{
			if(!in_array($row['uid'], $groups))	{ $groups[] = $row['uid']; }
			$groupRows[$row['uid']] = $row;
		}

		// Traversing records in the correct order
		$include_staticArr = t3lib_div::intExplode(',', $grList);
		foreach($include_staticArr as $uid)	{	// traversing list
			// Get row:
			$row=$groupRows[$uid];
			if (is_array($row) && !t3lib_div::inList($idList,$uid))	{	// Must be an array and $uid should not be in the idList, because then it is somewhere previously in the grouplist
				// Include sub groups
				if (trim($row['subgroup']))	{
					$theList = implode(',',t3lib_div::intExplode(',',$row['subgroup']));	// Make integer list
					tx_nG6_db::get_sub_groups($theList, $idList.','.$uid, $groups);		// Call recursively, pass along list of already processed groups so they are not recursed again.
				}
			}
		}
	}
997
	
998
999
1000
1001
1002
1003
1004
1005
1006
	/**
	 * Return if the user is authorized or not to access the specified project and run
	 *
	 * @param	string	$user_group	the user group
	 * @param	string	$project_id	the project id
	 * @param	string	$project_id	the run id
	 * @return	boolean
	 */
	function user_is_authorized ($user_group, $project_id, $run_id) {
1007
		$authorized = false;
1008
1009
		if ($project_id) {
			$queryParts = array(
1010
				'SELECT' => 'fe_group,public',
1011
1012
1013
1014
1015
1016
1017
1018
				'FROM' => 'tx_nG6_project',
				'WHERE' => 'uid='.$project_id,
				'GROUPBY' => '',
				'ORDERBY' => '',
				'LIMIT' => ''
			);
			$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
			$val = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
1019
1020
1021
1022
1023
			if ($GLOBALS['TSFE']->loginUser) {
				$authorized = in_array($val['fe_group'], preg_split("/,/", tx_nG6_db::get_user_groups($user_group)));
			} else {
				$authorized = ($val['public'] == 0);
			}
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
		} elseif ($run_id) {
			$queryParts = array(
				'SELECT' => 'project_id',
				'FROM' => 'tx_nG6_project_run',
				'WHERE' => 'run_id='.$run_id,
				'GROUPBY' => '',
				'ORDERBY' => '',
				'LIMIT' => ''
			);
			$res1 = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
			$val = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res1);
			$queryParts = array(
1036
				'SELECT' => 'fe_group,public',
1037
1038
1039
1040
1041
1042
1043
1044
				'FROM' => 'tx_nG6_project',
				'WHERE' => 'uid='.$val['project_id'],
				'GROUPBY' => '',
				'ORDERBY' => '',
				'LIMIT' => ''
			);
			$res2 = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
			$val = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res2);
1045
1046
1047
1048
1049
			if ($GLOBALS['TSFE']->loginUser) {
				$authorized = in_array($val['fe_group'], preg_split("/,/", tx_nG6_db::get_user_groups($user_group)));
			} else {
				$authorized = ($val['public'] == 0);
			}
1050
		} else {
1051
			$authorized = true;
1052
		}
1053
		return $authorized;
1054
	}
Jerome Mariette's avatar
Jerome Mariette committed
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111

	/**
	 * Return if the user is a superuser for the specified project/run/analyse
	 *
	 * @param	string	$user_group		the user group
	 * @param	string	$type			can be project/run/analyse
	 * @param	string	$uid			the uid
	 * @return	boolean {1 = ok, 0 = nok}
	 */
    function is_super_user($user_group, $type, $uid) {
    	
   		if ($type == 'project') {
			$queryParts = array(
				'SELECT' => 'fe_group',
				'FROM' => 'tx_nG6_project',
				'WHERE' => 'uid='.$uid,
				'GROUPBY' => '',
				'ORDERBY' => '',
				'LIMIT' => ''
			);
			$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
			$val = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
			$authorized_group = $val['fe_group'];
			
		} elseif ($type == 'run') {
			$queryParts = array(
				'SELECT' => 'project_id',
				'FROM' => 'tx_nG6_project_run',
				'WHERE' => 'run_id='.$uid,
				'GROUPBY' => '',
				'ORDERBY' => '',
				'LIMIT' => ''
			);
			$res1 = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
			$val = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res1);
			$queryParts = array(
				'SELECT' => 'fe_group',
				'FROM' => 'tx_nG6_project',
				'WHERE' => 'uid='.$val['project_id'],
				'GROUPBY' => '',
				'ORDERBY' => '',
				'LIMIT' => ''
			);
			$res2 = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
			$val = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res2);
			$authorized_group = $val['fe_group'];
			
		} elseif ($type == 'analyze') {
			$queryParts = array(
				'SELECT' => 'run_id',
				'FROM' => 'tx_nG6_run_analyze',
				'WHERE' => 'analyze_id='.$uid,
				'GROUPBY' => '',
				'ORDERBY' => '',
				'LIMIT' => ''
			);
			$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
			if ($GLOBALS['TYPO3_DB']->sql_num_rows($res) > 0) {
				$val = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
				$queryParts = array(
					'SELECT' => 'project_id',
					'FROM' => 'tx_nG6_project_run',
					'WHERE' => 'run_id='.$val['run_id'],
					'GROUPBY' => '',
					'ORDERBY' => '',
					'LIMIT' => ''
				);
				$res2 = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
				$val = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res2);
				$queryParts = array(
					'SELECT' => 'fe_group',
					'FROM' => 'tx_nG6_project',
					'WHERE' => 'uid='.$val['project_id'],
					'GROUPBY' => '',
					'ORDERBY' => '',
					'LIMIT' => ''
				);
				$res3 = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
				$val = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res3);
				$authorized_group = $val['fe_group'];
			} else {
				$queryParts = array(
					'SELECT' => 'project_id',
					'FROM' => 'tx_nG6_project_analyze',
					'WHERE' => 'analyze_id='.$uid,
					'GROUPBY' => '',
					'ORDERBY' => '',
					'LIMIT' => ''
				);
				$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
				$val = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
				$queryParts = array(
					'SELECT' => 'fe_group',
					'FROM' => 'tx_nG6_project',
					'WHERE' => 'uid='.$val['project_id'],
					'GROUPBY' => '',
					'ORDERBY' => '',
					'LIMIT' => ''
				);
				$res2 = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
				$val = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res2);
				$authorized_group = $val['fe_group'];
			}
Jerome Mariette's avatar
Jerome Mariette committed
1158
1159
1160
1161
1162
1163
			
		} else {
			return true;
		}
		
		if (!$GLOBALS['TSFE']->loginUser) { // If the user is not logged on
1164
			return false;
Jerome Mariette's avatar
Jerome Mariette committed
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
		} else {
			
			// super groups string => supergroup1,supergroup2,...
			$all_list = tx_nG6_db::get_all_super_groups_of($authorized_group,'all');
			$groups_str='';
			foreach ($all_list as $asd) { $groups_str .= $asd.','; }
			$effective_supergroups = substr($groups_str, 0, -1); //delete the last comma
			
			
			if (strlen($effective_supergroups) == 0) {
				$is_super_user = true; 		// means : current user is a super user (no super groups exists)
			} else {
				$is_super_user = in_array($user_group, preg_split("/,/", $effective_supergroups));
			}
Jerome Mariette's avatar
Jerome Mariette committed
1179

Jerome Mariette's avatar
Jerome Mariette committed
1180
1181
1182
1183
			return $is_super_user;
		}
    }

1184
1185
1186
1187
1188
1189
1190
}

if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/nG6/class.tx_nG6_db.php']) {
    include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/nG6/class.tx_nG6_db.php']);
}

?>