class.tx_nG6_db.php 135 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
<?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!
***************************************************************/

25
require_once(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('nG6').'/lib/class.tx_nG6_utils.php');
26
27
28
29
30
31
32
33

/**
 * 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
	*/
Gerald Salin's avatar
Gerald Salin committed
47
        static function update_field($table, $id, $field, $value,$no_quote_fields=FALSE) {
48
49
50
51
52
53
54
55
56
57
58
            if( $field == 'uid' ) {
                throw new Exception ("The function 'update_field' cannot change an id.") ;
            }
            $array=array();
            if( is_array($field) ){
                    for($i =0; $i<count($field);$i++){
                            $array[$field[$i]]=$value[$i];
                    }
            }else{
                     $array=array($field => $value);
            }
Gerald Salin's avatar
Gerald Salin committed
59
            if (is_array($id)){
Celine Noirot's avatar
Celine Noirot committed
60
                $GLOBALS['TYPO3_DB']-> exec_UPDATEquery ($table, 'uid IN ('.implode(',',$id).')', $array,$no_quote_fields);
61
            }else{ 
Celine Noirot's avatar
Celine Noirot committed
62
                $GLOBALS['TYPO3_DB']-> exec_UPDATEquery ($table, 'uid='.$id, $array,$no_quote_fields);
63
64
            }
	}
65
	
66
	static function select_all_in_view($view, $where) {
67
		$queryParts = array(
68
69
70
					'SELECT' 	=> '*',
					'FROM' 		=> $view,
					'WHERE' 	=> $where ,
71
					'GROUPBY' 	=> '',
Gerald Salin's avatar
Gerald Salin committed
72
73
					'ORDERBY' 	=> '',
					'LIMIT' 	=> ''
74
			);	
75
76
77
78
			$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
			return ($res);
	}
	
79
	static function select_count_in_view($view,$where) {
Celine Noirot's avatar
Celine Noirot committed
80
		#select count All runs per project
81
82
83
84
85
86
87
88
89
		$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);
Celine Noirot's avatar
Celine Noirot committed
90
                return ($res);
Gerald Salin's avatar
Gerald Salin committed
91

92
93
	}

94
	static function get_project_list_by_group_filter($create_user_id, $without_group_id, $with_group_id){
Celine Noirot's avatar
Celine Noirot committed
95
		$where=Array();
96
97
		$list_project_id_filter_by_group="";
		$list_project_id_laboratories="";
Celine Noirot's avatar
Celine Noirot committed
98
99
		### Search for project without group_id implied
                if($without_group_id !=""){
100
101
102
103
			#select all project_id with group OR/AND without group
			$queryParts=array(
				'SELECT' => 'project_id',
				'FROM' => 'tx_nG6_view_project_user',
Celine Noirot's avatar
Celine Noirot committed
104
				'WHERE' => 'user_group IN ( '.$without_group_id .')',
105
106
107
108
109
110
111
				'GROUPBY' 	=> '',
				'ORDERBY' 	=> '',
				'LIMIT' 	=> ''
			);
		
			$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
			while($res_row= $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
112
				if( $list_project_id_filter_by_group != ""){
113
					$list_project_id_filter_by_group .= ",".$res_row['project_id'];
114
115
				}else{
					$list_project_id_filter_by_group = $res_row['project_id'];
116
117
				}
			}
Celine Noirot's avatar
Celine Noirot committed
118
			$where[] = "uid NOT IN (".$list_project_id_filter_by_group.")";
119
		}
Celine Noirot's avatar
Celine Noirot committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
                ### Search for project with group_id implied
                if($with_group_id !=""){
			#select all project_id with group OR/AND without group
			$queryParts=array(
				'SELECT' => 'project_id',
				'FROM' => 'tx_nG6_view_project_user',
				'WHERE' => 'user_group IN ( '.$with_group_id .')',
				'GROUPBY' 	=> '',
				'ORDERBY' 	=> '',
				'LIMIT' 	=> ''
			);
		
			$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
			while($res_row= $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
				if( $list_project_id_filter_by_group != ""){
					$list_project_id_filter_by_group .= ",".$res_row['project_id'];
				}else{
					$list_project_id_filter_by_group = $res_row['project_id'];
				}
139
			}
Celine Noirot's avatar
Celine Noirot committed
140
141
142
143
144
			$where[] = "uid IN (".$list_project_id_filter_by_group.")";
		}
		### add create user filter
		if($create_user_id != ""){
				$where[] = "cruser_id IN (".$create_user_id.")";
145
146
147
		}
		$queryParts=array(
			'SELECT' => 'uid AS project_id',
148
			'FROM' => 'tx_nG6_project',
Celine Noirot's avatar
Celine Noirot committed
149
			'WHERE' => join (' AND ', $where),
150
151
152
153
154
155
156
			'GROUPBY' 	=> '',
			'ORDERBY' 	=> '',
			'LIMIT' 	=> ''
		);
		
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
157
158
			if ($list_project_id_laboratories != "") {
				$list_project_id_laboratories .=",".$res_row['project_id'];
159
			}else{
160
				$list_project_id_laboratories=$res_row['project_id'];
161
162
163
164
165
166
				
			}
		}
    	return ($list_project_id_laboratories);
		
	}
