class.tx_nG6_db.php 42.3 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
<?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 {
Penom Nom's avatar
Penom Nom committed
34
35
36
37
38
39
40
41
	
	/*
	 * Global functions
	*------------------------------------------------------------*/
	
	/**
	* Update the specified field of the table
	 *
42
	* @param	string  $table the entity to change
Penom Nom's avatar
Penom Nom committed
43
44
45
46
	* @param	string  $id     the element id to update
	* @param	string  $field  the field to change
	* @param	string  $value  the new value
	*/
47
	function update_field($table, $id, $field, $value) {
48
49
50
		if( $field == 'uid' ) {
			throw new Exception ("The function 'update_field' cannot change an id.") ;
		}	
51
		$GLOBALS['TYPO3_DB']-> exec_UPDATEquery ($table, 'uid='.$id, array($field => $value));
52
	}
Penom Nom's avatar
Penom Nom committed
53
54
55
	
	
	
56
57
58
59
60
61
	/*
	 * Project functions
	 *------------------------------------------------------------*/

	/**
	 * Select all project for the current user
62
	 * 
63
	 * @param	string	$user_id	the user id
64
65
	 * @return	table with all projects
	 */
66
	function select_all_user_projects($user_id, $orderby='', $limit='') {		
67
68
		$projects = array();
		// If the user is not logged on display demonstration project
69
		if (!$GLOBALS['TSFE']->loginUser) { 
70
			$where = 'tx_nG6_project.public=0';
71
		} else {
72
			$where = 'fe_rights.fe_user_id='.$user_id;	
73
74
		}
		//First, get the analyzis project-scale
75
		$queryParts = array(
76
77
78
			'SELECT' => 'tx_nG6_project.uid AS project_id, '.
						'tx_nG6_project.name AS project_name, '.
						'tx_nG6_project.description AS project_description, '.
79
						'tx_nG6_project.public AS project_public, '.
Jerome Mariette's avatar
Jerome Mariette committed
80
						'tx_nG6_project.crdate AS project_crdate, '.
81
82
						'tx_nG6_project.hidden AS project_hidden ',
			'FROM' => 'tx_nG6_project INNER JOIN fe_rights ON fe_rights.project_id=tx_nG6_project.uid',
83
84
			'WHERE' => $where,
			'GROUPBY' => '',
Jerome Mariette's avatar
Jerome Mariette committed
85
86
			'ORDERBY' => $orderby,
			'LIMIT' => $limit
87
		);
88
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);		
89
90
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			$project_id = $row['project_id'];
91
			
92
93
			// Is the user allowed to see this project
			$user_allowed = false;
94
			if ($row['project_hidden'] == 0 || $row['project_public'] == 0) {
95
				$user_allowed = true;
96
			} 
97
98
			
			if (!isset ($projects['project_'.$project_id]) and $user_allowed) {		
99
				$projects['project_'.$project_id] = array(
100
					'id'			=> $project_id,				
101
					'name'			=> $row['project_name'],
Jerome Mariette's avatar
Jerome Mariette committed
102
					'hidden'		=> $row['project_hidden'],
Jerome Mariette's avatar
Jerome Mariette committed
103
					'date'			=> $row['project_crdate'],
104
					'public'		=> $row['project_public'],
105
106
					'description'	=> $row['project_description']);
			}
107
						
108
		}		
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
		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
128
129
130
131
		$vals = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
		return array('id'			=> $vals['uid'],				
					 'name'			=> $vals['name'],
					 'hidden'		=> $vals['hidden'],
Jerome Mariette's avatar
Jerome Mariette committed
132
					 'date'			=> $vals['crdate'],
Jerome Mariette's avatar
Jerome Mariette committed
133
134
					 'public'		=> $vals['public'],
					 'description'	=> $vals['description']);
135
136
137
138
139
	}

	/**
	 * Select all runs linked to the specified project
	 *
140
	 * @param	string	$user_id	the user id
141
142
143
	 * @param	string	$project_id	the project id
	 * @return	hash table with all runs information
	 */
