class.tx_nG6_db.php 110 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
	}
53
	
Gerald Salin's avatar
Gerald Salin committed
54
	/**
55
56
57
58
59
60
61
62
63
64
65
66
67
68
	 * Update run and analyze for purge managment
	 * 
	 * @param	tab	$all_runs_ids	the entire runs ids to change
	 * @param	tab	$all_analyses_ids	the entire analyses ids to change
	 * @param	string	$field	the field to change
	 * @param	string	$value	the new value
	 */
	 
	function update_purge_date( $all_runs_ids,  $all_analyses_ids, $field, $value){
				
				foreach(explode(",", $all_runs_ids) as $run_id){
					tx_nG6_db::update_field('tx_nG6_run', $run_id, $field, $value);
				}
				
69
				error_log("RETENTION DATE run:".$all_runs_ids." analyse:".$all_analyses_ids." filed: ".$field." value".$value." \n",3,"/save/ng6-test/src/nG6/ui/nG6/pi6/test.log");
70
71
72
73
74
				foreach(explode(",", $all_analyses_ids) as $analyse_id){
					tx_nG6_db::update_field('tx_nG6_analyze',$analyse_id,$field,$value);
				}
	}
	
75

76
	function select_all_in_view($view, $where) {
77
		$queryParts = array(
78
79
80
					'SELECT' 	=> '*',
					'FROM' 		=> $view,
					'WHERE' 	=> $where ,
81
					'GROUPBY' 	=> '',
Gerald Salin's avatar
Gerald Salin committed
82
83
					'ORDERBY' 	=> '',
					'LIMIT' 	=> ''
84
			);	
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
			$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
			return ($res);
	}
	
	function select_count_in_view($view,$where) {
			#select count All runs per project
		$queryParts = array(
					'SELECT' 	=> 'project_id , count(*) AS count',
					'FROM' 		=> $view ,
					'WHERE' 	=> $where,
					'GROUPBY' 	=> 'project_id',
					'ORDERBY' 	=> '',
					'LIMIT' 	=> ''
			);
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	return ($res);
Gerald Salin's avatar
Gerald Salin committed
101

102
103
104
	}


105
	function get_project_list_by_group_filter($create_user_id, $user_group_id){
106
107
108
109
		
		$where="";
		$list_project_id_filter_by_group="";
		$list_project_id_laboratories="";
110
		if($user_group_id !=""){
111
112
113
114
			#select all project_id with group OR/AND without group
			$queryParts=array(
				'SELECT' => 'project_id',
				'FROM' => 'tx_nG6_view_project_user',
115
				'WHERE' => 'user_group IN ( '.$user_group_id .')',
116
117
118
119
120
121
122
				'GROUPBY' 	=> '',
				'ORDERBY' 	=> '',
				'LIMIT' 	=> ''
			);
		
			$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
			while($res_row= $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
123
				if( $list_project_id_filter_by_group != ""){
124
					$list_project_id_filter_by_group .= ",".$res_row['project_id'];
125
126
				}else{
					$list_project_id_filter_by_group = $res_row['project_id'];
127
128
				}
			}
129
			$where .= "uid NOT IN (".$list_project_id_filter_by_group.")";
130
131
132
133
		}
			
		if($create_user_id != ""){
			if($where != ""){
134
				$where .= " AND cruser_id IN (".$create_user_id.")";
135
136
137
138
139
140
141
			}else{
				$where .= "cruser_id IN (".$create_user_id.")";
			}
		}
		
		$queryParts=array(
			'SELECT' => 'uid AS project_id',
142
			'FROM' => 'tx_nG6_project',
143
144
145
146
147
148
149
150
			'WHERE' => $where,
			'GROUPBY' 	=> '',
			'ORDERBY' 	=> '',
			'LIMIT' 	=> ''
		);
		
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
151
152
			if ($list_project_id_laboratories != "") {
				$list_project_id_laboratories .=",".$res_row['project_id'];
153
			}else{
154
				$list_project_id_laboratories=$res_row['project_id'];
155
156
157
				
			}
		}
158
159
		error_log("WHERE : ".$where."\n",3,"/save/ng6-test/src/nG6/ui/nG6/pi6/test.log");
		error_log("LIST project : ".$list_project_id_laboratories."\n",3,"/save/ng6-test/src/nG6/ui/nG6/pi6/test.log");
160
161
162
    	return ($list_project_id_laboratories);
		
	}
163
	
164
165
166
167
	function select_obsolete_list_retention_data_info() {
		$where =  ' DATEDIFF( FROM_UNIXTIME( retention_date ) , now( ) ) <0';
		return (tx_nG6_db::select_list_retention_data_info($where));
	}
