class.tx_nG6_db.php 76.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?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
	
Jerome Mariette's avatar
Jerome Mariette committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
	/**
     * Return the size used by ng6 datas from a starting date to a end date
     * 
     * @param int $usergroup		the usergroup
     * @param string $data_folder	the folder data
     * @param bool $get_analyzes	true if the admin wants to include analyzes
     * @param bool $cumul_values	true if the admin wants cumulatives values
     * @param int $step				the step (0 = one month, 1 = six months, 2 = one year)
     * @param int $date_start		the date start
     * @param int $date_end			the date end
     * @return array
     */
    function get_size($usergroup, $data_folder, $get_analyzes=false, $cumul_values=false, $step=2, $date_start=0, $date_end=0){
		
    	//const - tstamp is in seconds, nb of seconds in one day
    	$one_day_val = 24 * 3600;
    	
    	// init min max
    	if($date_start==0){ $date_start = intval(tx_nG6_db::get_run_min_date()); }
    	if($date_end==0){ $date_end = intval(tx_nG6_db::get_run_max_date()); }
  
    	$date_cursors = $date_start;
    	$res_tab = null;

		// get all users
Jerome Mariette's avatar
Jerome Mariette committed
79
80
81
82
83
84
85
86
87
88
		if ($usergroup != "0") {
			$users_in_group = tx_nG6_db::get_all_users_in_group($usergroup);
			$project_list_final = array();
			foreach($users_in_group as $list_current_user_id){
				// get all user projects
				$all_user_projects = tx_nG6_db::select_all_user_projects($list_current_user_id);
				foreach($all_user_projects as $p_val){
					if(!in_array($p_val['id'], $project_list_final)){
						$project_list_final[] = $p_val['id'];
					}
Jerome Mariette's avatar
Jerome Mariette committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
				}
			}
		}

		$cumulated_size = 0;
    	while($date_cursors < $date_end){
    		// increment step iterator
    		$total_day_count_tstamp = 0;
    		switch ($step) {
    			case 0:		// 1 month
    				$nb_days_in_month = date('t', $date_cursors);
    				$total_day_count_tstamp = $one_day_val * $nb_days_in_month;
    				break;
    			case 1:		// 6 months
    				$date_cursor_tmp = $date_cursors;
    				for($i = 0;$i < 6; $i++){
    					$nb_days_in_month = cal_days_in_month(CAL_GREGORIAN, date('n', $date_cursor_tmp), date('Y', $date_cursor_tmp));
    					$total_day_count_tstamp += $one_day_val * $nb_days_in_month;
    					// +1 month * 6
    					$date_cursor_tmp = strtotime('+1 month', $date_cursor_tmp);
    				}
    				break;
    			case 2:		// 1 year
    				$nb_days_in_year = date("z", mktime(0,0,0,12,31,date('Y', $date_cursors)));
    				$total_day_count_tstamp = $one_day_val * $nb_days_in_year;
    				break;
    		}
    		
    		$date_cursore = $date_cursors + $total_day_count_tstamp;
    		$current_size = 0;
Jerome Mariette's avatar
Jerome Mariette committed
119
120
121
122
123
124
    		
			if ($usergroup != "0") {
				$where = 'tx_nG6_project.uid IN ('.implode(",",$project_list_final).') AND tx_nG6_run.date BETWEEN '.$date_cursors.' AND '.$date_cursore;	
			} else {
				$where = 'tx_nG6_run.date BETWEEN '.$date_cursors.' AND '.$date_cursore;
			}
Jerome Mariette's avatar
Jerome Mariette committed
125
126
127
			
    		// get all runs the usergroup can access to
    		$queryParts = array(
Jerome Mariette's avatar
Jerome Mariette committed
128
    			'SELECT' 	=> 'tx_nG6_run.uid as run_id',
Jerome Mariette's avatar
Jerome Mariette committed
129
130
131
    			'FROM' 		=> 'tx_nG6_project '
    				.' 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
132
    			'WHERE' 	=> $where,
Jerome Mariette's avatar
Jerome Mariette committed
133
134
135
136
137
138
139
140
    			'GROUPBY' 	=> '',
    			'ORDERBY' 	=> '',
    			'LIMIT' 	=> ''
    		);
			$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
			while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
				$current_size += tx_nG6_db::get_run_size($res_row['run_id'], $data_folder, $get_analyzes);
			}
Jerome Mariette's avatar
Jerome Mariette committed
141
142
143
			
			// get all project analysis the usergroup can access to
			if ($get_analyzes) {
Jerome Mariette's avatar
Jerome Mariette committed
144
145
146
147
148
				if ($usergroup != "0") {
					$where = 'tx_nG6_project.uid IN ('.implode(",",$project_list_final).') AND tx_nG6_analyze.date BETWEEN '.$date_cursors.' AND '.$date_cursore;	
				} else {
					$where = 'tx_nG6_analyze.date BETWEEN '.$date_cursors.' AND '.$date_cursore;
				}
Jerome Mariette's avatar
Jerome Mariette committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
	    		$queryParts = array(
	    			'SELECT' 	=> 'tx_nG6_analyze.uid as analyze_id',
	    			'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_project_analyze.analyze_id=tx_nG6_analyze.uid ',
	    			'WHERE' 	=> $where,
	    			'GROUPBY' 	=> '',
	    			'ORDERBY' 	=> '',
	    			'LIMIT' 	=> ''
	    		);
				$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
				while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
					$current_size += tx_nG6_db::get_analysis_size($res_row['analyze_id'], $data_folder);
				}
			}
Jerome Mariette's avatar
Jerome Mariette committed
164
165

    		$cumulated_size += $current_size;
Jerome Mariette's avatar
Jerome Mariette committed
166
167
168
169
170
    		if(!$cumul_values){
    	    	$res_tab[] = array($date_cursore*1000, $current_size);
    	    }else{
    	    	$res_tab[] = array($date_cursore*1000, $cumulated_size);
    	    }
Jerome Mariette's avatar
Jerome Mariette committed
171
172
173
174
175

    		$date_cursors = $date_cursore + 1;
    	}
    	return $res_tab;	
    }
