class.tx_nG6_db.php 50.2 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
	/*
	 * Project functions
	 *------------------------------------------------------------*/

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

	/**
	 * Select all runs linked to the specified project
	 *
	 * @param	string	$project_id	the project id
	 * @return	hash table with all runs information
	 */
Jerome Mariette's avatar
Jerome Mariette committed
134
    function get_project_runs($project_id, $orderby='', $limit='') {
135
    	
136
		// First select all runs from the database
137
		$queryParts = Array( 
138
139
140
141
			'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
142
143
144
						'tx_nG6_run.data_nature AS run_data_nature, '.
						'tx_nG6_run.sequencer AS run_sequencer, '.
						'tx_nG6_run.type AS run_type, '.
145
						'tx_nG6_run.name AS run_name, '.
Jerome Mariette's avatar
Jerome Mariette committed
146
147
						'tx_nG6_run.nb_sequences AS run_nb_sequences, '.
						'tx_nG6_run.full_seq_size AS run_full_seq_size, '.
148
149
						'tx_nG6_run.date AS run_date, '.
						'tx_nG6_run.hidden AS run_hidden, '.
150
151
						'tx_nG6_project.uid AS project_id, '.
						'tx_nG6_project.name AS project_name ',
Jerome Mariette's avatar
Jerome Mariette committed
152
			'FROM' => 'tx_nG6_project '.
153
154
  						'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
155
			'WHERE' => 'tx_nG6_project.uid='.$project_id,
156
			'GROUPBY' => '',
Jerome Mariette's avatar
Jerome Mariette committed
157
158
			'ORDERBY' => $orderby,
			'LIMIT' 	=> $limit
159
		); 
Jerome Mariette's avatar
Jerome Mariette committed
160
		
161
162
163
164
		// 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)) {
Jerome Mariette's avatar
Jerome Mariette committed
165
166
			$run_id = $row['run_id'];				
			if (!isset($results['run_'.$run_id])) {				
167
				$results['run_'.$run_id] = array( 
Jerome Mariette's avatar
Jerome Mariette committed
168
					'id'			=> $run_id,
169
					'directory'		=> $row['run_directory'], 
170
					'name' 			=> $row['run_name'],
Jerome Mariette's avatar
Jerome Mariette committed
171
172
					'project_name' 	=> $row['project_name'],
					'hidden'		=> $row['run_hidden'],
173
					'species' 		=> $row['run_species'],
Jerome Mariette's avatar
Jerome Mariette committed
174
175
176
					'nb_sequences' 	=> $row['run_nb_sequences'],
					'full_seq_size' => $row['run_full_seq_size'],
					'project_id' 	=> $row['project_id'],
177
					'date'			=> $row['run_date'],
Jerome Mariette's avatar
Jerome Mariette committed
178
179
180
					'data_nature'	=> $row['run_data_nature'],
					'sequencer'		=> $row['run_sequencer'],
					'type'			=> $row['run_type'],
Jerome Mariette's avatar
Jerome Mariette committed
181
					'description' 	=> $row['run_description']
182
				);
183
			}						
184
185
186
187
188
189
190
191
192
193
		}
		return $results;
    }

	/**
	 * Select all analysis linked to the specified project
	 *
	 * @param	string	$project_id	the project id
	 * @return	hash table with all analysis information
	 */
