<?php

class navigationUtility extends MasterClass
{

    /*
    Option Categories
     */

    ####################################################################################################################################################
    ####################################################################################################################################################

    //function for adding a new admin option category
    public function insertOptionCategory()
    {

        $data = $this->cleanData();

        if ($data['Name'] != '' && $data['Desc'] != '') {

            $this->dbConn();

            $this->query = "INSERT INTO Admin_Categories VALUES ('','";
            $this->query .= $data['Name'];
            $this->query .= "','";
            $this->query .= $data['Desc'];
            $this->query .= "','";
            $this->query .= $data['Directory'];
            $this->query .= "','";
            $this->query .= $data['Page'];
            $this->query .= "','";
            $this->query .= $data['Status'];
            $this->query .= "')";

            $insert = $this->db->query($this->query);

            $this->dbClose();

            $result['Status'] = 1;
            $result['Msg'] = 'The new administrative category has been successfully added. <a href="' . WEBSITE_ADMIN_PANEL . '?PageId=1" title="Refresh Navigation">CLICK HERE</a> to refresh the navigation bar.';
            $result['Class'] = 'success';

        } else {

            $result['Status'] = 0;
            $result['Msg'] = 'The administrative category was not added. Please make sure you fill out all the fields before submitting.';
            $result['Class'] = 'error';

        }

        return $result;

    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //function for getting list of option categories
    public function getOptionCategories($all=false)
    {

        $list = array();

        $this->dbConn();

        $this->query = "SELECT * FROM Admin_Categories ";
        if($all === false){

            $this->query .= "WHERE admin_category_status = '1' ";
        
        }
        $this->query .= "ORDER BY admin_category_nbr ASC";

        $getInfo = $this->db->query($this->query);

        if ($getInfo->num_rows > 0) {

            $i = 0;
            while ($row = $getInfo->fetch_assoc()) {

                $list[$i]['Nbr'] = stripslashes($row['admin_category_nbr']);
                $list[$i]['Name'] = stripslashes($row['admin_category_name']);
                $list[$i]['Desc'] = stripslashes($row['admin_category_description']);
                $list[$i]['Directory'] = stripslashes($row['admin_category_directory']);
                $list[$i]['Page'] = stripslashes($row['admin_category_page']);
                $list[$i]['Status'] = stripslashes($row['admin_category_status']);
                $i++;

            }

        }

        $this->dbClose();

        return $list;

    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //function for deleting an admin option category
    public function deleteAdminCategory()
    {

        $result = array();

        $data = $this->cleanData();

        if ($data['Id'] != '') {

            $this->dbConn();

            $this->query = "SELECT * FROM Admin_Options WHERE admin_option_category = '";
            $this->query .= $this->db->real_escape_string($data['Id']);
            $this->query .= "'";

            $check = $this->db->query($this->query);

            if ($check->num_rows > 0) {

                $result['Status'] = 0;
                $result['Msg'] = 'You can not delete this option category because the following options are still associated with it:<br/>';
                while ($row = $check->fetch_assoc()) {

                    $result['Msg'] .= stripslashes($row['admin_option_name']) . '<br/>';

                }
                $result['Class'] = 'error';

            } else {

                $this->query = "DELETE FROM Admin_Categories WHERE admin_category_nbr = '";
                $this->query .= $this->db->real_escape_string($data['Id']);
                $this->query .= "'";

                $this->db->query($this->query);

                $result['Status'] = 1;
                $result['Msg'] = 'The administrative category has been successfully deleted. <a href="' . WEBSITE_ADMIN_PANEL . '?PageId=2" title="Refresh Navigation">CLICK HERE</a> to refresh the navigation bar.';
                $result['Class'] = 'success';

            }

            $this->dbClose();

        } else {

            $result['Status'] = 0;
            $result['Msg'] = 'No option category ID was present.';
            $result['Class'] = 'error';

        }

        return $result;

    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //function for modifying an admin option category
    public function modifyAdminCategory()
    {

        $data = $this->cleanData();

        if ($data['Name'] != '' && $data['Desc'] != '') {

            $this->dbConn();

            $this->query = "UPDATE Admin_Categories SET ";
            $this->query .= "admin_category_name = '";
            $this->query .= $data['Name'];
            $this->query .= "', admin_category_description = '";
            $this->query .= $data['Desc'];
            $this->query .= "', admin_category_directory = '";
            $this->query .= $data['Directory'];
            $this->query .= "', admin_category_page = '";
            $this->query .= $data['Page'];
            $this->query .= "', admin_category_status = '";
            $this->query .= $data['Status'];
            $this->query .= "' WHERE admin_category_nbr = '";
            $this->query .= $data['Id'];
            $this->query .= "'";

            $this->db->query($this->query);

            $this->dbClose();

            $result['Status'] = 1;
            $result['Msg'] = 'The administrative category has been successfully modified.  <a href="' . WEBSITE_ADMIN_PANEL . '?PageId=2" title="Refresh Navigation">CLICK HERE</a> to refresh the navigation bar.';
            $result['Class'] = 'success';

        } else {

            $result['Status'] = 0;
            $result['Msg'] = 'The administrative category was not modified. Please make sure you fill out all the fields before submitting.';
            $result['Class'] = 'error';

        }

        return $result;

    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //function for modifying an admin option category status
    public function updateNavStatus()
    {

        $result = '';

        $data = $this->cleanData();

        if ($data['Status'] != '' && $data['NavCat'] != '') {

            $this->dbConn();

            $this->query = "UPDATE Admin_Categories SET ";
            $this->query .= "admin_category_status = '";
            $this->query .= $data['Status'];
            $this->query .= "' WHERE admin_category_nbr = '";
            $this->query .= $data['NavCat'];
            $this->query .= "'";

            $this->db->query($this->query);

            $this->dbClose();

        } 
        
    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //function for adding a new admin category option
    public function insertCategoryOption()
    {

        $data = $this->cleanData();

        if ($data['Name'] != '' && $data['Desc'] != '' && $data['Category'] != '') {

            $this->dbConn();

            $this->query = "INSERT INTO Admin_Options VALUES ('','";
            $this->query .= $data['Category'];
            $this->query .= "','";
            $this->query .= $data['Name'];
            $this->query .= "','";
            $this->query .= $data['Desc'];
            $this->query .= "','";
            $this->query .= $data['Page'];
            $this->query .= "','";
            $this->query .= $data['Visible'];
            $this->query .= "')";

            $this->db->query($this->query);

            $this->dbClose();

            $result['Status'] = 1;
            $result['Msg'] = 'The new administrative category option has been successfully added. <a href="' . WEBSITE_ADMIN_PANEL . '?PageId=3" title="Refresh Navigation">CLICK HERE</a> to refresh the navigation bar.';
            $result['Class'] = 'success';

        } else {

            $result['Status'] = 0;
            $result['Msg'] = 'The administrative category option was not added. Please make sure you fill out all the fields before submitting.';
            $result['Class'] = 'error';

        }

        return $result;

    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //function for getting list of category options
    public function getCategoryOptions($category = '',$all=false)
    {

        $list = array();

        $this->dbConn();

        if ($category == '') {

            $this->query = "SELECT * FROM Admin_Options ";
            $this->query .= "LEFT JOIN Admin_Categories ON admin_option_category = admin_category_nbr ";
            if($all === false){

                $this->query .= "WHERE admin_option_visible = '1' ";

            }
            $this->query .= "ORDER BY admin_option_nbr ASC";

        } else {

            $this->query = "SELECT * FROM Admin_Options ";
            $this->query .= "LEFT JOIN Admin_Categories ON admin_option_category = admin_category_nbr ";
            $this->query .= "WHERE admin_option_category = '";
            $this->query .= $this->db->real_escape_string($category);
            $this->query .= "' ";
            if($all === false){
            
               $this->query .= "AND admin_option_visible = '1' ";
               
            }
            $this->query .= "ORDER BY admin_option_nbr ASC";

        }

        $getInfo = $this->db->query($this->query);

        if ($getInfo->num_rows > 0) {

            $i = 0;
            while ($row = $getInfo->fetch_assoc()) {

                $list[$i]['Nbr'] = stripslashes($row['admin_option_nbr']);
                $list[$i]['Category'] = stripslashes($row['admin_option_category']);
                $list[$i]['Category_Name'] = stripslashes($row['admin_category_name']);
                $list[$i]['Directory'] = stripslashes($row['admin_category_directory']);
                $list[$i]['Name'] = stripslashes($row['admin_option_name']);
                $list[$i]['Desc'] = stripslashes($row['admin_option_description']);
                $list[$i]['Page'] = stripslashes($row['admin_option_page']);
                $list[$i]['Visible'] = stripslashes($row['admin_option_visible']);
                $i++;

            }

        }

        $this->dbClose();

        return $list;

    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //function for deleting an admin category option
    public function deleteAdminCategoryOption()
    {

        $result = array();

        $data = $this->cleanData();

        if ($data['Id'] != '') {

            $this->dbConn();

            $this->query = "DELETE FROM Admin_Options WHERE admin_option_nbr = '";
            $this->query .= $this->db->real_escape_string($data['Id']);
            $this->query .= "'";

            $this->db->query($this->query);

            $result['Status'] = 1;
            $result['Msg'] = 'The administrative category options has been successfully deleted. <a href="' . WEBSITE_ADMIN_PANEL . '?PageId=4" title="Refresh Navigation">CLICK HERE</a> to refresh the navigation bar.';
            $result['Class'] = 'success';

            $this->dbClose();

        } else {

            $result['Status'] = 0;
            $result['Msg'] = 'No category option ID was present.';
            $result['Class'] = 'error';

        }

        return $result;

    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //function for modifying an admin category option
    public function modifyAdminCategoryOption()
    {

        $data = $this->cleanData();

        if ($data['Name'] != '' && $data['Category'] != '') {

            $this->dbConn();

            $this->query = "UPDATE Admin_Options SET ";
            $this->query .= "admin_option_category = '";
            $this->query .= $data['Category'];
            $this->query .= "', admin_option_name = '";
            $this->query .= $data['Name'];
            $this->query .= "', admin_option_description = '";
            $this->query .= $data['Desc'];
            $this->query .= "', admin_option_page = '";
            $this->query .= $data['Page'];
            $this->query .= "', admin_option_visible = '";
            $this->query .= $data['Visible'];
            $this->query .= "' WHERE admin_option_nbr = '";
            $this->query .= $data['Id'];
            $this->query .= "'";

            $this->db->query($this->query);

            $this->dbClose();

            $result['Status'] = 1;
            $result['Msg'] = 'The administrative category option has been successfully modified. <a href="' . WEBSITE_ADMIN_PANEL . '?PageId=4" title="Refresh Navigation">CLICK HERE</a> to refresh the navigation bar.';
            $result['Class'] = 'success';

        } else {

            $result['Status'] = 0;
            $result['Msg'] = 'The administrative category option was not modified. Please make sure you fill out all the fields before submitting.';
            $result['Class'] = 'error';

        }

        return $result;

    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //function for getting list of category options
    public function getOptionInfo($id)
    {

        $info = array();

        $this->dbConn();

        $this->query = "SELECT * FROM Admin_Options WHERE admin_option_nbr = '";
        $this->query .= $this->db->real_escape_string($id);
        $this->query .= "'";

        $getInfo = $this->db->query($this->query);

        if ($getInfo->num_rows > 0) {

            $row = $getInfo->fetch_assoc();

            $info['Nbr'] = stripslashes($row['admin_option_nbr']);
            $info['Category'] = stripslashes($row['admin_option_category']);
            $info['Category_Name'] = stripslashes($row['admin_category_name']);
            $info['Name'] = stripslashes($row['admin_option_name']);
            $info['Desc'] = stripslashes($row['admin_option_description']);
            $info['Page'] = stripslashes($row['admin_option_page']);
            $info['Visible'] = stripslashes($row['admin_option_visible']);

        }

        $this->dbClose();

        return $info;

    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //function for getting page information
    public function getPageInfo($pageId)
    {

        $info = array();

        $this->dbConn();

        $this->query = "SELECT * FROM Admin_Options LEFT JOIN Admin_Categories ON admin_option_category = admin_category_nbr WHERE admin_option_nbr = '";
        $this->query .= $this->db->real_escape_string($pageId);
        $this->query .= "'";

        $getInfo = $this->db->query($this->query);

        if ($getInfo->num_rows > 0) {

            $row = $getInfo->fetch_assoc();
            $info['Nbr'] = stripslashes($row['admin_option_nbr']);
            $info['Category'] = stripslashes($row['admin_option_category']);
            $info['Name'] = stripslashes($row['admin_option_name']);
            $info['Desc'] = stripslashes($row['admin_option_description']);
            $info['Page'] = stripslashes($row['admin_option_page']);
            $info['Directory'] = stripslashes($row['admin_category_directory']);
            $info['Visible'] = stripslashes($row['admin_option_visible']);

        }

        $this->dbClose();

        return $info;

    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //function for adding a new content managed page
    public function addContentManagedPage()
    {

        $result = array();

        if ($this->checkRef()) {

            $data = $this->cleanData();

            $now = time();

            if ($data['Page_Name'] != '') {

                $this->dbConn();

                $this->query = "INSERT INTO Pages VALUES ('','";
                $this->query .= $data['Page_Name'];
                $this->query .= "','','','','','";
                $this->query .= $now;
                $this->query .= "','0','";
                $this->query .= $now;
                $this->query .= "')";

                if ($this->db->query($this->query)) {

                    $result['Msg'] = 'The new page was successfully created.';
                    $result['Class'] = 'success';
                    $result['Status'] = 1;

                } else {

                    $result['Msg'] = 'DB Error';
                    $result['Class'] = 'error';
                    $result['Status'] = 0;

                }

                $this->dbClose();

            } else {

                $result['Msg'] = 'Please enter a name for the new page being created.';
                $result['Class'] = 'error';
                $result['Status'] = 0;

            }

        }

        return $result;

    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //function for getting all content managed pages
    public function getContentManagedPages()
    {

        $info = array();

        if ($this->checkRef()) {

            $this->dbConn();

            $this->query = "SELECT * FROM Pages ORDER BY page_name ASC";

            $getPages = $this->db->query($this->query);

            if ($getPages->num_rows > 0) {

                $i = 0;
                while ($row = $getPages->fetch_assoc()) {

                    $info[$i]['Nbr'] = stripslashes($row['page_nbr']);
                    $info[$i]['Name'] = stripslashes($row['page_name']);
                    $info[$i]['Title_Tag'] = stripslashes($row['page_title_tag']);
                    $info[$i]['Keywords_Tag'] = stripslashes($row['page_keywords_tag']);
                    $info[$i]['Description_Tag'] = stripslashes($row['page_description_tag']);
                    $info[$i]['Content'] = stripslashes($row['page_content']);
                    $info[$i]['Date_Created'] = stripslashes($row['page_date_created']);
                    $info[$i]['Status'] = stripslashes($row['page_status']);
                    $i++;

                }

            }

            $this->dbClose();

        }

        return $info;

    }

}