144
    function get_project_runs($user_id, $project_id, $orderby='', $limit='') {
145
146
147
148
149
150
151
152
153
    	
    	// where clause
    	if (!$GLOBALS['TSFE']->loginUser) {
    		$where = 'tx_nG6_project.public=0';
    	} else {
    		$where = 'fe_rights.fe_user_id='.$user_id;
    	}
    	$where .= ' AND tx_nG6_project.uid='.$project_id;
    	
154
		// First select all runs from the database
155
		$queryParts = Array( 
156
157
158
159
			'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
160
161
162
						'tx_nG6_run.data_nature AS run_data_nature, '.
						'tx_nG6_run.sequencer AS run_sequencer, '.
						'tx_nG6_run.type AS run_type, '.
163
						'tx_nG6_run.name AS run_name, '.
Jerome Mariette's avatar
Jerome Mariette committed
164
165
						'tx_nG6_run.nb_sequences AS run_nb_sequences, '.
						'tx_nG6_run.full_seq_size AS run_full_seq_size, '.
166
167
						'tx_nG6_run.date AS run_date, '.
						'tx_nG6_run.hidden AS run_hidden, '.
168
169
170
171
172
173
						'tx_nG6_project.uid AS project_id, '.
						'tx_nG6_project.name AS project_name ',
			'FROM' => 'fe_rights '.
  						'INNER JOIN tx_nG6_project ON fe_rights.project_id=tx_nG6_project.uid '.
  						'INNER JOIN tx_nG6_project_run ON  tx_nG6_project.uid=tx_nG6_project_run.project_id '.
  						'INNER JOIN tx_nG6_run ON tx_nG6_project_run.run_id=tx_nG6_run.uid ',
174
			'WHERE' => $where,
175
			'GROUPBY' => '',
Jerome Mariette's avatar
Jerome Mariette committed
176
177
			'ORDERBY' => $orderby,
			'LIMIT' 	=> $limit
178
		); 
179
180
181
182
183
		// Then create the result hash table
		$results = array();
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			$run_id = $row['run_id'];
184
			
185
186
187
188
			// Is the user allowed to see this run
			$user_allowed = false;
			if ($row['run_hidden'] == 0) {
				$user_allowed = true;
189
190
			} 
								
191
192
			if (!isset($results['run_'.$run_id]) and $user_allowed) {				
				$results['run_'.$run_id] = array( 
Jerome Mariette's avatar
Jerome Mariette committed
193
					'id'			=> $run_id,
194
					'directory'		=> $row['run_directory'], 
195
					'name' 			=> $row['run_name'],
Jerome Mariette's avatar
Jerome Mariette committed
196
197
					'project_name' 	=> $row['project_name'],
					'hidden'		=> $row['run_hidden'],
198
					'species' 		=> $row['run_species'],
Jerome Mariette's avatar
Jerome Mariette committed
199
200
201
					'nb_sequences' 	=> $row['run_nb_sequences'],
					'full_seq_size' => $row['run_full_seq_size'],
					'project_id' 	=> $row['project_id'],
202
					'date'			=> $row['run_date'],
Jerome Mariette's avatar
Jerome Mariette committed
203
204
205
					'data_nature'	=> $row['run_data_nature'],
					'sequencer'		=> $row['run_sequencer'],
					'type'			=> $row['run_type'],
Jerome Mariette's avatar
Jerome Mariette committed
206
					'description' 	=> $row['run_description']
207
				);
208
			}						
209
210
211
212
213
214
215
		}
		return $results;
    }

	/**
	 * Select all analysis linked to the specified project
	 *
216
	 * @param	string	$user_id	the user group
217
218
219
	 * @param	string	$project_id	the project id
	 * @return	hash table with all analysis information
	 */
220
    function get_project_analysis($user_id, $project_id, $orderby='', $limit='') {
221
222
223
224
225
226
227
228
229
230
		
    	// where clause
    	if (!$GLOBALS['TSFE']->loginUser) {
    		$where = 'tx_nG6_project.public=0';
    	} else {
    		$where = 'fe_rights.fe_user_id='.$user_id;
    	}
    	$where .= ' AND tx_nG6_project.uid='.$project_id;
    	
    	// First select all analysis from the database    	
231
    	$queryParts = array( 
232
233
			'SELECT' => 'tx_nG6_analyze.uid AS analyze_id,'. 
						'tx_nG6_analyze.directory AS analyze_directory,'. 
234
						'tx_nG6_analyze.name AS analyze_name, '.
235
						'tx_nG6_analyze.class AS analyze_class, '.
236
						'tx_nG6_analyze.date AS analyze_date, '.
Jerome Mariette's avatar
Jerome Mariette committed
237
238
						'tx_nG6_analyze.software AS analyze_software, '.
						'tx_nG6_analyze.version AS analyze_version, '.
239
						'tx_nG6_analyze.params AS analyze_params, '.
240
						'tx_nG6_analyze.description AS analyze_description, '.
Jerome Mariette's avatar
Jerome Mariette committed
241
						'tx_nG6_analyze.parent_uid AS analyze_parent_uid, '.
242
243
244
245
246
						'tx_nG6_analyze.hidden AS analyze_hidden ',
			'FROM' => 'fe_rights '.
						'INNER JOIN tx_nG6_project ON fe_rights.project_id=tx_nG6_project.uid '.
						'INNER JOIN tx_nG6_project_analyze ON tx_nG6_project.uid = tx_nG6_project_analyze.project_id '.
						'INNER JOIN tx_nG6_analyze ON tx_nG6_analyze.uid=tx_nG6_project_analyze.analyze_id ',
247
			'WHERE' => $where,
248
			'GROUPBY' => '',
Jerome Mariette's avatar
Jerome Mariette committed
249
250
			'ORDERBY' => $orderby,
			'LIMIT' => $limit
251
252
253
254
255
256
		);
		// Then create the result hash table
		$results = array();
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			$analyze_id = $row['analyze_id'];
257
			
258
259
260
261
262
			// Is the user allowed to see this analyse
			$user_allowed = false;
			if ($row['analyze_hidden'] == 0) {
				$user_allowed = true;
			}
263
264
265
266
			
			if (!isset ($results['analyse_'.$analyze_id]) and $user_allowed) {				
				$results['analyse_'.$analyze_id] = array( 
					'directory'		=> $row['analyze_directory'], 
267
					'name' 			=> $row['analyze_name'],
268
					'class'			=> $row['analyze_class'],
Jerome Mariette's avatar
Jerome Mariette committed
269
270
					'id' 			=> $row['analyze_id'],
					'hidden' 		=> $row['analyze_hidden'],
271
					'params' 		=> $row['analyze_params'],
Jerome Mariette's avatar
Jerome Mariette committed
272
273
					'software' 		=> $row['analyze_software'],
					'version' 		=> $row['analyze_version'],
274
					'date'			=> $row['analyze_date'],
Jerome Mariette's avatar
Jerome Mariette committed
275
276
					'description' 	=> $row['analyze_description'],
					'parent_id'		=> $row['analyze_parent_uid']	
277
				);
278
			}						
279
280
281
282
		}
		return $results;
    }