168
169
170
	
	function filter_list_retention_data_info($filter_size, $max_retention_date, $create_user_id, $user_group_id) {
		
171
172
173
174
175
176
177
178
		$where =  ' DATEDIFF( FROM_UNIXTIME( retention_date ) , now( ) ) <0';
		if ($max_retention_date != "") {
			$where = ' DATEDIFF( FROM_UNIXTIME( retention_date ) , STR_TO_DATE( "'.$max_retention_date.'", "%d/%m/%Y" ) ) <0';
		}
		if ($filter_size != "") {
			$where .=" AND storage_size > $filter_size";
		}
		$project_list_ids = "";
179
180
181
182
183
184
		error_log("user group".$user_group_id."\n",3,"/save/ng6-test/src/nG6/ui/nG6/pi6/test.log");
		
		if ($create_user_id != "" or $user_group_id != "") {
			error_log(" \nFILTER USER ".$create_user_id."\n",3,"/save/ng6-test/src/nG6/ui/nG6/pi6/test.log");
			error_log(" \nFILTER GROUP ID".$user_group_id."\n",3,"/save/ng6-test/src/nG6/ui/nG6/pi6/test.log");
			$project_list_ids = tx_nG6_db::get_project_list_by_group_filter($create_user_id,$user_group_id);
185
186
			$where .=" AND project_id in (" . $project_list_ids.")" ;
		}
187
		error_log("FILTER - WHERE : ".$where."\n",3,"/save/ng6-test/src/nG6/ui/nG6/pi6/test.log");
188
189
190
191
192
193
194
195
196
197
198
199
		return (tx_nG6_db::select_list_retention_data_info($where));
	}
	
	function select_a_project_retention_data_info($project_id) {
		
		$where =" project_id = " . $project_id ;
		return(tx_nG6_db::select_list_retention_data_info($where));
		
	}
	
	function select_list_retention_data_info($where) {
		$by_project=array();
200
		
201
202
		$res = tx_nG6_db::select_all_in_view( "tx_nG6_view_project_run" , $where);
		while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
203
			if (!isset ($by_project[$res_row['project_id']])) {
204
205
206
207
208
209
210
				$by_project[$res_row['project_id']]= array(
						'project_id'			=> $res_row['project_id'],
						'project_name'			=> $res_row['project_name'],
						'state'					=> array( $res_row['state']  => array ('nb_run' => 1, 'size_run' => $res_row['storage_size']+$res_row['purged_size'], 'run_ids'=> $res_row['run_id'])),
						'total_purgeable_size'	=>  $res_row['storage_size'],
						'nb_runs'				=>0,
						'nb_analyses'				=>0);
Gerald Salin's avatar
Gerald Salin committed
211
212
213
			}
			else
			{
214
215
216
				if (!isset ($by_project[$res_row['project_id']]['state'][$res_row['state']])) {
					$by_project[$res_row['project_id']]['state'][$res_row['state']]  = array ('nb_run' => 1, 'size_run' =>$res_row['storage_size']+$res_row['purged_size'], 'run_ids'=>$res_row['run_id']);
					$by_project[$res_row['project_id']]['total_purgeable_size'] += $res_row['storage_size'];
217
				} else {
218
219
220
221
					$by_project[$res_row['project_id']]['state'][$res_row['state']]['nb_run']+=1;
					$by_project[$res_row['project_id']]['state'][$res_row['state']]['size_run']+=$res_row['storage_size']+$res_row['purged_size'];
					$by_project[$res_row['project_id']]['state'][$res_row['state']]['run_ids'].=",".$res_row['run_id'];
					$by_project[$res_row['project_id']]['total_purgeable_size'] += $res_row['storage_size'];
222
				}
Gerald Salin's avatar
Gerald Salin committed
223
224
225
			}
		}
		
226
		
227
228
229
230
231
232
233
234
235
236
237
238
239
		$res = tx_nG6_db::select_all_in_view("tx_nG6_view_project_run_analyze",$where);
		while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			if (!isset ($by_project[$res_row['project_id']])) {
				
    			$by_project[$res_row['project_id']]= array(
    					'project_id'		=> $res_row['project_id'],
    					'project_name'	=> $res_row['project_name'],
    					'state'				=> array($res_row['state'] => array("nb_analyze"=> 1, 
																						"size_analyze"=> $res_row['storage_size']+$res_row['purged_size'], 
																						'analysis_ids'=> $res_row['analyze_id'])),
						'total_purgeable_size'	=>  $res_row['storage_size'],
						'nb_runs'				=>0,
						'nb_analyses'				=>0);
Gerald Salin's avatar
Gerald Salin committed
240
				
241
242
243
			}else{
			
			
244
			if (!isset ($by_project[$res_row['project_id']]['state'][$res_row['state']]['analysis_ids'])){
245
246
247
248
249
				if (!isset($by_project[$res_row['project_id']]['state'][$res_row['state']])){
					$by_project[$res_row['project_id']]['state'][$res_row['state']] = array("nb_analyze"=>1, "size_analyze"=>$res_row['storage_size']+$res_row['purged_size'],'analysis_ids'=>$res_row['analyze_id']);
				}else{
					$by_project[$res_row['project_id']]['state'][$res_row['state']] = array_merge($by_project[$res_row['project_id']]['state'][$res_row['state']],array("nb_analyze"=>1, "size_analyze"=>$res_row['storage_size']+$res_row['purged_size'],'analysis_ids'=>$res_row['analyze_id']));
				}
250
				$by_project[$res_row['project_id']]['total_purgeable_size'] += $res_row['storage_size'];
251
			} else {				
252
253
254
255
				$by_project[$res_row['project_id']]['state'][$res_row['state']]['nb_analyze'] += 1;
				$by_project[$res_row['project_id']]['state'][$res_row['state']]['size_analyze'] += $res_row['storage_size']+$res_row['purged_size'];
				$by_project[$res_row['project_id']]['state'][$res_row['state']]['analysis_ids'].=",".$res_row['analyze_id'];
				$by_project[$res_row['project_id']]['total_purgeable_size'] += $res_row['storage_size'];
Gerald Salin's avatar
Gerald Salin committed
256
			}
257
			}
258
		}		
Gerald Salin's avatar
Gerald Salin committed
259
		
260
		
261
262
263
264
265
266
267
268
269
270
		$res = tx_nG6_db::select_all_in_view("tx_nG6_view_project_analyze", $where);
		while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			if (!isset ($by_project[$res_row['project_id']])) {
    			$by_project[$res_row['project_id']]= array(
    					'project_id'		=> $res_row['project_id'],
    					'project_name'		=> $res_row['project_name'],
    					'state'				=> array(),
    					'total_purgeable_size'	=>  0,
    					'nb_runs'				=>0,
						'nb_analyses'				=>0);
271
			}
272
			if (!isset ($by_project[$res_row['project_id']]['state'][$res_row['state']]['analysis_ids'])){
273
274
275
276
277
				if (!isset($by_project[$res_row['project_id']]['state'][$res_row['state']])){
					$by_project[$res_row['project_id']]['state'][$res_row['state']] = array("nb_analyze"=>1, "size_analyze"=>$res_row['storage_size']+$res_row['purged_size'],'analysis_ids'=>$res_row['analyze_id']);
				}else{
					$by_project[$res_row['project_id']]['state'][$res_row['state']] = array_merge($by_project[$res_row['project_id']]['state'][$res_row['state']],array("nb_analyze"=>1, "size_analyze"=>$res_row['storage_size']+$res_row['purged_size'],'analysis_ids'=>$res_row['analyze_id']));
				}
278
				$by_project[$res_row['project_id']]['total_purgeable_size'] += $res_row['storage_size'];
Gerald Salin's avatar
Gerald Salin committed
279
280
281
			}
			else
			{
282
283
284
285
				$by_project[$res_row['project_id']]['state'][$res_row['state']]['nb_analyze'] += 1;
				$by_project[$res_row['project_id']]['state'][$res_row['state']]['size_analyze'] += $res_row['storage_size']+$res_row['purged_size'];
				$by_project[$res_row['project_id']]['state'][$res_row['state']]['analysis_ids'].=",".$res_row['analyze_id'];
				$by_project[$res_row['project_id']]['total_purgeable_size'] += $res_row['storage_size'];
Gerald Salin's avatar
Gerald Salin committed
286
287
			}
		}