Penom Nom's avatar
Penom Nom committed
176
	
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
    /**
     * Select all project for the current user
     *
     * @param	string	$by	by can group, organism, location
     * @param	string	$role	role can be create_user, manager
     * @return	table with all projects
     */
    function select_projects_repartition($role, $by='title') {
    	
    	if ($role == "create_user") {
    		$queryParts = array(
    				'SELECT' 	=> 'tx_nG6_project.uid as pid, fe_groups.title as title, fe_groups.tx_nG6_organism as organism, fe_groups.tx_nG6_location as location',
    				'FROM' 		=> 'tx_nG6_project '
    				.' INNER JOIN fe_users ON tx_nG6_project.cruser_id=fe_users.uid '
    				.' INNER JOIN fe_groups ON fe_groups.uid=fe_users.usergroup ',
    				'WHERE' 	=> "",
    				'GROUPBY' 	=> '',
    				'ORDERBY' 	=> '',
    				'LIMIT' 	=> ''
    		);
    		//$where = "tx_nG6_project.cruser_id="
    	} else if ($role == "manager") {
	    	$queryParts = array(
    				'SELECT' 	=> 'tx_nG6_project.uid as pid, fe_groups.title as title, fe_groups.tx_nG6_organism as organism, fe_groups.tx_nG6_location as location',
	    			'FROM' 		=> 'tx_nG6_project '
	    			.' INNER JOIN fe_rights ON tx_nG6_project.uid=fe_rights.project_id '
	    			.' INNER JOIN fe_users ON fe_rights.fe_user_id=fe_users.uid '
	    			.' INNER JOIN fe_groups ON fe_groups.uid=fe_users.usergroup ',
	    			'WHERE' 	=> "fe_rights.right_id=1",
	    			'GROUPBY' 	=> '',
	    			'ORDERBY' 	=> '',
	    			'LIMIT' 	=> ''
	    	);
    	}
    	$by_title = array() ;
    	$by_organism = array();
    	$by_location = array();
    	$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
    		if (!isset ($by_title[$res_row['title']])) {
    			$by_title[$res_row['title']] = array(
    					'count'			=> 1,
    					'organism'		=> $row['organism'],
    					'location'		=> $row['location']);
    		} else {
    			$by_title[$res_row['title']]['count'] += 1;
    		}
    		
    		if (!isset ($by_organism[$res_row['organism']])) {
    			$by_organism[$res_row['organism']] = array(
    					'count'			=> 1);
    		} else {
    			$by_organism[$res_row['organism']]['count'] += 1;
    		}
    		
    		if (!isset ($by_location[$res_row['location']])) {
    			$by_location[$res_row['location']] = array(
    					'count'			=> 1);
    		} else {
    			$by_location[$res_row['location']]['count'] += 1;
    		}
    	}
    	if ($by=='title') {
    		return ($by_title);
    	} else if ($by=='organism') {
    		return ($by_organism);
    	} else if ($by=='location') {
    		return ($by_location);
    	}
    }
    
    
249
250
251
252
253
254
	/*
	 * Project functions
	 *------------------------------------------------------------*/

	/**
	 * Select all project for the current user
255
	 * 
256
	 * @param	string	$user_id	the user id
257
258
	 * @return	table with all projects
	 */
259
	function select_all_user_projects($user_id, $orderby='', $limit='') {		
260
		$projects = array();
Jerome Mariette's avatar
Jerome Mariette committed
261
		
262
		// If the user is not logged on display demonstration project
Jerome Mariette's avatar
Jerome Mariette committed
263
264
265
		if ($user_id == null) { $where = 'tx_nG6_project.public=0'; } 
		else { $where = 'fe_rights.fe_user_id='.$user_id; }
		
266
		//First, get the analyzis project-scale
267
		$queryParts = array(
268
269
270
			'SELECT' => 'tx_nG6_project.uid AS project_id, '.
						'tx_nG6_project.name AS project_name, '.
						'tx_nG6_project.description AS project_description, '.
271
						'tx_nG6_project.public AS project_public, '.
Jerome Mariette's avatar
Jerome Mariette committed
272
						'tx_nG6_project.crdate AS project_crdate, '.
273
274
						'tx_nG6_project.hidden AS project_hidden ',
			'FROM' => 'tx_nG6_project INNER JOIN fe_rights ON fe_rights.project_id=tx_nG6_project.uid',
275
276
			'WHERE' => $where,
			'GROUPBY' => '',
Jerome Mariette's avatar
Jerome Mariette committed
277
278
			'ORDERBY' => $orderby,
			'LIMIT' => $limit
279
		);
280
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);		
281
282
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			$project_id = $row['project_id'];
Jerome Mariette's avatar
Jerome Mariette committed
283
			if (!isset ($projects['project_'.$project_id])) {		
284
				$projects['project_'.$project_id] = array(
285
					'id'			=> $project_id,				
286
					'name'			=> $row['project_name'],
Jerome Mariette's avatar
Jerome Mariette committed
287
					'hidden'		=> $row['project_hidden'],
Jerome Mariette's avatar
Jerome Mariette committed
288
					'date'			=> $row['project_crdate'],
289
					'public'		=> $row['project_public'],
290
291
					'description'	=> $row['project_description']);
			}
292
						
293
		}		
294
295
296
		return $projects;
	}

Penom Nom's avatar
Penom Nom committed
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
	/**
	* Select all project for the current user
	*
	* @param	string	$user_id	the user id
	* @return	table with all projects
	*/
	function get_user_projects($user_id, $orderby='', $limit='') {
		$projects = array();
	
		// If the user is not logged on display demonstration project
		if ($user_id == null) {
			$where = 'tx_nG6_project.public=0';
		}
		else { 
				$where = 'fe_rights.fe_user_id='.$user_id.' AND ((fe_rights.right_id<>2 AND tx_nG6_project.hidden=0) '
				.'OR fe_rights.right_id=2)' ;
		}
	
		//First, get the analyzis project-scale
		$queryParts = array(
				'SELECT' => 'tx_nG6_project.uid AS project_id, '.
							'tx_nG6_project.name AS project_name, '.
							'tx_nG6_project.description AS project_description, '.
							'tx_nG6_project.public AS project_public, '.
							'tx_nG6_project.crdate AS project_crdate, '.
							'tx_nG6_project.hidden AS project_hidden ',
				'FROM' => 'tx_nG6_project INNER JOIN fe_rights ON fe_rights.project_id=tx_nG6_project.uid',
				'WHERE' => $where,
				'GROUPBY' => '',
				'ORDERBY' => $orderby,
				'LIMIT' => $limit
		);
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			$project_id = $row['project_id'];
			if (!isset ($projects['project_'.$project_id])) {
				$projects['project_'.$project_id] = array(
						'id'			=> $project_id,				
						'name'			=> $row['project_name'],
						'hidden'		=> $row['project_hidden'],
						'date'			=> $row['project_crdate'],
						'public'		=> $row['project_public'],
						'description'	=> $row['project_description']);
			}
	
		}
		return $projects;
	}

