summaryrefslogtreecommitdiffstats
path: root/admin/survey/classes/surveyData/class.SurveySNDataFile.php
blob: aba152bab86bfb3424ec8dab3e8ac247b46bac6f (plain) (blame)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
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
<?php 
/**
 * Created on Dec.2010
 *
 * @author: Gorazd Vesleič
 *
 * @desc: za kreacijo datoteke s podatki za SN (Alterji)
 * 			Datoteko z alterji skreira iz obstoječe datoteke s podatki
 *
 * funkcije:
 * 		- Init() - inicializacija
 *
 * CHANGELOG:
 * - 9.12.2011
 *   Po novem bo potrebno ločit tabele za vsak SN loop. Zato se bo kreiralo tudi več datotek (koliko je pač glavnih loopov)
 *   Hkrati je potrebno navest za kateri krog atunučuja gre :) 
 *   zato je na začetek dodana funkcija ki prešteje loope
 */

#KONSTANTE
define(EXPORT_FOLDER, "admin/survey/SurveyData");
DEFINE (STR_DLMT, "|");

class SurveySNDataFile {
	
	private $surveyId = null;			# Id ankete
	private $folder = null;				# pot do datotek s podatki
	private $_HEADERS = null;			# Header podatki
	
	private $headFileName = null;						# pot do header fajla
	private $dataFileName = null;						# pot do data fajla
	private $dataFileStatus = null;						# status data datoteke
	private $SDF = null;								# class za inkrementalno dodajanje fajlov
	
	private $sn_loop_parents = null;		#glavni loopi
	private $sn_loop_spremenljivke = null;	# loopi po spremenljivkah
	private $sn_loop_data = null;			# ime loop variable, antonucci...
	private $snCreateFullTable = true;		# Ali prikazujemo celotno tabelo
	
	
	private $_VARS = array();		
	
	function __construct ($sid) {
		# nastavimo privzeto pot do folderjev
		global $site_path, $global_user_id;
		
		$this->surveyId = $sid;
		$this->folder = $site_path . EXPORT_FOLDER.'/';
		
		#inicializiramo class za datoteke
		$this->SDF = SurveyDataFile::get_instance();
		$this->SDF->init($sid);
		$this->headFileName = $this->SDF->getHeaderFileName();
		$this->dataFileName = $this->SDF->getDataFileName();
		$this->dataFileStatus = $this->SDF->getStatus();

		session_start();
		
		$this->snCreateFullTable = $_SESSION['sid_'.$sid]['snCreateFullTable'];
		if ( $this->dataFileStatus == FILE_STATUS_NO_DATA
				|| $this->dataFileStatus == FILE_STATUS_SRV_DELETED) {
			Common::noDataAlert();
			return false;
		}
	}
	
	public function setVars($_VARS) {
		$this->_VARS = $_VARS;
	}
	public function setParameter($parameter,$value) {
		$this->{$parameter} = $value;
	}
	/** Preštejemo loope
	 * 
	 */
	function countLoops() {
		if ($this->dataFileStatus >= 0 && $this->headFileName != '') {
			$this -> _HEADERS = unserialize(file_get_contents($this->headFileName)) ;
			# poiščemo skevence za vse variable loopa
			#$_headers = $this -> _HEADERS;
			$_headers = $this->getCleanHeader();
			
			#preštejemo koliko loopov imamo
			$this->sn_loop_parents = array();
			$this->sn_loop_spremenljivke = array();
			
			if (count($_headers) > 0) {
				foreach ($_headers as $spr => $spremenljivka) {
					if ($spremenljivka['loop_parent'] > 0) {
						$this->sn_loop_data[$spremenljivka['loop_parent']]['antonucci'] = $spremenljivka['antonucci'];
						$this->sn_loop_data[$spremenljivka['loop_parent']]['spr'] = $spr;
						$this->sn_loop_data[$spremenljivka['loop_parent']]['variable'] = $spremenljivka['variable'];
						$this->sn_loop_data[$spremenljivka['loop_parent']]['naslov'] = $spremenljivka['naslov'];
					}
					# spremenljivka je parent za loop, preštejemo variable
					if (is_countable($spremenljivka['grids']) && count($spremenljivka['grids']) > 0) {
						foreach($spremenljivka['grids'] AS $gid => $grid) {
							if (count($grid['variables']) > 0) {
								foreach($grid['variables'] AS $vid => $variable) {
									# če smo v loop parent 
									if ($spremenljivka['loop_parent'] > 0) {
										$this->sn_loop_parents[$spremenljivka['loop_parent']][$variable['sequence']] = $variable['sequence'];

									}
									# če smo v loop spremenljivki
									if ($spremenljivka['parent_loop_id'] > 0) {
										$this->sn_loop_spremenljivke[$spremenljivka['parent_loop_id']][$spremenljivka['loop_id']][$variable['sequence']] = $variable['sequence'];
									}
								}
							}						
						}
					}				
				}
			}
		}
	}