167
	
168
169
170
        ################################
        # OBSOLETE / PURGE management
        ################################
171
	//Get list of obsolete run/analyses per project
Romain Therville's avatar
Romain Therville committed
172
	static function filter_list_retention_data_info($filter_space, $max_retention_date, $create_user_id, $without_user_group_id, $with_user_group_id) {
173
		
174
175
176
177
178
179
180
181
182
183
184
185
		$conditions=Array();
                //filter by date
                if ( $max_retention_date != "" ) {
                        //from user filter value
                        $conditions[] =  ' DATEDIFF( FROM_UNIXTIME( retention_date ) , STR_TO_DATE( "'.$max_retention_date.'", "%d/%m/%Y" ) ) <0';
                } else {
                        //from now
                        $conditions[] =  ' DATEDIFF( FROM_UNIXTIME( retention_date ) , now( ) ) <0';
                }
                $conditions[] = 'purge_demand_id IS NULL';
                $conditions[] = 'state IN ( \'stored\', \'extended\')';                
		
186
187
188
                if ($filter_space != "") {
					$conditions[] = "space_id = '$filter_space'";
				}
189

190
		$project_list_ids = "";
Celine Noirot's avatar
Celine Noirot committed
191
192
193
		if ($create_user_id != "" or $without_user_group_id != "" or $with_user_group_id != "") {
                        #retrieve list of project id created by a user and with or without a list of group_id
			$project_list_ids = tx_nG6_db::get_project_list_by_group_filter($create_user_id,$without_user_group_id, $with_user_group_id);
194
			$conditions[] = "project_id in (" . $project_list_ids.")" ;
195
		}
196
197
        $where = join(' AND ', $conditions);
        
Romain Therville's avatar
Romain Therville committed
198
        return (tx_nG6_db::select_list_retention_data_info($where));
199
200
	}
	
Romain Therville's avatar
Romain Therville committed
201
	static function select_a_project_retention_data_info($project_id=null, $all=FALSE, $count_hidden=FALSE) {
202
203
204
205
206
207
208
		$condition=Array();
		if ($all === FALSE) {
			$condition[]='DATEDIFF( FROM_UNIXTIME( retention_date ) , now( ) ) <0';
		}
		if (null != $project_id) {
			$condition[]="project_id = " . $project_id ;
		}
Romain Therville's avatar
Romain Therville committed
209
210
211
		if(!$count_hidden){
			$condition[]="hidden = 0";
		}
212
		$where = join( ' AND ', $condition);
213
214
215
		return(tx_nG6_db::select_list_retention_data_info($where));
	}
	
216
	static function select_list_retention_data_info($where) {
217
		$by_project=array();
218
219
220
221
222
223
224
225
226
227
228
229
		$empty_res_structure = array(
						'project_name'			=> "",
						'state'					=> array("purged"  => array ('nb_run' => 0, 'size_run' => 0, 'run_ids'=> array(),
                                                "nb_analyze"=> 0, "size_analyze"=> 0, 'analysis_ids'=> array()),
                                                "extended"  => array ('nb_run' => 0, 'size_run' => 0, 'run_ids'=> array(),
                                                "nb_analyze"=> 0, "size_analyze"=> 0, 'analysis_ids'=> array()),
                                                "stored"  => array (	'nb_run' => 0, 'size_run' => 0, 'run_ids'=> array(),
							                                                "nb_analyze"=> 0, "size_analyze"=> 0, 'analysis_ids'=> array()
						                                                 )
                                                ),
						'total_purgeable_size'	=> 0,
						'nb_runs'				=>0,
230
231
						'nb_analyses'			=>0,
						'space_id' 		=> "");
232
		//select obsolete runs
233
234
		$res = tx_nG6_db::select_all_in_view( "tx_nG6_view_project_run" , $where);
		while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
235
			$current_project_id = $res_row['project_id'];			
236
			if (!isset ($by_project[$res_row['project_id']])) {
237
238
				$by_project[$current_project_id]= $empty_res_structure;
				$by_project[$current_project_id]['project_name']= $res_row['project_name'];
239
				$by_project[$res_row['project_id']]['space_id'] = $res_row['space_id'];
Gerald Salin's avatar
Gerald Salin committed
240
			}
241
242
243
			
			
			
244
245
246
247
248
249
250
251
252
			if ($res_row['run_id'] != "") {
             $by_project[$res_row['project_id']]['state'][$res_row['state']]['nb_run']+=1;
             $key = 'storage_size';
				 if($res_row['state'] == "purged"){
				 	$key = "purged_size";
             }
             $by_project[$current_project_id]['state'][$res_row['state']]['size_run']+=$res_row[$key];
             $by_project[$current_project_id]['state'][$res_row['state']]['run_ids'][]=$res_row['run_id'];
				 $by_project[$current_project_id]['total_purgeable_size'] += $res_row['storage_size'];
253
			}
Gerald Salin's avatar
Gerald Salin committed
254
		}
