1<?php
2
3/**
4 *	Print all available locations
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# create csrf token
21$csrf = $_POST['action']=="add" ? $User->Crypto->csrf_cookie ("create", "pstn_add") : $User->Crypto->csrf_cookie ("create", "pstn_".$_POST['id']);
22
23# perm check popup
24if($_POST['action']=="edit") {
25    $User->check_module_permissions ("pstn", 2, true, true);
26}
27else {
28    $User->check_module_permissions ("pstn", 3, true, true);
29}
30
31
32# get Location object
33if($_POST['action']!="add") {
34	$prefix = $Admin->fetch_object ("pstnPrefixes", "id", $_POST['id']);
35	$prefix!==false ? : $Result->show("danger", _("Invalid ID"), true, true);
36
37	$master_prefix = $Admin->fetch_object ("pstnPrefixes", "id", $prefix->master);
38}
39else {
40    # init object
41    $prefix = new StdClass ();
42    $prefix->master = 0;
43
44    $master_prefix = new StdClass ();
45    $master_prefix->name = 'root';
46    $master_prefix->prefix = "/";
47
48    # if id is set we are adding slave prefix
49    if (isset($_POST['id'])) {
50        if($_POST['id']!=0) {
51        	$master_prefix = $Admin->fetch_object ("pstnPrefixes", "id", $_POST['id']);
52        	$master_prefix!==false ? : $Result->show("danger", _("Invalid master ID"), true, true);
53
54            $prefix->master = $master_prefix->id;
55            $prefix->prefix = $master_prefix->prefix;
56            $prefix->start  = $master_prefix->start;
57            $prefix->deviceId = $master_prefix->deviceId;
58        }
59    }
60}
61
62# disable edit on delete
63$readonly = $_POST['action']=="delete" ? "readonly" : "";
64$link = $readonly ? false : true;
65
66# fetch custom fields
67$custom = $Tools->fetch_custom_fields('pstnPrefixes');
68
69?>
70
71
72<!-- header -->
73<div class="pHeader"><?php print ucwords(_("$_POST[action]")); ?> <?php print _('PSTN prefix'); ?></div>
74
75<!-- content -->
76<div class="pContent">
77
78	<form id="editPSTN">
79	<table id="editPSTN" class="table table-noborder table-condensed">
80
81	<tbody>
82
83        <!-- Master prefix -->
84        <?php if($prefix->master!=0) { ?>
85    	<tr>
86        	<th><?php print _('Master prefix'); ?></th>
87        	<th colspan="2">
88            	<?php print $master_prefix->name. " (".$master_prefix->prefix.")"; ?>
89        	</th>
90        </tr>
91    	<tr>
92        	<th></th>
93        	<th colspan="2">
94            	<?php print _("Range").": ".$master_prefix->start. " - ".$master_prefix->stop; ?>
95        	</th>
96        </tr>
97        <tr>
98            <td colspan="3"><hr></td>
99        </tr>
100        <?php } ?>
101
102    	<!-- name -->
103    	<tr>
104        	<th><?php print _('Name'); ?></th>
105        	<td>
106            	<input type="text" class="form-control input-sm" name="name" value="<?php print $Tools->strip_xss($prefix->name); ?>" placeholder='<?php print _('Name'); ?>' <?php print $readonly; ?>>
107            	<input type="hidden" name="csrf_cookie" value="<?php print $csrf; ?>">
108            	<input type="hidden" name="id" value="<?php print $prefix->id; ?>">
109            	<input type="hidden" name="master" value="<?php print $prefix->master; ?>">
110            	<input type="hidden" name="action" value="<?php print $_POST['action']; ?>">
111        	</td>
112        	<td>
113            	<span class="text-muted"><?php print _("Set Prefix name"); ?></span>
114        	</td>
115        </tr>
116
117        <!-- Prefix -->
118    	<tr>
119        	<th><?php print _('Prefix'); ?></th>
120        	<td>
121            	<input type="text" class="form-control input-sm" name="prefix" value="<?php print $Tools->strip_xss($prefix->prefix); ?>" placeholder='<?php print _('Prefix'); ?>' <?php print $readonly; ?>>
122        	</td>
123        	<td>
124            	<span class="text-muted"><?php print _("Prefix"); ?></span>
125        	</td>
126        </tr>
127
128        <!-- Start -->
129    	<tr>
130        	<th><?php print _('Start'); ?></th>
131        	<td>
132            	<input type="text" class="form-control input-sm" name="start" style="width:70px;" value="<?php print $prefix->start; ?>" placeholder='<?php print _('Start'); ?>' <?php print $readonly; ?>>
133        	</td>
134        	<td>
135            	<span class="text-muted"><?php print _("Set start number"); ?></span>
136        	</td>
137        </tr>
138
139        <!-- Stop -->
140    	<tr>
141        	<th><?php print _('Stop'); ?></th>
142        	<td>
143            	<input type="text" class="form-control input-sm" name="stop" style="width:70px;"  value="<?php print $prefix->stop; ?>" placeholder='<?php print _('Stop'); ?>' <?php print $readonly; ?>>
144        	</td>
145        	<td>
146            	<span class="text-muted"><?php print _("Set stop number"); ?></span>
147        	</td>
148        </tr>
149
150
151    	<tr>
152        	<td colspan="3"><hr></td>
153        </tr>
154
155        <!-- Master prefix -->
156<!--
157        <tr>
158            <th><?php print _('Master prefix'); ?></th>
159            <td>
160    			<?php $Tools->print_masterprefix_dropdown_menu ($prefix->master); ?>
161            </td>
162            <td>
163                <span class='text-muted'><?php print _('Enter master prefix if you want to nest it under existing subnet, or select root to create root prefix'); ?></span>
164            </td>
165        </tr>
166-->
167
168
169    	<!-- Device -->
170        <?php if($User->get_module_permissions ("devices")>0) { ?>
171    	<tr>
172    		<th><?php print _('Device'); ?></th>
173    		<td id="deviceDropdown">
174    			<select name="deviceId" class="form-control input-sm input-w-auto">
175    				<option value="0"><?php print _('None'); ?></option>
176    				<?php
177    				// fetch all devices
178    				$devices = $Admin->fetch_all_objects("devices", "hostname");
179    				// loop
180    				if ($devices!==false) {
181    					foreach($devices as $device) {
182							//if same
183							if($device->id == $prefix->deviceId) 	{ print '<option value="'. $device->id .'" selected>'. $device->hostname .'</option>'. "\n"; }
184							else 									{ print '<option value="'. $device->id .'">'. $device->hostname .'</option>'. "\n";			 }
185    					}
186    				}
187    				?>
188    			</select>
189    		</td>
190    		<td class="info2"><?php print _('Select device where prefix is located'); ?></td>
191        </tr>
192        <?php } ?>
193
194        <!-- description -->
195    	<tr>
196        	<td colspan="3"><hr></td>
197        </tr>
198    	<tr>
199        	<th><?php print _('Description'); ?></th>
200        	<td colspan="2">
201            	<textarea class="form-control input-sm" name="description" placeholder='<?php print $prefix->description; ?>' <?php print $readonly; ?>><?php print $prefix->description; ?></textarea>
202        	</td>
203        </tr>
204
205
206    	<!-- Custom -->
207    	<?php
208    	if(sizeof($custom) > 0) {
209    		print '<tr>';
210    		print '	<td colspan="2"><hr></td>';
211    		print '</tr>';
212    		# count datepickers
213    		$timepicker_index = 0;
214    		# all my fields
215    		foreach($custom as $field) {
216        		// create input > result is array (required, input(html), timepicker_index)
217        		$custom_input = $Tools->create_custom_field_input ($field, $prefix, $_POST['action'], $timepicker_index);
218        		// add datepicker index
219        		$timepicker_index = $timepicker_index + $custom_input['timepicker_index'];
220                // print
221    			print "<tr>";
222    			print "	<td>".ucwords($Tools->print_custom_field_name ($field['name']))." ".$custom_input['required']."</td>";
223    			print "	<td>".$custom_input['field']."</td>";
224    			print "</tr>";
225    		}
226    	}
227    	?>
228	</tbody>
229
230	</table>
231	</form>
232</div>
233
234
235<!-- footer -->
236<div class="pFooter">
237	<div class="btn-group">
238		<button class="btn btn-sm btn-default hidePopups"><?php print _('Cancel'); ?></button>
239		<button class="btn btn-sm btn-default <?php if($_POST['action']=="delete") { print "btn-danger"; } else { print "btn-success"; } ?>" id="editPSTNSubmit"><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> <?php print ucwords(_($_POST['action'])); ?></button>
240	</div>
241	<!-- result -->
242	<div class="editPSTNResult"></div>
243</div>