346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
	/**
	 * 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
362
363
364
365
		$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
366
					 'date'			=> $vals['crdate'],
Jerome Mariette's avatar
Jerome Mariette committed
367
368
					 'public'		=> $vals['public'],
					 'description'	=> $vals['description']);
369
370
371
372
373
374
375
376
	}

	/**
	 * 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
377
    function get_project_runs($project_id, $orderby='', $limit='') {
378
    	
379
		// First select all runs from the database
380
		$queryParts = Array( 
381
382
383
384
			'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
385
386
387
						'tx_nG6_run.data_nature AS run_data_nature, '.
						'tx_nG6_run.sequencer AS run_sequencer, '.
						'tx_nG6_run.type AS run_type, '.
388
						'tx_nG6_run.name AS run_name, '.
Jerome Mariette's avatar
Jerome Mariette committed
389
390
						'tx_nG6_run.nb_sequences AS run_nb_sequences, '.
						'tx_nG6_run.full_seq_size AS run_full_seq_size, '.
391
392
						'tx_nG6_run.date AS run_date, '.
						'tx_nG6_run.hidden AS run_hidden, '.
393
394
						'tx_nG6_project.uid AS project_id, '.
						'tx_nG6_project.name AS project_name ',
Jerome Mariette's avatar
Jerome Mariette committed
395
			'FROM' => 'tx_nG6_project '.
396
397
  						'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
398
			'WHERE' => 'tx_nG6_project.uid='.$project_id,
399
			'GROUPBY' => '',
Jerome Mariette's avatar
Jerome Mariette committed
400
401
			'ORDERBY' => $orderby,
			'LIMIT' 	=> $limit
Penom Nom's avatar
Penom Nom committed
402
403
404
		);

	
405
406
407
408
		// 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
409
410
			$run_id = $row['run_id'];				
			if (!isset($results['run_'.$run_id])) {				
411
				$results['run_'.$run_id] = array( 
Jerome Mariette's avatar
Jerome Mariette committed
412
					'id'			=> $run_id,
413
					'directory'		=> $row['run_directory'], 
414
					'name' 			=> $row['run_name'],
Jerome Mariette's avatar
Jerome Mariette committed
415
416
					'project_name' 	=> $row['project_name'],
					'hidden'		=> $row['run_hidden'],
417
					'species' 		=> $row['run_species'],
Jerome Mariette's avatar
Jerome Mariette committed
418
419
420
					'nb_sequences' 	=> $row['run_nb_sequences'],
					'full_seq_size' => $row['run_full_seq_size'],
					'project_id' 	=> $row['project_id'],
421
					'date'			=> $row['run_date'],
Jerome Mariette's avatar
Jerome Mariette committed
422
423
424
					'data_nature'	=> $row['run_data_nature'],
					'sequencer'		=> $row['run_sequencer'],
					'type'			=> $row['run_type'],
Jerome Mariette's avatar
Jerome Mariette committed
425
					'description' 	=> $row['run_description']
426
				);
427
			}						
428
429
430
431
		}
		return $results;
    }

Penom Nom's avatar
Penom Nom committed
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
    /**
     * Select all runs linked to the specified project for the user
     *
     * @param	string	$user_id	the user id
     * @param	string	$project_id	the project id
     * @return	hash table with all runs information
     */
     function get_user_project_runs($user_id, $project_id, $orderby='', $limit='') {
    	// If the user is not logged on display demonstration project
	    if ($user_id == null) {
	    	$where = 'tx_nG6_project.uid='.$project_id.' AND tx_nG6_project.public=0 AND tx_nG6_run.hidden=0';
	    }
	    else {
	    	$where = 'fe_users.uid='.$user_id.' AND '.'tx_nG6_project.uid='.$project_id.
	    		' AND ((fe_rights.right_id<>2 AND tx_nG6_run.hidden=0) OR fe_rights.right_id=2)' ;
	    }
    	
	    // First select all runs from the database
	    $queryParts = Array(
		    'SELECT' => 'tx_nG6_run.uid AS run_id,'.
			    'tx_nG6_run.directory AS run_directory,'.
			    'tx_nG6_run.species AS run_species,'.
			    'tx_nG6_run.description AS run_description, '.
			    'tx_nG6_run.data_nature AS run_data_nature, '.
			    'tx_nG6_run.sequencer AS run_sequencer, '.
			    'tx_nG6_run.type AS run_type, '.
			    'tx_nG6_run.name AS run_name, '.
			    'tx_nG6_run.nb_sequences AS run_nb_sequences, '.
			    'tx_nG6_run.full_seq_size AS run_full_seq_size, '.
			    'tx_nG6_run.date AS run_date, '.
			    'tx_nG6_run.hidden AS run_hidden, '.
			    'tx_nG6_project.uid AS project_id, '.
			    'tx_nG6_project.name AS project_name ',
		    'FROM' => 'tx_nG6_run '.
			    'INNER JOIN tx_nG6_project_run ON tx_nG6_run.uid=tx_nG6_project_run.run_id '.
			    'INNER JOIN tx_nG6_project ON tx_nG6_project_run.project_id=tx_nG6_project.uid '.
			    'INNER JOIN fe_rights ON tx_nG6_project.uid=fe_rights.project_id '.
			    'INNER JOIN fe_users ON fe_rights.fe_user_id=fe_users.uid ',
		    'WHERE' => $where,
		    'GROUPBY' => '',
		    'ORDERBY' => $orderby,
		    'LIMIT' 	=> $limit
	    );
    
	    // 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'];
	    	if (!isset($results['run_'.$run_id])) {
			    $results['run_'.$run_id] = array(
			    'id'			=> $run_id,
			    'directory'		=> $row['run_directory'],
			    'name' 			=> $row['run_name'],
			    							'project_name' 	=> $row['project_name'],
			    'hidden'		=> $row['run_hidden'],
			    							'species' 		=> $row['run_species'],
			    'nb_sequences' 	=> $row['run_nb_sequences'],
			    							'full_seq_size' => $row['run_full_seq_size'],
			    							'project_id' 	=> $row['project_id'],
			    							'date'			=> $row['run_date'],
			    							'data_nature'	=> $row['run_data_nature'],
			    							'sequencer'		=> $row['run_sequencer'],
			    							'type'			=> $row['run_type'],
			    'description' 	=> $row['run_description']
			    				);
		    }
	    }
	    return $results;
    }