Jerome Mariette's avatar
Jerome Mariette committed
194
    function get_project_analysis($project_id, $orderby='', $limit='') {
195
196
    	
    	// First select all analysis from the database    	
197
    	$queryParts = array( 
198
199
			'SELECT' => 'tx_nG6_analyze.uid AS analyze_id,'. 
						'tx_nG6_analyze.directory AS analyze_directory,'. 
200
						'tx_nG6_analyze.name AS analyze_name, '.
201
						'tx_nG6_analyze.class AS analyze_class, '.
202
						'tx_nG6_analyze.date AS analyze_date, '.
Jerome Mariette's avatar
Jerome Mariette committed
203
204
						'tx_nG6_analyze.software AS analyze_software, '.
						'tx_nG6_analyze.version AS analyze_version, '.
205
						'tx_nG6_analyze.params AS analyze_params, '.
206
						'tx_nG6_analyze.description AS analyze_description, '.
Jerome Mariette's avatar
Jerome Mariette committed
207
						'tx_nG6_analyze.parent_uid AS analyze_parent_uid, '.
208
						'tx_nG6_analyze.hidden AS analyze_hidden ',
Jerome Mariette's avatar
Jerome Mariette committed
209
			'FROM' => 'tx_nG6_project '.
210
211
						'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 ',
Jerome Mariette's avatar
Jerome Mariette committed
212
			'WHERE' => 'tx_nG6_project.uid='.$project_id,
213
			'GROUPBY' => '',
Jerome Mariette's avatar
Jerome Mariette committed
214
215
			'ORDERBY' => $orderby,
			'LIMIT' => $limit
216
217
218
219
220
221
		);
		// 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'];
Jerome Mariette's avatar
Jerome Mariette committed
222
			if (!isset ($results['analyse_'.$analyze_id])) {				
223
224
				$results['analyse_'.$analyze_id] = array( 
					'directory'		=> $row['analyze_directory'], 
225
					'name' 			=> $row['analyze_name'],
226
					'class'			=> $row['analyze_class'],
Jerome Mariette's avatar
Jerome Mariette committed
227
228
					'id' 			=> $row['analyze_id'],
					'hidden' 		=> $row['analyze_hidden'],
229
					'params' 		=> $row['analyze_params'],
Jerome Mariette's avatar
Jerome Mariette committed
230
231
					'software' 		=> $row['analyze_software'],
					'version' 		=> $row['analyze_version'],
232
					'date'			=> $row['analyze_date'],
Jerome Mariette's avatar
Jerome Mariette committed
233
234
					'description' 	=> $row['analyze_description'],
					'parent_id'		=> $row['analyze_parent_uid']	
235
				);
236
			}						
237
238
239
240
		}
		return $results;
    }

241
242
243
244
245
    /**
     * Hide a project and sublevels (runs & analyzes) datas from database project
     * 
     * @param	string	$p_id	the project id to hide
     */