283
284
285
286
    /**
     * Hide a project and sublevels (runs & analyzes) datas from database project
     * 
     * @param	string	$p_id	the project id to hide
287
     * @param	string	$user_id	the current user id
288
     */
289
	function hide_project($p_id, $user_id) {
290
291
		
		// First change all project runs
292
		$project_runs = tx_nG6_db::get_project_runs($user_id, $p_id);
293
		foreach($project_runs as $run_id => $run_values) {
294
			tx_nG6_db::hide_run($run_values['id'], $user_id);
295
296
297
		}

		// Then all project analysis
298
    	$project_analysis = tx_nG6_db::get_project_analysis($user_id, $p_id);
299
300
301
302
303
304
305
306
307
308
309
310
		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
311
     * @param	string	$user_id	the current user id
312
     */
313
	function unhide_project($p_id, $user_id) {
314
315
		
		// First change all project runs
316
		$project_runs = tx_nG6_db::get_project_runs($user_id, $p_id);
317
		foreach($project_runs as $run_id => $run_values) {
318
			tx_nG6_db::unhide_run($run_values['id'], $user_id);
319
320
321
		}

		// Then all project analysis
322
    	$project_analysis = tx_nG6_db::get_project_analysis($user_id, $p_id);
323
324
325
326
327
328
329
330
331
332
333
334
		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
335
     * @param	int 	$user_id	the current user id
336
     */
337
	function publish_project($p_id, $user_id) {
338
		// First unhide the project
339
		tx_nG6_db::unhide_project($p_id, $user_id);
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
		// 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
     * 
356
     * @param string $user_id		the user id
357
358
359
360
361
     * @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
     */
362
	function delete_project($user_id, $p_id, $user_login, $user_pwd, $data_folder) {
363
364
365
		
		$res = 0;
		// All runs
366
		$project_runs = tx_nG6_db::get_project_runs($user_id, $p_id);
367
368
369
		foreach($project_runs as $run_id => $run_values) {
			if ($run_values['id'] != 'undefined' && $run_values['id'] != '') {
				//delete the run sublevels
370
				$res = tx_nG6_db::delete_run($user_id, $run_values['id'], $user_login, $user_pwd, $data_folder);
371
372
373
374
375
376
				if ($res != 0) { break; }				
			}
		}
		
		// All project_analysis
		if ($res == 0) {
377
			$project_analysis = tx_nG6_db::get_project_analysis($user_id, $p_id);
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
			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;
	}

398
399
400
401
402
    /**
     * Return the size of the project
     * 
     * @param	string	$p_id	the project id to return the size
	 * @param	string	$data_folder	the data folder
403
	 * @param	string	$user_id	the user id
404
     */
405
	function get_project_size($p_id, $data_folder, $user_id) {
406
407
408
		
		$full_size = 0;
		// All runs
409
		$project_runs = tx_nG6_db::get_project_runs($user_id, $p_id);
410
		foreach($project_runs as $run_id => $run_values) {
411
			$full_size += tx_nG6_db::get_run_size($run_values['id'], $data_folder, $user_id);
412
413
		}
		// All analysis
414
		$project_analysis = tx_nG6_db::get_project_analysis($user_id, $p_id);
415
416
417
418
419
420
		foreach($project_analysis as $analyse_id => $analyze_values) {  
			$full_size += tx_nG6_db::get_analysis_size($analyze_values['id'], $data_folder);
		}
		return $full_size;
	}

421
422
423
424

	/*
	 * Run functions
	 *------------------------------------------------------------*/
425
426
	
	
Jerome Mariette's avatar
Jerome Mariette committed
427
428
	/**
	 * Select all run for the specified user
429
	 * 
430
	 * @param	string	$user_id	the user id
Jerome Mariette's avatar
Jerome Mariette committed
431
432
	 * @return	table with all projects
	 */
433
	function select_all_user_runs($user_id, $orderby='', $limit='') {
434

Jerome Mariette's avatar
Jerome Mariette committed
435
		// If the user is not logged on display demonstration project
436
		if (!$GLOBALS['TSFE']->loginUser) { 
437
			$where = 'tx_nG6_project.public=0';
Jerome Mariette's avatar
Jerome Mariette committed
438
		} else {
439
			$where = 'fe_rights.fe_user_id='.$user_id;
Jerome Mariette's avatar
Jerome Mariette committed
440
441
		}
     	// Execute the request
442
		$queryParts = Array( 
Jerome Mariette's avatar
Jerome Mariette committed
443
444
445
446
			'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, '.
447
						'tx_nG6_run.data_nature AS run_data_nature, '. 
Jerome Mariette's avatar
Jerome Mariette committed
448
449
450
451
452
453
454
455
						'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, '.
456
457
458
459
460
						'tx_nG6_project.name AS project_name ',
			'FROM' => 'fe_rights '.
						'INNER JOIN tx_nG6_project ON tx_nG6_project.uid=fe_rights.project_id '.
						'INNER JOIN tx_nG6_project_run ON tx_nG6_project.uid=tx_nG6_project_run.project_id '.
						'INNER JOIN tx_nG6_run ON tx_nG6_project_run.run_id=tx_nG6_run.uid ',
Jerome Mariette's avatar
Jerome Mariette committed
461
462
463
464
			'WHERE' => $where,
			'GROUPBY' => '',
			'ORDERBY' => $orderby,
			'LIMIT' 	=> $limit
465
		); 
Jerome Mariette's avatar
Jerome Mariette committed
466
467
468
469
470
		// Then create the result hash table
		$results = array();
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			$run_id = $row['run_id'];
471
			
Jerome Mariette's avatar
Jerome Mariette committed
472
473
474
475
476
			// Is the user allowed to see this run
			$user_allowed = false;
			if ($row['run_hidden'] == 0) {
				$user_allowed = true;
			}
477
478
					
			if (!isset($results['run_'.$run_id]) and $user_allowed) {				
Jerome Mariette's avatar
Jerome Mariette committed
479
				$results['run_'.$run_id] = array(
480
481
					'id'			=> $run_id,  
					'directory'		=> $row['run_directory'], 
Jerome Mariette's avatar
Jerome Mariette committed
482
483
484
485
486
487
488
489
490
491
492
					'name' 			=> $row['run_name'],
					'project_name' 	=> $row['project_name'],
					'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'],
493
					'description' 	=> $row['run_description']				
Jerome Mariette's avatar
Jerome Mariette committed
494
				);
495
			}						
Jerome Mariette's avatar
Jerome Mariette committed
496
497
498
499
500
		}
		return $results;

	}

501
502
503
504
505
506
	/**
	 * 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
507
	function select_run($run_id) {
508
		
509
		// First select the run from the database
510
		$queryParts = Array( 
511
512
513
514
515
			'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
516
						'tx_nG6_run.species AS run_species, '.
517
						'tx_nG6_run.name AS run_name, '.
Jerome Mariette's avatar
Jerome Mariette committed
518
519
520
521
						'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
522
						'tx_nG6_run.hidden AS run_hidden, '.
523
524
						'tx_nG6_run.nb_sequences, '.
						'tx_nG6_run.date AS run_date',
525
526
527
528
			'FROM' => 'fe_rights '.
						'INNER JOIN tx_nG6_project ON tx_nG6_project.uid=fe_rights.project_id '.
						'INNER JOIN tx_nG6_project_run ON tx_nG6_project.uid=tx_nG6_project_run.project_id '.
						'INNER JOIN tx_nG6_run ON tx_nG6_project_run.run_id=tx_nG6_run.uid ',
529
530
531
532
			'WHERE' => 'tx_nG6_run.uid='.$run_id,
			'GROUPBY' => '',
			'ORDERBY' => 'tx_nG6_run.date DESC',
			'LIMIT' 	=> ''
533
		); 
534
535
536
		// Then create the result hash table
		$result = array();
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
537
538
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {			
			$result = array( 
Jerome Mariette's avatar
Jerome Mariette committed
539
				'id'			=> $row['run_id'],  
540
541
542
				'project_id'	=> $row['project_id'], 
				'project_name'	=> $row['project_name'], 
				'directory'		=> $row['run_directory'], 
Jerome Mariette's avatar
Jerome Mariette committed
543
				'name' 			=> $row['run_name'],
Jerome Mariette's avatar
Jerome Mariette committed
544
				'hidden'		=> $row['run_hidden'],
Jerome Mariette's avatar
Jerome Mariette committed
545
546
				'species' 		=> $row['run_species'],
				'nb_sequences' 	=> $row['nb_sequences'],
Jerome Mariette's avatar
Jerome Mariette committed
547
				'full_seq_size' => $row['full_seq_size'],
Jerome Mariette's avatar
Jerome Mariette committed
548
				'date'			=> $row['run_date'],
Jerome Mariette's avatar
Jerome Mariette committed
549
550
551
				'data_nature' 	=> $row['run_data_nature'],
				'sequencer' 	=> $row['run_sequencer'],
				'type' 			=> $row['run_type'],
552
553
				'description' 	=> $row['run_description']				
			);					
554
555
556
		}
		return $result;
    }
557

558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
	/**
	 * 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;
		
	}	

585
586
587
	/**
	 * Select all analysis linked to the specified run
	 *
588
	 * @param	int	$user_id	the user id
589
590
591
	 * @param	string	$run_id	the run id
	 * @return	hash table with all analysis information
	 */
592
    function get_run_analysis($user_id, $run_id, $orderby='', $limit='') {
593
		// First select all analysis from the database
594
595
596
		$queryParts = array( 
			'SELECT' => 'tx_nG6_analyze.uid AS analyze_id,'. 
						'tx_nG6_analyze.directory AS analyze_directory,'. 
597
						'tx_nG6_analyze.name AS analyze_name, '.
598
599
						'tx_nG6_analyze.params AS analyze_params, '.
						'tx_nG6_analyze.class AS analyze_class, '.
600
						'tx_nG6_analyze.date AS analyze_date, '.
Jerome Mariette's avatar
Jerome Mariette committed
601
602
						'tx_nG6_analyze.software AS analyze_software, '.
						'tx_nG6_analyze.version AS analyze_version, '.
603
604
						'tx_nG6_analyze.hidden AS analyze_hidden, '.
						'tx_nG6_analyze.description AS analyze_description, '.
605
606
607
608
609
610
611
612
						'tx_nG6_analyze.parent_uid AS analyze_parent_uid ',
			'FROM' => 'fe_rights '.
						'INNER JOIN tx_nG6_project ON tx_nG6_project.uid=fe_rights.project_id '.
						'INNER JOIN tx_nG6_project_run ON tx_nG6_project_run.project_id = tx_nG6_project.uid '.
						'INNER JOIN tx_nG6_run ON tx_nG6_run.uid = tx_nG6_project_run.run_id '.
						'INNER JOIN tx_nG6_run_analyze ON tx_nG6_run_analyze.run_id = tx_nG6_run.uid '.
						'INNER JOIN tx_nG6_analyze ON tx_nG6_analyze.uid = tx_nG6_run_analyze.analyze_id ',
			'WHERE' => 'tx_nG6_run_analyze.run_id='.$run_id.' AND fe_rights.fe_user_id='.$user_id,
613
			'GROUPBY' => '',
Jerome Mariette's avatar
Jerome Mariette committed
614
615
			'ORDERBY' => $orderby,
			'LIMIT' => $limit
616
617
618
619
620
621
		);
		// Then create the result hash table
		$results = array();
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			$analyze_id = $row['analyze_id'];
622
			
623
624
625
626
			// Is the user allowed to see this analyse
			$user_allowed = false;
			if ($row['analyze_hidden'] == 0) {
				$user_allowed = true;
627
			} 
628
629
630
631
			
			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
632
					'name' 			=> $row['analyze_name'],
633
634
					'params' 		=> $row['analyze_params'],
					'class' 		=> $row['analyze_class'],
Jerome Mariette's avatar
Jerome Mariette committed
635
636
637
638
639
					'id' 			=> $row['analyze_id'],
					'hidden' 		=> $row['analyze_hidden'],
					'software' 		=> $row['analyze_software'],
					'version' 		=> $row['analyze_version'],
					'date'			=> $row['analyze_date'],
Jerome Mariette's avatar
Jerome Mariette committed
640
641
					'description' 	=> $row['analyze_description'],
					'parent_id'		=> $row['analyze_parent_uid']
642
				);
643
			}						
644
645
646
647
		}
		return $results;
    }