503
504
505
506
507
508
	/**
	 * 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
509
    function get_project_analysis($project_id, $orderby='', $limit='') {
510
511
    	
    	// First select all analysis from the database    	
512
    	$queryParts = array( 
513
514
			'SELECT' => 'tx_nG6_analyze.uid AS analyze_id,'. 
						'tx_nG6_analyze.directory AS analyze_directory,'. 
515
						'tx_nG6_analyze.name AS analyze_name, '.
516
						'tx_nG6_analyze.class AS analyze_class, '.
517
						'tx_nG6_analyze.date AS analyze_date, '.
Jerome Mariette's avatar
Jerome Mariette committed
518
519
						'tx_nG6_analyze.software AS analyze_software, '.
						'tx_nG6_analyze.version AS analyze_version, '.
520
						'tx_nG6_analyze.params AS analyze_params, '.
521
						'tx_nG6_analyze.description AS analyze_description, '.
522
    					'tx_nG6_analyze.is_editable AS analyze_is_editable, '.
Jerome Mariette's avatar
Jerome Mariette committed
523
						'tx_nG6_analyze.parent_uid AS analyze_parent_uid, '.
524
						'tx_nG6_analyze.hidden AS analyze_hidden ',
Jerome Mariette's avatar
Jerome Mariette committed
525
			'FROM' => 'tx_nG6_project '.
526
527
						'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
528
			'WHERE' => 'tx_nG6_project.uid='.$project_id,
529
			'GROUPBY' => '',
Jerome Mariette's avatar
Jerome Mariette committed
530
531
			'ORDERBY' => $orderby,
			'LIMIT' => $limit
532
533
534
535
536
537
		);
		// 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
538
			if (!isset ($results['analyse_'.$analyze_id])) {				
539
540
				$results['analyse_'.$analyze_id] = array( 
					'directory'		=> $row['analyze_directory'], 
541
					'name' 			=> $row['analyze_name'],
542
					'class'			=> $row['analyze_class'],
Jerome Mariette's avatar
Jerome Mariette committed
543
544
					'id' 			=> $row['analyze_id'],
					'hidden' 		=> $row['analyze_hidden'],
545
					'params' 		=> $row['analyze_params'],
Jerome Mariette's avatar
Jerome Mariette committed
546
547
					'software' 		=> $row['analyze_software'],
					'version' 		=> $row['analyze_version'],
548
					'date'			=> $row['analyze_date'],
Jerome Mariette's avatar
Jerome Mariette committed
549
					'description' 	=> $row['analyze_description'],
550
					'is_editable' 	=> $row['analyze_is_editable'],
Jerome Mariette's avatar
Jerome Mariette committed
551
					'parent_id'		=> $row['analyze_parent_uid']	
552
				);
553
			}						
554
555
556
557
		}
		return $results;
    }

Penom Nom's avatar
Penom Nom committed
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
585
    /**
    * Select all analysis linked to the specified project for the user
    *
    * @param	string	$user_id	the user id
    * @param	string	$project_id	the project id
    * @return	hash table with all analysis information
    */
    function get_user_project_analysis($user_id, $project_id, $orderby='', $limit='') {
    	// If the user is not logged on display demonstration project
    	if ($user_id == null) {
    		$where = 'tx_nG6_project.uid='.$project_id.' AND tx_nG6_project.public=0 AND tx_nG6_run.hidden=0';
    	}
    	else {
    		$where = 'fe_users.uid='.$user_id.' AND tx_nG6_project.uid='.$project_id.
    					' AND ((fe_rights.right_id<>2 AND tx_nG6_analyze.hidden=0) OR fe_rights.right_id=2)' ;
    	}
    	
    	// First select all analysis from the database
    	$queryParts = array(
    			'SELECT' => 'tx_nG6_analyze.uid AS analyze_id,'. 
    						'tx_nG6_analyze.directory AS analyze_directory,'. 
    						'tx_nG6_analyze.name AS analyze_name, '.
    						'tx_nG6_analyze.class AS analyze_class, '.
    						'tx_nG6_analyze.date AS analyze_date, '.
    						'tx_nG6_analyze.software AS analyze_software, '.
    						'tx_nG6_analyze.version AS analyze_version, '.
    						'tx_nG6_analyze.params AS analyze_params, '.
    						'tx_nG6_analyze.description AS analyze_description, '.
586
    						'tx_nG6_analyze.is_editable AS analyze_is_editable, '.
Penom Nom's avatar
Penom Nom committed
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
    						'tx_nG6_analyze.parent_uid AS analyze_parent_uid, '.
    						'tx_nG6_analyze.hidden AS analyze_hidden ',
    			'FROM' => 'tx_nG6_analyze '.
    						'INNER JOIN tx_nG6_project_analyze ON tx_nG6_analyze.uid = tx_nG6_project_analyze.analyze_id '.
    						'INNER JOIN tx_nG6_project ON tx_nG6_project_analyze.project_id=tx_nG6_project.uid '.
							'INNER JOIN fe_rights ON tx_nG6_project.uid=fe_rights.project_id '.
					  		'INNER JOIN fe_users ON fe_rights.fe_user_id=fe_users.uid ',
    			'WHERE' => $where,
    			'GROUPBY' => '',
    			'ORDERBY' => $orderby,
    			'LIMIT' => $limit
    	);
    	// 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'];
    		if (!isset ($results['analyse_'.$analyze_id])) {
    			$results['analyse_'.$analyze_id] = array(
    					'directory'		=> $row['analyze_directory'], 
    					'name' 			=> $row['analyze_name'],
    					'class'			=> $row['analyze_class'],
    					'id' 			=> $row['analyze_id'],
    					'hidden' 		=> $row['analyze_hidden'],
    					'params' 		=> $row['analyze_params'],
    					'software' 		=> $row['analyze_software'],
    					'version' 		=> $row['analyze_version'],
    					'date'			=> $row['analyze_date'],
    					'description' 	=> $row['analyze_description'],
616
    					'is_editable' 	=> $row['analyze_is_editable'],
Penom Nom's avatar
Penom Nom committed
617
618
619
620
621
622
623
    					'parent_id'		=> $row['analyze_parent_uid']	
    			);
    		}
    	}
    	return $results;
    }