288
289
290
291
292
		
		$res = tx_nG6_db::select_count_in_view("tx_nG6_view_project_run","");
		while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			if (isset ($by_project[$res_row['project_id']])) {
				$by_project[$res_row['project_id']]["nb_runs"]=$res_row['count'];
293
294
			}
		}
295
296
297
298
299
		
		$res = tx_nG6_db::select_count_in_view("tx_nG6_view_project_run_analyze","");
		while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			if (isset ($by_project[$res_row['project_id']])) {
				$by_project[$res_row['project_id']]["nb_analyses"]=$res_row['count'];
300
301
302
			}
		}

303
304
305
306
307
308
309
310
		$res = tx_nG6_db::select_count_in_view("tx_nG6_view_project_analyze","");
		while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			if (isset ($by_project[$res_row['project_id']])) {
				if (!isset ($by_project[$res_row['project_id']]["nb_analyses"])) {
					$by_project[$res_row['project_id']]["nb_analyses"]=$res_row['count'];
				}else{
					$by_project[$res_row['project_id']]["nb_analyses"]+=$res_row['count'];
				}
Gerald Salin's avatar
Gerald Salin committed
311
312
			}
		}
313
314
315
316
317
318
319
320
		
		#retrieve user per project
		$res = tx_nG6_db::select_all_in_view("tx_nG6_view_project_user", "");
		while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			if(isset($by_project[$res_row['project_id']])){
				if (!isset ($by_project[$res_row['project_id']]["users"])) {
					$by_project[$res_row['project_id']]["users"]=array();
				}
321
				array_push($by_project[$res_row['project_id']]["users"],array ('user_name' => $res_row['user_name'], 'email' =>$res_row['email'], 'groups' =>$res_row['user_group'], 'right_level_label' =>$res_row['right_level_label'], 'user_group_title' => $res_row['user_group_title']));
322
323
324
325
326
			}
		}
		
		return($by_project);
	}
Gerald Salin's avatar
Gerald Salin committed
327

Jerome Mariette's avatar
Jerome Mariette committed
328
		
329
330
331
    /**
     * Select all project for the current user
     *
332
     * @param	string	$by	by can group, organism, location
333
334
     * @param	string	$role	role can be create_user, manager
     * @return	table with all projects
335
336
337
338
     */
    function select_projects_repartition($role, $by='title') {
    	
    	if ($role == "create_user") {
339
    		$queryParts = array(
340
    				'SELECT' 	=> 'tx_nG6_project.uid as pid, fe_groups.title as title, fe_groups.uid as group_id , fe_groups.tx_nG6_organism as organism, fe_groups.tx_nG6_location as location',
341
342
343
344
345
346
347
    				'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' 	=> ''
348
349
350
351
    		);
    		//$where = "tx_nG6_project.cruser_id="
    	} else if ($role == "manager") {
	    	$queryParts = array(
352
    				'SELECT' 	=> 'tx_nG6_project.uid as pid, fe_groups.title as title, fe_groups.uid as group_id , fe_groups.tx_nG6_organism as organism, fe_groups.tx_nG6_location as location',
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
	    			'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)) {
368
369
370
    		if (!isset ($by_title[$res_row['title']])) {
    			$by_title[$res_row['title']] = array(
    					'count'			=> 1,
371
    					'group_id'	=> $res_row['group_id'],
372
373
    					'organism'		=> $res_row['organism'],
    					'location'		=> $res_row['location']);
374
375
376
377
    		} else {
    			$by_title[$res_row['title']]['count'] += 1;
    		}
    		
378
379
380
381
382
    		if (!isset ($by_organism[$res_row['organism']])) {
    			$by_organism[$res_row['organism']] = array(
    					'count'			=> 1);
    		} else {
    			$by_organism[$res_row['organism']]['count'] += 1;
383
384
    		}
    		
385
386
387
388
389
390
    		if (!isset ($by_location[$res_row['location']])) {
    			$by_location[$res_row['location']] = array(
    					'count'			=> 1);
    		} else {
    			$by_location[$res_row['location']]['count'] += 1;
    		}
391
392
393
394
395
396
397
398
399
    	}
    	if ($by=='title') {
    		return ($by_title);
    	} else if ($by=='organism') {
    		return ($by_organism);
    	} else if ($by=='location') {
    		return ($by_location);
    	}
    }
Jerome Mariette's avatar
Jerome Mariette committed
400

401
    function select_projects_evolution($values, $by, $role, $cumulate) {
Penom Nom's avatar
Penom Nom committed
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
		if ($by == "organism" || $by == "location") {
			$by = "tx_nG6_".$by;
		}
	
		$from = "" ;
		$where = "fe_groups.".$by." IN ('".implode("', '", $values)."')" ;
    	if ($role == "create_user") {
    		$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 ' ;
    	} else if ($role == "manager") {
    		$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 ';
Jerome Mariette's avatar
Jerome Mariette committed
417
    		$where .= " AND fe_rights.right_id=1" ;
Penom Nom's avatar
Penom Nom committed
418
419
    	}
    	$queryParts = array(
Jerome Mariette's avatar
Jerome Mariette committed
420
    			'SELECT' 	=> 'tx_nG6_project.crdate, count(tx_nG6_project.crdate) nb',
Penom Nom's avatar
Penom Nom committed
421
422
    			'FROM' 		=> $from,
    			'WHERE' 	=> $where,
Jerome Mariette's avatar
Jerome Mariette committed
423
424
    			'GROUPBY' 	=> 'tx_nG6_project.crdate',
    			'ORDERBY' 	=> 'tx_nG6_project.crdate',
Penom Nom's avatar
Penom Nom committed
425
426
427
428
429
    			'LIMIT' 	=> ''
    	);
    	$results = array();
    	$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
430
431
432
433
434
    		if ($cumulate && count($results) > 0) {
    			$results[] = array($res_row["crdate"], $res_row["nb"] + $results[count($results)-1][1]);
    		} else {
    			$results[] = array($res_row["crdate"], $res_row["nb"]);
    		}
Penom Nom's avatar
Penom Nom committed
435
436
437
438
439
    	}

    	return $results;
    }