648
649
650
	/**
	 * Delete a run, sample and sublevel (analyzes) datas from database project
	 * 
651
	 * @param int 	$user_id	the user id
652
653
654
655
656
	 * @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
	 */
657
	function delete_run($user_id, $r_id, $user_login, $user_pwd, $data_folder) {		
658
659

		$res = 0;		
660
		$run_analysis = tx_nG6_db::get_run_analysis($user_id, $r_id);
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
698
		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;
	}

699
700
701
702
703
    /**
     * Return the size of the run
     * 
     * @param	string	$r_id	the run id to return the size
	 * @param	string	$data_folder	the data folder
704
	 * @param	int	$user_id	the user id
705
     */
706
	function get_run_size($r_id, $data_folder, $user_id) {
707
		$full_size = 0;
708
		$run_analysis = tx_nG6_db::get_run_analysis($user_id, $r_id);
709
710
711
712
713
714
715
716
717
718
719
720
721
		foreach($run_analysis as $analyse_id => $analyze_values) {
			$full_size += tx_nG6_db::get_analysis_size($analyze_values['id'], $data_folder);
		}
		// Then select the run
		$my_run = tx_nG6_db::select_run($r_id);
		foreach(scandir($data_folder.$my_run['directory']) as $file) {
			if (is_file($data_folder.$my_run['directory']."/".$file)) {
				$full_size += filesize($data_folder.$my_run['directory']."/".$file);
			}
		}
		return $full_size;
	}