Romain Therville's avatar
Romain Therville committed
255
		
256
		//select obsolete analyses of a run
257
		$res = tx_nG6_db::select_all_in_view("tx_nG6_view_project_run_analyze",$where,'10');
258
		while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
259
			$current_project_id = $res_row['project_id'];
260
			if (!isset ($by_project[$res_row['project_id']])) {
261
262
				$by_project[$current_project_id]= $empty_res_structure;
				$by_project[$current_project_id]['project_name']= $res_row['project_name'];
263
				$by_project[$current_project_id]['space_id'] = $res_row['space_id'];
264
			}
265
			if ($res_row['analyze_id'] != ""){
Romain Therville's avatar
Romain Therville committed
266
267
268
269
270
271
272
273
        		$by_project[$current_project_id]['state'][$res_row['state']]['nb_analyze'] += 1;
           		$key = 'storage_size';
           		if($res_row['state'] == "purged"){
           			$key = "purged_size";
           		}
           		$by_project[$current_project_id]['state'][$res_row['state']]['size_analyze'] += $res_row[$key];
           		$by_project[$current_project_id]['state'][$res_row['state']]['analysis_ids'][]=$res_row['analyze_id'];
           		$by_project[$current_project_id]['total_purgeable_size'] += $res_row['storage_size'];
274
			}
Romain Therville's avatar
Romain Therville committed
275
		}
Gerald Salin's avatar
Gerald Salin committed
276
		
277
		//select obsolete analyses of a project
278
279
		$res = tx_nG6_db::select_all_in_view("tx_nG6_view_project_analyze", $where);
		while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
280
			$current_project_id = $res_row['project_id'];
281
			if (!isset ($by_project[$res_row['project_id']])) {
282
283
				$by_project[$current_project_id]= $empty_res_structure;
				$by_project[$current_project_id]['project_name']= $res_row['project_name'];
284
				$by_project[$current_project_id]['space_id'] = $res_row['space_id'];
Gerald Salin's avatar
Gerald Salin committed
285
			}
Celine Noirot's avatar
Celine Noirot committed
286
287
			
			if ($res_row['analyze_id'] != ""){
288
				$by_project[$current_project_id]['state'][$res_row['state']]['nb_analyze'] += 1;
289
                $key = 'storage_size';
noirot's avatar
noirot committed
290
                if($res_row['state'] == "purged"){
291
292
                	$key = "purged_size";
                }
293
294
295
                $by_project[$current_project_id]['state'][$res_row['state']]['size_analyze'] += $res_row[$key];
				$by_project[$current_project_id]['state'][$res_row['state']]['analysis_ids'][]=$res_row['analyze_id'];
				$by_project[$current_project_id]['total_purgeable_size'] += $res_row['storage_size'];
296
                               
Gerald Salin's avatar
Gerald Salin committed
297
298
			}
		}
299
		
Romain Therville's avatar
Romain Therville committed
300
		$res = tx_nG6_db::select_count_in_view("tx_nG6_view_project_run",$where);
301
302
303
		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'];
304
305
			}
		}
306
		
Romain Therville's avatar
Romain Therville committed
307
		$res = tx_nG6_db::select_count_in_view("tx_nG6_view_project_run_analyze",$where);
308
309
310
		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'];
311
312
313
			}
		}

Romain Therville's avatar
Romain Therville committed
314
		$res = tx_nG6_db::select_count_in_view("tx_nG6_view_project_analyze",$where);
315
316
317
318
319
320
321
		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
