summaryrefslogtreecommitdiffstats
path: root/admin/survey/classes/class.SurveyMissingProfiles.php
blob: bb9e2513d1cf5541dc563b96038e9ab23b43bb77 (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
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
<?php
/** @author: Gorazd Veselič
 * 
 * 	@Desc: za upravljanje z profili missingov za analize

 *  @Date: 28.09.2010
 */

session_start();
define('SMP_DEFAULT_PROFILE', 1);

class SurveyMissingProfiles
{
	static private $sid = null;					# id ankete
	static private $uid = null;					# id userja
	static private $inited = false;				# ali so profili ze inicializirani
	static private $currentProfileId = null;	# trenutno profil
	static private $profiles = array();			# seznam vseh profilov od uporabnika
	static private $smv = null;					# manjkajoče vrednosti
	
																		
																		// lurker je mal poseben, ker je neodvisen od ostalih (user je npr. 6 in lurker)
//	static private $allStatus = array('null',0,1,2,3,4,5,6,'lurker'); 			// Statusi anket katere štejemo kot ustrezne
//	static private $appropriateStatus = array(6,5); 			// Statusi anket katere štejemo kot ustrezne
//	static private $unAppropriateStatus = array(4,3,2,1,0); 	// Statusi anket katere štejemo kot neustrezne
//	static private $unKnownStatus = array('null'); 					// Statusi anket katere štejemo kot neustrezne
//	# za awk komando 
//	static private $_AWK_FILTER_TEXT = array(6=>'6',5=>'5',4=>'4',3=>'3',2=>'2',1=>'^1',0=>'0','null'=>'-1'); # texti za statuse. 6=>'6',5=>'5',4=>'4',3=>'3',2=>'2',1=>'^1',0=>'0',null=>'-1' Pri 1 je ^pomemben, če ne lahko vzame tudi -1
	
	static function Init($sid, $uid = null) {
		# nastavimo surveyId
		self::setSId($sid);
		self::$smv = new SurveyMissingValues($sid); 
		self::$smv -> Init();
		# nastavimo userja
		self::setGlobalUserId($uid);
		SurveyUserSetting :: getInstance()->Init(self::$sid, self::getGlobalUserId());
		if (self::$inited == false) {
			self::$inited = self :: RefreshData();
		}
	}
	
	static function RefreshData() {
		
		self::$profiles = array();
		# dodamo sejo če obstaja
		
		if (isset($_SESSION['missingProfile'])) {
			self::$profiles[$_SESSION['missingProfile']['id']] = $_SESSION['missingProfile'];
		}
		# dodamo sistemske profile, skreiramo jih "on the fly"
		self :: addSystemProfiles();
		
		# preberemo podatke vseh porfilov ki so na voljo in jih dodamo v array
		$stringSelect = "SELECT * FROM srv_missing_profiles WHERE uid = '".self::getGlobalUserId()."' OR (uid = '0' AND `system` = 1) ORDER BY id";
		$querySelect = sisplet_query($stringSelect);

		if (mysqli_num_rows($querySelect)) {
			while ( $rowSelect = mysqli_fetch_assoc($querySelect) ) {
				self::$profiles[$rowSelect['id']] = $rowSelect;
			}
		}
		# poiscemo privzet profil
		self::$currentProfileId = SurveyUserSetting :: getInstance()->getSettings('default_missing_profile');

		if (!self::$currentProfileId || self::$currentProfileId == 1)
			self::$currentProfileId = 1;

		# ce imamo nastavljen curent pid in profil z tem pid ne obstaja nastavomo na privzet profil 
		if (self::$currentProfileId != 1) {
			if (!isset(self::$profiles[self::$currentProfileId])) {
				self::$currentProfileId = 1;
				self::setDefaultProfileId(self::$currentProfileId);
			} 
		}
		
		# ce ne obstajajo podatki za cpid damo error
		if (!isset(self::$profiles[self::$currentProfileId])) {
			die("Profile data is missing!");
			return false;
		} else {
			return true;
		}

	}
	static function DisplayProfile( $pid = null) {
		global $lang;
		
        echo '<h2>'.$lang['srv_missing_settings'].'</h2>';
        
        echo '<div class="popup_close"><a href="#" onClick="missingProfileAction(\'cancle\'); return false;">✕</a></div>';
		
		if ($pid == null ) {
			$pid = self::$currentProfileId;
		}
	
		if ( self::$currentProfileId != SMP_DEFAULT_PROFILE ) {
	       	echo '<div id="not_default_setting" class="popup_note">';
	        echo $lang['srv_not_default_setting'];
	        echo '</div>';
        }
		
		
        echo '<div class="popup_main with_menu">';
        
		echo ' <div id="missing_profile_holder" class="popup_left">';
		self :: DisplayProfileOptions($pid);
		echo '<button class="small white-black" onclick="missingProfileAction(\'newName\'); return false;"><span class="faicon plus_32"></span>'.$lang['srv_create_new_profile'].'</button>';
		echo ' </div>';
		
		echo ' <div id="missing_profile_data_holder" class="popup_right">';
		self :: DisplayProfileData($pid);		
		echo ' </div>';

        echo '</div>';

		
		// GUMBI
		echo '<div class="button_holder">';
		
        # preklici - zapri
		echo '<button class="medium white-blue" onclick="missingProfileAction(\'cancle\'); return false;">'.$lang['srv_close_profile'].'</button>';			
		
        # shrani kot seja
		if (self::$profiles[$pid]['system'] == 1 || $pid == -1) {
			$run_lbl = $lang['srv_run_as_session_profile'];
			echo '<button class="medium blue" onclick="missingProfileAction(\'runSession\'); return false;">'.$run_lbl.'</</button>';
		} 
        else {
			# shrani - pozeni
			$run_lbl = $lang['srv_run_profile'];
			echo '<button class="medium blue" onclick="missingProfileAction(\'run\'); return false;">'.$run_lbl.'</</button>';
		}

        echo '</div>';
		
		
		// cover Div
        echo '<div id="missingProfileCoverDiv"></div>';
        
        // div za shranjevanje novega profila
        echo '<div id="newProfile">';
		
		echo '<div class="setting_holder">';

        echo '<div class="setting_item">';
        echo '<label>'.$lang['srv_missing_profile_name'].':</label>';
		echo '<input id="newProfileName" name="newProfileName" type="text" class="text large"  />';
        echo '</div>';
        echo '</div>';

        echo '<div class="button_holder">';
        echo '<button class="medium white-blue" onClick="missingProfileAction(\'newCancle\'); return false;">'.$lang['srv_close_profile'].'</button>';             
        echo '<button class="medium blue" onclick="missingProfileAction(\'newSave\'); return false;">'.$lang['srv_save_profile'].'</button>';
        echo '</div>';
		
        echo '</div>';

        // div za preimenovanje
        echo '<div id="renameProfileDiv">';
		
		echo '<div class="setting_holder">';

        echo '<div class="setting_item">';
        echo '<label>'.$lang['srv_missing_profile_name'].':</label>';
        echo '<input id="renameProfileName" class="text large" name="renameProfileName" type="text" value="' . self::$profiles[$pid]['name'] . '" />';
        echo '<input id="renameProfileId" type="hidden" value="' . $pid . '"  />';
		echo '</div>';
        echo '</div>';

        echo '<div class="button_holder">';
        echo '<button class="medium white-blue" onClick="missingProfileAction(\'renameCancle\'); return false;">'.$lang['srv_close_profile'].'</button>';             
        echo '<button class="medium blue" onclick="missingProfileAction(\'rename\'); return false;">'.$lang['srv_rename_profile_yes'].'</button>';
        echo '</div>';

        echo '</div>';

        // div za brisanje
        echo '<div id="deleteProfileDiv">'.$lang['srv_missing_profile_delete_confirm'].': <span class="semi-bold">' . self::$profiles[$pid]['name'] . '</span>?';
        echo '<input id="deleteProfileId" type="hidden" value="' . $pid . '"  />';

		echo '<div class="button_holder">';
        echo '<button class="medium white-blue" onClick="missingProfileAction(\'deleteCancle\'); return false;">'.$lang['srv_close_profile'].'</button>';             
        echo '<button class="medium blue" onclick="missingProfileAction(\'deleteConfirm\'); return false;">'.$lang['srv_delete_profile_yes'].'</button>';
        echo '</div>';

		echo '</div>';
	}

	static function DisplayProfileData($pid) {
		global $lang;
		$curentProfileData = self :: getProfileValues($pid); 
		
		$_tpes_array = array(MISSING_TYPE_DESCRIPTOR=>$lang['srv_analiza_arhiviraj_type_'.MISSING_TYPE_DESCRIPTOR],MISSING_TYPE_FREQUENCY=>$lang['srv_analiza_arhiviraj_type_'.MISSING_TYPE_FREQUENCY],MISSING_TYPE_CROSSTAB=>$lang['srv_analiza_arhiviraj_type_'.MISSING_TYPE_CROSSTAB]);
		$_sys_missings = self::$smv->GetMissingValuesForSurvey();
		$_sys_unset = self::$smv->GetUnsetValuesForSurvey();

		echo '<div id="infoMissingProfile" class="profile_right_note">' . $lang['srv_missing_profile_title1'] . '</div>';

		echo '<div id="missingProfileFieldsetHolder1">';
		
		echo '<fieldset id="missingProfileFieldset">';
		echo '<legend>' . $lang['srv_missing_profile_title2'] . '</legend>';

		echo '<form name="" id="" autocomplete="off">';

		echo '<table id="missing_table_1">';
		echo '<tr>';
		foreach ($_tpes_array AS $tkey => $tlabel) {
			echo '<th class="center">'. $tlabel .'</th>';
		}
		echo '<th></th>';
		echo '</tr>';
		
		# loop sozi sistemske missinge
		foreach ($_sys_unset  as $mkey => $mvalue) {
			echo '<tr>';

			# loop skozi tipe
			foreach ($_tpes_array AS $tkey => $tlabel) {
				echo '<td class="center">';
				echo '<input name="profile_value[]" type="checkbox" id="mv_'.$tkey.'_'.$mkey . '"' .
				(isset($curentProfileData[$tkey][$mkey]) ? ' checked' : '').
				'/>';
				echo '<label class="empty"></label>';
				echo '</td>';
			}
			echo '<td>';
			echo '(' . $mkey . ") " . $mvalue;
			echo '</td>';

			echo '</tr>';
		}


		echo '</table>';
		
		echo '</form>';
		echo '</fieldset>';
		
		echo '<fieldset id="missingProfileFieldset">';
		echo '<legend>' . $lang['srv_missing_profile_title3'] . '</legend>';
		
		echo '<table id="missing_table_2">';
		echo '<tr>';
		foreach ($_tpes_array AS $tkey => $tlabel) {
			echo '<th class="center">'. $tlabel .'</th>';
		}
		echo '<th></th>';
		echo '</tr>';
		
		echo '<form name="" id="" autocomplete="off">';

		# loop sozi sistemske missinge
		foreach ($_sys_unset  as $mkey => $mvalue) {
			echo '<tr>';

			# loop skozi tipe
			foreach ($_tpes_array AS $tkey => $tlabel) {
				echo '<td class="center">';
				echo '<input name="profile_value[]" type="checkbox" id="mv_'.$tkey.'_'.$mkey . '"' .
				(isset($curentProfileData[$tkey][$mkey]) ? ' checked' : '').
				'/>';
				echo '<label class="empty"></label>';
				echo '</td>';
			}
			echo '<td>';
			echo '(' . $mkey . ") " . $mvalue;
			echo '</td>';

			echo '</tr>';
		}


		echo '</table>';
		
		echo '</form>';
		echo '</fieldset>';

		echo '</div>';

		echo '<div id="missingProfileFieldsetHolder1">';
		echo '<fieldset id="missingProfileFieldset">';
		echo '<legend>'.$lang['srv_missing_profile_title5'].'</legend>';
		# prikažemo še radio gumbe za način prikaza MV
		
		$radio_selected = self::$profiles[$pid]['display_mv_type'];

		echo '<div class="mv_settings_other">';
		echo '<input name="display_mv_type" id="display_mv_type_0" type="radio" value="0"'.($radio_selected == 0 ? ' checked' : '').'><label for="display_mv_type_0">'.$lang['srv_missing_profile_display_radio0'].'</label>';
		echo '&nbsp;&nbsp;<input name="display_mv_type" id="display_mv_type_1" type="radio" value="1"'.($radio_selected == 1 ? ' checked' : '').'><label for="display_mv_type_1">'.$lang['srv_missing_profile_display_radio1'].'</label>';
		echo '&nbsp;&nbsp;<input name="display_mv_type" id="display_mv_type_2" type="radio" value="2"'.($radio_selected == 2 ? ' checked' : '').'><label for="display_mv_type_2">'.$lang['srv_missing_profile_display_radio2'].'</label>';
		echo '</div>';

		echo '<div class="setting_holder">';
		echo '<input name="show_zerro" id="show_zerro" type="checkbox" ' .
		 (self::$profiles[$pid]['show_zerro'] == 1 ? ' checked="checked"' : '') . ' autocomplete="off"/>'.NEW_LINE;
		echo '<label>'.$lang['srv_missing_profile_other_show_zerro'].'</label>';
		echo '</div>';
	
		echo '<div class="setting_holder">';
		echo '<input name="merge_missing" id="merge_missing" type="checkbox" ' .
		 (self::$profiles[$pid]['merge_missing'] == 1 ?  ' checked="checked"' : '') . ' autocomplete="off"/>';
		echo '<label>'.$lang['srv_missing_profile_other_merge_missing'].'</label>';
		echo '</div>';

		echo '</fieldset>';
		echo '</div>';
	}
	
	static function DisplayProfileOptions($pid) {
		global $lang;
		
		echo '<div id="missing_profile" class="list select">';	

		foreach ( self::$profiles as $key => $profile ) {
			
			echo '<div id="missing_profile_'.$profile['id'].'" class="list-item option' . ($profile['id'] == $pid ? ' active' : '') . '" value="' . $profile['id'] . '">';

			echo $profile['name'];

			if($profile['id'] == $pid){
                echo '<div class="profile_icons">';
                
				if (self::$profiles[$pid]['system'] != 1 ) {
                    echo '<a href="#" title="'.$lang['srv_delete_profile'].'" onclick="missingProfileAction(\'deleteAsk\'); return false;"><span class="faicon delete_circle icon-orange_link floatRight" style="margin-top:1px;"></span></a>'."\n";
				}
				if (self::$profiles[$pid]['system'] != 1 && $pid != -1) {		
                    echo '<a href="#" title="'.$lang['srv_rename_profile'].'" onclick="missingProfileAction(\'renameAsk\'); return false;"><span class="faicon edit floatRight spaceRight"></span></a>'."\n";
				}

                echo '</div>';
			}
			
			echo '</div>';	
		}
		echo '</div>';

		echo '<script>';
		echo '  $( function(){scrollToProfile("#missing_profile_'.$pid.'");} );';
		echo '</script>';
	}
	
	/** klici ajax funkcij
	 * 
	 */
	static function ajax() {
		
		$pid = $_POST['pid'];
		switch ($_GET['a']) {
			case 'change_profile' :
				self :: setDefaultProfileId($pid);
			break;
				
			break;
			case 'show_profile' :
				self :: DisplayProfile($pid);
			break;
			case 'run_profile' :
				self :: SaveProfile($pid);
				self :: setDefaultProfileId($pid);
			break;
			case 'delete_profile':
				self :: DeleteProfile($pid);
				self :: setDefaultProfileId(1);
			break;
			case 'save_profile':
				$new_id = self :: saveNewProfile();
				self :: setDefaultProfileId($new_id);
				echo $new_id;
			break;
			case 'rename_profile':
				$_rename = self :: RenameProfile($pid);
				echo $_rename;
			break;
			
			default:
				echo 'ERROR! Missing function for action: '.$_GET['a'].'!';
			break;
		}
	}

	/** getProfiles
	 * 
	 */
	static function getProfiles() {
		return self::$profiles;	
	}

	static public function getProfile($pid) {
		return self::$profiles[$pid];	
	}
	
	/** setSurveyId
	 * 
	 */
	static function setSId($surveyId) {
		self::$sid = $surveyId;
	}
	
	/** setGlobalUserId
	 * 
	 */
	static function setGlobalUserId($uid = null) {
		if ($uid == null) {
			global $global_user_id;
			self::$uid = $global_user_id;
		} else {
			self::$uid = $uid;
		}
	}
	
	/** getGlobalUserId
	 * 
	 */
	static function getGlobalUserId() {
		return self::$uid;
	}
	static function getCurentProfileId() {
		return self::$currentProfileId;
	}
	/**
	 * 
	 * @param unknown_type $pid
	 */
	static function setCurentProfileId($pid) {
		# ce je seja, preverimo ali obstaja cene nardimo privzetega 1
		if ($pid == -1) {
			if (!(isset($_SESSION['missingProfile']['-1'])))
				$pid = 1;
		} else if (!$pid) {
			$pid = 1;
		}
		
		return self::$currentProfileId = $pid;
	}

	static function setDefaultProfileId($pid) {
		# ce je seja, preverimo ali obstaja cene nardimo privzetega 1
		if ($pid == -1) {
			if (!(isset($_SESSION['missingProfile']))) {
				$pid = 1;
			}
		} else if (!$pid) {
			$pid = 1;
		}

		SurveyUserSetting :: getInstance()->saveSettings('default_missing_profile', $pid);
		self::$currentProfileId = $pid;
		return true; 
	}
	
	static function SaveProfile($pid) {
		global $lang;
		$insert_id = 0;
		$missing_values = $_POST['missing_values'];
		$display_mv_type = isset($_POST['display_mv_type']) ? $_POST['display_mv_type'] : 0;
		
		if (isset($pid) && $pid != null) {
			# ce mamo sistemski profil ga ne shranjujemo
			$checkSelect = "SELECT * as cnt FROM srv_missing_profiles WHERE id = '".$pid."' and `system` = 1"; 
			$checkQry = sisplet_query($checkSelect);
			if (mysqli_num_rows($checkQry) > 0) {
				return $pid;
			}
			
			$show_zerro = (isset($_POST['show_zerro']) && $_POST['show_zerro'] == 'true') ? 1 : 0;
			$merge_missing = (isset($_POST['merge_missing']) && $_POST['merge_missing'] == 'true') ? 1 : 0;
			
			# ce imamo session pozenemo kot sejo
			if ($pid == -1) {
				$missing_values = explode(',',$missing_values);
				$_SESSION['missingProfile'] = array('id'=>'-1','uid'=>0,'name'=>$lang['srv_temp_profile'],'system'=>0, 'display_mv_type'=>$display_mv_type, 'show_zerro'=>$show_zerro, 'merge_missing'=>$merge_missing);
				# pobrišemo predhodne nastavitve
				unset($_SESSION['missingProfile']['values']);
				if (count($missing_values) > 0) {
					foreach ($missing_values as $_missing_value) {
						$_missing_value = substr($_missing_value,3);
						list($type,$key) = explode('_',$_missing_value);
						$_SESSION['missingProfile']['values'][$type][$key] = true;
					}
				}
				return -1;				
			}
			# imamo podatke, updejtamo profil v bazi (profili z id 1,2,3 so sitemski)
			if ($pid > 3) {
				# shranimo morebitno spremembo nastavitve display_mv_type
				$updateString = "UPDATE srv_missing_profiles SET display_mv_type='".$display_mv_type."', show_zerro='".$show_zerro."', merge_missing='".$merge_missing."' WHERE id='".$pid."' AND `system` = 0";
				$updatequery = sisplet_query($updateString);
				
				# najprej pobrišemo stare podatke za ta profil
				$stringDelete = "DELETE FROM srv_missing_profiles_values WHERE missing_pid ='".$pid."'";
				$deleteQuery = sisplet_query($stringDelete);
				
				# shranimo nove vrednosti
				$missing_values = explode(',',$missing_values);
				if (count($missing_values ) > 0) {
					$insertString = "INSERT INTO srv_missing_profiles_values (missing_pid, missing_value, type) VALUES";
					$prefix = '';

					foreach ($missing_values as $_missing_value) {
						$_missing_value = substr($_missing_value,3);
						list($type,$key) = explode('_',$_missing_value);
						$insertString .= $prefix . " (".$pid.", ".$key.", ".$type.")";
						$prefix = ',';
					}
					$queryInsert = sisplet_query($insertString) 
						or die(mysqli_error($GLOBALS['connect_db']));
				}
			}
			return $pid;	
		}
		
	}

	static function saveNewProfile() {
		
		$name = $_POST['name'];
		$missing_values = $_POST['missing_values'];
		$display_mv_type = isset($_POST['display_mv_type']) ? $_POST['display_mv_type'] : 0;
		
		$insert_id = 0;
		if (isset($name) && $name != null ) {
			# imamo podatke, vstavimo nov profil v bazo
			$show_zerro = (isset($_POST['show_zerro']) && $_POST['show_zerro'] == 'true') ? 1 : 0;
			$merge_missing = (isset($_POST['merge_missing']) && $_POST['merge_missing'] == 'true') ? 1 : 0;
			
			$insertString = "INSERT INTO srv_missing_profiles (uid,name,system,display_mv_type,show_zerro,merge_missing) VALUES ('".self::getGlobalUserId()."', '".$name."', 0, '".$display_mv_type."', '".$show_zerro."', '".$merge_missing."')";
			$queryInsert = sisplet_query($insertString);
			$insert_id = mysqli_insert_id($GLOBALS['connect_db']);
			if ($insert_id > 0) {
				# če je insert id <= 3 ga popravimo da je večji od 3.
				if ($insert_id <= 3) {
					$selectId = "SELECT max(id) FROM srv_missing_profiles";
					list($maxInseredId) = mysqli_fetch_row(sisplet_query($selectId));
					$newInsert_id = max(4,$maxInseredId);
					$updateString = "UPDATE srv_missing_profiles SET id='".$newInsert_id."' WHERE id='".$insert_id."'";
					$updatequery = sisplet_query($updateString);
					$insert_id = $newInsert_id;
				}
				
				self::$profiles[$insert_id] = array('id'=>$insert_id,'uid'=>self::getGlobalUserId(),'name'=>$name,'system'=>0);
				
				# shranimo nove vrednosti
				$missing_values = explode(',',$missing_values);
				if (count($missing_values ) > 0) {
					$insertString = "INSERT INTO srv_missing_profiles_values (missing_pid, missing_value, type) VALUES";
					$prefix = '';

					foreach ($missing_values as $_missing_value) {
						$_missing_value = substr($_missing_value,3);
						list($type,$key) = explode('_',$_missing_value);
						$insertString .= $prefix . " (".$insert_id.", ".$key.", ".$type.")";
						$prefix = ',';
					}
					$queryInsert = sisplet_query($insertString);
				}
				
			} else {
				
			}
		}
		return $insert_id;
	}
	
	static function RenameProfile($pid) {
		$name = $_POST['name'];
		if (isset($pid) && $pid != null && isset($name) && $name != null ) {
			$updateString = "UPDATE srv_missing_profiles SET name = '".$name."' WHERE id = ".$pid;
			$updated = sisplet_query($updateString);
			if ($updated) {
				return 0;
			} else {
				return mysqli_error($GLOBALS['connect_db']);
			}
		} else {
			if (!isset($pid) || $pid == null || trim($pid) == '') {
				return 'Invalid profile Id';
			} else  if (!isset($name) || $name == null || trim($name) == '') {
				return 'Invalid profile name';
			} else {
				return 'Error!';
			}
		}

	}
	
	static function DeleteProfile($pid) {
		if ($pid == -1) {
			unset($_SESSION['missingProfile']);
		}
		# izbrišemo lahko samo nesistemske profile
		if (self::$profiles[$pid]['system'] != 1) {
			# zaradi ključev se avtomatsko pobriše tudi: srv_missing_profiles_values
			$sqlDelete = sisplet_query("DELETE FROM srv_missing_profiles WHERE id = '$pid' AND `system` != '1'");		
		} 
	}
		
	static function DisplayLink($hideAdvanced = true) {
		global $lang;
        
		// profili missingov
        $missingProfiles = self :: getProfiles();
        $izbranMissingProfile = self :: getCurentProfileId();
        
        $css = ($izbranMissingProfile == SMP_DEFAULT_PROFILE ? ' gray' : '');
        if ($hideAdvanced == false || $izbranMissingProfile != SMP_DEFAULT_PROFILE ) {
        	echo '<li>';
        	echo '  <span class="'.$css.'" id="link_missing_profile" title="' . $lang['srv_analiza_setup_profile'] . '" onClick="show_missing_profiles();">' . $lang['srv_analiza_setup_profile'] . '</span>'."\n";
        	echo '</li>';
        }
	}

	/** Ustvarimo tri sistemske profile ustvarimo jih navidezno
	 * 
	 * Brez manjkajočih vrednosti - ne prikazuje manjkajočih vrednosti (display_mv_type = 0)
	 * Skupaj MV - prikaže samo skupaj manjkajoče vrednosti (display_mv_type = 1)
	 * Podrobno MV - prikaže razširjeno manjkajoče vrednosti (display_mv_type = 2)
	 */
	static function addSystemProfiles() {
		global $lang;
		
		# skreiramo 3 sistemske manjkajoče profile
		for ( $i = 1; $i <= 3; $i++ ) {
			self::$profiles[$i] = array('id'=>$i,'uid'=>self::getGlobalUserId(),'name'=>$lang['srv_missing_profiles_profile'.$i.'_lbl'],'system'=>1, 'display_mv_type'=>(int)$i-1);
		}
	}
	
	/** Vrenmo podatke o izbranem profilu v obliki arraya
	 * 
	 * @param unknown_type $pid
	 */
	static function getProfileValues($pid) {
		$result = array();

		if ($pid == -1) {
			# beremo iz seje
			if (isset($_SESSION['missingProfile'])) {
				$result = $_SESSION['missingProfile']['values'];
				return $result;
			} else {
				$pid = 1;
			}
		}
		# če je sistemski profil preberemo iz avtomatsko skreiranih
		if ( ($pid < 3 && $pid >= 0) || (isset(self::$profiles[$pid]) && self::$profiles[$pid]['system'] == 1)) {
			# imamo sistemski profil, preberemo podatke ki so bili skreirani "on the fly"
			#najprej poiščemo sistemske missinge
			$_sys_missings = self::$smv->GetMissingValuesForSurvey();
			$_sys_unset = self::$smv->GetUnsetValuesForSurvey(array(2,3));
			
			# sistemske missinge (-1,-2,-3,-4) dodamo k vsem profilom
			if (count($_sys_missings) > 0) {
				foreach($_sys_missings AS $key => $value) {
					$result[MISSING_TYPE_DESCRIPTOR][$key] = true;
					$result[MISSING_TYPE_FREQUENCY][$key] = true;
					$result[MISSING_TYPE_CROSSTAB][$key] = true;
				}
				 
			}
			# neopredeljene vrednosti  (-99,-98,-97) dodamo samo k opisnim in krostabulacijam
			if (count($_sys_unset) > 0) {
				foreach($_sys_unset AS $key => $value) {
					$result[MISSING_TYPE_DESCRIPTOR][$key] = true;
					$result[MISSING_TYPE_CROSSTAB][$key] = true;
				}
				 
			}
		} else {
			$selectString = "SELECT missing_value, type FROM srv_missing_profiles_values WHERE missing_pid = '".$pid."'";
			$querySelect = sisplet_query($selectString);
			if (mysqli_num_rows($querySelect)) {
				while ( $rowSelect = mysqli_fetch_assoc($querySelect) ) {
					$result[$rowSelect['type']][$rowSelect['missing_value']] = true;
				}
			}
		} 

		return $result;
	}
	
	/*
	 * type = 1 => opisne
	 * type = 2 => frekvence
	 */
	static function GetMissingValuesForAnalysis($type,$pid=null) {
		$result = array();
	    
		#poiščemo sistemske missinge, z labele
		
		$_survey_missings = self::$smv->GetMissingValuesForSurvey(array(1,2,3));
		
		if ($pid == null)
		{
			$pid = self::$currentProfileId;
		}
		$curentProfileData = self :: getProfileValues($pid);
		if (count($curentProfileData[$type]) > 0) {
		    foreach ($curentProfileData[$type] AS $key => $is_set) {
		    	$result[$key] = isset($_survey_missings[$key]) ? $_survey_missings[$key] : $key;  
		    }
		}
		return $result;
		
	}
}

?>