ona  18.1.1
About: OpenNetAdmin provides a database managed inventory of your IP network (with Web and CLI interface).
  Fossies Dox: ona-18.1.1.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

location.inc.php
Go to the documentation of this file.
1 <?php
2 // DON'T put whitespace at the beginning or end of this file!!!
3 
4 
6 // Function: location_add (string $options='')
7 //
8 // $options = key=value pairs of options for this function.
9 // multiple sets of key=value pairs should be separated
10 // by an "&" symbol.
11 //
12 // Input Options:
13 // reference=STRING
14 //
15 // Output:
16 // Adds a location into the database called 'name'
17 // Returns a two part list:
18 // 1. The exit status of the function. 0 on success, non-zero on
19 // error. All errors messages are stored in $self['error'].
20 // 2. A textual message for display on the console or web interface.
21 //
22 // Example: list($status, $result) = location_add('name=blah');
24 function location_add($options="") {
25 
26  // The important globals
27  global $conf, $self, $onadb;
28 
29  // Version - UPDATE on every edit!
30  $version = '1.02';
31 
32  printmsg("DEBUG => location_add({$options}) called", 3);
33 
34  // Parse incoming options string to an array
35  $options = parse_options($options);
36 
37  // Return the usage summary if we need to
38  if ($options['help'] or !($options['reference'] and $options['name']) ) {
39  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
40  $self['error'] = 'ERROR => Insufficient parameters';
41  return(array(1,
42 <<<EOM
43 
45 Adds a location into the database
46 
47  Synopsis: location_add [KEY=VALUE] ...
48 
49  Required:
50  reference=STRING reference for identifying and searching for location
51  name=STRING location descriptive name
52 
53  Optional:
54  address=STRING
55  city=STRING
56  state=STRING
57  zip_code=NUMBER
58  latitude=STRING
59  longitude=STRING
60  misc=STRING
61 
62 \n
63 EOM
64 
65  ));
66  }
67 
68 
69  // The formatting rule on location reference is all upper and trim it
70  $options['reference'] = strtoupper(trim($options['reference']));
71 
72  if (!$options['zip_code']) { $options['zip_code'] = 0; }
73 
74  // check to see if the campus already exists
75  list($status, $rows, $loc) = ona_get_location_record(array('reference' => $options['reference']));
76 
77  if ($status or $rows) {
78  printmsg("DEBUG => The location {$options['reference']} already exists!",3);
79  $self['error'] = "ERROR => The location {$options['reference']} already exists!";
80  return(array(3, $self['error'] . "\n"));
81  }
82 
83 
84 
85  // Check permissions
86  if (!auth('location_add')) {
87  $self['error'] = "Permission denied!";
88  printmsg($self['error'], 0);
89  return(array(10, $self['error'] . "\n"));
90  }
91 
92  // Get the next ID for the new location
93  $id = ona_get_next_id('locations');
94  if (!$id) {
95  $self['error'] = "ERROR => The ona_get_next_id() call failed!";
96  printmsg($self['error'], 0);
97  return(array(5, $self['error'] . "\n"));
98  }
99  printmsg("DEBUG => ID for new location: $id", 3);
100 
101  // Add the record
102  list($status, $rows) =
104  $onadb,
105  'locations',
106  array(
107  'id' => $id,
108  'reference' => $options['reference'],
109  'name' => $options['name'],
110  'address' => $options['address'],
111  'city' => $options['city'],
112  'state' => $options['state'],
113  'zip_code' => $options['zip_code'],
114  'latitude' => $options['latitude'],
115  'longitude' => $options['longitude'],
116  'misc' => $options['misc']
117  )
118  );
119  if ($status or !$rows) {
120  $self['error'] = "ERROR => location_add() SQL Query failed: " . $self['error'];
121  printmsg($self['error'],0);
122  return(array(6, $self['error'] . "\n"));
123  }
124 
125  // Return the success notice
126  $self['error'] = "INFO => Location ADDED: {$options['reference']}: {$options['name']}";
127  printmsg($self['error'],0);
128  return(array(0, $self['error'] . "\n"));
129 }
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
143 // Function: location_del (string $options='')
144 //
145 // $options = key=value pairs of options for this function.
146 // multiple sets of key=value pairs should be separated
147 // by an "&" symbol.
148 //
149 // Input Options:
150 // name=ID
151 //
152 // Output:
153 // Deletes a location from the database.
154 // Returns a two part list:
155 // 1. The exit status of the function. 0 on success, non-zero on
156 // error. All errors messages are stored in $self['error'].
157 // 2. A textual message for display on the console or web interface.
158 //
159 // Example: list($status, $result) = location_del('ref=1223543');
161 function location_del($options="") {
162 
163  // The important globals
164  global $conf, $self, $onadb;
165 
166  // Version - UPDATE on every edit!
167  $version = '1.01';
168 
169  printmsg("DEBUG => location_del({$options}) called", 3);
170 
171  // Parse incoming options string to an array
172  $options = parse_options($options);
173 
174  // Return the usage summary if we need to
175  if ($options['help'] or !$options['reference'] ) {
176  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
177  $self['error'] = 'ERROR => Insufficient parameters';
178  return(array(1,
179 <<<EOM
180 
181 locaiton_del-v{$version}
182 Deletes a location from the database
183 
184  Synopsis: location_del [KEY=VALUE] ...
185 
186  Required:
187  reference=NAME or ID Reference or ID of the location to delete
188 
189  Optional:
190  commit=[yes|no] commit db transaction (no)
191 
192 \n
193 EOM
194 
195  ));
196  }
197 
198 
199  // Sanitize options[commit] (default is no)
200  $options['commit'] = sanitize_YN($options['commit'], 'N');
201 
202 
203  // Find the Location to use
204  list($status, $rows, $loc) = ona_find_location($options['reference']);
205  if ($status or !$rows) {
206  printmsg("DEBUG => The location specified, {$options['reference']}, does not exist!", 3);
207  return(array(2, "ERROR => The location specified, {$options['reference']}, does not exist!\n"));
208  }
209  printmsg("DEBUG => Location selected: {$loc['reference']}, location name: {$loc['name']}", 3);
210 
211 
212  list($status, $rows, $usage) = db_get_records($onadb, 'devices', array('location_id' => $loc['id']), '' ,0);
213  if ($rows != 0) {
214  printmsg("DEBUG => The location ({$loc['reference']}) is in use by {$rows} devices(s)!",3);
215  $self['error'] = "ERROR => The location ({$loc['reference']}) is in use by {$rows} devices(s)!";
216  return(array(6, $self['error']."\n"));
217  }
218 
219  // If "commit" is yes, delete the record
220  if ($options['commit'] == 'Y') {
221 
222  // Check permissions
223  if (!auth('location_del')) {
224  $self['error'] = "Permission denied!";
225  printmsg($self['error'], 0);
226  return(array(10, $self['error'] . "\n"));
227  }
228 
229  list($status, $rows) = db_delete_records($onadb, 'locations', array('id' => $loc['id']));
230  if ($status or !$rows) {
231  $self['error'] = "ERROR => location_del() SQL Query failed: " . $self['error'];
232  printmsg($self['error'], 0);
233  return(array(4, $self['error'] . "\n"));
234  }
235 
236  // Return the success notice
237  $self['error'] = "INFO => Location DELETED: {$loc['reference']} ({$loc['name']})";
238  printmsg($self['error'],0);
239  return(array(0, $self['error'] . "\n"));
240  }
241 
242 
243 
244  // Otherwise display the record that would have been deleted
245  $text = <<<EOL
246 Record(s) NOT DELETED (see "commit" option)
247 Displaying record(s) that would have been deleted:
248 
249 
250 EOL;
251 
252  $text .= format_array($loc);
253  $text .= "\n";
254 
255  return(array(6, $text));
256 
257 }
258 
259 
260 
261 
262 
263 
264 
265 
266 
268 // Function: location_modify (string $options='')
269 //
270 // $options = key=value pairs of options for this function.
271 // multiple sets of key=value pairs should be separated
272 // by an "&" symbol.
273 //
274 // Input Options:
275 // reference=STRING or ID location reference or ID
276 // set_name=STRING change location name
277 //
278 // Output:
279 // Updates a location record in the IP database.
280 // Returns a two part list:
281 // 1. The exit status of the function. 0 on success, non-zero on
282 // error. All errors messages are stored in $self['error'].
283 // 2. A textual message for display on the console or web interface.
284 //
285 // Example: list($status, $result) = location_modify('reference=23452&name=blah');
287 function location_modify($options="") {
288 
289  // The important globals
290  global $conf, $self, $onadb;
291 
292  // Version - UPDATE on every edit!
293  $version = '1.01';
294 
295  printmsg("DEBUG => location_modify({$options}) called", 3);
296 
297  // Parse incoming options string to an array
298  $options = parse_options($options);
299 
300  // Return the usage summary if we need to
301  if ($options['help'] or (!$options['reference']) ) {
302  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
303  $self['error'] = 'ERROR => Insufficient parameters';
304  return(array(1,
305 <<<EOM
306 
308 Modifies an existing location entry in the database
309 
310  Synopsis: location_modify [KEY=VALUE] ...
311 
312  Where:
313  reference=STRING or ID location reference or ID
314 
315  Update:
316  set_reference=NAME change location reference
317  set_name=NAME change location name
318  set_address=STRING
319  set_city=STRING
320  set_state=STRING
321  set_zip_code=NUMBER
322  set_latitude=STRING
323  set_longitude=STRING
324  set_misc=STRING
325 
326 \n
327 EOM
328  ));
329  }
330 
331 
332  // See if it's an vlan_campus_id
333  list($status, $rows, $loc) = ona_find_location($options['reference']);
334 
335  if (!$loc['id']) {
336  printmsg("DEBUG => Unable to find location using: {$options['reference']}!",3);
337  $self['error'] = "ERROR => Unable to find location using: {$options['reference']}!";
338  return(array(1, $self['error'] . "\n"));
339  }
340 
341 
342  printmsg("DEBUG => Found location: {$loc['reference']}", 3);
343 
344 
345  // This variable will contain the updated info we'll insert into the DB
346  $SET = array();
347 
348  if ($loc['reference'] != $options['set_reference']) {
349  $SET['reference'] = $options['set_reference'];
350  $msg .= "INFO => Location UPDATED reference: {$loc['reference']} => {$options['set_reference']}\n";
351  }
352 
353  // If they are specifying a new name, process it.
354  if ($loc['name'] != $options['set_name']) {
355  $SET['name'] = $options['set_name'];
356  $msg .= "INFO => Location UPDATED name: {$loc['name']} => {$options['set_name']}\n";
357  }
358 
359  if ($loc['address'] != $options['set_address']) {
360  $SET['address'] = $options['set_address'];
361  $msg .= "INFO => Location UPDATED address: {$loc['address']} => {$options['set_address']}\n";
362  }
363 
364  if ($loc['city'] != $options['set_city']) {
365  $SET['city'] = $options['set_city'];
366  $msg .= "INFO => Location UPDATED city: {$loc['city']} => {$options['set_city']}\n";
367  }
368 
369  if ($loc['state'] != $options['set_state']) {
370  $SET['state'] = $options['set_state'];
371  $msg .= "INFO => Location UPDATED state: {$loc['state']} => {$options['set_state']}\n";
372  }
373 
374  if ($loc['zip_code'] != $options['set_zip_code']) {
375  $SET['zip_code'] = $options['set_zip_code'];
376  $msg .= "INFO => Location UPDATED zip_code: {$loc['zip_code']} => {$options['set_zip_code']}\n";
377  }
378 
379  if ($loc['latitude'] != $options['set_latitude']) {
380  $SET['latitude'] = $options['set_latitude'];
381  $msg .= "INFO => Location UPDATED latitude: {$loc['latitude']} => {$options['set_latitude']}\n";
382  }
383 
384  if ($loc['longitude'] != $options['set_longitude']) {
385  $SET['longitude'] = $options['set_longitude'];
386  $msg .= "INFO => Location UPDATED longitude: {$loc['longitude']} => {$options['set_longitude']}\n";
387  }
388 
389  if ($loc['misc'] != $options['set_misc']) {
390  $SET['misc'] = $options['set_misc'];
391  $msg .= "INFO => Location UPDATED misc: {$loc['misc']} => {$options['set_misc']}\n";
392  }
393 
394  if (!$SET) {
395  $self['error'] = "ERROR => You did not update anything.";
396  printmsg($self['error'], 1);
397  return(array(2, $self['error'] . "\n"));
398  }
399 
400  // Check permissions
401  if (!auth('location_add')) {
402  $self['error'] = "Permission denied!";
403  printmsg($self['error'], 0);
404  return(array(2, $self['error'] . "\n"));
405  }
406 
407 
408  // Update the record
409  list($status, $rows) = db_update_record($onadb, 'locations', array('id' => $loc['id']), $SET);
410  if ($status or !$rows) {
411  $self['error'] = "ERROR => location_modify() SQL Query failed: " . $self['error'];
412  printmsg($self['error'], 0);
413  return(array(3, $self['error'] . "\n"));
414  }
415 
416  // Return the success notice
417  $self['error'] = $msg;
418  printmsg($self['error'], 0);
419  return(array(0, $self['error'] . "\n"));
420 }
421 
422 
423 
424 
425 
426 
427 
428 
429 
430 
431 
432 
433 
434 
435 // DON'T put whitespace at the beginning or end of this file!!!
436 ?>
db_insert_record
db_insert_record($dbh=0, $table="", $insert="")
Definition: functions_db.inc.php:375
sanitize_YN
sanitize_YN($string="", $default="Y")
Definition: functions_general.inc.php:1637
ona_get_location_record
ona_get_location_record($array='', $order='')
Definition: functions_db.inc.php:1122
ona_get_next_id
ona_get_next_id($tablename)
Definition: functions_db.inc.php:1369
db_get_records
db_get_records($dbh=0, $table="", $where="", $order="", $rows=-1, $offset=-1)
Definition: functions_db.inc.php:891
$status
$status
Definition: install.php:12
$onadb
global $onadb
Definition: 2-to-3.php:15
db_delete_records
db_delete_records($dbh=0, $table="", $where="")
Definition: functions_db.inc.php:582
printmsg
if(6<=$conf['debug']) printmsg($msg="", $debugLevel=0)
Definition: functions_general.inc.php:48
format_array
format_array($array=array())
Definition: functions_general.inc.php:1745
location_modify
location_modify($options="")
Definition: location.inc.php:287
ona_find_location
ona_find_location($search="")
Definition: functions_db.inc.php:1810
db_update_record
db_update_record($dbh=0, $table="", $where="", $insert="")
Definition: functions_db.inc.php:474
EOL
< a style="text-decoration: none;" href="/">< input class='edit' type="button" value="I don't like free stuff?" onclick=""/></a ></center ></div > EOL
Definition: install.php:40
location_del
location_del($options="")
Definition: location.inc.php:161
$conf
global $conf
Definition: 2-to-3.php:15
$text
$text
Definition: install.php:11
$self
global $self
Definition: 2-to-3.php:15
auth
auth($resource, $msg_level=1)
Definition: functions_general.inc.php:1330
location_add
location_add($options="")
Definition: location.inc.php:24
parse_options
parse_options($options="")
Definition: functions_general.inc.php:1579
$version
$version
Definition: main.inc.php:22