Jerome Mariette's avatar
Jerome Mariette committed
246
	function hide_project($p_id) {
247
248
		
		// First change all project runs
Jerome Mariette's avatar
Jerome Mariette committed
249
		$project_runs = tx_nG6_db::get_project_runs($p_id);
250
		foreach($project_runs as $run_id => $run_values) {
Jerome Mariette's avatar
Jerome Mariette committed
251
			tx_nG6_db::hide_run($run_values['id']);
252
253
254
		}

		// Then all project analysis
Jerome Mariette's avatar
Jerome Mariette committed
255
    	$project_analysis = tx_nG6_db::get_project_analysis($p_id);
256
257
258
259
260
261
262
263
264
265
266
267
268
		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
     */
Jerome Mariette's avatar
Jerome Mariette committed
269
	function unhide_project($p_id) {
270
271
		
		// First change all project runs
Jerome Mariette's avatar
Jerome Mariette committed
272
		$project_runs = tx_nG6_db::get_project_runs($p_id);
273
		foreach($project_runs as $run_id => $run_values) {
Jerome Mariette's avatar
Jerome Mariette committed
274
			tx_nG6_db::unhide_run($run_values['id']);
275
276
277
		}

		// Then all project analysis
Jerome Mariette's avatar
Jerome Mariette committed
278
    	$project_analysis = tx_nG6_db::get_project_analysis($p_id);
279
280
281
282
283
284
285
286
287
288
289
290
291
		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
     */
Jerome Mariette's avatar
Jerome Mariette committed
292
	function publish_project($p_id) {
293
		// First unhide the project
Jerome Mariette's avatar
Jerome Mariette committed
294
		tx_nG6_db::unhide_project($p_id);
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
		// 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 $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
     */
Jerome Mariette's avatar
Jerome Mariette committed
316
	function delete_project($p_id, $user_login, $user_pwd, $data_folder) {
317
318
319
		
		$res = 0;
		// All runs
Jerome Mariette's avatar
Jerome Mariette committed
320
		$project_runs = tx_nG6_db::get_project_runs($p_id);
321
322
323
		foreach($project_runs as $run_id => $run_values) {
			if ($run_values['id'] != 'undefined' && $run_values['id'] != '') {
				//delete the run sublevels
Jerome Mariette's avatar
Jerome Mariette committed
324
				$res = tx_nG6_db::delete_run($run_values['id'], $user_login, $user_pwd, $data_folder);
325
326
327
328
329
330
				if ($res != 0) { break; }				
			}
		}
		
		// All project_analysis
		if ($res == 0) {
Jerome Mariette's avatar
Jerome Mariette committed
331
			$project_analysis = tx_nG6_db::get_project_analysis($p_id);
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
			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;
	}

352
353
354
355
356
357
    /**
     * Return the size of the project
     * 
     * @param	string	$p_id	the project id to return the size
	 * @param	string	$data_folder	the data folder
     */
Jerome Mariette's avatar
Jerome Mariette committed
358
	function get_project_size($p_id, $data_folder) {
359
360
361
		
		$full_size = 0;
		// All runs
Jerome Mariette's avatar
Jerome Mariette committed
362
		$project_runs = tx_nG6_db::get_project_runs($p_id);
363
		foreach($project_runs as $run_id => $run_values) {
Jerome Mariette's avatar
Jerome Mariette committed
364
			$full_size += tx_nG6_db::get_run_size($run_values['id'], $data_folder);
365
366
		}
		// All analysis
Jerome Mariette's avatar
Jerome Mariette committed
367
		$project_analysis = tx_nG6_db::get_project_analysis($p_id);
368
369
370
371
372
373
		foreach($project_analysis as $analyse_id => $analyze_values) {  
			$full_size += tx_nG6_db::get_analysis_size($analyze_values['id'], $data_folder);
		}
		return $full_size;
	}

374
375
376
377

	/*
	 * Run functions
	 *------------------------------------------------------------*/
378
379
	
	
Jerome Mariette's avatar
Jerome Mariette committed
380
381
	/**
	 * Select all run for the specified user
382
	 * 
383
	 * @param	string	$user_id	the user id
Jerome Mariette's avatar
Jerome Mariette committed
384
385
	 * @return	table with all projects
	 */
386
	function select_all_user_runs($user_id, $orderby='', $limit='') {
387

Jerome Mariette's avatar
Jerome Mariette committed
388
		// If the user is not logged on display demonstration project
Jerome Mariette's avatar
Jerome Mariette committed
389
390
391
		if ($user_id == null) { $where = 'tx_nG6_project.public=0'; } 
		else { $where = 'fe_rights.fe_user_id='.$user_id; }
		
Jerome Mariette's avatar
Jerome Mariette committed
392
     	// Execute the request
393
		$queryParts = Array( 
Jerome Mariette's avatar
Jerome Mariette committed
394
395
396
397
			'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, '.
398
						'tx_nG6_run.data_nature AS run_data_nature, '. 
Jerome Mariette's avatar
Jerome Mariette committed
399
400
401
402
403
404
405
406
						'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, '.
407
408
409
410
411
						'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
412
413
414
415
			'WHERE' => $where,
			'GROUPBY' => '',
			'ORDERBY' => $orderby,
			'LIMIT' 	=> $limit
416
		); 
Jerome Mariette's avatar
Jerome Mariette committed
417
418
419
420
		// 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)) {
Jerome Mariette's avatar
Jerome Mariette committed
421
422
			$run_id = $row['run_id'];	
			if (!isset($results['run_'.$run_id])) {				
Jerome Mariette's avatar
Jerome Mariette committed
423
				$results['run_'.$run_id] = array(
424
425
					'id'			=> $run_id,  
					'directory'		=> $row['run_directory'], 
Jerome Mariette's avatar
Jerome Mariette committed
426
427
428
429
430
431
432
433
434
435
436
					'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'],
437
					'description' 	=> $row['run_description']				
Jerome Mariette's avatar
Jerome Mariette committed
438
				);
439
			}						
Jerome Mariette's avatar
Jerome Mariette committed
440
441
442
443
444
		}
		return $results;

	}

445
446
447
448
449
450
	/**
	 * 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
451
	function select_run($run_id) {
452
		
453
		// First select the run from the database
454
		$queryParts = Array( 
455
456
457
458
459
			'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
460
						'tx_nG6_run.species AS run_species, '.
461
						'tx_nG6_run.name AS run_name, '.
Jerome Mariette's avatar
Jerome Mariette committed
462
463
464
465
						'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
466
						'tx_nG6_run.hidden AS run_hidden, '.
467
468
						'tx_nG6_run.nb_sequences, '.
						'tx_nG6_run.date AS run_date',
469
470
471
472
			'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 ',
473
474
475
476
			'WHERE' => 'tx_nG6_run.uid='.$run_id,
			'GROUPBY' => '',
			'ORDERBY' => 'tx_nG6_run.date DESC',
			'LIMIT' 	=> ''
477
		); 
478
479
480
		// Then create the result hash table
		$result = array();
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
481
482
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {			
			$result = array( 
Jerome Mariette's avatar
Jerome Mariette committed
483
				'id'			=> $row['run_id'],  
484
485
486
				'project_id'	=> $row['project_id'], 
				'project_name'	=> $row['project_name'], 
				'directory'		=> $row['run_directory'], 
Jerome Mariette's avatar
Jerome Mariette committed
487
				'name' 			=> $row['run_name'],
Jerome Mariette's avatar
Jerome Mariette committed
488
				'hidden'		=> $row['run_hidden'],
Jerome Mariette's avatar
Jerome Mariette committed
489
490
				'species' 		=> $row['run_species'],
				'nb_sequences' 	=> $row['nb_sequences'],
Jerome Mariette's avatar
Jerome Mariette committed
491
				'full_seq_size' => $row['full_seq_size'],
Jerome Mariette's avatar
Jerome Mariette committed
492
				'date'			=> $row['run_date'],
Jerome Mariette's avatar
Jerome Mariette committed
493
494
495
				'data_nature' 	=> $row['run_data_nature'],
				'sequencer' 	=> $row['run_sequencer'],
				'type' 			=> $row['run_type'],
496
497
				'description' 	=> $row['run_description']				
			);					
498
499
500
		}
		return $result;
    }
501

502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
	/**
	 * 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;
		
	}	

529
530
531
532
533
534
	/**
	 * Select all analysis linked to the specified run
	 *
	 * @param	string	$run_id	the run id
	 * @return	hash table with all analysis information
	 */
Jerome Mariette's avatar
Jerome Mariette committed
535
    function get_run_analysis($run_id, $orderby='', $limit='') {
536
		// First select all analysis from the database
537
538
539
		$queryParts = array( 
			'SELECT' => 'tx_nG6_analyze.uid AS analyze_id,'. 
						'tx_nG6_analyze.directory AS analyze_directory,'. 
540
						'tx_nG6_analyze.name AS analyze_name, '.
541
542
						'tx_nG6_analyze.params AS analyze_params, '.
						'tx_nG6_analyze.class AS analyze_class, '.
543
						'tx_nG6_analyze.date AS analyze_date, '.
Jerome Mariette's avatar
Jerome Mariette committed
544
545
						'tx_nG6_analyze.software AS analyze_software, '.
						'tx_nG6_analyze.version AS analyze_version, '.
546
547
						'tx_nG6_analyze.hidden AS analyze_hidden, '.
						'tx_nG6_analyze.description AS analyze_description, '.
548
						'tx_nG6_analyze.parent_uid AS analyze_parent_uid ',
Jerome Mariette's avatar
Jerome Mariette committed
549
			'FROM' => 'tx_nG6_run_analyze '.
550
						'INNER JOIN tx_nG6_analyze ON tx_nG6_analyze.uid = tx_nG6_run_analyze.analyze_id ',
Jerome Mariette's avatar
Jerome Mariette committed
551
			'WHERE' => 'tx_nG6_run_analyze.run_id='.$run_id,
552
			'GROUPBY' => '',
Jerome Mariette's avatar
Jerome Mariette committed
553
554
			'ORDERBY' => $orderby,
			'LIMIT' => $limit
555
556
557
558
559
560
		);
		// 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'];
Jerome Mariette's avatar
Jerome Mariette committed
561
			if (!isset ($results['analyse_'.$analyze_id])) {				
562
563
				$results['analyse_'.$analyze_id] = array( 
					'directory'		=> $row['analyze_directory'], 
Jerome Mariette's avatar
Jerome Mariette committed
564
					'name' 			=> $row['analyze_name'],
565
566
					'params' 		=> $row['analyze_params'],
					'class' 		=> $row['analyze_class'],
Jerome Mariette's avatar
Jerome Mariette committed
567
568
569
570
571
					'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
572
573
					'description' 	=> $row['analyze_description'],
					'parent_id'		=> $row['analyze_parent_uid']
574
				);
575
			}						
576
577
578
579
		}
		return $results;
    }

580
581
582
583
584
585
586
587
	/**
	 * Delete a run, sample and sublevel (analyzes) datas from database project
	 * 
	 * @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
	 */
Jerome Mariette's avatar
Jerome Mariette committed
588
	function delete_run($r_id, $user_login, $user_pwd, $data_folder) {		
589
590

		$res = 0;		
Jerome Mariette's avatar
Jerome Mariette committed
591
		$run_analysis = tx_nG6_db::get_run_analysis($r_id);
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
		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;
	}

630
631
632
633
634
635
    /**
     * Return the size of the run
     * 
     * @param	string	$r_id	the run id to return the size
	 * @param	string	$data_folder	the data folder
     */
Jerome Mariette's avatar
Jerome Mariette committed
636
	function get_run_size($r_id, $data_folder) {
637
		$full_size = 0;
Jerome Mariette's avatar
Jerome Mariette committed
638
		$run_analysis = tx_nG6_db::get_run_analysis($r_id);
639
640
641
642
643
644
645
646
647
648
649
650
651
		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;
	}

652
653
654
655
656
    /**
     * Hide a run and sublevels (analysis)
     * 
     * @param	string	$r_id	the run id to hide
     */
Jerome Mariette's avatar
Jerome Mariette committed
657
	function hide_run($r_id) {
658
659

		// First select all run analysis
Jerome Mariette's avatar
Jerome Mariette committed
660
    	$run_analysis = tx_nG6_db::get_run_analysis($r_id);
661
662
663
664
665
666
667
668
669
670
671
672
673
		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
     */
Jerome Mariette's avatar
Jerome Mariette committed
674
675
676
677
678
679
680
681
682
683
	function unhide_run($r_id, $unhide_up=false) {
	
		if ($unhide_up) {
			// 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')
			);
		}
684
685

		// Then all run analysis
Jerome Mariette's avatar
Jerome Mariette committed
686
    	$run_analysis = tx_nG6_db::get_run_analysis($r_id);
687
688
		foreach($run_analysis as $analysis_id => $analysis_values) {  
			tx_nG6_db::unhide_analysis($analysis_values['id']);
Jerome Mariette's avatar
Jerome Mariette committed
689
		}
690
691
692
693
		
		// Finaly change the run itself
		$GLOBALS['TYPO3_DB']-> exec_UPDATEquery ('tx_nG6_run', 'uid='.$r_id, array('hidden' => '0'));
	}
694
695

	/*
696
	 * Analysis functions
697
	 *------------------------------------------------------------*/
698
	 
699
700
701
702
703
704
	/**
	 * 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
705
706
    function select_analyse($analyse_id) {

707
		// First select all analyse information
708
709
710
711
712
		$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,'. 
713
						'tx_nG6_analyze.name AS analyze_name, '.
714
						'tx_nG6_analyze.params AS analyze_params, '.
Jerome Mariette's avatar
Jerome Mariette committed
715
						'tx_nG6_analyze.software AS analyze_software, '.
716
						'tx_nG6_analyze.class AS analyze_class, '.
717
						'tx_nG6_analyze.date AS analyze_date, '.
Jerome Mariette's avatar
Jerome Mariette committed
718
719
720
						'tx_nG6_analyze.description AS analyze_description, '.
						'tx_nG6_analyze.version AS analyze_version, '.
						'tx_nG6_analyze.parent_uid AS analyze_parent_uid ',
721
722
723
			'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 ',
724
725
726
727
728
			'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
			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
746
747
748
749
					'description' 	=> $row['analyze_description'],
					'parent_id'		=> $row['analyze_parent_uid'],
					'software'		=> $row['analyze_software'],
					'version'		=> $row['analyze_version']				
750
				);				
751
752
753
			}
			return $result;
		} else { // If it's a run analyse, add run information
754
			$queryParts = array( 
755
756
				'SELECT' => 'tx_nG6_project.name AS project_name,'.
							'tx_nG6_project.uid AS project_id,'.
757
							'tx_nG6_run.uid AS run_id,'. 
758
							'tx_nG6_run.name AS run_name,'.
759
760
							'tx_nG6_analyze.uid AS analyze_id,'. 
							'tx_nG6_analyze.directory AS analyze_directory,'. 
761
							'tx_nG6_analyze.name AS analyze_name, '.
762
763
							'tx_nG6_analyze.params AS analyze_params, '.
							'tx_nG6_analyze.class AS analyze_class, '.
764
							'tx_nG6_analyze.date AS analyze_date, '.
Jerome Mariette's avatar
Jerome Mariette committed
765
766
767
768
							'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 ',
769
770
771
772
773
				'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 ',
774
775
776
777
778
779
780
				'WHERE' => 'tx_nG6_analyze.uid='.$analyse_id,
				'GROUPBY' => '',
				'ORDERBY' => 'tx_nG6_analyze.name',
				'LIMIT' => ''
			);
			$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
			$result = array();
781
782
783
784
785
786
787
788
789
790
791
			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
792
793
794
795
					'description' 	=> $row['analyze_description'],
					'software'		=> $row['analyze_software'],
					'parent_id'		=> $row['analyze_parent_uid'],
					'version'		=> $row['analyze_version']			
796
				);				
797
798
799
800
			}
			return $result;
		}
    }
801

802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
	/**
	 * 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;
    }
835

836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
    /**
     * 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;
	}

854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
    /**
     * 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
     */
Jerome Mariette's avatar
Jerome Mariette committed
869
870
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
905
	function unhide_analysis($a_id, $unhide_up=false) {
	
		if ($unhide_up) {
			$queryParts = array(
				'SELECT'  => 'tx_nG6_project_analyze.uid',
				'FROM'    => 'tx_nG6_project_analyze',
				'WHERE'   => 'tx_nG6_project_analyze.analyze_id = '.$a_id,
				'GROUPBY' => '',
				'ORDERBY' => '',
				'LIMIT'   => ''
			);
			$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', 
					'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 ,
					array('tx_nG6_project.hidden' => '0', 'tx_nG6_run.hidden' => '0')
				) ;
			}
906
907
		}