440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
    function select_projects_distribution($values, $by, $role) {
		$from = "" ;
		$where = "" ;
		if ($by == "organism" || $by == "location") {
			$by = "tx_nG6_".$by;
		}
    	if ($role == "create_user") {
    		$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 ' ;
    	} else if ($role == "manager") {
    		$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" ;
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
    	$queryParts = array(
    			'SELECT' 	=> 'fe_groups.'.$by.', count(fe_groups.'.$by.') nb',
    			'FROM' 		=> $from,
    			'WHERE' 	=> $where,
    			'GROUPBY' 	=> 'fe_groups.'.$by,
    			'ORDERBY' 	=> '',
    			'LIMIT' 	=> ''
    	);
    	$results = array();
    	$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	$others = 0 ;
    	while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
    		if( in_array($res_row[$by], $values) ) {
    			$results[] = array($res_row[$by], $res_row["nb"]);
    		} else {
    			$others += $res_row["nb"] ;
    		}
    	}
    	if ($others != 0) { 
    		$results[] = array("OTHERS", $others);
    	}
    	return $results;
    }
    
481
482
483
    function select_storage_distribution($values, $by, $role, $get_analyzes, $octet) {
    	if ($by == "organism" || $by == "location") {
    		$by = "tx_nG6_".$by;
Jerome Mariette's avatar
Jerome Mariette committed
484
485
486
487
488
    	}
    	$storage_unit = "full_seq_size";
    	if ($octet) {
    		$storage_unit = "storage_size";
    	}
489
490
491
    	$from = "" ;
    	$where = "" ;
    	if ($role == "create_user") {
Jerome Mariette's avatar
Jerome Mariette committed
492
493
    		$from = 'tx_nG6_project '
    		.' INNER JOIN tx_nG6_project_run ON tx_nG6_project_run.project_id= tx_nG6_project.uid'
494
495
496
497
    		.' INNER JOIN tx_nG6_run ON tx_nG6_project_run.run_id= tx_nG6_run.uid'
    		.' INNER JOIN fe_users ON tx_nG6_project.cruser_id=fe_users.uid '
    		.' INNER JOIN fe_groups ON fe_groups.uid=fe_users.usergroup ' ;
    	} else if ($role == "manager") {
Jerome Mariette's avatar
Jerome Mariette committed
498
    		$from = 'tx_nG6_project '
499
    		.' INNER JOIN tx_nG6_project_run ON tx_nG6_project_run.project_id= tx_nG6_project.uid'
Jerome Mariette's avatar
Jerome Mariette committed
500
    		.' INNER JOIN tx_nG6_run ON tx_nG6_project_run.run_id= tx_nG6_run.uid'
501
502
503
504
505
506
507
508
509
510
511
512
    		.' 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" ;
    	}
    	$queryParts = array(
    			'SELECT' 	=> 'tx_nG6_run.uid, tx_nG6_run.'.$storage_unit.', fe_groups.'.$by,
    			'FROM' 		=> $from,
    			'WHERE' 	=> $where,
    			'GROUPBY' 	=> '',
    			'ORDERBY' 	=> '',
    			'LIMIT' 	=> ''
Jerome Mariette's avatar
Jerome Mariette committed
513
    	);
514
515
516
517
518
    	
    	$results = array();
    	$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	$others = 0 ;
    	while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
Jerome Mariette's avatar
Jerome Mariette committed
519
			
520
521
522
523
524
525
    		$analyses_size = 0;
    		if ($get_analyzes && $octet) {
    			$run_analysis = tx_nG6_db::get_run_analysis($res_row["uid"]);
    			foreach($run_analysis as $analyse_id => $analyze_values) {
    				$analyses_size += intval($analyze_values["storage_size"]);
    			}
Jerome Mariette's avatar
Jerome Mariette committed
526
527
528
    		}
    		if( in_array($res_row[$by], $values) ) {
    			
529
    			if( array_key_exists( $res_row[$by], $results) ) {
Jerome Mariette's avatar
Jerome Mariette committed
530
531
532
    				$results[$res_row[$by]] += $res_row[$storage_unit] + $analyses_size;
    			} else {
    				$results[$res_row[$by]] = $res_row[$storage_unit] + $analyses_size;
533
534
535
536
537
538
539
    			}
    		} else {
    			$others += $res_row[$storage_unit] + $analyses_size;
    		}
    	}
    	if ($others != 0) {
    		$results["OTHERS"] = $others;
Jerome Mariette's avatar
Jerome Mariette committed
540
541
    	}
    	$final_results = array();
542
543
544
545
    	foreach($results as $group_key => $group_values) {
    		$final_results[] = array( $group_key, $group_values );
    	}
    	return $final_results;
546
547
    }
    