722
723
724
725
    /**
     * Hide a run and sublevels (analysis)
     * 
     * @param	string	$r_id	the run id to hide
726
     * @param	int	$user_id	the current user id
727
     */
728
	function hide_run($r_id, $user_id) {
729
730

		// First select all run analysis
731
    	$run_analysis = tx_nG6_db::get_run_analysis($user_id, $r_id);
732
733
734
735
736
737
738
739
740
741
742
743
		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
744
     * @param	int	$user_id	the current user id
745
     */
746
	function unhide_run($r_id, $user_id) {
747
748

		// Then all run analysis
749
    	$run_analysis = tx_nG6_db::get_run_analysis($user_id, $r_id);
750
751
		foreach($run_analysis as $analysis_id => $analysis_values) {  
			tx_nG6_db::unhide_analysis($analysis_values['id']);
752
753
754
755
756
757
758
		}		
		
		// Unhide the project
		$GLOBALS['TYPO3_DB']-> exec_UPDATEquery (
			'tx_nG6_project, tx_nG6_project_run',
			'tx_nG6_project.uid = tx_nG6_project_run.project_id AND tx_nG6_project_run.run_id = '.$r_id ,
			array('tx_nG6_project.hidden' => '0')
759
		);
760
761
762
763
		
		// Finaly change the run itself
		$GLOBALS['TYPO3_DB']-> exec_UPDATEquery ('tx_nG6_run', 'uid='.$r_id, array('hidden' => '0'));
	}
764
765

	/*
766
	 * Analysis functions
767
	 *------------------------------------------------------------*/
768
	 
769
770
771
772
773
774
	/**
	 * 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
775
776
    function select_analyse($analyse_id) {

777
		// First select all analyse information
778
779
780
781
782
		$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,'. 
783
						'tx_nG6_analyze.name AS analyze_name, '.
784
						'tx_nG6_analyze.params AS analyze_params, '.
Jerome Mariette's avatar
Jerome Mariette committed
785
						'tx_nG6_analyze.software AS analyze_software, '.
786
						'tx_nG6_analyze.class AS analyze_class, '.
787
						'tx_nG6_analyze.date AS analyze_date, '.
Jerome Mariette's avatar
Jerome Mariette committed
788
789
790
						'tx_nG6_analyze.description AS analyze_description, '.
						'tx_nG6_analyze.version AS analyze_version, '.
						'tx_nG6_analyze.parent_uid AS analyze_parent_uid ',
791
792
793
			'FROM' => 'tx_nG6_project '.
						'INNER JOIN tx_nG6_project_analyze ON tx_nG6_project.uid = tx_nG6_project_analyze.project_id '.
						'INNER JOIN tx_nG6_analyze ON tx_nG6_analyze.uid=tx_nG6_project_analyze.analyze_id ',
794
795
796
797
798
			'WHERE' => 'tx_nG6_analyze.uid='.$analyse_id,
			'GROUPBY' => '',
			'ORDERBY' => '',
			'LIMIT' => ''
		);
799
		
800
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
801
		
802
803
804
		// If it's a project analyse, add project information
		if ($GLOBALS['TYPO3_DB']->sql_num_rows($res) > 0) {
			$result = array();
805
806
807
808
809
810
811
812
813
814
815
			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'],
Jerome Mariette's avatar
Jerome Mariette committed
816
817
818
819
					'description' 	=> $row['analyze_description'],
					'parent_id'		=> $row['analyze_parent_uid'],
					'software'		=> $row['analyze_software'],
					'version'		=> $row['analyze_version']				
820
				);				
821
822
823
			}
			return $result;
		} else { // If it's a run analyse, add run information
824
			$queryParts = array( 
825
826
				'SELECT' => 'tx_nG6_project.name AS project_name,'.
							'tx_nG6_project.uid AS project_id,'.
827
							'tx_nG6_run.uid AS run_id,'. 
828
							'tx_nG6_run.name AS run_name,'.
829
830
							'tx_nG6_analyze.uid AS analyze_id,'. 
							'tx_nG6_analyze.directory AS analyze_directory,'. 
831
							'tx_nG6_analyze.name AS analyze_name, '.
832
833
							'tx_nG6_analyze.params AS analyze_params, '.
							'tx_nG6_analyze.class AS analyze_class, '.
834
							'tx_nG6_analyze.date AS analyze_date, '.
Jerome Mariette's avatar
Jerome Mariette committed
835
836
837
838
							'tx_nG6_analyze.software AS analyze_software, '.
							'tx_nG6_analyze.version AS analyze_version, '.
							'tx_nG6_analyze.description AS analyze_description,'.
							'tx_nG6_analyze.parent_uid AS analyze_parent_uid ',
839
840
841
842
843
				'FROM' => 'tx_nG6_project '.
							'INNER JOIN tx_nG6_project_run ON tx_nG6_project_run.project_id = pro.uid '.
							'INNER JOIN tx_nG6_run ON tx_nG6_run.uid = tx_nG6_project_run.run_id '.
							'INNER JOIN tx_nG6_run_analyze ON tx_nG6_run_analyze.run_id = tx_nG6_run.uid '.
							'INNER JOIN tx_nG6_analyze ON tx_nG6_analyze.uid = tx_nG6_run_analyze.analyze_id ',
844
845
846
847
848
849
850
				'WHERE' => 'tx_nG6_analyze.uid='.$analyse_id,
				'GROUPBY' => '',
				'ORDERBY' => 'tx_nG6_analyze.name',
				'LIMIT' => ''
			);
			$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
			$result = array();
851
852
853
854
855
856
857
858
859
860
861
			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'],
Jerome Mariette's avatar
Jerome Mariette committed
862
863
864
865
					'description' 	=> $row['analyze_description'],
					'software'		=> $row['analyze_software'],
					'parent_id'		=> $row['analyze_parent_uid'],
					'version'		=> $row['analyze_version']			
866
				);				
867
868
869
870
			}
			return $result;
		}
    }
871

872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
	/**
	 * 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;
    }
905

906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
    /**
     * Return the size of the analysis
     * 
     * @param	string	$a_id	the analysis id to return the size
	 * @param	string	$data_folder	the data folder
     */
	function get_analysis_size($a_id, $data_folder) {
		// First select the analyse
		$my_analysis = tx_nG6_db::select_analyse($a_id);
		$full_size = 0;
		foreach(scandir($data_folder.$my_analysis['directory']) as $file) {
			if (is_file($data_folder.$my_analysis['directory']."/".$file)) {
				$full_size += filesize($data_folder.$my_analysis['directory']."/".$file);
			}
		}
		return $full_size;
	}

924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
    /**
     * 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
     */
	function unhide_analysis($a_id) {
940
		$queryParts = array(
941
942
943
944
945
946
			'SELECT'  => 'tx_nG6_project_analyze.uid',
			'FROM'    => 'tx_nG6_project_analyze',
			'WHERE'   => 'tx_nG6_project_analyze.analyze_id = '.$a_id,
			'GROUPBY' => '',
			'ORDERBY' => '',
			'LIMIT'   => ''
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
		);
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts) ;
		$is_project_analyze = $GLOBALS['TYPO3_DB']->sql_num_rows( $res ) ;
		
		// If the analyse isn't link to run
		if( $is_project_analyze ) 
		{
			// unhide the project
			$GLOBALS['TYPO3_DB']-> exec_UPDATEquery (
				'tx_nG6_project, tx_nG6_project_analyze',
				'tx_nG6_project.uid = tx_nG6_project_analyze.project_id AND tx_nG6_project_analyze.analyze_id = '.$a_id ,
				array('tx_nG6_project.hidden' => '0')
			) ;
		}
		// If the analyse is link to run
		else
		{
			// unhide the project and the run
			$GLOBALS['TYPO3_DB']->exec_UPDATEquery (
				'tx_nG6_project, tx_nG6_project_run, tx_nG6_run, tx_nG6_run_analyze', 
967
968
969
970
				'tx_nG6_project.uid = tx_nG6_project_run.project_id '.
					'AND tx_nG6_project_run.run_id = tx_nG6_run.uid '.
					'AND tx_nG6_run.uid = tx_nG6_run_analyze.run_id '.
					'AND tx_nG6_run_analyze.analyze_id = '.$a_id ,
971
972
973
974
				array('tx_nG6_project.hidden' => '0', 'tx_nG6_run.hidden' => '0')
			) ;
		}

975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
		// 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;
	}