624
625
626
627
628
    /**
     * 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
629
	function hide_project($p_id) {
630
631
		
		// First change all project runs
Jerome Mariette's avatar
Jerome Mariette committed
632
		$project_runs = tx_nG6_db::get_project_runs($p_id);
633
		foreach($project_runs as $run_id => $run_values) {
Jerome Mariette's avatar
Jerome Mariette committed
634
			tx_nG6_db::hide_run($run_values['id']);
635
636
637
		}

		// Then all project analysis
Jerome Mariette's avatar
Jerome Mariette committed
638
    	$project_analysis = tx_nG6_db::get_project_analysis($p_id);
639
640
641
		foreach($project_analysis as $analysis_id => $analysis_values) {  
			tx_nG6_db::hide_analysis($analysis_values['id']);
		}
642
		tx_nG6_db::unpublish_project($p_id);
643
644
645
646
647
648
649
650
651
		// 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
652
	function unhide_project($p_id) {
653
654
		
		// First change all project runs
Jerome Mariette's avatar
Jerome Mariette committed
655
		$project_runs = tx_nG6_db::get_project_runs($p_id);
656
		foreach($project_runs as $run_id => $run_values) {
Jerome Mariette's avatar
Jerome Mariette committed
657
			tx_nG6_db::unhide_run($run_values['id']);
658
659
660
		}

		// Then all project analysis
Jerome Mariette's avatar
Jerome Mariette committed
661
    	$project_analysis = tx_nG6_db::get_project_analysis($p_id);
662
663
664
665
666
667
668
669
670
671
672
673
674
		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
675
	function publish_project($p_id) {
676
		// First unhide the project
Jerome Mariette's avatar
Jerome Mariette committed
677
		tx_nG6_db::unhide_project($p_id);
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
		// 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
699
	function delete_project($p_id, $user_login, $user_pwd, $data_folder) {
700
701
702
		
		$res = 0;
		// All runs
Jerome Mariette's avatar
Jerome Mariette committed
703
		$project_runs = tx_nG6_db::get_project_runs($p_id);
704
705
706
		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
707
				$res = tx_nG6_db::delete_run($run_values['id'], $user_login, $user_pwd, $data_folder);
708
709
710
711
712
713
				if ($res != 0) { break; }				
			}
		}
		
		// All project_analysis
		if ($res == 0) {
Jerome Mariette's avatar
Jerome Mariette committed
714
			$project_analysis = tx_nG6_db::get_project_analysis($p_id);
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
			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;}
		}
		
Jerome Mariette's avatar
Jerome Mariette committed
732
733
734
735
736
737
738
739
		// Delete right associated to the project
		if ($res == 0) {
			$table='fe_rights';
			$where='fe_rights.project_id='.$p_id;
			$res1 = $GLOBALS['TYPO3_DB']-> exec_DELETEquery ($table, $where);
			if ($res1 != 1) {$res = 1;}
		}
		
740
741
742
		return $res;
	}

743
744
745
746
747
    /**
     * 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
748
	 * @param	boolean $get_analyzes	get analyzes size ? 
749
     */
Jerome Mariette's avatar
Jerome Mariette committed
750
	function get_project_size($p_id, $data_folder, $get_analyzes=false) {
751
752
753
		
		$full_size = 0;
		// All runs
Jerome Mariette's avatar
Jerome Mariette committed
754
		$project_runs = tx_nG6_db::get_project_runs($p_id);
755
		foreach($project_runs as $run_id => $run_values) {
Jerome Mariette's avatar
Jerome Mariette committed
756
			$full_size += tx_nG6_db::get_run_size($run_values['id'], $data_folder, $get_analyzes);
757
		}
Jerome Mariette's avatar
Jerome Mariette committed
758
759
760
761
762
763
		if ($get_analyzes) {
			// All analysis
			$project_analysis = tx_nG6_db::get_project_analysis($p_id);
			foreach($project_analysis as $analyse_id => $analyze_values) {  
				$full_size += tx_nG6_db::get_analysis_size($analyze_values['id'], $data_folder);
			}
764
765
766
767
		}
		return $full_size;
	}

768
769
770
771

	/*
	 * Run functions
	 *------------------------------------------------------------*/
772
773
	
	
Jerome Mariette's avatar
Jerome Mariette committed
774
775
	/**
	 * Select all run for the specified user
776
	 * 
777
	 * @param	string	$user_id	the user id
Jerome Mariette's avatar
Jerome Mariette committed
778
779
	 * @return	table with all projects
	 */