	/** Skreira datoteko z alterji iz obstoječe datoteke s podatki
	 * 
	 */
	function createSNDataFile($lpid, $sn_loop_parent,$_SN_head_file_name,$_sn_filename) {
		if ($this->dataFileStatus >= 0 && $this->headFileName != '') {
			# če kreiramo nove, pobrišemo morebitne obstoječe datoteke 
			$this->deleteOldSnFiles();
			
			
			#popucamo headers
			$_headers = $this->getCleanHeader();
		
			$sn_loop_spremenljivke = $this->sn_loop_spremenljivke;

			# resetiramo array za SN_HEADER 
			$_empty_name_filter = array();
			$SN_HEADER = array();
			
			$sequences = array(); # kam shranimo sequence
			$loop_header_cnt = 0;
					
			if (count($sn_loop_parent) > 0 ) {
				foreach ($sn_loop_parent as $lsid => $loop_sequence) {
					$loop_header_cnt ++;
					$_new_sequence = 1;
					
					# poiščemo variable loopa
					if (isset($sn_loop_spremenljivke[$lpid]) && count($sn_loop_spremenljivke[$lpid]) > 0) {
						$_loop_cnt = array_shift($sn_loop_spremenljivke[$lpid]);
					} else {
						$_loop_cnt  = array();
					}
					
					$sequences[$lpid.'_'.$lsid] = array();

					# Zloopamo skozi vse spremenljivke in dodamo v primerno skupino
					if (count($_headers) > 0) { 
						foreach ($_headers as $spr => $spremenljivka) {
									
							if ($loop_header_cnt === 1 ) {
								$tmp_spremenljivka = $spremenljivka;
							}
							# odvisno ali smo v loopu ali ne dodamo primerne spremnljivke
							if ((int)$spremenljivka['loop_parent'] > 0 || (int)$spremenljivka['parent_loop_id']) {
								# ali smo v parent spremenljivki ali v loop spremenljivkah
								if ((int)$spremenljivka['loop_parent'] > 0 && $lpid == (int)$spremenljivka['loop_parent']){
									
									$_first_parent_variable = 0;

									#smo v parent splemenljivki, dodamo samo variablo z primerno sekvenco
									if (count($spremenljivka['grids']) > 0) {
										foreach($spremenljivka['grids'] AS $gid => $grid) {
											if (count($grid['variables']) > 0) {
												foreach($grid['variables'] AS $vid => $variable) {
													
													if ($variable['sequence'] == $loop_sequence) {
														#$sequences[$lpid.'_'.$lsid][$spr] = $variable['sequence'];
														$sequences[$lpid.'_'.$lsid][] = $variable['sequence'];
														# v sn imenih imamo samo 1 variablo
														if ($loop_header_cnt === 1 && $_first_parent_variable === 0) {
															$_empty_name_filter[] = '($'.$_new_sequence .' != -1 && $'.$_new_sequence .' != -2 && $'.$_new_sequence .' != -3 && $'.$_new_sequence .' != -4 && $'.$_new_sequence .' != -5)';
															$tmp_spremenljivka['grids'][$gid]['variables'][$vid]['sequence'] = $_new_sequence;
															$tmp_spremenljivka['sequences'] = $_new_sequence;
															$tmp_spremenljivka['cnt_grids'] = 1;
															$tmp_spremenljivka['cnt_all'] = 1;
															$tmp_spremenljivka['grids']['0']['cnt_vars'] = 1;
																													 
															$_new_sequence++;
															$_first_parent_variable ++;	
															
														}
													}
													# odstranimo ostale variable
													if ( $gid == 0 && $vid > 0) {
														unset($tmp_spremenljivka['grids'][$gid]['variables'][$vid]);
													} else if ( $gid > 0) {
														unset($tmp_spremenljivka['grids'][$gid]);
													}
																
												}
											}
										}
										
									}
									
									# dodamo spremenljivko v nov header
									if ($loop_header_cnt === 1) {
										$SN_HEADER[$spr] = $tmp_spremenljivka;
									}
								}
								# ali smo v spremenljivki v loopu
								if ((int)$spremenljivka['parent_loop_id'] > 0) {
									$add_this_spr = false;
									$sequenceses = array();
									# smo v loop spremenljivkah, dodamo variable s pravo frekvenco
									if (count($spremenljivka['grids']) > 0) {
										foreach($spremenljivka['grids'] AS $gid => $grid) {
											if (count($grid['variables']) > 0) {
												foreach($grid['variables'] AS $vid => $variable) {
													if (isset($_loop_cnt[$variable['sequence']])) {
														$add_this_spr = true;
														#$sequences[$lpid.'_'.$lsid][$spr] = $variable['sequence'];
														$sequences[$lpid.'_'.$lsid][] = $variable['sequence'];
														if ($loop_header_cnt === 1) {
															$tmp_spremenljivka['grids'][$gid]['variables'][$vid]['sequence'] = $_new_sequence;
															$sequenceses[] = $_new_sequence; 
															$_new_sequence++;
														}
													}
												}
											}
										}
									}
									
									# dodamo spremenljivko v nov header
									if ($loop_header_cnt === 1 && $add_this_spr == true) {
										$SN_HEADER[$spr] = $tmp_spremenljivka;
										$SN_HEADER[$spr]['sequences'] = implode('_',$sequenceses);
									}
								}
							} else {
									
								# nismo v loopu
								$sequenceses = array();
								if (is_countable($spremenljivka['grids']) && count($spremenljivka['grids']) > 0) {
									foreach($spremenljivka['grids'] AS $gid => $grid) {
										if (count($grid['variables']) > 0) {
											foreach($grid['variables'] AS $vid => $variable) {
												#$sequences[$lpid.'_'.$lsid][$spr] = $variable['sequence'];
												$sequences[$lpid.'_'.$lsid][] = $variable['sequence'];
												if ($loop_header_cnt === 1) {
													$tmp_spremenljivka['grids'][$gid]['variables'][$vid]['sequence'] = $_new_sequence;
													$sequenceses[] = $_new_sequence;
													$_new_sequence++;
												}
																										
											}
										}
									}
								}
								
								# dodamo spremenljivko v nov header
								if ($loop_header_cnt === 1) {
									$SN_HEADER[$spr] = $tmp_spremenljivka;
									$SN_HEADER[$spr]['sequences'] = implode('_',$sequenceses);
								}
							}
						}
					}
				}
			}

			# zapišemo header za SN datoteko
			if (is_array($SN_HEADER) && count($SN_HEADER) > 0) {
				#zapišemo SN header datoteko
				file_put_contents($_SN_head_file_name, serialize($SN_HEADER));
				
			}

			# KREACIJA DATA DATOTEKE
			# skreiramo fajle z potrebnimi skevencami
			if (count($sequences) > 0) {
				$_original_data_file = $this->dataFileName;
				$_paste_files = '';

				foreach ($sequences AS $skey => $sequence) {
					
					if (is_array($sequence) && count($sequence)>0) {
						$_sequence = implode(",",$sequence);

						if (IS_WINDOWS) {
							$cmdLn1 = 'cut -d "|" -f '.$_sequence.' '.$_original_data_file.' > '.$this->folder.'export_sn_data_'.$this->surveyId.'_'.$lpid.'_'.$skey.'.dat';
						} else {
							$cmdLn1 = 'cut -d \'|\' -f '.$_sequence.' '.$_original_data_file.' > '.$this->folder.'export_sn_data_'.$this->surveyId.'_'.$lpid.'_'.$skey.'.dat';
						}
						$out1 = shell_exec($cmdLn1);
						$_paste_files .= $this->folder.'export_sn_data_'.$this->surveyId.'_'.$lpid.'_'.$skey.'.dat ';
					}					
				}

				# združimo datoteke v eno
				$_orig_date = explode("_",$_original_data_file);
				$_orig_date = explode(".",$_orig_date[3]);
				
				#$_merged_file_name = $this->folder.'export_sn_data_'.$this->surveyId.'_'.$_orig_date[0].'.dat';
				$_merged_file_name = $_sn_filename;
				$tmp_merged_file_name = $this->folder.'tmp_export_sn_data_'.$this->surveyId.'_'.$lpid.'_'.$_orig_date[0].'.dat';
				if (IS_WINDOWS) {
					$cmdLn2 = 'paste -d "\n" '.$_paste_files .'> '.$tmp_merged_file_name;
				} 
                else {
					$cmdLn2 = 'paste -d \'\n\' '.$_paste_files. '>' .$tmp_merged_file_name;
				}
				$out2 = shell_exec($cmdLn2);

				# pripravimo filtre, za data datoteko, da odstranimo zapise ki nimajo imen ( če je nekdo dodal samo 2 imena nekdo pa 5)
				$_empty_name_filter = implode(' && ',$_empty_name_filter);
				
				# sfiltriramo zapise  ki nimajo imen 
				if (IS_WINDOWS) {
					$cmdLn3 = 'awk -F"'.STR_DLMT.'" "BEGIN {OFS=\"\x7C\"} '.$_empty_name_filter.' { print $0 }" ' . $tmp_merged_file_name .' > ' . $_merged_file_name;
				} 
                else {
					$cmdLn3 = 'awk -F"'.STR_DLMT.'" \'BEGIN {OFS="\x7C"} '.$_empty_name_filter.' { print $0 }\' ' . $tmp_merged_file_name . ' > ' . $_merged_file_name;
				}
				$out3 = shell_exec($cmdLn3);
			
				# pobrišemo odvečne datoteke
				foreach (explode(" ",$_paste_files) as $filename_to_delete) {
					if (trim($filename_to_delete) != '') {
						$this->SDF->deleteFile($filename_to_delete);
					}
				}

				if (trim($tmp_merged_file_name) != '') {
					$this->SDF->deleteFile($tmp_merged_file_name);
				}
			}
		}		
	}
	