322
323
			}
		}
324
325
326
327
328
329
330
331
		
		#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();
				}
332
				array_push($by_project[$res_row['project_id']]["users"],array ('user_id' => $res_row['user_id'], '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']));
333
334
335
336
337
			}
		}
		
		return($by_project);
	}
Gerald Salin's avatar
Gerald Salin committed
338

339
340
341
342
343
344
345
    /**
     * Add purge initial purge demand in db
     *
     * @param	int	$project_id	project id link to the purge demand
     * @param	int	$total_purgeable_size	size of files to purge
     * @return	New purge id
     */
346
    static function add_purge_demand($cruser_id, $project_id, $total_purgeable_size, $all_runs_ids, $all_analyses_ids,$user_ids){
347
      $date= time();
348
349
    	$purge_data = array(
			'cruser_id'			=>	$cruser_id,
350
351
352
         'project_id'      => $project_id,
         'analyze_ids'     => implode(',',$all_analyses_ids),
         'run_ids'         => implode(',',$all_runs_ids),
353
			'mail_sent_date'	=>	$date,
354
         'purge_size'      => $total_purgeable_size,
355
356
357
358
359
360
361
			'demand_state'	=>	"sent",
			'tstamp'			=> 	$date,
			'crdate'			=> 	$date
        );
    
    	$GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_nG6_purge_demand', $purge_data);
        $purge_demand_id = $GLOBALS['TYPO3_DB']->sql_insert_id();
Celine Noirot's avatar
Celine Noirot committed
362
363
        tx_nG6_db::update_field('tx_nG6_run', $all_runs_ids, array('mail_sent_date','purge_demand_id'),array($date,$purge_demand_id));
        tx_nG6_db::update_field('tx_nG6_analyze',$all_analyses_ids,array('mail_sent_date','purge_demand_id'),array($date,$purge_demand_id));
364
        
365
        foreach($user_ids as $a_id){
366
367
                $purge_data_user = array(
                        'tstamp'			=> 	$date,
368
369
								'crdate'			=> 	$date,
                        'cruser_id'          =>	$cruser_id,
370
                        'purge_demand_id'    =>  $purge_demand_id,
371
                        'fe_users_id'        =>  $a_id
372
373
374
375
376
                );
                $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_nG6_purge_demand_fe_users', $purge_data_user);
        }
    	return $purge_demand_id;
    }
Celine Noirot's avatar
Celine Noirot committed
377
    
378
    static function get_purge_demand_from_id($values_str){
Celine Noirot's avatar
Celine Noirot committed
379
380
        #select all demand id not processed
        $queryParts=array(
381
382
                'SELECT' => ' uid as demand_id,project_id,purge_size,mail_sent_date,processed_date,demand_state,analyze_ids,run_ids',
                'FROM' => 'tx_nG6_purge_demand',
383
                'WHERE' => 'uid IN ('.$values_str.')',
384
385
386
387
388
389
390
391
                'GROUPBY' 	=> '',
                'ORDERBY' 	=> '',
                'LIMIT' 	=> ''
        );
        
        $res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
        $result=array();
        while($res_row= $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
392
393
394
395
396
397
398
399
400
401
402
403
404
405
                 $queryPartsEmails=array(
                        'SELECT' => ' email ',
                        'FROM' => 'fe_users JOIN  tx_nG6_purge_demand_fe_users ON fe_users.uid = tx_nG6_purge_demand_fe_users.fe_users_id',
                        'WHERE' => 'purge_demand_id = '.$res_row["demand_id"],
                        'GROUPBY' 	=> '',
                        'ORDERBY' 	=> '',
                        'LIMIT' 	=> ''
                );
                $emails=Array();
                $res_email = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryPartsEmails);
                while($email = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res_email)) {
                        $emails[]=$email["email"];
                }
                
406
407
408
409
410
411
412
413
                $result[]=array(
                "demand_id"=>$res_row["demand_id"],
                "project_id"=>$res_row["project_id"],
                "purge_size"=>$res_row["purge_size"],
                "mail_sent_date"=>$res_row["mail_sent_date"],
                "processed_date"=>$res_row["processed_date"],
                "demand_state"=>$res_row["demand_state"],
                "analyze_ids"=>$res_row["analyze_ids"],
414
415
                "run_ids"=>$res_row["run_ids"],
                "emails"=>$emails
416
417
418
419
                );
        }
        return($result);
    }  