780
	function select_all_user_runs($user_id, $orderby='', $limit='') {
781

Jerome Mariette's avatar
Jerome Mariette committed
782
		// If the user is not logged on display demonstration project
Jerome Mariette's avatar
Jerome Mariette committed
783
784
785
		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
786
     	// Execute the request
787
		$queryParts = Array( 
Jerome Mariette's avatar
Jerome Mariette committed
788
789
790
791
			'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, '.
792
						'tx_nG6_run.data_nature AS run_data_nature, '. 
Jerome Mariette's avatar
Jerome Mariette committed
793
794
795
796
797
798
799
800
						'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, '.
801
802
803
804
805
						'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
806
807
808
809
			'WHERE' => $where,
			'GROUPBY' => '',
			'ORDERBY' => $orderby,
			'LIMIT' 	=> $limit
810
		); 
Jerome Mariette's avatar
Jerome Mariette committed
811
812
813
814
		// 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
815
816
			$run_id = $row['run_id'];	
			if (!isset($results['run_'.$run_id])) {				
Jerome Mariette's avatar
Jerome Mariette committed
817
				$results['run_'.$run_id] = array(
818
819
					'id'			=> $run_id,  
					'directory'		=> $row['run_directory'], 
Jerome Mariette's avatar
Jerome Mariette committed
820
821
822
823
824
825
826
827
828
829
830
					'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'],
831
					'description' 	=> $row['run_description']				
Jerome Mariette's avatar
Jerome Mariette committed
832
				);
833
			}						
Jerome Mariette's avatar
Jerome Mariette committed
834
835
836
837
838
		}
		return $results;

	}

839
840
841
842
843
844
	/**
	 * 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
845
	function select_run($run_id) {
846
		
847
		// First select the run from the database
848
		$queryParts = Array( 
849
850
851
852
853
			'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
854
						'tx_nG6_run.species AS run_species, '.
855
						'tx_nG6_run.name AS run_name, '.
Jerome Mariette's avatar
Jerome Mariette committed
856
857
858
859
						'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
860
						'tx_nG6_run.hidden AS run_hidden, '.
861
862
						'tx_nG6_run.nb_sequences, '.
						'tx_nG6_run.date AS run_date',
863
864
865
866
			'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 ',
867
868
869
870
			'WHERE' => 'tx_nG6_run.uid='.$run_id,
			'GROUPBY' => '',
			'ORDERBY' => 'tx_nG6_run.date DESC',
			'LIMIT' 	=> ''
871
		); 
872
873
874
		// Then create the result hash table
		$result = array();
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
875
876
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {			
			$result = array( 
Jerome Mariette's avatar
Jerome Mariette committed
877
				'id'			=> $row['run_id'],  
878
879
880
				'project_id'	=> $row['project_id'], 
				'project_name'	=> $row['project_name'], 
				'directory'		=> $row['run_directory'], 
Jerome Mariette's avatar
Jerome Mariette committed
881
				'name' 			=> $row['run_name'],
Jerome Mariette's avatar
Jerome Mariette committed
882
				'hidden'		=> $row['run_hidden'],
Jerome Mariette's avatar
Jerome Mariette committed
883
884
				'species' 		=> $row['run_species'],
				'nb_sequences' 	=> $row['nb_sequences'],
Jerome Mariette's avatar
Jerome Mariette committed
885
				'full_seq_size' => $row['full_seq_size'],
Jerome Mariette's avatar
Jerome Mariette committed
886
				'date'			=> $row['run_date'],
Jerome Mariette's avatar
Jerome Mariette committed
887
888
889
				'data_nature' 	=> $row['run_data_nature'],
				'sequencer' 	=> $row['run_sequencer'],
				'type' 			=> $row['run_type'],
890
891
				'description' 	=> $row['run_description']				
			);					
892
893
894
		}
		return $result;
    }
895

896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
	/**
	 * 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;
		
	}	

923
924
925
926
927
928
	/**
	 * 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
929
    function get_run_analysis($run_id, $orderby='', $limit='') {
930
		// First select all analysis from the database
931
932
933
		$queryParts = array( 
			'SELECT' => 'tx_nG6_analyze.uid AS analyze_id,'. 
						'tx_nG6_analyze.directory AS analyze_directory,'. 
934
						'tx_nG6_analyze.name AS analyze_name, '.
935
936
						'tx_nG6_analyze.params AS analyze_params, '.
						'tx_nG6_analyze.class AS analyze_class, '.
937
						'tx_nG6_analyze.date AS analyze_date, '.
Jerome Mariette's avatar
Jerome Mariette committed
938
939
						'tx_nG6_analyze.software AS analyze_software, '.
						'tx_nG6_analyze.version AS analyze_version, '.
940
941
						'tx_nG6_analyze.hidden AS analyze_hidden, '.
						'tx_nG6_analyze.description AS analyze_description, '.
942
						'tx_nG6_analyze.is_editable AS analyze_is_editable, '.
943
						'tx_nG6_analyze.parent_uid AS analyze_parent_uid ',
Jerome Mariette's avatar
Jerome Mariette committed
944
			'FROM' => 'tx_nG6_run_analyze '.
945
						'INNER JOIN tx_nG6_analyze ON tx_nG6_analyze.uid = tx_nG6_run_analyze.analyze_id ',
Jerome Mariette's avatar
Jerome Mariette committed
946
			'WHERE' => 'tx_nG6_run_analyze.run_id='.$run_id,
947
			'GROUPBY' => '',
Jerome Mariette's avatar
Jerome Mariette committed
948
949
			'ORDERBY' => $orderby,
			'LIMIT' => $limit
950
951
952
953
954
955
		);
		// 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
956
			if (!isset ($results['analyse_'.$analyze_id])) {				
957
958
				$results['analyse_'.$analyze_id] = array( 
					'directory'		=> $row['analyze_directory'], 
Jerome Mariette's avatar
Jerome Mariette committed
959
					'name' 			=> $row['analyze_name'],
960
961
					'params' 		=> $row['analyze_params'],
					'class' 		=> $row['analyze_class'],
Jerome Mariette's avatar
Jerome Mariette committed
962
963
964
965
966
					'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
967
					'description' 	=> $row['analyze_description'],
968
					'is_editable' 	=> $row['analyze_is_editable'],
Jerome Mariette's avatar
Jerome Mariette committed
969
					'parent_id'		=> $row['analyze_parent_uid']
970
				);
971
			}						
972
973
974
975
		}
		return $results;
    }

Penom Nom's avatar
Penom Nom committed
976
977
978
979
980
981
982
983
984
985
986
987
988
989
    /**
    * Select all analysis linked to the specified run for the user
    *
    * @param	string	$user_id	the user id
    * @param	string	$run_id	the run id
    * @return	hash table with all analysis information
    */
    function get_user_run_analysis($user_id, $run_id, $orderby='', $limit='') {
    	// If the user is not logged on display demonstration project
    	if ($user_id == null) {
    		$where = 'tx_nG6_run_analyze.run_id='.$run_id.' AND tx_nG6_project.public=0 AND tx_nG6_analyze.hidden=0' ;
    	}
    	else {
    		$where = 'fe_users.uid='.$user_id.' AND '.'tx_nG6_run_analyze.run_id='.$run_id.
990
    						' AND ((fe_rights.right_id<>2 AND tx_nG6_analyze.hidden=0) OR fe_rights.right_id=2)' ;
Penom Nom's avatar
Penom Nom committed
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
    	}
    	
    	// First select all analysis from the database
    	$queryParts = array(
    			'SELECT' => 'tx_nG6_analyze.uid AS analyze_id,'. 
    						'tx_nG6_analyze.directory AS analyze_directory,'. 
    						'tx_nG6_analyze.name AS analyze_name, '.
    						'tx_nG6_analyze.params AS analyze_params, '.
    						'tx_nG6_analyze.class AS analyze_class, '.
    						'tx_nG6_analyze.date AS analyze_date, '.
    						'tx_nG6_analyze.software AS analyze_software, '.
    						'tx_nG6_analyze.version AS analyze_version, '.
    						'tx_nG6_analyze.hidden AS analyze_hidden, '.
    						'tx_nG6_analyze.description AS analyze_description, '.
1005
    						'tx_nG6_analyze.is_editable AS analyze_is_editable, '.
Penom Nom's avatar
Penom Nom committed
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
    						'tx_nG6_analyze.parent_uid AS analyze_parent_uid ',
    			'FROM' => 'tx_nG6_analyze '.
							'INNER JOIN tx_nG6_run_analyze ON tx_nG6_analyze.uid = tx_nG6_run_analyze.analyze_id '.
							'INNER JOIN tx_nG6_run ON tx_nG6_run_analyze.run_id = tx_nG6_run.uid '.
							'INNER JOIN tx_nG6_project_run ON tx_nG6_run.uid = tx_nG6_project_run.run_id '.
							'INNER JOIN tx_nG6_project ON tx_nG6_project_run.project_id = tx_nG6_project.uid '. 
							'INNER JOIN fe_rights ON tx_nG6_project.uid=fe_rights.project_id '.
							'INNER JOIN fe_users ON fe_rights.fe_user_id=fe_users.uid ',
    			'WHERE' => $where,
    			'GROUPBY' => '',
    			'ORDERBY' => $orderby,
    			'LIMIT' => $limit
    	);
    	// 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'];
    		if (!isset ($results['analyse_'.$analyze_id])) {
    			$results['analyse_'.$analyze_id] = array(
    					'directory'		=> $row['analyze_directory'], 
    					'name' 			=> $row['analyze_name'],
    					'params' 		=> $row['analyze_params'],
    					'class' 		=> $row['analyze_class'],
    					'id' 			=> $row['analyze_id'],
    					'hidden' 		=> $row['analyze_hidden'],
    					'software' 		=> $row['analyze_software'],
    					'version' 		=> $row['analyze_version'],
    					'date'			=> $row['analyze_date'],
    					'description' 	=> $row['analyze_description'],
1036
    					'is_editable' 	=> $row['analyze_is_editable'],
Penom Nom's avatar
Penom Nom committed
1037
1038
1039
1040
1041
1042
1043
    					'parent_id'		=> $row['analyze_parent_uid']
    			);
    		}
    	}
    	return $results;
    }

