<?php

/**
 * This is a class file used for Menu operations.
 *
 * PHP version 5
 *
 * @category School Management System
 * @package  School Management System
 * @author   Aum Infosys <falguni@auminfosys.com>
 * @license Auminfosys
 * @version  CVS:1.0.1
 * @link     http://auminfosys.com/
 * @filesource
 */

/**
 * This is a class named MenuMgt.
 *
 * @category School Management System
 * @package  School Management System
 * @author   Aum Infosys <falguni@auminfosys.com>
 * @license Auminfosys
 * @version  Release: 1.0.1
 * @link     http://auminfosys.com/
 *
 */

class MenuMgmt
{
    /**
     * @var public $menu_id This is the Menu Id feteched from the database.
     */
    public $menu_id;
    /**
     * @var public $menu_name This is the Menu Title.
     */
    public $menu_name;
    /**
     * @var public $menu_order This is the Menu Display Order.
     */
    public $menu_order;
    /**
     * @var public $menuactive Whether the Menu is active or not.
     */
    public $menuactive;
    /**
     * @var public $menumsg This is the message.
     */
    public $menumsg;
    /**
     * @var public $menucount This is a counter.
     */
    public $menucount;
    /**
     * @var public $menuredirectfile This is file name to be redirected on after the operation.
     */
    public $menuredirectfile;
    /**
     * @var public $arractiveIds List of active Id's.
     */
    public $arractiveIds;
    /**
     * @var public $inactiveIds List of inactive Id's.
     */
    public $inactiveIds;
    /**
     * @var public $deleteId List of Id's to be deleted.
     */
    public $deleteId;
    /**
     * @var public $bullet This is bullet.
     */
    public $bullet;

    /**
     * Constructor to inialize manu_count to zero
     *
     */
    function MenuMgmt()
    {
        $this->bullet = "<font size='2'>&raquo;</font>";
        $this->bullet = htmlentities($this->bullet);
        $this->manu_count = 0;
    }

    /**
     * This function is used to get a particular record.
     *
     * @param integer $menu_id This is the database banner id.
     *
     * @return array
     */
    function getRecordById($menu_id)
    {
        $qry = "select * from cms_menumaster where menu_active='1' ";

        if ($menu_id != "") {
            $qry .= " and menu_id=$menu_id ";
        }

        $out = dbQuery($qry);

        if (dbRows($out) > 0) {
            $this->manu_count = 1;

            $row = dbArray($out);
            extract($row);

            $this->menu_id = ($menu_id);
            $this->menu_name = ($menu_name);
            $this->manu_order = ($menuorder);
            $this->menu_active = ($menuactive);

            dbResult($out);
        }
    }

    /**
     * This is a function for getting the list of records
     *
     * @param integer $limit This is the limit of number or records.
     * @param integer $start This is the starting location of records.
     *
     * @return mix
     */
    function getRecords($limit = 0, $start = 0)
    {
        $qry = "select * from cms_menumaster where menu_active='1' order by manu_order asc";

        if (intval($limit) > 0 && intval($start) >= 0) {
            $qry .= " Limit $start , $limit";
        }

        $out = dbQuery($qry);

        if (dbRows($out) > 0) {
            $this->manu_count = dbRows($out);
            return $out;
        }
        return $out;
    }