1022
1023
1024
1025

	/*
	 * User functions
	 *------------------------------------------------------------*/
1026
	
1027
1028
1029
	/**
	 * Return if the user is authorized or not to access the specified project and run
	 *
1030
	 * @param	int	$user_id	the user id
1031
1032
1033
1034
	 * @param	string	$project_id	the project id
	 * @param	string	$project_id	the run id
	 * @return	boolean
	 */
1035
	function user_is_authorized ($user_id, $project_id, $run_id) {
1036
		$authorized = false;
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
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
		
		// public project ?
		$queryParts = array(
			'SELECT' => 'public',
			'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);
		$authorized = ($val['public'] == 0);
		
		// not a public project
		if(!$authorized){
		
			// authorized in project ?
			if ($project_id) {			
				$queryParts = array(
					'SELECT' => 'fe_rights.right_id ',
					'FROM' => 'fe_rights INNER JOIN tx_nG6_project ON tx_nG6_project.uid=fe_rights.project_id ',
					'WHERE' => 'fe_rights.fe_user_id='.$user_id.' AND tx_nG6_project.uid='.$project_id,
					'GROUPBY' => '',
					'ORDERBY' => '',
					'LIMIT' => ''
				);
				$res1 = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
				$val = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res1);
				
				if(isset($val['right_id'])){
					$authorized = true;
				}
				
			// authorized in run ?
			} elseif ($run_id) {
				$queryParts = array(
					'SELECT' => 'fe_rights.right_id ',
					'FROM' => 'fe_rights '.
							'INNER JOIN tx_nG6_project ON tx_nG6_project.uid=fe_rights.project_id '.
							'INNER JOIN tx_nG6_project_run ON tx_nG6_project.uid=tx_nG6_project_run.project_id ',
					'WHERE' => 'fe_rights.fe_user_id='.$user_id.' AND tx_nG6_project_run.uid='.$run_id,
					'GROUPBY' => '',
					'ORDERBY' => '',
					'LIMIT' => ''
				);
				$res1 = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
				$val = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res1);
				
				if(isset($val['right_id'])){
					$authorized = true;
				}
				
			} 
1091
		}