548
	function select_storage_evolution($values, $by, $role, $get_analyzes, $octet, $cumulate) {
549
550
551
552
553
        if ($by == "organism" || $by == "location") {
                $by = "tx_nG6_".$by;
        }
        $storage_unit = "full_seq_size";
        if ($octet) {
554
        	$storage_unit = "storage_size";
555
556
557
        }
        $from = "" ;
        $where = "fe_groups.".$by." IN ('".implode("', '", $values)."')" ;
558
       
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
        if ($role == "create_user") {
                $from = 'tx_nG6_project '
                .' INNER JOIN tx_nG6_project_run ON tx_nG6_project_run.project_id= tx_nG6_project.uid'
                .' INNER JOIN tx_nG6_run ON tx_nG6_project_run.run_id= tx_nG6_run.uid'
                .' INNER JOIN fe_users ON tx_nG6_project.cruser_id=fe_users.uid '
                .' INNER JOIN fe_groups ON fe_groups.uid=fe_users.usergroup ' ;
        } else if ($role == "manager") {
                $from = 'tx_nG6_project '
                .' INNER JOIN tx_nG6_project_run ON tx_nG6_project_run.project_id= tx_nG6_project.uid'
                .' INNER JOIN tx_nG6_run ON tx_nG6_project_run.run_id= tx_nG6_run.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 '
                .' INNER JOIN fe_groups ON fe_groups.uid=fe_users.usergroup ';
                $where .= " AND fe_rights.right_id=1" ;
        }
        $queryParts = array(
                        'SELECT'        => 'tx_nG6_run.uid, tx_nG6_run.crdate, tx_nG6_run.'.$storage_unit.', tx_nG6_run.purged_size, fe_groups.'.$by,
                        'FROM'          => $from,
                        'WHERE'         => $where,
                        'GROUPBY'       => '',
                        'ORDERBY'       => 'tx_nG6_run.crdate',
                        'LIMIT'         => ''
        );
Jerome Mariette's avatar
Jerome Mariette committed
582

583
584
585
586
587
588
589
590
591
592
593
594
595
596
        $results = array();
        $res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
        while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
                $analyses_size = 0;
                $analyses_size_purged = 0;
                if ($get_analyzes && $octet) {
                        $run_analysis = tx_nG6_db::get_run_analysis($res_row["uid"]);
                        foreach($run_analysis as $analyse_id => $analyze_values) {
                                $analyses_size += intval($analyze_values["storage_size"]);
                                $analyses_size_purged += intval($analyze_values["purged_size"]) + intval($analyze_values["storage_size"]);
                        }
                }

                if( array_key_exists( $res_row["crdate"], $results) ) {
597
598
                        $results[$res_row["crdate"]][0] += $res_row[$storage_unit] + $analyses_size;
                        $results[$res_row["crdate"]][1] += $res_row["purged_size"] + $analyses_size_purged;
599
600
601
602
                } else {
                        $results[$res_row["crdate"]] = array($res_row[$storage_unit] + $analyses_size, $res_row["purged_size"] + $analyses_size_purged);
                }
        }
603
        
604
605
606
607
608
609
610
611
612
613
        $final_results = array();
        foreach($results as $group_key => $group_values) {
                if ($cumulate && count($final_results) > 0) {
                        $final_results[] = array( $group_key, $group_values[0] + $final_results[count($final_results)-1][1],$group_values[0] + $group_values[1] + $final_results[count($final_results)-1][2]);
                } else {
                        $final_results[] = array( $group_key, $group_values[0], $group_values[0] + $group_values[1] );
                }

        }
        return $final_results;
Jerome Mariette's avatar
Jerome Mariette committed
614
    }
615

616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
	/**
	 * Add a comment to the comment table
	 * @param unknown $table_name
	 * @param unknown $id_label
	 * @param unknown $element_id
	 * @param unknown $cruser_id
	 * @param unknown $new_comment
	 */
    function add_comment($table_name, $id_label, $id, $cruser_id, $new_comment){
    	$new_comment = htmlentities($new_comment, ENT_QUOTES | ENT_COMPAT | ENT_HTML5);
    	$comment_data = array(
    	 		'cruser_id'			=>	$cruser_id,
    	 		'comment'			=>	$new_comment,
    	 		'tstamp'			=> 	time(),
    	 		'crdate'			=> 	time()
    	);
    	$GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_nG6_comment', $comment_data);
    	$comment_id =  $GLOBALS['TYPO3_DB']->sql_insert_id();
    	
    	$comment_link_data = array(
    	 		'cruser_id'		=>	$cruser_id,
    	 		$id_label		=>	$id,
    			'comment_id'	=> 	$comment_id,
    	 		'tstamp'		=> 	time(),
    	 		'crdate'		=> 	time()
    	);
    	
    	$GLOBALS['TYPO3_DB']->exec_INSERTquery($table_name, $comment_link_data);
    	return $GLOBALS['TYPO3_DB']->sql_insert_id();
    }

    /**
     * Retrieve all comments from a given project, run or analysis
     * @param unknown $table_name
     * @param unknown $id_label
     * @param unknown $uid
     * @return multitype:multitype:unknown
     */
    function get_all_comments($table_name, $id_label, $id){
    	$comments = array();
    	$temp = array();
    	$queryParts = array(
    			'SELECT' 	=> 'tx_nG6_comment.uid AS comment_id, '.
    			'tx_nG6_comment.comment AS comment, '.
    			'tx_nG6_comment.cruser_id AS cruser_id, '.
    			'tx_nG6_comment.crdate AS comment_crdate ',
    			'FROM' 		=> $table_name .' INNER JOIN tx_nG6_comment ON '. $table_name . '.comment_id=tx_nG6_comment.uid',
    			'WHERE' 	=> $table_name . '.' . $id_label . '=' . $id,
    			'ORDERBY' 	=> 'comment_id',
    	);
    	 
    	$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
    		$comment_id = $row['comment_id'];
    		if (!isset ($temp['comment_'.$comment_id])) {
    			$temp['comment_'.$comment_id] = '';
    			$user_info = tx_nG6_db::get_user_informations($row['cruser_id']);
    			$comments[] = array(
    					'id'			=> $comment_id,
    					'comment'		=> html_entity_decode($row['comment'], ENT_QUOTES | ENT_COMPAT | ENT_HTML5),
    					'date'			=> date( 'd.M.Y-H:i' ,$row['comment_crdate']),
    					'cruser_info'	=> $user_info,
    			);
    		}
    	}
    	return $comments;
    }
    
    /**
     * Delete a given commentary from the database
     * @param unknown $table_name
     * @param unknown $id_label
     * @param unknown $id
     * @param unknown $comment_id
     */
    function delete_comment($table_name, $id_label, $id, $comment_id){
    	
    	$res = 0 ;
    	//from association table
695
    	$where = $table_name.'.'.$id_label.'='.$id.' AND '.$table_name.'.comment_id='.$comment_id ;
696
697
    	$res = $GLOBALS['TYPO3_DB']-> exec_DELETEquery ($table_name, $where);
    	
698
699
700
701
702
    	//from comment table
    	$where = 'tx_nG6_comment.uid='.$comment_id ;
    	$res = $GLOBALS['TYPO3_DB']-> exec_DELETEquery ('tx_nG6_comment', $where);
    	if ($res != 1) {
    		$res = 1 ;
703
704
705
706
707
708
709
710
711
712
713
714
715
716
    	}
    	return $res ;
    }
    
    /**
     * Update an existing comment
     * @param unknown $comment_id
     * @param unknown $new_comment
     */
    function update_comment($comment_id, $new_comment){
    	return $GLOBALS['TYPO3_DB']-> exec_UPDATEquery ('tx_nG6_comment', 'uid='.$comment_id, array('comment' => $new_comment));
    }
    
    