1044
1045
1046
1047
1048
1049
1050
1051
	/**
	 * 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
1052
	function delete_run($r_id, $user_login, $user_pwd, $data_folder) {		
1053
1054

		$res = 0;		
Jerome Mariette's avatar
Jerome Mariette committed
1055
		$run_analysis = tx_nG6_db::get_run_analysis($r_id);
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
		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;
	}

1094
1095
1096
1097
1098
    /**
     * 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
1099
	 * @param	boolean $get_analyzes	get analyzes size ? 
1100
     */
Jerome Mariette's avatar
Jerome Mariette committed
1101
	function get_run_size($r_id, $data_folder, $get_analyzes=false) {
1102
		$full_size = 0;
Jerome Mariette's avatar
Jerome Mariette committed
1103
1104
1105
1106
1107
		if($get_analyzes){
			$run_analysis = tx_nG6_db::get_run_analysis($r_id);
			foreach($run_analysis as $analyse_id => $analyze_values) {
				$full_size += tx_nG6_db::get_analysis_size($analyze_values['id'], $data_folder);
			}
1108
1109
1110
		}
		// Then select the run
		$my_run = tx_nG6_db::select_run($r_id);
Jerome Mariette's avatar
Jerome Mariette committed
1111
		foreach(scandir($data_folder.$my_run['directory']) as $file) {			
1112
1113
1114
			if (is_file($data_folder.$my_run['directory']."/".$file)) {
				$full_size += filesize($data_folder.$my_run['directory']."/".$file);
			}
Jerome Mariette's avatar
Jerome Mariette committed
1115
		}		
1116
1117
1118
		return $full_size;
	}

1119
1120
1121
1122
1123
    /**
     * Hide a run and sublevels (analysis)
     * 
     * @param	string	$r_id	the run id to hide
     */
Jerome Mariette's avatar
Jerome Mariette committed
1124
	function hide_run($r_id) {
1125
1126

		// First select all run analysis
Jerome Mariette's avatar
Jerome Mariette committed
1127
    	$run_analysis = tx_nG6_db::get_run_analysis($r_id);
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
		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
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
	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')
			);
		}
1151
1152

		// Then all run analysis
Jerome Mariette's avatar
Jerome Mariette committed
1153
    	$run_analysis = tx_nG6_db::get_run_analysis($r_id);
1154
1155
		foreach($run_analysis as $analysis_id => $analysis_values) {  
			tx_nG6_db::unhide_analysis($analysis_values['id']);
Jerome Mariette's avatar
Jerome Mariette committed
1156
		}
1157
1158
1159
1160
		
		// Finaly change the run itself
		$GLOBALS['TYPO3_DB']-> exec_UPDATEquery ('tx_nG6_run', 'uid='.$r_id, array('hidden' => '0'));
	}