Celine Noirot's avatar
Celine Noirot committed
420
421
    
    
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
    static function get_purge_demand_list($delay_in_days,$space_filter,$delay_exceeded_filter){        
        #We first build the WHERE conditions from the given parameters
        if ($space_filter != "") {
			$conditions[] = "space_id = '$space_filter'";
		}
		
		//For an unknown reason, we can't use the alias 'delay_excedeed' in the WHERE clause...
		if ($delay_exceeded_filter == 'yes') {
			$conditions[] = "DATEDIFF( now( ), FROM_UNIXTIME( tx_nG6_purge_demand.mail_sent_date ) ) >".$delay_in_days;
		}elseif($delay_exceeded_filter == 'no'){
			$conditions[] = "DATEDIFF( now( ), FROM_UNIXTIME( tx_nG6_purge_demand.mail_sent_date ) ) <".$delay_in_days;
		}
		$conditions[] = '`tx_nG6_purge_demand`.processed_date IS NULL';
        $where = join(' AND ', $conditions);
        
437
        #select all demand id not processed
438
    	$queryParts=array(
439
440
                'SELECT' => 'tx_nG6_purge_demand.uid AS demand_id, `tx_nG6_purge_demand`.project_id AS project_id, tx_nG6_project.name as project_name, 
                `tx_nG6_purge_demand`.purge_size AS purge_size, `tx_nG6_purge_demand`.mail_sent_date, `tx_nG6_purge_demand`.demand_state, 
441
                `fe_users`.username, `fe_groups`.title AS user_group_title, `tx_nG6_project`.space_id AS space_id,
442
                DATEDIFF( now( ), FROM_UNIXTIME( `tx_nG6_purge_demand`.mail_sent_date ) ) >'.$delay_in_days.' AS delay_excedeed',
443
444
445
446
447
                'FROM' => '`tx_nG6_purge_demand`
INNER JOIN tx_nG6_project ON tx_nG6_project.uid = tx_nG6_purge_demand.project_id
INNER JOIN `tx_nG6_purge_demand_fe_users` ON `tx_nG6_purge_demand_fe_users`.`purge_demand_id` = tx_nG6_purge_demand.uid
INNER JOIN fe_users ON fe_users.uid = `tx_nG6_purge_demand_fe_users`.`fe_users_id`
INNER JOIN fe_groups ON fe_groups.uid = fe_users.usergroup',
448
                'WHERE' 	=> $where,
449
450
451
452
                'GROUPBY' 	=> '',
                'ORDERBY' 	=> '',
                'LIMIT' 	=> ''
        );
453
454
455
        
        file_put_contents ('/work/ng6-test/jflow_rtherville/ng6_php.log',"IN tx_nG6_db.php, get_purge_demand_list. queryParts=".print_r($queryParts,TRUE));
        
456
457
458
459
460
461
462
463
464
        $results=array();
        $res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
        while($res_row= $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
                
                if ( array_key_exists($res_row["demand_id"] , $results)){
                        $results[$res_row["demand_id"]]["users"][]=array("username"=>$res_row["username"], "user_group_title"=>$res_row["user_group_title"]);
                } else {
                        $results[$res_row["demand_id"]]=array("project_id"=>$res_row["project_id"],
                                "purge_size"=>$res_row["purge_size"],
Celine Noirot's avatar
Celine Noirot committed
465
                                "mail_sent_date"=>date("d/m/Y",$res_row["mail_sent_date"]),
466
467
468
                                "project_name"=>$res_row["project_name"],
                                "demand_state"=>$res_row["demand_state"],
                                "delay_excedeed"=>$res_row["delay_excedeed"],
469
                                "space_id"=>$res_row["space_id"],
470
471
472
473
                                "users"=>array(array("username"=>$res_row["username"], "user_group_title"=>$res_row["user_group_title"]))
                                );
                }
        }
474
        return ($results);
475
476
    }
	
477
        static function extend_retention_date_from_demand($demand_ids, $date, $type_extend){
478
                                
479
480
481
482
483
                //extend
                $all_demands=tx_nG6_db::get_purge_demand_from_id( join(', ', $demand_ids));
                $all_runs=array();
                $all_analyses=array();
                foreach($all_demands as $res){
484
                        if ($res["demand_state"] != "deleted")
485
                        {
486
487
                                $all_runs=array();
                                $all_analyses=array();
488
                                if ($type_extend == 'extend_all_project') {
489
                                        tx_nG6_db::extend_retention_date_from_project($res["project_id"],$date, FALSE);
490
                                } else {
491
492
493
                                        $all_analyses=explode(',',$res["analyze_ids"]);
                                        $all_runs=explode(',',$res["run_ids"]);
                                        tx_nG6_db::extend_runs_analyses  ($all_runs, $all_analyses, $date);
494
495
496
                                }
                        }
                }
497
498
499
500
501
502
                tx_nG6_db::update_field('tx_nG6_purge_demand',$demand_ids,
                        array("processed_date","demand_state"),
                        array($date,"extended"));
                return("Done extend");
        }	
        
