<?php
    /*****************************************************************************
    IP Reg, a PHP/MySQL IPAM tool
    Copyright (C) 2007-2009 Wietse Warendorff

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    For more information, visit http://sourceforge.net/projects/ipreg,
    or contact me at wietsew@users.sourceforge.net
    *****************************************************************************/

    class Db {

        protected $dblink;

        public function __construct ($dblink) {
            $this->dblink = $dblink;
        }

        function db_delete($query) {
            // run query
            $sql = mysqli_query($this->dblink, $query) or die(mysqli_error($this->dblink));
        }

        function db_insert($query) {
            // run query
            echo "<pre>$query</pre>";
            $sql = mysqli_query($this->dblink, $query) or die(mysqli_error($this->dblink));

            // return result
            return mysqli_insert_id($this->dblink);
        }

        function db_select($query) {
            // run query
            $sql = mysqli_query($this->dblink, $query) or die(mysqli_error($this->dblink));

            // loop results
            $result = array();
            while($record = mysqli_fetch_assoc($sql)) {
                $result[] = $record;
            }

            // return array
            return $result;
        }

        function db_update($query) {
            // run query
            $sql = mysqli_query($this->dblink, $query) or die(mysqli_error($this->dblink));
        }

        function options_asset($null_value=NULL) {
            $options = array();
            if (isset($null_value)) {
                $options[0] = $null_value;
            }
            $sql = "SELECT asset_id, asset_name
                    FROM asset
                    ORDER BY asset_name";
            $records = $this->db_select($sql);
            foreach ($records as $rec) {
                $options[$rec['asset_id']] = $rec['asset_name'];
            }
            return $options;
        }

        function options_assetclass($null_value=NULL) {
            $options = array();
            if (isset($null_value)) {
                $options[0] = $null_value;
            }
            $sql = "SELECT assetclass_id, assetclass_name
                    FROM assetclass
                    ORDER BY assetclass_name";
            $records = $this->db_select($sql);
            foreach ($records as $rec) {
                $options[$rec['assetclass_id']] = $rec['assetclass_name'];
            }
            return $options;
        }

        function options_assetclassgroup($null_value=NULL) {
            $options = array();
            if (isset($null_value)) {
                $options[0] = $null_value;
            }
            $sql = "SELECT assetclassgroup_id, assetclassgroup_name
                    FROM assetclassgroup
                    ORDER BY assetclassgroup_name";
            $records = $this->db_select($sql);
            foreach ($records as $rec) {
                $options[$rec['assetclassgroup_id']] = $rec['assetclassgroup_name'];
            }
            return $options;
        }

        function options_location($null_value=NULL) {
            $options = array();
            if (isset($null_value)) {
                $options[0] = $null_value;
            }
            $sql = "SELECT location_id,
                        location_name
                    FROM location
                    ORDER BY location_name";
            $records = $this->db_select($sql);
            foreach ($records as $rec) {
                $options[$rec['location_id']] = $rec['location_name'];
            }
            return $options;
        }

        function options_subnet($null_value=NULL) {
            $options = array();
            if (isset($null_value)) {
                $options[0] = $null_value;
            }
            $sql = "SELECT subnet_id,
                    CONCAT_WS('/', subnet_address, subnet_mask) AS subnet_name
                    FROM subnet
                    ORDER BY INET_ATON(subnet_address)";
            $records = $this->db_select($sql);
            foreach ($records as $rec) {
                $options[$rec['subnet_id']] = $rec['subnet_name'];
            }
            return $options;
        }

        function options_vlan($null_value=NULL) {
            $options = array();
            if (isset($null_value)) {
                $options[0] = $null_value;
            }
            $sql = "SELECT vlan_id,
                        CONCAT_WS(' - ', vlan_number, vlan_name) AS vlan_option
                    FROM vlan
                    ORDER BY vlan_number";
            $records = $this->db_select($sql);
            foreach ($records as $rec) {
                $options[$rec['vlan_id']] = $rec['vlan_option'];
            }
            return $options;
        }

        function options_zone($null_value=NULL) {
            $options = array();
            if (isset($null_value)) {
                $options[0] = $null_value;
            }
            $sql = "SELECT zone_id, zone_origin
                    FROM zone
                    ORDER BY zone_origin";
            $records = $this->db_select($sql);
            foreach ($records as $rec) {
                $options[$rec['zone_id']] = $rec['zone_origin'];
            }
            return $options;
        }

    }
?>