	function displayFullTableCheckbox() {
		global $lang;
		session_start();
		echo '<label><input id="snCreateFullTable" name="snCreateFullTable" onclick="setSnDisplayFullTableCheckbox();" type="checkbox"'.($this->snCreateFullTable==true?' checked="checked"':'').'>Prikaži razširjeno tabelo</label>';
	}
	
	
	function outputSNDataFile() {
		global $lang;
		$this->countLoops();
		
		# forsamo novo kreiranje! malo slaba rešitev - mogoče dodat enako kontrolao na zadnjega userja v SN fajlih
		$this->deleteOldSnFiles();
		
		$_original_head_file = $this->headFileName;
		$_original_data_file = $this->dataFileName;
		
		# timestam head datoteke
		$_orig_date_h = explode("_",$_original_head_file);
		$_orig_date_h = explode(".",$_orig_date_h[3]);
		# združimo datoteke v eno
		$_orig_date_d = explode("_",$_original_data_file);
		$_orig_date_d = explode(".",$_orig_date_d[3]);

		
		$this-> displayFullTableCheckbox();
		
		# Tukaj začnemo loopat po glavnih  loopih in nardimo ločene tabele za vsak loop

		# zloopamo tolikokrat koliko imamo variabel za loop ( v loop_parent)
		if (count($this->sn_loop_parents) > 0) {
			foreach ($this->sn_loop_parents as $lpid => $sn_loop_parent) {

				# head ime datoteke za loop
				$_SN_head_file_name = $this->folder.'export_sn_header_'.$this->surveyId.'_'.$lpid.'_'.$_orig_date_h[0].'.dat';
				# data ime datoteke za loop
				$_sn_filename = $this->folder.'export_sn_data_'.$this->surveyId.'_'.$lpid.'_'.$_orig_date_d[0].'.dat';
				# začasno ime datoteke za loop		
				$_sn_tmp1 = $this->folder.'tmp_1_export_sn_data_'.$this->surveyId.'_'.$lpid.'_'.$_orig_date_d[0].'.dat';
				# če SN header in SN data datoteka obstaja
				if (!file_exists($_SN_head_file_name) || !file_exists($_sn_filename)) {
					$this->createSNDataFile($lpid, $sn_loop_parent,$_SN_head_file_name,$_sn_filename);
				}
				
				# če SN header in SN data datoteka obstaja
				if (file_exists($_SN_head_file_name) && file_exists($_sn_filename)) {
					
					# naložimo header
					$SN_HEADER = unserialize(file_get_contents($_SN_head_file_name));
					echo '<div id="tableContainer" class="tableContainer">';

					echo '<h3>'.$lang['srv_loop_for_variable'].' <b>['. $this->sn_loop_data[$lpid]['variable']. '] - '. $this->sn_loop_data[$lpid]['naslov']. '</b>  ('.$lang['srv_loop_antonucci_circle'].' '.$this->sn_loop_data[$lpid]['antonucci'].')</h3>';
					

                    // TABELA
                    echo '<table id="dataTable" border="0" cellpadding="0" cellspacing="0" width="100%" class="scrollTable no_wrap_td social_network">';
					

                    // COLGROUP - Nastavimo colgroup, da na njega vezemo vse sirine v tabeli, zaradi resizinga stolpcev
                    echo '<colgroup>';

					$spr_cont = 0;
					foreach ($SN_HEADER AS $spid => $spremenljivka) {
						
                        if ($spr_cont > 0 && $spid != 'uid' && count($spremenljivka['grids']) > 0) {
								
                            foreach ($spremenljivka['grids'] AS $gid => $grid) {
                                
                                if (count ($grid['variables']) > 0) {
                                    
                                    foreach ($grid['variables'] AS $vid => $variable ){
                                        echo '<col>';
                                    }
                                }
                            }
						}
						$spr_cont++;
					}	

                    echo '</colgroup>';


                    // THEAD
                    echo '<thead class="fixedHeader">';
					echo '<tr>';
		
					# dodamo skrit stolpec uid
					echo '<th class="data_uid">&nbsp;</th>';
					
					$spr_cont = 0;
					foreach ($SN_HEADER AS $spid => $spremenljivka) {
						
                        if ($spr_cont > 0 && $spid != 'uid') {
							echo '<th colspan="'.$spremenljivka['cnt_all'].'" title="'.$spremenljivka['naslov'].'">';
							echo '<div class="headerCell">'.$spremenljivka['naslov'].'</div>';
							echo '</th>';
						}

						$spr_cont++;
					}

					# nova vrstica
					echo '</tr><tr>';
					
					# dodamo skrit stolpec uid
					echo '<th class="data_uid">&nbsp;</th>';
								
					$spr_cont = 0;
					foreach ($SN_HEADER AS $spid => $spremenljivka) {
						if ($spr_cont > 0 && $spid != 'uid') {
							if (count($spremenljivka['grids']) > 0) {
								
                                foreach ($spremenljivka['grids'] AS $gid => $grid) {
									echo '<th colspan="'.$grid['cnt_vars'].'" title="'.$grid['naslov'].'">';
									echo '<div class="headerCell">'.$grid['naslov'].'</div>';
									echo '</th>';
								}
							}	
						}

						$spr_cont++;
					}

					# nova vrstica
					echo '</tr><tr>';
					
					## dodamo skrit stolpec uid
					echo '<th class="data_uid">&nbsp;</th>';
								
					$spr_cont = 0;
					foreach ($SN_HEADER AS $spid => $spremenljivka) {
						
                        if ($spr_cont > 0 && $spid != 'uid' && count($spremenljivka['grids']) > 0) {
								
                            foreach ($spremenljivka['grids'] AS $gid => $grid) {
                                
                                if (count ($grid['variables']) > 0) {
                                    
                                    foreach ($grid['variables'] AS $vid => $variable ){
                                        echo '<th title="'.$variable['naslov'].($variable['other'] ? '&nbsp;(text)' : '').'">';
                                        echo '<div class="dataCell">'.$variable['naslov'];
                                        
                                        if ($variable['other'] == 1) {
                                            echo '&nbsp;(text)';
                                        }

                                        echo '</div>';
                                        echo '</th>';
                                    }
                                }
                            }
						}
						$spr_cont++;
					}			
					echo'</tr>';
					echo '</thead>';


					// TBODY - dodamo podatke
					if (file_exists($_sn_filename)) {
						
						// zamenjamo | z </td><td>
						if (IS_WINDOWS) {
							$cmdLn3 = 'sed "s*'.STR_DLMT.'*</td><td>*g" '.$_sn_filename.' > '.$_sn_tmp1;
						} else {
							$cmdLn3 = 'sed \'s*'.STR_DLMT.'*</td><td>*g\' '.$_sn_filename.' > '.$_sn_tmp1;
						}
						$out3 = shell_exec($cmdLn3);
		
						echo '<tbody class="scrollContent">';

						$file_handler = fopen ($_sn_tmp1, 'r');
					    while ($line = fgets ($file_handler)) {
					    	echo '<tr>';
							echo '<td class="data_uid">'.$line.'</td>';
                            echo '</tr>'; 
					    }

					    echo '</tbody>';
		
						if ($file_handler) {	
							fclose($file_handler);
						}
						
						# pobrišemo tmp falj
						if (trim($_sn_tmp1) != '') {
							$this->SDF->deleteFile($_sn_tmp1);
						}
					}

                    echo '</table>';

                    echo '</div>';
				}	
		
			}
		}
		
	}
	/** Pobriše morebitne stare SN daoteke 
	 * 
	 */
	function deleteOldSnFiles() {
		if ($this->surveyId > 0) {
			
			# odstranimo morebitne SN datoteke - header
			$files = glob($this->folder.'export_sn_header_'.$this->surveyId.'_*.dat');
			if(count($files ) > 0) {
				foreach ($files AS $file) {
					unlink($file);
				}
			}
			# odstranimo morebitne SN datoteke - data
			$files = glob($this->folder.'export_sn_data_'.$this->surveyId.'_*.dat');
			if(count($files ) > 0) {
				foreach ($files AS $file) {
					unlink($file);
				}
			}
			
		}
	}
	