503
        static function extend_retention_date_from_project($projects_ids, $date, $check_purge){
504
505
506
507
                
                        $all_project_filter = " project_id in (".$projects_ids. ") AND DATEDIFF( FROM_UNIXTIME( retention_date ), FROM_UNIXTIME(" .$date. ") ) <0
                        AND state in ('extended','stored') ";
                        
508
                        //$all_project=tx_nG6_db::get_purge_demand_from_id( join(', ', $demand_ids));
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
                        $res_project_analyze = tx_nG6_db::select_all_in_view("tx_nG6_view_project_analyze", $all_project_filter);
                        $res_project_run = tx_nG6_db::select_all_in_view("tx_nG6_view_project_run", $all_project_filter);
                        $res_project_run_analyze = tx_nG6_db::select_all_in_view("tx_nG6_view_project_run_analyze", $all_project_filter);
                        $purge_ids=Array();
                        while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res_project_analyze)) {
                                $all_analyses[] = $res_row["analyze_id"];
                                if ($res_row["purge_demand_id"] != NULL) {
                                        $purge_ids[] = $res_row["purge_demand_id"];
                                }
                        }
                        while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res_project_run_analyze)) {
                                $all_analyses[] = $res_row["analyze_id"];
                                if ($res_row["purge_demand_id"] != NULL) {
                                        $purge_ids[] = $res_row["purge_demand_id"];
                                }
                        }
                        while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res_project_run)) {
                                $all_runs[] = $res_row["run_id"];
                                if ($res_row["purge_demand_id"] != NULL) {
                                        $purge_ids[] = $res_row["purge_demand_id"];
                                }
                        }
                       tx_nG6_db::extend_runs_analyses  ($all_runs, $all_analyses, $date);
                       if ($check_purge == True){
                               $purge_uniq = array_unique ($purge_ids);
534
                               
535
536
537
538
539
540
541
                               if (count($purge_uniq) > 0) {
                                        tx_nG6_db::update_field('tx_nG6_purge_demand',$purge_uniq,
                                        array("processed_date","demand_state"),
                                        array($date,"extended"));
                               }
                       }
        }
542
        static function extend_runs_analyses($all_runs, $all_analyses, $date){
543
544
545
546
547
548
549
550
                tx_nG6_db::update_field('tx_nG6_run', $all_runs, 
                        array("retention_date","data_state", "mail_sent_date","purge_demand_id"),
                        array($date,"extended","","NULL"),
                        array("purge_demand_id"));
                tx_nG6_db::update_field('tx_nG6_analyze',$all_analyses,
                        array("retention_date","data_state", "mail_sent_date","purge_demand_id"),
                        array($date,"extended","","NULL"),
                        array("purge_demand_id"));
551
        }
552
        
553
        static function delete_purge_demand($demand_ids,$user_login, $user_pwd, $data_folder){
554
555
556
557
558
559
                $date= time();
                $all_demands=tx_nG6_db::get_purge_demand_from_id($demand_ids);
                $directories=array();
                $res = 0 ;
                foreach($all_demands as $demand){
                        if ($demand["state"] != "deleted"){      
560
561
562
563
564
565
                                if ($demand["analyze_ids"] != ""){
                                        foreach(explode(',',$demand["analyze_ids"]) as $a_id){
                                               $res = tx_nG6_db::purge_analysis($a_id, $user_login, $user_pwd, $data_folder);
                                               if ($res != 0) {
                                                      break;  
                                                }
566
567
568
                                        }
                                }
                                if ($res == 0) {
569
570
571
572
573
574
575
                                        if ($demand["run_ids"] != ""){
                                                foreach(explode(',',$demand["run_ids"]) as $r_id){
                                                        
                                                        $res = tx_nG6_db::purge_run($r_id, $user_login, $user_pwd, $data_folder);
                                                        if ($res != 0) {
                                                              break;  
                                                        }
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
                                                }
                                        }
                                }
                                else {
                                        break;
                                }
                        }
                }
                # finally set purge demand as purged
                if ($res == 0){
                        tx_nG6_db::update_field('tx_nG6_purge_demand',explode(',',$demand_ids),
                        array("processed_date","demand_state"),
                        array($date,"purged"));
                }
                return $res;
        }	