717
718
719
720
	/*
	 * Project functions
	 *------------------------------------------------------------*/

721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
    /**
     * Add a comment to the project described by project_id
     * @param unknown $project_id
     * @param unknown $cruser_id
     * @param unknown $new_comment
     */
    function add_project_comment($project_id, $cruser_id, $new_comment){
    	return tx_nG6_db::add_comment('tx_nG6_project_comment', 'project_id', $project_id, $cruser_id, $new_comment);
    }
    
    /**
     * Retrieve all comments from a given project
     * @param unknown $project_id
     */
    function get_all_project_comments($project_id){
    	return tx_nG6_db::get_all_comments('tx_nG6_project_comment', 'project_id', $project_id);
    }
    
    /**
     * Delete a given project comment
     * @param unknown $project_id
     * @param unknown $comment_id
     */
    function delete_project_comment($project_id, $comment_id){
    	return tx_nG6_db::delete_comment('tx_nG6_project_comment', 'project_id', $project_id, $comment_id);
    }
    
748
749
	/**
	 * Select all project for the current user
750
	 * 
751
	 * @param	string	$user_id	the user id
752
753
	 * @return	table with all projects
	 */
754
	function select_all_user_projects($user_id, $orderby='', $limit='') {		
755
		$projects = array();
Jerome Mariette's avatar
Jerome Mariette committed
756
		
757
		// If the user is not logged on display demonstration project
Jerome Mariette's avatar
Jerome Mariette committed
758
759
760
		if ($user_id == null) { $where = 'tx_nG6_project.public=0'; } 
		else { $where = 'fe_rights.fe_user_id='.$user_id; }
		
761
		//First, get the analyzis project-scale
762
		$queryParts = array(
763
764
765
			'SELECT' => 'tx_nG6_project.uid AS project_id, '.
						'tx_nG6_project.name AS project_name, '.
						'tx_nG6_project.description AS project_description, '.
766
						'tx_nG6_project.public AS project_public, '.
Jerome Mariette's avatar
Jerome Mariette committed
767
						'tx_nG6_project.crdate AS project_crdate, '.
768
769
						'tx_nG6_project.hidden AS project_hidden ',
			'FROM' => 'tx_nG6_project INNER JOIN fe_rights ON fe_rights.project_id=tx_nG6_project.uid',
770
771
			'WHERE' => $where,
			'GROUPBY' => '',
Jerome Mariette's avatar
Jerome Mariette committed
772
773
			'ORDERBY' => $orderby,
			'LIMIT' => $limit
774
		);
775
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);		
776
777
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			$project_id = $row['project_id'];
Jerome Mariette's avatar
Jerome Mariette committed
778
			if (!isset ($projects['project_'.$project_id])) {		
779
				$projects['project_'.$project_id] = array(
780
					'id'			=> $project_id,				
781
					'name'			=> $row['project_name'],
Jerome Mariette's avatar
Jerome Mariette committed
782
					'hidden'		=> $row['project_hidden'],
Jerome Mariette's avatar
Jerome Mariette committed
783
					'date'			=> $row['project_crdate'],
784
					'public'		=> $row['project_public'],
785
786
					'description'	=> $row['project_description']);
			}
787
						
788
		}		
789
790
791
		return $projects;
	}

Penom Nom's avatar
Penom Nom committed
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
	/**
	* 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;
	}

841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
	/**
	 * 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
857
858
859
860
		$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
861
					 'date'			=> $vals['crdate'],
Jerome Mariette's avatar
Jerome Mariette committed
862
863
					 'public'		=> $vals['public'],
					 'description'	=> $vals['description']);
864
865
866
867
868
869
870
871
	}

	/**
	 * Select all runs linked to the specified project
	 *
	 * @param	string	$project_id	the project id
	 * @return	hash table with all runs information
	 */
872
function get_project_runs($project_id, $orderby='', $limit='') {
873
    	
874
		// First select all runs from the database
875
		$queryParts = Array( 
876
877
878
879
			'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
880
881
882
						'tx_nG6_run.data_nature AS run_data_nature, '.
						'tx_nG6_run.sequencer AS run_sequencer, '.
						'tx_nG6_run.type AS run_type, '.
883
						'tx_nG6_run.name AS run_name, '.
Jerome Mariette's avatar
Jerome Mariette committed
884
885
						'tx_nG6_run.nb_sequences AS run_nb_sequences, '.
						'tx_nG6_run.full_seq_size AS run_full_seq_size, '.
886
887
						'tx_nG6_run.date AS run_date, '.
						'tx_nG6_run.hidden AS run_hidden, '.
888
889
890
891
892
						'tx_nG6_run.data_state AS run_data_state, '.
						'tx_nG6_run.retention_date AS run_retention_date, '.
						'tx_nG6_run.purged_date AS run_purged_date, '.
						'tx_nG6_run.purged_size AS run_purged_size, '.
						'tx_nG6_run.mail_sent_date AS run_mail_sent_date, '.
893
894
						'tx_nG6_project.uid AS project_id, '.
						'tx_nG6_project.name AS project_name ',
Jerome Mariette's avatar
Jerome Mariette committed
895
			'FROM' => 'tx_nG6_project '.
896
897
  						'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
898
			'WHERE' => 'tx_nG6_project.uid='.$project_id,
899
			'GROUPBY' => '',
Jerome Mariette's avatar
Jerome Mariette committed
900
901
			'ORDERBY' => $orderby,
			'LIMIT' 	=> $limit
Penom Nom's avatar
Penom Nom committed
902
903
904
		);

	
905
906
907
908
		// 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
909
910
			$run_id = $row['run_id'];				
			if (!isset($results['run_'.$run_id])) {				
911
				$results['run_'.$run_id] = array( 
Jerome Mariette's avatar
Jerome Mariette committed
912
					'id'			=> $run_id,
913
					'directory'		=> $row['run_directory'], 
914
					'name' 			=> $row['run_name'],
Jerome Mariette's avatar
Jerome Mariette committed
915
916
					'project_name' 	=> $row['project_name'],
					'hidden'		=> $row['run_hidden'],
917
					'species' 		=> $row['run_species'],
Jerome Mariette's avatar
Jerome Mariette committed
918
919
920
					'nb_sequences' 	=> $row['run_nb_sequences'],
					'full_seq_size' => $row['run_full_seq_size'],
					'project_id' 	=> $row['project_id'],
921
					'date'			=> $row['run_date'],
Jerome Mariette's avatar
Jerome Mariette committed
922
923
924
					'data_nature'	=> $row['run_data_nature'],
					'sequencer'		=> $row['run_sequencer'],
					'type'			=> $row['run_type'],
925
926
927
928
929
930
					'description' 	=> $row['run_description'],
					'data_state' 	=> $row['run_data_state'],
					'retention_date' 	=> $row['run_retention_date'],
					'purged_date' 	=> $row['run_purged_date'],
					'purged_size' 	=> $row['run_purged_size'],
					'mail_sent_date' 	=> $row['run_mail_sent_date'],
931
				);
932
			}						
933
934
935
		}
		return $results;
    }