	/* Tukaj pripravimo redosled in prikaz glavnih spremenljivk
	 * 
	 */
	function getCleanHeader() {
		# poiščemo skevence za vse variable loopa
		$header = $this -> _HEADERS;
		$cleanHeader = array();
		if (count($header) > 0) {
			foreach ($header AS $spr_id => $spremenljivka) {

				if ($this->_VARS[VAR_DATA] == '1') {
					$add_data = true;
				} else {
					$add_data = false;
				}
				
				# preverimo ali delamo kompleksno tabelo al samo simpl
				if ($this->snCreateFullTable == false) {
					$add_data = $add_data && ((int)$spremenljivka['loop_parent'] > 0 || (int)$spremenljivka['parent_loop_id'] > 0);
				}
				if ( $spremenljivka['tip'] == 'm' || $spremenljivka['tip'] == 'sm') {
					$add_data = false;
					switch ($spremenljivka['variable']) {
						case 'uid':
						case 'recnum':
							$add_data = true;
						break;
						case 'code':
							# ce prikazujemo sistemske ne prikazujemo recnumber
							if (!$this->_VARS[VAR_SHOW_SYSTEM] && $this->_VARS[VAR_META] && $this->_VARS[VAR_METAFULL]) {
								$add_data = true;
							}
							break;
						case 'status':
						case 'lurker':
							if ($this->_VARS[VAR_META] && $this->_VARS[VAR_METAFULL]) {
								$add_data = true;
							}
							break;
						case 'relevance':
							if ($this->_VARS[VAR_RELEVANCE] && $this->canDisplayRelevance) {
								$add_data = true;
							}
							break;
						case 'invitation':
							if ($this->_VARS[VAR_EMAIL]) {
								$add_data = true;
							}
							break;
						case 'testdata':
							$header = $this->SDF->getHeader();
							if (isset($header['testdata'])) {
								$add_data = true;
							}
							break;
						case 'smeta':
						case 'meta':
							if ($this->_VARS[VAR_METAFULL]) {
								$add_data = true;
							}
							break;
						case 'itime':
							if ($this->showItime == true) {
								$add_data = true;
							}
							break;
						}
					}
					if ($spremenljivka['hide_system'] == '1') {
						$add_data = false;
					}
					if ($add_data == true ) {
						$cleanHeader[$spr_id] = $spremenljivka;
					}
			}
		}
		return $cleanHeader;
	}
}
?>