1<?php
2
3/**
4 *	Edit logical circuit details
5 ************************/
6
7/* functions */
8require_once( dirname(__FILE__) . '/../../../functions/functions.php' );
9
10# initialize user object
11$Database 	= new Database_PDO;
12$User 		= new User ($Database);
13$Admin	 	= new Admin ($Database, false);
14$Tools	 	= new Tools ($Database);
15$Result 	= new Result ();
16
17# verify that user is logged in
18$User->check_user_session();
19
20# perm check popup
21if($_POST['action']=="edit") {
22    $User->check_module_permissions ("circuits", 2, true, true);
23}
24else {
25    $User->check_module_permissions ("circuits", 3, true, true);
26}
27
28# create csrf token
29$csrf = $User->Crypto->csrf_cookie ("create", "circuitsLogical");
30
31# strip tags - XSS
32$_POST = $User->strip_input_tags ($_POST);
33
34# validate action
35$Admin->validate_action ($_POST['action'], true);
36
37# fetch custom fields
38$custom = $Tools->fetch_custom_fields('circuitsLogical');
39
40# ID must be numeric
41if($_POST['action']!="add" && !is_numeric($_POST['circuitid']))	{ $Result->show("danger", _("Invalid ID"), true, true); }
42
43# fetch circuit details
44if( ($_POST['action'] == "edit") || ($_POST['action'] == "delete") ) {
45	$logical_circuit = $Admin->fetch_object("circuitsLogical", "id", $_POST['circuitid']);
46	// false
47	if ($circuit===false)                                          { $Result->show("danger", _("Invalid ID"), true, true);  }
48}
49// defaults
50else {
51	$circuit = new StdClass ();
52	$circuit->provider = 0;
53}
54
55# fetch all providers, devices, locations
56$circuit_providers = $Tools->fetch_all_objects("circuitProviders", "name");
57$all_devices       = $Tools->fetch_all_objects("devices", "hostname");
58$all_locations     = $Tools->fetch_all_objects("locations", "name");
59$all_circuits 	   = $Tools->fetch_all_circuits();
60$circuit_types 	   = $Tools->fetch_all_objects ("circuitTypes", "ctname");
61$type_hash = [];
62foreach($circuit_types as $t) {
63	$type_hash[$t->id] = $t->ctname;
64}
65
66# no providers
67if($circuit_providers===false) 	{
68	$btn = $User->is_admin(false) ? "<hr><a href='' class='btn btn-sm btn-default open_popup' data-script='app/admin/circuits/edit-provider.php' data-class='700' data-action='add' data-providerid='' style='margin-bottom:10px;'><i class='fa fa-plus'></i> "._('Add provider')."</a>" : "";
69	$Result->show("danger", _("No circuit providers configured."."<hr>".$btn), true, true);
70}
71
72# set readonly flag
73$readonly = $_POST['action']=="delete" ? "readonly" : "";
74?>
75
76<script type="text/javascript">
77
78function addAllCircuitsHandlers(){
79	$('#all_circuits tbody tr td').on("click","a[name='addbtn']",function(event){
80		var row = $(this).parents('tr').clone();
81		var id = row.find("td:first input").val();
82		//var row_clone = row.clone();
83		//var id = row_clone.find("td:last input").val();
84		row.find("td:first").remove();
85
86		row.prepend("<td><a name='rembtn' class='btn btn-xs btn-default btn-danger' rel='tooltip' title='Remove'><i class='fa fa-times'></i></a></td>");
87		row.append("\
88							 <td><input class='id' type='hidden' value='"+ id +"'> \
89<div class='input-group pull-right'> \
90<a name='mvdnbtn' class='btn btn-xs btn-default' rel='tooltip' title='Move down'><i class='fa fa-angle-down'></i></a>\
91<a name='mvupbtn' class='btn btn-xs btn-default' rel='tooltip' title='Move up'><i class='fa fa-angle-up'></i></a>\
92\
93</div>\
94							 </td> \
95		");
96		$('#selected_circuits tbody').append(row);
97		update_hidden_input();
98		if(!verifySelectedCircuits()){
99			$('#duplicate_error').css('display','block');
100		}else{
101			$('#duplicate_error').css('display','none');
102		}
103		//Since the new row is added, need to make sure it has its event handler
104		addSelectedCircuitsHandlers();
105		$(".tooltip").hide();
106	});
107}
108
109function addSelectedCircuitsHandlers(){
110	//Remove from list
111	$('#selected_circuits tr td').on("click","a[name='rembtn']",function(event){
112		$(this).parents('tr').remove();
113		update_hidden_input();
114		if(verifySelectedCircuits()){
115			$('#duplicate_error').css('display','none');
116		}
117	});
118
119	//Move selected circuit up
120	$('#selected_circuits tr td').on("click","a[name='mvupbtn']",function(event){
121		var r = $(this).parents("tr:first");
122		r.insertBefore(r.prev());
123		update_hidden_input();
124	});
125
126	//Move selected circuit down
127	$('#selected_circuits tr td').on("click","a[name='mvdnbtn']",function(event){
128		var r = $(this).parents("tr:first");
129		r.insertAfter(r.next());
130		update_hidden_input();
131	});
132}
133
134function verifySelectedCircuits(){
135	var list = $('#circuit_list').val();
136	var ids = list.split(".");
137	var seen_ids = [];
138	for(var i = 0; i < ids.length; i++){
139		if(seen_ids.includes(ids[i])){
140			return false;
141		}
142		seen_ids.push(ids[i]);
143	};
144	return true;
145};
146
147function update_hidden_input(){
148	var id_string = "";
149	$('#selected_circuits tbody tr').each(function(){
150		id_string += $(this).find("input.id").val();
151		id_string += ".";
152	});
153	$('#circuit_list').val(id_string);
154 };
155
156 //This is a little nasty, is a result of using jQuery only before using bootstrap tables
157 $(document).ready(function(){
158	 addSelectedCircuitsHandlers();
159	 update_hidden_input();
160     if ($("[rel=tooltip]").length) { $("[rel=tooltip]").tooltip(); }
161 		 $('#all_circuits').bootstrapTable({
162 			 pagination:true,
163 			 search:true,
164 			 pageSize:5,
165 			 pageList:[5],
166			 onPostBody: function(data) { addAllCircuitsHandlers(); }
167 		 });
168 		 $('#selected_circuits').bootstrapTable({
169 			 pagination:false,
170 			 search:true,
171			 onPostBody: function(data) { addSelectedCircuitsHandlers(); update_hidden_input(); }
172 		 });
173     });
174</script>
175
176
177
178
179
180<!-- header -->
181<div class="pHeader"><?php print ucwords(_("$_POST[action]")); ?> <?php print _('Logical circuit'); ?></div>
182
183
184<!-- content. Override first div to place to the left of the form -->
185<br>
186<!-- Selected circuit table and form -->
187<div class="pContent">
188
189	<form id="circuitManagementEdit">
190	<input id="circuit_list" type="hidden" value="" name="circuit_list">
191	<table class="table table-noborder table-condensed">
192		<!-- name -->
193		<tr>
194			<td><?php print _('Circuit ID'); ?></td>
195			<td>
196				<input type="text" name="logical_cid" style='width:200px;' class="form-control input-sm" placeholder="<?php print _('ID'); ?>" value="<?php if(isset($logical_circuit->logical_cid)) print $Tools->strip_xss($logical_circuit->logical_cid); ?>" <?php print $readonly; ?>>
197				<?php
198				if( ($_POST['action'] == "edit") || ($_POST['action'] == "delete") ) {
199					print '<input type="hidden" name="id" value="'. $_POST['circuitid'] .'">'. "\n";
200				} ?>
201				<input type="hidden" name="action" value="<?php print $_POST['action']; ?>">
202				<input type="hidden" name="csrf_cookie" value="<?php print $csrf; ?>">
203			</td>
204		</tr>
205
206
207		<!-- purpose -->
208		<tr>
209			<td><?php print _('Purpose'); ?></td>
210			<td>
211				<input type="text" name="purpose" style='width:200px;'  class="form-control input-sm" placeholder="<?php print _('Purpose'); ?>" value="<?php if(isset($logical_circuit->purpose)) print $Tools->strip_xss($logical_circuit->purpose); ?>" <?php print $readonly; ?>>
212			</td>
213		</tr>
214
215		<!-- comment -->
216		<tr>
217			<td colspan="2"><hr></td>
218		</tr>
219		<tr>
220			<td><?php print _('Comments'); ?></td>
221			<td>
222				<textarea name="comments" class="form-control input-sm" <?php print $readonly; ?>><?php if(isset($logical_circuit->comments)) print $logical_circuit->comments; ?></textarea>
223			</td>
224		</tr>
225
226
227		<!-- Custom -->
228		<?php
229		if(sizeof($custom) > 0) {
230
231			print '<tr>';
232			print '	<td colspan="2"><hr></td>';
233			print '</tr>';
234
235			# count datepickers
236			$timepicker_index = 0;
237
238			# all my fields
239			foreach($custom as $field) {
240				// readonly
241				$disabled = $readonly == "readonly" ? true : false;
242	    		// create input > result is array (required, input(html), timepicker_index)
243	    		$custom_input = $Tools->create_custom_field_input ($field, $logical_circuit, $_POST['action'], $timepicker_index, $disabled);
244	    		// add datepicker index
245	    		$timepicker_index = $timepicker_index + $custom_input['timepicker_index'];
246	            // print
247				print "<tr>";
248				print "	<td>".ucwords($Tools->print_custom_field_name ($field['name']))." ".$custom_input['required']."</td>";
249				print "	<td>".$custom_input['field']."</td>";
250				print "</tr>";
251			}
252		}
253
254		?>
255
256		<tr>
257			<td colspan="2">
258				<p id="duplicate_error" style="display:none" class='alert alert-danger'>Duplicate circuits have been found. Please remove! Reordering will be affected.</p>
259			</td>
260		</tr>
261
262	</table>
263	</form>
264
265
266	<p style='margin-bottom:0px;margin-top:50px;'><strong><?php print _("Logical circuit physical members"); ?>:</strong></p>
267
268	<table class="table table-striped table-condensed table-top table-no-bordered" id='selected_circuits'>
269		<thead>
270			<th></th>
271			<th>Circuit ID</th>
272			<th>Type</th>
273			<th>Point A</th>
274			<th>Point B</th>
275			<?php if($_POST['action'] != "delete") { ?>
276			<th></th>
277			<?php } ?>
278		</thead>
279
280		<tbody>
281			<?php
282				// print existing logical circuits on edit / delete
283				if(isset($logical_circuit)){
284					$member_circuits = $Tools->fetch_all_logical_circuit_members($logical_circuit->id);
285					// reformat locations
286					if($member_circuits != false){
287						foreach($member_circuits as $circuit){
288							$locationA = $Tools->reformat_circuit_location ($circuit->device1, $circuit->location1);
289							$locationA_html = "<span class='text-muted'>Not set</span>";
290							if($locationA!==false) {
291								$locationA_html = "<a href='".create_link('tools',$locationA['type'],$locationA['id'])."' target='_blank'>$locationA[name]</a>";
292							}
293
294							$locationB = $Tools->reformat_circuit_location ($circuit->device2, $circuit->location2);
295							$locationB_html = "<span class='text-muted'>Not set</span>";
296							if($locationB!==false) {
297								$locationB_html = "<a href='".create_link('tools',$locationB['type'],$locationB['id'])."' target='_blank'>$locationB[name]</a>";
298							}
299
300							print "<tr>";
301							print "	<td><a name='rembtn' class='btn btn-xs btn-default btn-danger' rel='tooltip' title='Remove'><i class='fa fa-times'></i></a></td>";
302							print "	<td><a class='btn btn-xs btn-default' href='".create_link('tools',"circuits",$circuit->id)."' target='_blank'><i class='fa fa-random prefix'></i> $circuit->cid</a></td>";
303							print "	<td>".$type_hash[$circuit->type]."</td>";
304							print "	<td class='hidden-xs hidden-sm'>$locationA_html</td>";
305							print "	<td class='hidden-xs hidden-sm'>$locationB_html</td>";
306							if($_POST['action'] != "delete") {
307								print "	<td class='text-right'>";
308								print "		<input class='id' type='hidden' value='$circuit->id'>";
309								print "		<div class='input-group pull-right'>";
310								print "		<a name='mvdnbtn' class='btn btn-xs btn-default' rel='tooltip' title='Move down'><i class='fa fa-angle-down'></i></a>";
311								print "		<a name='mvupbtn' class='btn btn-xs btn-default' rel='tooltip' title='Move up'><i class='fa fa-angle-up'></i></a>";
312								print "		</div>";
313								print "	</td>";
314						    }
315						    else {
316						    	print "<tr>";
317						    }
318							print "</tr>";
319						}
320					}
321				}
322			?>
323	  </tbody>
324	</table>
325</div>
326
327<!-- All circuit table -->
328<div class="pContent" style="<?php if( $_POST['action'] == "delete") { echo "display:none;"; } ?>; padding-top:50px;" >
329
330	<p style='margin-bottom:0px;'><strong><?php print _("Available physical circuits"); ?>:</strong></p>
331
332	<table id="all_circuits" class="table table-striped table-condensed table-top table-no-bordered">
333		<thead>
334			<th></th>
335			<th>Circuit ID</th>
336			<th>Type</th>
337			<th>Point A</th>
338			<th>Point B</th>
339		</thead>
340		<tbody>
341		<?php
342		//Loop through and create list of circuits to choose from
343		//Also, open up links in a new window to not interrupt creation
344		if($all_circuits!==false) {
345			foreach($all_circuits as $circuit) {
346				// reformat locations
347				$locationA = $Tools->reformat_circuit_location ($circuit->device1, $circuit->location1);
348				$locationA_html = "<span class='text-muted'>Not set</span>";
349				if($locationA!==false) {
350					$locationA_html = "<a href='".create_link('tools',$locationA['type'],$locationA['id'])."' target='_blank'>$locationA[name]</a>";
351				}
352
353				$locationB = $Tools->reformat_circuit_location ($circuit->device2, $circuit->location2);
354				$locationB_html = "<span class='text-muted'>Not set</span>";
355				if($locationB!==false) {
356					$locationB_html = "<a href='".create_link('tools',$locationB['type'],$locationB['id'])."' target='_blank'>$locationB[name]</a>";
357				}
358
359				print '<tr>'. "\n";
360				print " <td><input class='id' type='hidden' value='$circuit->id'><a name='addbtn' class='btn btn-xs btn-success' rel='tooltip' title='Add to logical circuit'><i class='fa fa-plus'></i></a></td>";
361				print "	<td><a class='btn btn-xs btn-default' href='".create_link('tools',"circuits",$circuit->id)."' target='_blank'><i class='fa fa-random prefix'></i> $circuit->cid</a></td>";
362				print "	<td>".$type_hash[$circuit->type]."</td>";
363				print "	<td class='hidden-xs hidden-sm'>$locationA_html</td>";
364				print "	<td class='hidden-xs hidden-sm'>$locationB_html</td>";
365				print '</tr>'. "\n";
366			}
367		}
368		?>
369	</tbody>
370	</table>
371</div>
372
373
374
375
376
377<!-- footer -->
378<div class="pFooter">
379	<div class="btn-group">
380		<button class="btn btn-sm btn-default hidePopups"><?php print _('Cancel'); ?></button>
381		<button class="btn btn-sm btn-default submit_popup <?php if($_POST['action']=="delete") { print "btn-danger"; } else { print "btn-success"; } ?>" data-script="app/admin/circuits/edit-logical-circuit-submit.php" data-result_div="circuitManagementEditResult" data-form='circuitManagementEdit'>
382			<i class="fa <?php if($_POST['action']=="add") { print "fa-plus"; } else if ($_POST['action']=="delete") { print "fa-trash-o"; } else { print "fa-check"; } ?>"></i>
383			<?php print ucwords(_($_POST['action'])); ?>
384		</button>
385	</div>
386
387	<!-- result -->
388	<div class='circuitManagementEditResult' id="circuitManagementEditResult"></div>
389</div>
390