<?php

/**
 * This is a class file used for Page 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 PageMgt.
 *
 * @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 SubPageMgmt
{
    /**
     * @var public $subpage_id This is the Page Id feteched from the database.
     */
    public $subpage_id;
	/**
     * @var public $page_id This is the Menu Id feteched from the database.
     */
    public $page_id;
    /**
     * @var public $subpage_name This is the Page Title.
     */
    public $subpage_name;
	/**
     * @var public $subpage_link This is the Page URL Link.
     */
    public $subpage_link;
    /**
     * @var public $subpage_order This is the Page Display Order.
     */
    public $subpage_order;
    /**
     * @var public $subpage_active Whether the Page is active or not.
     */
    public $subpage_active;
    /**
     * @var public $page_msg This is the message.
     */
    public $page_msg;
    /**
     * @var public $page_count This is a counter.
     */
    public $deleteId;
    /**
     * @var public $bullet This is bullet.
     */
    public $bullet;
    public $page_count;

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

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

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

        $out=dbQuery($qry);

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

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

            $this->subpage_id		= ($subpage_id);
			$this->page_id		= ($page_id);
            $this->subpage_link	= ($subpage_link);
            $this->subpage_name	= ($subpage_name);
            $this->subpage_order  	= ($subpage_order);
            $this->subpage_active 	= ($subpage_active);

            dbFreeResult($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 lms_subpagemaster where subpage_active='1' order by subpage_order asc";

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

        $out=dbQuery($qry);

        if (dbRows($out)>0) {
            $this->page_count=dbRows($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->page_count=0;
		
        $qry = "SELECT subpage_id,page_id, subpage_name, subpage_link,subpage_order, subpage_active FROM lms_subpagemaster 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->page_count=dbRows($out);            
			dbFreeResult($out);
        }
		return $this->page_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->page_count=0;
		 $qry = "SELECT subpage_id,page_id, subpage_name, subpage_link,subpage_order, subpage_active,subpage_access FROM lms_subpagemaster 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";
        }
		//echo $qry;
        $out=dbQuery($qry);

        if (dbRows($out)>0) {
            $this->page_count=dbRows($out);
			return $out;							
        }
		return $out;		
    }
		
/**
     * This is a function for getting the all page ids with/without condition
      * @param string  $condition This is the condition for fetching records.	 
      * @return Mix Result Set
     */
function getAllPageIds($condition="")
    {
		
        $qry = "SELECT subpage_id FROM lms_subpagemaster where 1='1' ";
		
		if ($condition != "" ) {
            $qry.= $condition;
        }
		
		$result=dbQuery($qry);

		if(dbRows($result) >0 ){
		
			$strIds="";
			
			while($row=dbArray($result)) {
				
				$strIds .=$row["subpage_id"].",";		
			}
			
			$strIds =substr($strIds,0,strlen($strIds)-1);
		
		}
		
		return $strIds;
		
}
		

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

            $Qry1 .= "page_id='"	.Escape($this->page_id)."',";
		    $Qry1 .= "subpage_name='"	.Escape($this->subpage_name)."',";
            $Qry1 .= "subpage_link='"	.Escape($this->subpage_link)."',";			
            $Qry1 .= "subpage_order='"	.Escape($this->subpage_order)."',";
			$Qry1 .= "blnBackup = '0',";				
						
			$Qry1 .= ($this->subpage_active=="") ? "subpage_active='0'," : "subpage_active='1',";

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

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

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

            $Qry1 .= "page_id='"	.Escape($this->page_id)."',";
		    $Qry1 .= "subpage_name='"	.Escape($this->subpage_name)."',";
            $Qry1 .= "subpage_link='"	.Escape($this->subpage_link)."',";			
            $Qry1 .= "subpage_order='"	.Escape($this->subpage_order)."',";
			$Qry1 .= "blnBackup = '0',";				
						
			$Qry1 .= ($this->subpage_active=="") ? "subpage_active='0'," : "subpage_active='1',";

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

            $this->page_msg= "<strong>".$this->bullet." Memu updated successfully.</strong>";
            $this->page_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 lms_subpagemaster set subpage_active = '0',blnBackup = '0' where 1=1 and subpage_id in ($strinactiveIds)";
        dbQuery($qry);

        $qry = "update lms_subpagemaster set subpage_active = '1',blnBackup = '0' where 1=1 and subpage_id in ($strActiveids)";
        dbQuery($qry);

        $this->page_msg="<strong>" . $this->bullet . " Memu(s) Status updated successfully.</strong><br>Selected Memu(s) has been updated successfully.";
        $this->page_redirectfile="viewmenu.php";
    }
}
?>