592
593
594
    /**
     * Select all project for the current user
     *
595
     * @param	string	$by	by can group, organism, location
596
597
     * @param	string	$role	role can be create_user, manager
     * @return	table with all projects
598
     */
599
    static function select_projects_repartition($role, $by='title') {
600
    	if ($role == "create_user") {
601
    		$queryParts = array(
602
    				'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',
603
604
605
606
607
608
609
    				'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' 	=> ''
610
611
612
613
    		);
    		//$where = "tx_nG6_project.cruser_id="
    	} else if ($role == "manager") {
	    	$queryParts = array(
614
    				'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',
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
	    			'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)) {
630
631
632
    		if (!isset ($by_title[$res_row['title']])) {
    			$by_title[$res_row['title']] = array(
    					'count'			=> 1,
633
    					'group_id'	=> $res_row['group_id'],
634
635
    					'organism'		=> $res_row['organism'],
    					'location'		=> $res_row['location']);
636
637
638
639
    		} else {
    			$by_title[$res_row['title']]['count'] += 1;
    		}
    		
640
641
642
643
644
    		if (!isset ($by_organism[$res_row['organism']])) {
    			$by_organism[$res_row['organism']] = array(
    					'count'			=> 1);
    		} else {
    			$by_organism[$res_row['organism']]['count'] += 1;
645
646
    		}
    		
647
648
649
650
651
652
    		if (!isset ($by_location[$res_row['location']])) {
    			$by_location[$res_row['location']] = array(
    					'count'			=> 1);
    		} else {
    			$by_location[$res_row['location']]['count'] += 1;
    		}
653
654
655
656
657
658
659
660
661
    	}
    	if ($by=='title') {
    		return ($by_title);
    	} else if ($by=='organism') {
    		return ($by_organism);
    	} else if ($by=='location') {
    		return ($by_location);
    	}
    }
662
    static function select_all_projects(){
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
            $result=Array();
            $queryParts = array(
    				'SELECT' 	=> 'tx_nG6_project.uid as pid,  tx_nG6_project.name as project_name ',
    				'FROM' 		=> 'tx_nG6_project ',
    				'WHERE' 	=> "",
    				'GROUPBY' 	=> '',
    				'ORDERBY' 	=> '',
    				'LIMIT' 	=> ''
    		);
                $res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
                while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
                        $results[] =  $res_row;
                }
                return($results);
    }
678
    static function select_projects_evolution($values, $by, $role, $cumulate) {
Celine Noirot's avatar
Celine Noirot committed
679
680
681
682
683
684
        if ($by == "organism" || $by == "location") {
                $by = "tx_nG6_".$by;
        }

        $from = "" ;
        $where = "fe_groups.".$by." IN ('".implode("', '", $values)."')" ;
Penom Nom's avatar
Penom Nom committed
685
686
687
688
689
690
691
692
693
    	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
694
    		$where .= " AND fe_rights.right_id=1" ;
Penom Nom's avatar
Penom Nom committed
695
696
    	}
    	$queryParts = array(
Jerome Mariette's avatar
Jerome Mariette committed
697
    			'SELECT' 	=> 'tx_nG6_project.crdate, count(tx_nG6_project.crdate) nb',
Penom Nom's avatar
Penom Nom committed
698
699
    			'FROM' 		=> $from,
    			'WHERE' 	=> $where,
Jerome Mariette's avatar
Jerome Mariette committed
700
701
    			'GROUPBY' 	=> 'tx_nG6_project.crdate',
    			'ORDERBY' 	=> 'tx_nG6_project.crdate',
Penom Nom's avatar
Penom Nom committed
702
703
704
705
706
    			'LIMIT' 	=> ''
    	);
    	$results = array();
    	$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
    	while($res_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
707
708
709
710
711
    		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
712
713
714
715
    	}
    	return $results;
    }

716
    static function select_projects_distribution($values, $by, $role) {
Celine Noirot's avatar
Celine Noirot committed
717
718
719
720
721
        $from = "" ;
        $where = "" ;
        if ($by == "organism" || $by == "location") {
                $by = "tx_nG6_".$by;
        }
722
723
724
725
726
727
728
729
730
731
    	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" ;
732
    	}
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
    	$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;
    }
    
