summaryrefslogtreecommitdiffstats
path: root/admin/survey/classes/class.SurveyCondition.php
blob: 08b6239baa277442a297cb65097bf4144452abff (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
<?php
class SurveyCondition
{
	private $_sid;

	private $_conditions = array();
	
	private $_chooseProfileJSAction = null;
	
	function __construct($sid)
	{
		global $lang;
		$this->_sid = $sid;
		# polovimo vse obstoječe ife
		$this->_conditions[0] = array(
			'id' => '0',
			'name' => $lang['srv_inv_condition_new_filter'],
			'number' => '0',
			'tip' => '0',
			'label' => $lang['srv_inv_condition_new_filter'],
			'collapsed' => '0',
			'folder' => '0',
			'enabled' => '1',
			'tab' => '0'
			);
		$str = "SELECT sif.*, ssc.name FROM srv_if AS sif JOIN srv_survey_conditions AS ssc ON (sif.id = ssc.if_id) WHERE ssc.ank_id = '$this->_sid'";
		$qry = sisplet_query($str);
		while ($row = mysqli_fetch_assoc($qry))
		{
			if (isset($row['id']) && (int)$row['id'] > 0)
			{
				$this->_conditions[$row['id']] = $row;
			}
		}
		if (isset($_REQUEST['surveyConditionProfileAction']) && !empty($_REQUEST['surveyConditionProfileAction']))
		{
			$this->_chooseProfileJSAction = $_REQUEST['surveyConditionProfileAction'];
		}
	}
	
	public function ajax()
	{
		switch ($_GET['a']) {
			case 'showCondition' :
				$this->displayConditions((int)$_POST['cid']);
				break;
			case 'newCondition' :
				$this->newCondition();
				break;
			case 'deleteCondition' :
				$this->deleteCondition((int)$_POST['cid']);
				break;
			case 'showRename' :
				$this->showRename();
				break;
			case 'renameCondition' :
				$this->renameCondition();
				break;
			default:
				echo 'ERROR! Missing function for action: '.$_GET['a'].'! (SurveyConditionProfile)';
				break;
		}
	}
	
	public function displayConditions($pid)
	{
		global $lang;
		
		$popUp = new PopUp();
		$popUp->setId('divConditionProfiles');
		$popUp->setHeaderText($lang[''].'Filtriranje s pogoji');
		
		#vsebino shranimo v buffer

		ob_start();
		
		echo '<input type="hidden" id="chooseProfileJSAction" value="'.$this->_chooseProfileJSAction.'" />';
		echo '<div class="condition_profile_holder">';
		
		echo '<div id="condition_profile" class="select">';
		if (count($this->_conditions) > 0)
		{
			foreach ($this->_conditions as $key => $value) {
				echo '<div class="option' . ( $pid == $value['id'] ? ' active' : '') . '" data-cid="' . $value['id'] . '" onclick="showSurveyCondition(\''.$value['id'].'\')">' . $value['name'] .'</div>';
			}
		}
		echo '</div>';
		echo '</div>';
		# tukaj prikazemo vsebino ifa
		echo '<div id="div_cp_preview">';
		echo '<div id="div_cp_preview_content">';
		if ($pid > 0) 
		{
			$b = new Branching($this->_sid);
			$b->condition_editing($pid, -2);
		
		} else {
			echo 'Dodaj nov pogoj:';
			echo '<input id="newSurveyConditionName" placeholder="Ime pogoja" >';
			echo '<a href="#" onclick="newSurveyCondition(); return false;" class="faicon if_add" style="margin-left: 5px;" title="Dodaj nov pogoj"> Dodaj nov pogoj</a>';
		}
		echo '</div>';
		echo '</div>';
		echo '<div id="surveyConditionCover"></div>';
		echo '<div id="renameProfileDiv"></div>';
		$content = ob_get_clean();
		
		#dodamo vsebino
		$popUp->setContent($content);
		
		# dodamo gumb Prekliči
		$popUp->addButton(new PopUpCancelButton());
		
		#dodamo gumb izberi profil
		$confirmAction = 'alert(\'No action set\')';
		if (isset($this->_chooseProfileJSAction) && !empty($this->_chooseProfileJSAction))
		{
			$confirmAction = $this->_chooseProfileJSAction;
		}
		$button = new PopUpButton($lang['srv_choose_profile']);
		$button -> setFloat('right')
				-> setButtonColor('orange')
				-> addAction('onClick',$confirmAction.'; return false;');
		$popUp->addButton($button);
		
		if ($pid > 0) {
			# dodamo gumb preimenuj
			$button = new PopUpButton($lang['srv_rename_profile']);
			$button -> setFloat('right')
				-> addAction('onClick','showRenameSurveyCondition(\''.$pid.'\'); return false;');
			$popUp->addButton($button);
			
			# dodamo gumb izbriši
			$button = new PopUpButton($lang['srv_delete_profile']);
			$button -> setFloat('right')
					-> addAction('onClick','deleteSurveyCondition(\''.$pid.'\'); return false;');
			$popUp->addButton($button);
		}
		
		echo $popUp;
		
						
	}
	
	private function newCondition()
	{
		global $lang;
		$result= array('error'=>1, 'if_id'=>0);
		
		$name = isset($_POST['name']) && !empty($_POST['name']) ? $_POST['name'] : $lang['srv_inv_condition_new_filter_name'];
		$result['name'] = $name;
		
		#kreiramo osnovo za if
		$str = "INSERT INTO srv_if (id) VALUES (NULL)";
		$sql = sisplet_query($str);
		$if_id = mysqli_insert_id($GLOBALS['connect_db']);

		if ((int)$if_id > 0)
		{
			$str = "INSERT INTO srv_condition (id, if_id, vrstni_red) VALUES ('', '$if_id', '1')";
			$sql = sisplet_query($str);
			$cond_id = mysqli_insert_id($GLOBALS['connect_db']);				
			
			if ((int)$cond_id > 0)
			{
				$result['cond_id'] = (int)$cond_id;
				$str = "INSERT INTO srv_survey_conditions (ank_id, if_id, name) VALUES ('$this->_sid', '$if_id', '$name')";
				$sql = sisplet_query($str);
				if (!sql)
				{
				}
				else
				{
					$result['error'] = 0;
					$result['if_id'] = (int)$if_id;
				}
			}
		}
			
		if ($result['error'] > 0)
		{
			# pobrišemo zgoraj kreiran if;
			if ((int)$if_id > 0)
			{
				$sql = sisplet_query("DELETE FROM srv_condition where if_id ='$if_id'");
				$sql = sisplet_query("DELETE FROM srv_if where id ='$if_id'");
			}	
		}
		
		
		echo json_encode($result);
		return;
	}
	
	private function deleteCondition($cid = 0)
	{
		global $lang;
		$result= array('error'=>1, 'cid'=>$cid);
		
		if ($cid > 0)
		{
			/* pobrisemo se za ifom*/
			$sql = sisplet_query("SELECT * FROM srv_condition WHERE if_id = '$cid'");
			while ($row = mysqli_fetch_array($sql)) 
			{
				if ((int)$row['id'] > 0) 
				{
					sisplet_query("DELETE FROM srv_condition_vre WHERE cond_id='".$row['id']."'");
				}
			}
			
			if ((int)$cid> 0) 
			{
				sisplet_query("DELETE FROM srv_condition WHERE if_id = '$cid'");
				sisplet_query("DELETE FROM srv_if WHERE id = '$cid'");
				
			}
			/*-- pobrisemo se za ifom*/
			
			$result['error'] = 0;
			$result['cid'] = 0;
		}
		
		# pobrišemo še morebitne seje
		SurveySession::sessionStart($this->_sid);
		SurveySession::remove('invitationAdvancedConditionId');
		
		echo json_encode($result);
		return;
	}
	
	public function setChooseAction($action)
	{
		if (!empty($action))
		{
			$this->_chooseProfileJSAction = $action;
		}
	}
	
	function getConditionName($if_id)
	{
		if (isset($this->_conditions[$if_id]['name']))
		{
			return $this->_conditions[$if_id]['name'];
		}
		global $lang;
		return $lang['srv_inv_condition_no_filter'];
	}
	
	
	function getConditionString($if_id)
	{
		$condition_label = '';
		ob_start();
		$b = new Branching($this->_sid);
		
		if ((int)$if_id > 0)
		{
			$b->display_if_label($if_id);
			#$condition_label = mysqli_escape_string(ob_get_contents());
			$condition_label = ob_get_contents();
			ob_end_clean();
		}
		return $condition_label;
	}
	
	function showRename() 
	{
		global $lang;
		// div za preimenovanje
	
		echo $lang['srv_missing_profile_name'].': ';
		echo '<input id="renameProfileName" name="renameProfileName" type="text" value="' . $this->_conditions[$_POST['cid']]['name'] . '" size="45"  />';
        echo '<input id="renameProfileId" type="hidden" value="' . $_POST['cid'] . '"  />';
        $button = new PopUpButton($lang['srv_rename_profile_yes']);
        echo $button -> setFloat('right')
        		->setButtonColor('orange')
        		-> addAction('onClick','renameSurveyCondition(); return false;');
        
        $button = new PopUpButton($lang['srv_cancel']);
        echo $button -> setFloat('right')
        		-> addAction('onClick','$(\'#divConditionProfiles #renameProfileDiv, #surveyConditionCover\').hide(); return false;');
	}
	
	function renameCondition()
	{
		global $lang;
		
		$result = array('error'=>1);
		
		$name = $_POST['name'];
		$cid = (int)$_POST['cid'];
		
		if ($this->_sid > 0 && isset($name) && !empty($name) && (int)$cid > 0)
		{
			$str = "UPDATE srv_survey_conditions SET name='$name' WHERE ank_id='$this->_sid' AND if_id='$cid'";
			$qry = sisplet_query($str);
			if ($qry)
			{
				$result['error'] = 0;
				$result['errorMsg'] = '';
				$result['if_id'] = (int)$cid;
			}
			else
			{
				$result['errorMsg'] = 'Prišlo je do napake!';
			}
		}
		if (!isset($name) || empty($name))
		{
			$result['errorMsg'] = 'Ime ne sme biti prazno!';
		}
		echo json_encode($result);
		return;			
	}
}