1161

Jerome Mariette's avatar
Jerome Mariette committed
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
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
    /**
     * Get the run min date
     * 
     * @return int timestamp
     */
    function get_run_min_date(){
    	$queryParts = Array(
    		'SELECT' 	=> 'MIN(tx_nG6_run.date) as min_date ',
    		'FROM' 		=> 'tx_nG6_run',
    		'WHERE' 	=> 'tx_nG6_run.date!=0',
    		'GROUPBY' 	=> '',
    		'ORDERBY' 	=> '',
    		'LIMIT' 	=> ''
    	);
    	$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
   		$min_date = 0;
    	if($GLOBALS['TYPO3_DB']->sql_num_rows($res) == 1) {
    		$tab = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
    		$min_date = intval($tab['min_date']);
    	}else{
    		$min_date = -1;
    	}
    	return $min_date;
    }
    
    /**
    * Get the run max date
    *
    * @return int timestamp
    */
    function get_run_max_date(){
    	$queryParts = Array(
        		'SELECT' 	=> 'MAX(tx_nG6_run.date) as max_date ',
        		'FROM' 		=> 'tx_nG6_run',
        		'WHERE' 	=> 'tx_nG6_run.date!=0',
        		'GROUPBY' 	=> '',
        		'ORDERBY' 	=> '',
        		'LIMIT' 	=> ''
    	);
    	$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	$max_date = 0;
    	if($GLOBALS['TYPO3_DB']->sql_num_rows($res) == 1) {
    		$tab = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
    		$max_date = intval($tab['max_date']);
    	}else{
    		$max_date = -1;
    	}
    	return $max_date;
    }
    
1212
	/*
1213
	 * Analysis functions
1214
	 *------------------------------------------------------------*/
1215
	 
1216
1217
1218
1219
1220
1221
	/**
	 * 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
1222
1223
    function select_analyse($analyse_id) {

1224
		// First select all analyse information
1225
1226
1227
1228
1229
		$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,'. 
1230
						'tx_nG6_analyze.name AS analyze_name, '.
1231
						'tx_nG6_analyze.params AS analyze_params, '.
Jerome Mariette's avatar
Jerome Mariette committed
1232
						'tx_nG6_analyze.software AS analyze_software, '.
1233
						'tx_nG6_analyze.class AS analyze_class, '.
1234
						'tx_nG6_analyze.date AS analyze_date, '.
Jerome Mariette's avatar
Jerome Mariette committed
1235
1236
						'tx_nG6_analyze.description AS analyze_description, '.
						'tx_nG6_analyze.version AS analyze_version, '.
1237
						'tx_nG6_analyze.is_editable AS analyze_is_editable, '.
Jerome Mariette's avatar
Jerome Mariette committed
1238
						'tx_nG6_analyze.parent_uid AS analyze_parent_uid ',
1239
1240
1241
			'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 ',
1242
1243
1244
1245
1246
			'WHERE' => 'tx_nG6_analyze.uid='.$analyse_id,
			'GROUPBY' => '',
			'ORDERBY' => '',
			'LIMIT' => ''
		);
1247
		
1248
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
1249
		
1250
1251
1252
		// If it's a project analyse, add project information
		if ($GLOBALS['TYPO3_DB']->sql_num_rows($res) > 0) {
			$result = array();
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
			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
1264
1265
1266
					'description' 	=> $row['analyze_description'],
					'parent_id'		=> $row['analyze_parent_uid'],
					'software'		=> $row['analyze_software'],
1267
1268
					'version'		=> $row['analyze_version'],
					'is_editable'   => $row['analyze_is_editable']
1269
				);				
1270
1271
1272
			}
			return $result;
		} else { // If it's a run analyse, add run information
1273
			$queryParts = array( 
1274
1275
				'SELECT' => 'tx_nG6_project.name AS project_name,'.
							'tx_nG6_project.uid AS project_id,'.
1276
							'tx_nG6_run.uid AS run_id,'. 
1277
							'tx_nG6_run.name AS run_name,'.
1278
1279
							'tx_nG6_analyze.uid AS analyze_id,'. 
							'tx_nG6_analyze.directory AS analyze_directory,'. 
1280
							'tx_nG6_analyze.name AS analyze_name, '.
1281
1282
							'tx_nG6_analyze.params AS analyze_params, '.
							'tx_nG6_analyze.class AS analyze_class, '.
1283
							'tx_nG6_analyze.date AS analyze_date, '.
Jerome Mariette's avatar
Jerome Mariette committed
1284
1285
1286
							'tx_nG6_analyze.software AS analyze_software, '.
							'tx_nG6_analyze.version AS analyze_version, '.
							'tx_nG6_analyze.description AS analyze_description,'.
1287
							'tx_nG6_analyze.is_editable AS analyze_is_editable,'.
Jerome Mariette's avatar
Jerome Mariette committed
1288
							'tx_nG6_analyze.parent_uid AS analyze_parent_uid ',
1289
				'FROM' => 'tx_nG6_project '.
Jerome Mariette's avatar
Jerome Mariette committed
1290
							'INNER JOIN tx_nG6_project_run ON tx_nG6_project_run.project_id = tx_nG6_project.uid '.
1291
1292
1293
							'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 ',
1294
1295
1296
1297
1298
1299
1300
				'WHERE' => 'tx_nG6_analyze.uid='.$analyse_id,
				'GROUPBY' => '',
				'ORDERBY' => 'tx_nG6_analyze.name',
				'LIMIT' => ''
			);
			$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
			$result = array();
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
			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
1312
1313
1314
					'description' 	=> $row['analyze_description'],
					'software'		=> $row['analyze_software'],
					'parent_id'		=> $row['analyze_parent_uid'],
1315
1316
					'version'		=> $row['analyze_version'], 
					'is_editable'	=> $row['analyze_is_editable']
1317
				);				
1318
1319
1320
1321
			}
			return $result;
		}
    }
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
	/**
	 * 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;
    }
1356

1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
    /**
     * 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;
	}

1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
    /**
     * 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
1390
1391
1392
1393
1394
1395
1396