1092
		return $authorized;
1093
	}
Jerome Mariette's avatar
Jerome Mariette committed
1094
1095

	/**
1096
	 * Return if the user is a superuser (administrator) for the specified project/run/analyse
Jerome Mariette's avatar
Jerome Mariette committed
1097
	 *
1098
	 * @param	string	$user_id		the user id
Jerome Mariette's avatar
Jerome Mariette committed
1099
	 * @param	string	$type			can be project/run/analyse
1100
	 * @param	string	$uid			the [project/run/analyse]'s uid
Jerome Mariette's avatar
Jerome Mariette committed
1101
1102
	 * @return	boolean {1 = ok, 0 = nok}
	 */
1103
    function is_administrator($user_id, $type, $uid) {
Jerome Mariette's avatar
Jerome Mariette committed
1104
    	
1105
1106
1107
1108
1109
1110
1111
    	$is_admin = false;
    	
    	if($GLOBALS['TSFE']->loginUser){
    	
	    	// Project
	   		if ($type == 'project') {
				$is_admin = tx_nG6_db::is_project_administrator($user_id, $uid);
Jerome Mariette's avatar
Jerome Mariette committed
1112
			
1113
1114
			// Run
			} elseif ($type == 'run') {
1115
1116
1117
				$queryParts = array(
					'SELECT' => 'project_id',
					'FROM' => 'tx_nG6_project_run',
1118
					'WHERE' => 'run_id='.$uid,
1119
1120
1121
1122
					'GROUPBY' => '',
					'ORDERBY' => '',
					'LIMIT' => ''
				);
1123
1124
1125
1126
1127
1128
				$res1 = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
				$val = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res1);
				$is_admin = tx_nG6_db::is_project_administrator($user_id, $val['project_id']);
			
			// Analyze
			} elseif ($type == 'analyze') {
1129
				$queryParts = array(
1130
1131
					'SELECT' => 'run_id',
					'FROM' => 'tx_nG6_run_analyze',
1132
1133
1134
1135
1136
1137
					'WHERE' => 'analyze_id='.$uid,
					'GROUPBY' => '',
					'ORDERBY' => '',
					'LIMIT' => ''
				);
				$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
				
				// Run_analyze
				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);
					$is_admin = tx_nG6_db::is_project_administrator($user_id, $val['project_id']);
				
				// project_analyze
				} else {
					$queryParts = array(
						'SELECT' => 'project_id',
						'FROM' => 'tx_nG6_project_analyze',
						'WHERE' => 'analyze_id='.$uid,
						'GROUPBY' => '',
						'ORDERBY' => '',
						'LIMIT' => ''
					);
					$res2 = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
					$val = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res2);
					$is_admin = tx_nG6_db::is_project_administrator($user_id, $val['project_id']);
				}
				
			} 
    	}