Penom Nom's avatar
Penom Nom committed
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
    /**
     * 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, '.
967
968
969
970
971
			    'tx_nG6_run.data_state AS run_data_state, '.
				'tx_nG6_run.retention_date AS run_retention_date, '.
				'tx_nG6_run.purged_date AS run_purged_date, '.
				'tx_nG6_run.purged_size AS run_purged_size, '.
				'tx_nG6_run.mail_sent_date AS run_mail_sent_date, '.
Penom Nom's avatar
Penom Nom committed
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
			    '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'],
995
			    'project_name' 	=> $row['project_name'],
Penom Nom's avatar
Penom Nom committed
996
			    'hidden'		=> $row['run_hidden'],
997
998
999
1000
1001
1002
			    'data_state' 	=> $row['run_data_state'],
				'retention_date' 	=> $row['run_retention_date'],
				'purged_date' 	=> $row['run_purged_date'],
				'purged_size' 	=> $row['run_purged_size'],
				'mail_sent_date' 	=> $row['run_mail_sent_date'],
			    'species' 		=> $row['run_species'],
Penom Nom's avatar
Penom Nom committed
1003
			    'nb_sequences' 	=> $row['run_nb_sequences'],
1004
1005
1006
1007
1008
1009
			    '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'],
Penom Nom's avatar
Penom Nom committed
1010
1011
1012
1013
1014
1015
1016
			    'description' 	=> $row['run_description']
			    				);
		    }
	    }
	    return $results;
    }

1017
1018
1019
1020
1021
1022
	/**
	 * 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
1023
    function get_project_analysis($project_id, $orderby='', $limit='') {
1024
1025
    	
    	// First select all analysis from the database    	
1026
    	$queryParts = array( 
1027
1028
			'SELECT' => 'tx_nG6_analyze.uid AS analyze_id,'. 
						'tx_nG6_analyze.directory AS analyze_directory,'. 
1029
						'tx_nG6_analyze.name AS analyze_name, '.
1030
						'tx_nG6_analyze.class AS analyze_class, '.
1031
						'tx_nG6_analyze.date AS analyze_date, '.
Jerome Mariette's avatar
Jerome Mariette committed
1032
1033
						'tx_nG6_analyze.software AS analyze_software, '.
						'tx_nG6_analyze.version AS analyze_version, '.
1034
1035
1036
1037
1038
						'tx_nG6_analyze.data_state AS analyze_data_state, '.
						'tx_nG6_analyze.retention_date AS analyze_retention_date, '.
						'tx_nG6_analyze.purged_date AS analyze_purged_date, '.
						'tx_nG6_analyze.purged_size AS analyze_purged_size, '.
						'tx_nG6_analyze.mail_sent_date AS analyze_mail_sent_date, '.
1039
						'tx_nG6_analyze.params AS analyze_params, '.
1040
						'tx_nG6_analyze.description AS analyze_description, '.
1041
    					'tx_nG6_analyze.is_editable AS analyze_is_editable, '.
Jerome Mariette's avatar
Jerome Mariette committed
1042
						'tx_nG6_analyze.parent_uid AS analyze_parent_uid, '.
1043
						'tx_nG6_analyze.hidden AS analyze_hidden ',
Jerome Mariette's avatar
Jerome Mariette committed
1044
			'FROM' => 'tx_nG6_project '.
1045
1046
						'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
1047
			'WHERE' => 'tx_nG6_project.uid='.$project_id,
1048
			'GROUPBY' => '',
Jerome Mariette's avatar
Jerome Mariette committed
1049
1050
			'ORDERBY' => $orderby,
			'LIMIT' => $limit
1051
1052
1053
1054
1055
1056
		);
		// 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
1057
			if (!isset ($results['analyse_'.$analyze_id])) {				
1058
1059
				$results['analyse_'.$analyze_id] = array( 
					'directory'		=> $row['analyze_directory'], 
1060
					'name' 			=> $row['analyze_name'],
1061
					'class'			=> $row['analyze_class'],
Jerome Mariette's avatar
Jerome Mariette committed
1062
1063
					'id' 			=> $row['analyze_id'],
					'hidden' 		=> $row['analyze_hidden'],
1064
					'params' 		=> $row['analyze_params'],
Jerome Mariette's avatar
Jerome Mariette committed
1065
1066
					'software' 		=> $row['analyze_software'],
					'version' 		=> $row['analyze_version'],
1067
1068
1069
1070
1071
					'data_state' 		=> $row['analyze_data_state'],
					'retention_date' 		=> $row['analyze_retention_date'],
					'purged_date' 		=> $row['analyze_purged_date'],
					'purged_size' 		=> $row['analyze_purged_size'],
					'mail_sent_date' 		=> $row['analyze_mail_sent_date'],
1072
					'date'			=> $row['analyze_date'],
Jerome Mariette's avatar
Jerome Mariette committed
1073
					'description' 	=> $row['analyze_description'],
1074
					'is_editable' 	=> $row['analyze_is_editable'],
Jerome Mariette's avatar
Jerome Mariette committed
1075
					'parent_id'		=> $row['analyze_parent_uid']	
1076
				);
1077
			}						
1078
1079
1080
1081
		}
		return $results;
    }

Penom Nom's avatar
Penom Nom committed
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
    /**
    * 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, '.
1108
1109
1110
1111
1112
    						'tx_nG6_analyze.data_state AS analyze_data_state, '.
							'tx_nG6_analyze.retention_date AS analyze_retention_date, '.
							'tx_nG6_analyze.purged_date AS analyze_purged_date, '.
							'tx_nG6_analyze.purged_size AS analyze_purged_size, '.
							'tx_nG6_analyze.mail_sent_date AS analyze_mail_sent_date, '.
Penom Nom's avatar
Penom Nom committed
1113
1114
    						'tx_nG6_analyze.params AS analyze_params, '.
    						'tx_nG6_analyze.description AS analyze_description, '.
1115
    						'tx_nG6_analyze.is_editable AS analyze_is_editable, '.
Penom Nom's avatar
Penom Nom committed
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
    						'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'],
1144
1145
1146
1147
1148
    					'data_state' 		=> $row['analyze_data_state'],
						'retention_date' 		=> $row['analyze_retention_date'],
						'purged_date' 		=> $row['analyze_purged_date'],
						'purged_size' 		=> $row['analyze_purged_size'],
						'mail_sent_date' 		=> $row['analyze_mail_sent_date'],
Penom Nom's avatar
Penom Nom committed
1149
    					'description' 	=> $row['analyze_description'],
1150
    					'is_editable' 	=> $row['analyze_is_editable'],
Penom Nom's avatar
Penom Nom committed
1151
1152
1153
1154
1155
1156
1157
    					'parent_id'		=> $row['analyze_parent_uid']	
    			);
    		}
    	}
    	return $results;
    }

1158
1159
1160
1161
1162
    /**
     * 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
1163
	function hide_project($p_id) {
1164
1165
		
		// First change all project runs
Jerome Mariette's avatar
Jerome Mariette committed
1166
		$project_runs = tx_nG6_db::get_project_runs($p_id);
1167
		foreach($project_runs as $run_id => $run_values) {
Jerome Mariette's avatar
Jerome Mariette committed
1168
			tx_nG6_db::hide_run($run_values['id']);
1169
1170
1171
		}

		// Then all project analysis
Jerome Mariette's avatar
Jerome Mariette committed
1172
    	$project_analysis = tx_nG6_db::get_project_analysis($p_id);
1173
1174
1175
		foreach($project_analysis as $analysis_id => $analysis_values) {  
			tx_nG6_db::hide_analysis($analysis_values['id']);
		}
1176
		tx_nG6_db::unpublish_project($p_id);
1177
1178
1179
1180
1181
1182
1183
1184
1185
		// 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
1186
	function unhide_project($p_id) {
1187
1188
		
		// First change all project runs
Jerome Mariette's avatar
Jerome Mariette committed
1189
		$project_runs = tx_nG6_db::get_project_runs($p_id);
1190
		foreach($project_runs as $run_id => $run_values) {
Jerome Mariette's avatar
Jerome Mariette committed
1191
			tx_nG6_db::unhide_run($run_values['id']);
1192
1193
1194
		}

		// Then all project analysis
Jerome Mariette's avatar
Jerome Mariette committed
1195
    	$project_analysis = tx_nG6_db::get_project_analysis($p_id);
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
		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
1209
	function publish_project($p_id) {
1210
		// First unhide the project
Jerome Mariette's avatar
Jerome Mariette committed
1211
		tx_nG6_db::unhide_project($p_id);
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
		// 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
1233
	function delete_project($p_id, $user_login, $user_pwd, $data_folder) {
1234
1235
1236
		
		$res = 0;
		// All runs
Jerome Mariette's avatar
Jerome Mariette committed
1237
		$project_runs = tx_nG6_db::get_project_runs($p_id);
1238
1239
1240
		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
1241
				$res = tx_nG6_db::delete_run($run_values['id'], $user_login, $user_pwd, $data_folder);
1242
1243
1244
1245
1246
1247
				if ($res != 0) { break; }				
			}
		}
		
		// All project_analysis
		if ($res == 0) {
Jerome Mariette's avatar
Jerome Mariette committed
1248
			$project_analysis = tx_nG6_db::get_project_analysis($p_id);
1249
1250
1251
1252
1253
1254
1255
1256
1257
			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; }
				}
			}
		}
		
1258
1259
1260
1261
1262
1263
		//all project comments
		$project_comments = tx_nG6_db::get_all_project_comments($p_id);
		foreach($project_comments as $id => $values ){
			tx_nG6_db::delete_project_comment($p_id, $values['id']);
		}
		
1264
1265
1266
1267
1268
1269
1270
1271
		// 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
1272
1273
1274
1275
1276
1277
1278
1279
		// 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;}
		}
		
1280
1281
1282
		return $res;
	}

1283
1284
1285
1286
1287
    /**
     * 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
1288
	 * @param	boolean $get_analyzes	get analyzes size ? 
1289
     */
1290
	function get_project_size($p_id, $get_analyzes=false) {
1291
1292
1293
		
		$full_size = 0;
		// All runs
Jerome Mariette's avatar
Jerome Mariette committed
1294
		$project_runs = tx_nG6_db::get_project_runs($p_id);
1295
		foreach($project_runs as $run_id => $run_values) {
1296
			$full_size += tx_nG6_db::get_run_size($run_values['id'], $get_analyzes);
1297
		}
Jerome Mariette's avatar
Jerome Mariette committed
1298
1299
1300
1301
		if ($get_analyzes) {
			// All analysis
			$project_analysis = tx_nG6_db::get_project_analysis($p_id);
			foreach($project_analysis as $analyse_id => $analyze_values) {  
1302
				$full_size += tx_nG6_db::get_analysis_size($analyze_values['id']);
Jerome Mariette's avatar
Jerome Mariette committed
1303
			}
1304
1305
1306
1307
		}
		return $full_size;
	}

1308
1309
1310
1311

	/*
	 * Run functions
	 *------------------------------------------------------------*/
1312
	
Penom Nom's avatar