908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
		// 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;
	}

955
956
957
958

	/*
	 * User functions
	 *------------------------------------------------------------*/
959
	
960
961
962
	/**
	 * Return if the user is authorized or not to access the specified project and run
	 *
963
	 * @param	int	$user_id	the user id
964
965
966
967
	 * @param	string	$project_id	the project id
	 * @param	string	$project_id	the run id
	 * @return	boolean
	 */
968
	function user_is_authorized ($user_id, $project_id, $run_id) {
969
		$authorized = false;
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
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
1022
1023
		
		// 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;
				}
				
			} 
1024
		}
1025
		return $authorized;
1026
	}
Jerome Mariette's avatar
Jerome Mariette committed
1027
1028

	/**
1029
	 * Return if the user is a superuser (administrator) for the specified project/run/analyse
Jerome Mariette's avatar
Jerome Mariette committed
1030
	 *
1031
	 * @param	string	$user_id		the user id
Jerome Mariette's avatar
Jerome Mariette committed
1032
	 * @param	string	$type			can be project/run/analyse
1033
	 * @param	string	$uid			the [project/run/analyse]'s uid
Jerome Mariette's avatar
Jerome Mariette committed
1034
1035
	 * @return	boolean {1 = ok, 0 = nok}
	 */
1036
    function is_administrator($user_id, $type, $uid) {
Jerome Mariette's avatar
Jerome Mariette committed
1037
    	
1038
1039
    	$is_admin = false;
    	
Jerome Mariette's avatar
Jerome Mariette committed
1040
    	if(!$user_id == null){
1041
1042
1043
1044
    	
	    	// Project
	   		if ($type == 'project') {
				$is_admin = tx_nG6_db::is_project_administrator($user_id, $uid);
Jerome Mariette's avatar
Jerome Mariette committed
1045
			
1046
1047
			// Run
			} elseif ($type == 'run') {
1048
1049
1050
				$queryParts = array(
					'SELECT' => 'project_id',
					'FROM' => 'tx_nG6_project_run',
1051
					'WHERE' => 'run_id='.$uid,
1052
1053
1054
1055
					'GROUPBY' => '',
					'ORDERBY' => '',
					'LIMIT' => ''
				);
1056
1057
1058
1059
1060
1061
				$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') {
1062
				$queryParts = array(
1063
1064
					'SELECT' => 'run_id',
					'FROM' => 'tx_nG6_run_analyze',
1065
1066
1067
1068
1069
1070
					'WHERE' => 'analyze_id='.$uid,
					'GROUPBY' => '',
					'ORDERBY' => '',
					'LIMIT' => ''
				);
				$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
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
				
				// 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
1104
		
1105
1106
		return $is_admin; 
    }
Jerome Mariette's avatar
Jerome Mariette committed
1107

1108
1109
1110
1111
1112
1113
1114
    /**
    * 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) {
1115
    	$right_id = -1;
1116
    	$queryParts = array(
1117
1118
    		'SELECT' => 'fe_rights.right_id AS right_id ',
    		'FROM' => 'fe_rights',
1119
1120
1121
1122
    		'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)) {
1123
1124
1125
    		$right_id = $row['right_id'];
    	}    	
    	return $right_id;
Jerome Mariette's avatar
Jerome Mariette committed
1126
    }
1127
1128
    
    /**
1129
     * Return true if the user is a member on the project
1130
1131
1132
1133
     *
     * @param int $user_id the user id
     * @param int $project_id ths project id
     */
1134
1135
1136
1137
1138
    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;
Jerome Mariette's avatar
Jerome Mariette committed
1139
    	}
1140
    	return $is_member;
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
    }
    
    /**
     * 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;
1151
1152
    	$right = tx_nG6_db::get_user_right_on_project($user_id, $project_id);
    	if ($right == 1) {
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
    		$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;
1166
1167
    	$right = tx_nG6_db::get_user_right_on_project($user_id, $project_id);
    	if ($right == 2) {
1168
1169
1170
    		$is_admin = true;
    	}
    	return $is_admin;
1171
1172
    }

1173
1174
    
    
Jerome Mariette's avatar
Jerome Mariette committed
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
    /**
    * Get all users  with rights on the project.
    *
    * @param int $project_id the project id
    */
    function get_all_users_on_project($project_id){
    	$users = array();
    	$queryParts = Array(
    		'SELECT' => 'fe_users.uid, fe_users.username, fe_users.lastlogin, fe_users.first_name, '
			    	.' fe_users.last_name, fe_users.email, fe_rights.right_id, fe_users.cruser_id ',
			'FROM' => 'fe_users INNER JOIN fe_rights ON fe_users.uid=fe_rights.fe_user_id ',
    		'WHERE' => 'fe_rights.project_id='.$project_id,
    		'GROUPBY' => '',
    		'ORDERBY' => '',
    		'LIMIT' 	=> '',
    	);
    	$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	$res_tab = array();
    	while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
    
    		$user_id = $res_row['uid'];
    		$lastlogin = date("d-m-Y", $res_row['lastlogin']);
    
    		$cruser_infos = tx_nG6_db::get_user_informations($res_row['cruser_id']);
    		
    		if (!isset ($users['user_'.$user_id])){
    			$users['user_'.$user_id] = array(
    				'id' 			=>	$user_id,
    				'username' 		=>	$res_row['username'],
    				'lastlogin' 	=>	$lastlogin,
    				'first_name' 	=>	$res_row['first_name'],
    				'last_name' 	=>	$res_row['last_name'],
    				'email' 		=>	$res_row['email'],
    				'right_id' 		=>	$res_row['right_id'],
    				'cruser_id'		=>  $res_row['cruser_id'],
    				'cruser_name'	=>  $cruser_infos['username']
    			);
    		}
    
    	}
    	return $users;
    }
    

    /**
     * Delete acces from a project to a specific member
     * 
     * @param int $user_id	the user id
     * @param int $project_id	the project id
     */
    function delete_access_to_project($user_id, $project_id){
    	$GLOBALS['TYPO3_DB']->exec_DELETEquery('fe_rights', 'fe_rights.fe_user_id='.$user_id.' AND fe_rights.project_id='.$project_id);
    }
    
    
    /**
     * Change right on project for a member.
     * 
     * @param int $user_id		the user id
     * @param int $project_id	the project id
     * @param int $right		the right id (0=member, 1=manager, 2=admin)
     */
    function change_access_to_project($user_id, $project_id, $right){
    	$GLOBALS['TYPO3_DB']->exec_UPDATEquery ('fe_rights', 'fe_rights.fe_user_id='.$user_id.' AND fe_rights.project_id='.$project_id, array('fe_rights.right_id' => $right));
    }
    
    /**
     * Add a new member to the project.
     *
     * @param int $user_id		the user id
     * @param int $project_id	the project id
     * @param int $right			the right id (0=member, 1=manager, 2=admin)
     */
    function add_access_to_project($user_id, $project_id, $right_id){
    	
    	$member_tab = array(
    		'fe_user_id'	=>	$user_id,
    		'project_id'	=>	$project_id,
    		'right_id'	=>	$right_id
    	);
    	
    	$GLOBALS['TYPO3_DB']->exec_INSERTquery ('fe_rights', $member_tab);
    }
    
    
    
    /**
     * Get all usernames, firstnames and lastnames starting with a string.
     * 
     * @param string $name_start	the string part
     * @param string $user_col		the fe_users column to use
     * @return array
     */
    function get_all_names_starting_with($name_start, $user_col) {
    	$queryParts = Array(
    		'SELECT' => 'fe_users.uid, fe_users.username, fe_users.last_name, fe_users.first_name, fe_users.password, fe_users.email, fe_users.cruser_id, fe_groups.title',
			'FROM' => 'fe_users INNER JOIN fe_groups ON fe_users.usergroup=fe_groups.uid',
			'WHERE' => 'fe_users.'.$user_col.' LIKE "'.$name_start.'%"',
			'GROUPBY' => '',
			'ORDERBY' => '',
			'LIMIT' 	=> '',
    	);
    	$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	$res_tab = array();
    	while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
    		$res_tab[] = array(
    			'uid'			=>	$res_row['uid'],
    			'username' 		=>	$res_row['username'],
    			'first_name' 	=>	$res_row['first_name'],
    			'last_name' 	=>	$res_row['last_name'],
    			'password' 		=>	$res_row['password'],
    			'email' 		=>	$res_row['email'],
    			'group' 		=>	$res_row['title'],
    			'cruser_id' 	=>	$res_row['cruser_id']
    		);
    	}
    	return $res_tab;
    }
    
    
   /**
    * Get all groupnames starting with a string.
    * 
    * @param string $group_name_start 	the string part
    * @return array
    */
    function get_all_group_starting_with($group_name_start) {
    	$queryParts = Array(
        		'SELECT' => 'fe_groups.uid, fe_groups.title',
    			'FROM' => 'fe_groups',
    			'WHERE' => 'fe_groups.title LIKE "'.$group_name_start.'%"',
    			'GROUPBY' => '',
    			'ORDERBY' => '',
    			'LIMIT' 	=> '',
    	);
    	$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	$res_tab = array();
    	while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
    		$res_tab[] = array(
    			'uid'			=>	$res_row['uid'],
        		'title'			=>	$res_row['title']
    		);
    	}
    	return $res_tab;
    }
    
    
    /**
     * Get user informations.
     * 
     * @param int  $user_id the user id
     */
    function get_user_informations($user_id){
    	$queryParts = Array(
    		'SELECT' => 'fe_users.uid, fe_users.username, fe_users.last_name, fe_users.first_name, fe_users.password, fe_users.email, fe_users.cruser_id',
    		'FROM' => 'fe_users',
    		'WHERE' => 'fe_users.uid='.$user_id,
    		'GROUPBY' => '',
    		'ORDERBY' => '',
    		'LIMIT' 	=> '',
    	);
    	$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	$res_tab = null;
    	while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
    		$res_tab = array(
    			'uid'			=>	$res_row['uid'],
    			'username' 		=>	$res_row['username'],
    			'first_name' 	=>	$res_row['first_name'],
    			'last_name' 	=>	$res_row['last_name'],
    			'password' 		=>	$res_row['password'],
    			'email' 		=>	$res_row['email'],
    			'cruser_id' 	=>	$res_row['cruser_id']
    		);
    	}
    	return $res_tab;
    }
    
    function get_user_id_by_username($username){
    	$uid = null;
    	$queryParts = Array(
    		'SELECT' => 'fe_users.uid',
    		'FROM' => 'fe_users',
    		'WHERE' => 'fe_users.username LIKE "'.$username.'" ',
			'GROUPBY' => '',
			'ORDERBY' => '',
			'LIMIT' 	=> '',
    	);
    	$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
	    	$uid = $res_row['uid'];
    	}
    	return $uid;
    }

    /**
     * Create a new user in the DB.
     * 
     * @param array $user_datas the user array
     */
Jerome Mariette's avatar
Jerome Mariette committed
1374
    function create_new_user($user_name, $first_name, $last_name, $email, $password, $cruser_id, $group_id, $pid){
Jerome Mariette's avatar
Jerome Mariette committed
1375
1376
1377
1378
    
		// Create new user
		$user_datas = array(
			'username'		=>	$user_name,
Jerome Mariette's avatar
Jerome Mariette committed
1379
			'pid'			=>	$pid,
Jerome Mariette's avatar
Jerome Mariette committed
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435