Jerome Mariette's avatar
Jerome Mariette committed
1171
		
1172
1173
		return $is_admin; 
    }
Jerome Mariette's avatar
Jerome Mariette committed
1174

1175
1176
1177
1178
1179
1180
1181
    /**
    * Get user's rights on project.
    *
    * @param int $project_id the project id
    * @param int $user_id the user id
    */
    function get_user_right_on_project($user_id, $project_id) {
1182
    	$right_id = -1;
1183
    	$queryParts = array(
1184
1185
    		'SELECT' => 'fe_rights.right_id AS right_id ',
    		'FROM' => 'fe_rights',
1186
1187
1188
1189
    		'WHERE' => 'fe_rights.fe_user_id='. $user_id .' AND fe_rights.project_id='.$project_id
    	);
    	$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
1190
1191
1192
    		$right_id = $row['right_id'];
    	}    	
    	return $right_id;
Jerome Mariette's avatar
Jerome Mariette committed
1193
    }
1194
1195
    
    /**
1196
     * Return true if the user is a member on the project
1197
1198
1199
1200
     *
     * @param int $user_id the user id
     * @param int $project_id ths project id
     */
1201
1202
1203
1204
1205
    function is_project_member($user_id, $project_id){
    	$is_member = false;
    	$right = tx_nG6_db::get_user_right_on_project($user_id, $project_id);
    	if ($right == 0) {
    		$is_member = true;
1206
    	}    	
1207
    	return $is_member;
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
    }
    
    /**
     * Return true if the user is a manager on the project
     *
     * @param int $user_id the user id
     * @param int $project_id ths project id
     */
    function is_project_manager($user_id, $project_id){
    	$is_manager = false;
1218
1219
    	$right = tx_nG6_db::get_user_right_on_project($user_id, $project_id);
    	if ($right == 1) {
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
    		$is_manager = true;
    	}
    	return $is_manager;
    }
    
    /**
     * Return true if the user is an administrator on the project
     *
     * @param int $user_id the user id
     * @param int $project_id ths project id
     */
    function is_project_administrator($user_id, $project_id){
    	$is_admin = false;
1233
1234
    	$right = tx_nG6_db::get_user_right_on_project($user_id, $project_id);
    	if ($right == 2) {
1235
1236
1237
    		$is_admin = true;
    	}
    	return $is_admin;
1238
1239
    }

1240
1241
1242
    
    
    /*
1243
1244
     * DB integrity functions
      *------------------------------------------------------------*/
1245
1246
    
    /**
1247
1248
    * Check the integrity of 'fe_rights_levels' table.
    */
1249
    function check_db_rights_level(){
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259