    /**
     * This is a function for getting the total no of records with/without condition
     *
     * @param integer $limit This is the limit of number or records.
     * @param integer $start This is the starting location of records.
     * @param string  $condition This is the condition for fetching records.	 
     * @param string  $order_field This is the field for fetching records orderby.	 
     * @param string  $order_type This is the order direction to fetch records orderby.	 	 
     *
     * @return Total Records Found
     */
    function getCountRecords($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
    {
        $this->manu_count = 0;

        $qry = "SELECT menu_id, menu_name, menu_order, menu_active FROM cms_menumaster where 1=1 ";

        if ($condition != "") {
            $qry .= $condition;
        }

        if ($order_field != "" && $order_type != "") {
            $qry .= " order by $order_field $order_type";
        }

        if (intval($limit) > 0 && intval($start) >= 0) {
            $qry .= " Limit $start , $limit";
        }
        $out = dbQuery($qry);

        if (dbRows($out) > 0) {
            $this->manu_count = dbRows($out);
            dbResult($out);
        }
        return $this->manu_count;
    }

    /**
     * This is a function for getting the all records with/without condition
     *
     * @param integer $limit This is the limit of number or records.
     * @param integer $start This is the starting location of records.
     * @param string  $condition This is the condition for fetching records.	 
     * @param string  $order_field This is the field for fetching records orderby.	 
     * @param string  $order_type This is the order direction to fetch records orderby.	 	 
     *
     * @return Mix Result Set
     */
    function getAllRecords($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
    {
        $this->manu_count = 0;

        $qry = "SELECT menu_id, menu_name, menu_order, menu_active FROM cms_menumaster where 1=1 ";

        if ($condition != "") {
            $qry .= $condition;
        }

        if ($order_field != "" && $order_type != "") {
            $qry .= " order by $order_field $order_type";
        }

        if (intval($limit) > 0 && intval($start) >= 0) {
            $qry .= " Limit $start , $limit";
        }

        //print $qry;
        $out = dbQuery($qry);

        if (dbRows($out) > 0) {
            $this->manu_count = dbRows($out);
            return $out;
        }
        return $out;
    }



    /**
     * This is a function for getting the  all menu ids with/without condition
     * @param string  $condition This is the condition for fetching records.	 
     * @return Mix Result Set
     */

    function getAllMenuIds($condition = "")
    {
        $qry = "SELECT menu_id FROM cms_menumaster where 1='1' ";

        if ($condition != "") {
            $qry .= $condition;
        }

        $result = dbQuery($qry);

        if (dbRows($result) > 0) {

            $strIds = "";

            while ($row = dbArray($result)) {

                $strIds .= $row["menu_id"] . ",";
            }

            $strIds = substr($strIds, 0, strlen($strIds) - 1);

        }

        return $strIds;
    }


    /**
     * This is a function for adding new record for Menu.
     *
     * @return void The redirect file name and the message are passed.
     *
     */
    function add()
    {
        if (duplicate("cms_menumaster", "menu_name", $this->menu_name, "menu_id", $this->menu_id)) {
            $this->manu_msg = "<strong>" . $this->bullet . " Duplicate Memu found.</strong><br>Memu [" . decode($this->menu_name) . "] already exists.";
            $this->manu_redirectfile = "addeditmenu.php";
        } else {
            $Qry1 = "";

            $Qry1 .= "menu_name='" . Escape($this->menu_name) . "',";
            $Qry1 .= "menu_order='" . Escape($this->menu_order) . "',";
            $Qry1 .= "blnBackup = '0',";

            $Qry1 .= ($this->menu_active == "") ? "menu_active='0'," : "menu_active='1',";

            if ($Qry1 != "") {
                $Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
                $qry = "insert into cms_menumaster set " . $Qry1 . " ";
                dbQuery($qry);
                $this->menu_id = dbInsertedId();
            }

            $this->manu_msg = "<strong>" . $this->bullet . " Memu inserted successfully.</strong>";
            $this->manu_redirectfile = "viewmenu.php";
        }
    }

    /**
     * This is a function for editing a Menu record.
     *
     * @return void The redirect file name and the message are passed.
     *
     */
    function edit()
    {
        if (duplicate("cms_menumaster", "menu_name", $this->menu_name, "menu_id", $this->menu_id)) {
            $this->manu_msg = "<strong>" . $this->bullet . " Duplicate Memu found.</strong><br>Memu [" . decode($this->menu_name) . "] already exists.";
            $this->manu_redirectfile = "addeditmenu.php";
        } else {
            $Qry1 = "";

            $Qry1 .= "menu_name='" . Escape($this->menu_name) . "',";
            $Qry1 .= "menu_order='" . Escape($this->menu_order) . "',";
            $Qry1 .= "blnBackup = '0',";

            $Qry1 .= ($this->menu_active == "") ? "menu_active='0'," : "menu_active='1',";

            if ($Qry1 != "") {
                $Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
                $qry = "update cms_menumaster set " . $Qry1 . " where menu_id=" . $this->menu_id;
                dbQuery($qry);
            }

            $this->manu_msg = "<strong>" . $this->bullet . " Memu updated successfully.</strong>";
            $this->manu_redirectfile = "viewmenu.php";
        }
    }

    /**
     * This is a function used to update the status of the records.
     *
     * @return void The redirect file name and the message are passed.
     *
     */
    function updateStatus()
    {
        if (count($this->arractiveIds) > 0) {
            $strActiveids = implode(",", $this->arractiveIds);
        }
        if (count($this->inactiveIds) > 0) {
            $strinactiveIds = implode(",", $this->inactiveIds);
        }
        $qry = "update cms_menumaster set menu_active = '0',blnBackup = '0' where 1=1 and menu_id in ($strinactiveIds)";
        dbQuery($qry);

        $qry = "update cms_menumaster set menu_active = '1',blnBackup = '0' where 1=1 and menu_id in ($strActiveids)";
        dbQuery($qry);

        $this->manu_msg = "<strong>" . $this->bullet . " Menu(s) Status updated successfully.</strong><br>Selected Menu(s) has been updated successfully.";
        $this->manu_redirectfile = "viewmenu.php";
    }


    /**
     * This is a function for getting the all records with/without condition
     *
     * @param integer $limit This is the limit of number or records.
     * @param integer $start This is the starting location of records.
     * @param string  $condition This is the condition for fetching records.	 
     * @param string  $order_field This is the field for fetching records orderby.	 
     * @param string  $order_type This is the order direction to fetch records orderby.	 	 
     *
     * @return Mix Result Set
     */
    function getAllModuleRecords($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
    {
        $this->manu_count = 0;

        $qry = "SELECT * FROM sms_modulemaster where 1=1 ";

        if ($condition != "") {
            $qry .= $condition;
        }

        if ($order_field != "" && $order_type != "") {
            $qry .= " order by $order_field $order_type";
        }

        if (intval($limit) > 0 && intval($start) >= 0) {
            $qry .= " Limit $start , $limit";
        }


        $out = dbQuery($qry);

        if (dbRows($out) > 0) {
            $this->manu_count = dbRows($out);
            return $out;
        }
        return $out;
    }


    /**
     * This is a function for getting the  all menu ids with/without condition
     * @param string  $condition This is the condition for fetching records.	 
     * @return Mix Result Set
     */

    function getAllModuleIds($condition = "")
    {
        $qry = "SELECT module_id FROM sms_modulemaster where 1='1' ";

        if ($condition != "") {
            $qry .= $condition;
        }

        $result = dbQuery($qry);

        if (dbRows($result) > 0) {

            $strIds = "";

            while ($row = dbArray($result)) {

                $strIds .= $row["module_id"] . ",";
            }

            $strIds = substr($strIds, 0, strlen($strIds) - 1);

        }

        return $strIds;
    }

//get-user-access-tree
    function getAllModule($condition)
    {
   

        $qry = "SELECT * FROM cms_modulemaster where module_active = 1";

        if ($condition != "") {
            $qry .= $condition;
        }

        $out = dbQuery($qry);

        if (dbRows($out) > 0) {
            $this->manu_count = dbRows($out);
            return $out;
        }
        return $out;
    }

    function getmenu($condition)
    {
   

        $qry = "SELECT * FROM cms_menumaster where menu_active = 1 ";
	
        if ($condition != "") {
            $qry .= $condition;
        }

        $out = dbQuery($qry);

        if (dbRows($out) > 0) {
            $this->manu_count = dbRows($out);
            return $out;
        }
        return $out;
    }
}
?>