757
    static function select_storage_distribution($values, $by, $role, $get_analyzes, $octet) {
758
759
    	if ($by == "organism" || $by == "location") {
    		$by = "tx_nG6_".$by;
Jerome Mariette's avatar
Jerome Mariette committed
760
761
762
763
764
    	}
    	$storage_unit = "full_seq_size";
    	if ($octet) {
    		$storage_unit = "storage_size";
    	}
765
766
767
    	$from = "" ;
    	$where = "" ;
    	if ($role == "create_user") {
Jerome Mariette's avatar
Jerome Mariette committed
768
769
    		$from = 'tx_nG6_project '
    		.' INNER JOIN tx_nG6_project_run ON tx_nG6_project_run.project_id= tx_nG6_project.uid'
770
771
772
773
    		.' 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
774
    		$from = 'tx_nG6_project '
775
    		.' INNER JOIN tx_nG6_project_run ON tx_nG6_project_run.project_id= tx_nG6_project.uid'
Jerome Mariette's avatar
Jerome Mariette committed
776
    		.' INNER JOIN tx_nG6_run ON tx_nG6_project_run.run_id= tx_nG6_run.uid'
777
778
779
780
781
782
783
784
785
786
787
788
    		.' 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
789
    	);
790
791
792
793
794
    	
    	$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
795
			
796
797
798
799
800
801
    		$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
802
803
804
    		}
    		if( in_array($res_row[$by], $values) ) {
    			
805
    			if( array_key_exists( $res_row[$by], $results) ) {
Jerome Mariette's avatar
Jerome Mariette committed
806
807
808
    				$results[$res_row[$by]] += $res_row[$storage_unit] + $analyses_size;
    			} else {
    				$results[$res_row[$by]] = $res_row[$storage_unit] + $analyses_size;
809
810
811
812
813
814
815
    			}
    		} else {
    			$others += $res_row[$storage_unit] + $analyses_size;
    		}
    	}
    	if ($others != 0) {
    		$results["OTHERS"] = $others;
Jerome Mariette's avatar
Jerome Mariette committed
816
817
    	}
    	$final_results = array();
818
819
820
821
    	foreach($results as $group_key => $group_values) {
    		$final_results[] = array( $group_key, $group_values );
    	}
    	return $final_results;
822
823
    }
    
824
	static function select_storage_evolution($values, $by, $role, $get_analyzes, $octet, $cumulate) {
825
826
827
828
829
        if ($by == "organism" || $by == "location") {
                $by = "tx_nG6_".$by;
        }
        $storage_unit = "full_seq_size";
        if ($octet) {
830
        	$storage_unit = "storage_size";
831
832
833
        }
        $from = "" ;
        $where = "fe_groups.".$by." IN ('".implode("', '", $values)."')" ;
834
       
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
        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
858

859
860
861
862
863
864
865
866
867
868
869
870
871
872
        $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) ) {
873
874
                        $results[$res_row["crdate"]][0] += $res_row[$storage_unit] + $analyses_size;
                        $results[$res_row["crdate"]][1] += $res_row["purged_size"] + $analyses_size_purged;
875
876
877
878
                } else {
                        $results[$res_row["crdate"]] = array($res_row[$storage_unit] + $analyses_size, $res_row["purged_size"] + $analyses_size_purged);
                }
        }
879
        
880
881
882
883
884
885
886
887
888
889
        $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
890
    }
891

892
893
894
895
896
897
898
899
	/**
	 * 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
	 */
900
    static function add_comment($table_name, $id_label, $id, $cruser_id, $new_comment){
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
    	$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
     */
930
    static function get_all_comments($table_name, $id_label, $id){
931
932
933
934
935
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
    	$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
     */
967
    static function delete_comment($table_name, $id_label, $id, $comment_id){
968
969
970
    	
    	$res = 0 ;
    	//from association table
971
    	$where = $table_name.'.'.$id_label.'='.$id.' AND '.$table_name.'.comment_id='.$comment_id ;
972
973
    	$res = $GLOBALS['TYPO3_DB']-> exec_DELETEquery ($table_name, $where);
    	
974
975
976
977
978
    	//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 ;
979
980
981
982
983
984
985
986
987
    	}
    	return $res ;
    }
    
    /**
     * Update an existing comment
     * @param unknown $comment_id
     * @param unknown $new_comment
     */
988
    static function update_comment($comment_id, $new_comment){
989
990
991
992
    	return $GLOBALS['TYPO3_DB']-> exec_UPDATEquery ('tx_nG6_comment', 'uid='.$comment_id, array('comment' => $new_comment));
    }
    
    
993
994
995
996
	/*
	 * Project functions
	 *------------------------------------------------------------*/

997
998
999
1000
    /**
     * Add a comment to the project described by project_id
     * @param unknown $project_id
     * @param unknown $cruser_id
For faster browsing, not all history is shown. View entire blame