<?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 PageMgmt
{
    /**
     * @var public $page_id This is the Page Id feteched from the database.
     */
    public $page_id;
    /**
     * @var public $menu_id This is the Menu Id feteched from the database.
     */
    public $menu_id;
    /**
     * @var public $page_name This is the Page Title.
     */
    public $page_name;
    /**
     * @var public $page_link This is the Page URL Link.
     */
    public $page_link;
    /**
     * @var public $page_order This is the Page Display Order.
     */
    public $page_order;
    /**
     * @var public $page_active Whether the Page is active or not.
     */
    public $page_active;
    /**
     * @var public $page_msg This is the message.
     */
    public $page_msg;
    /**
     * @var public $page_count This is a counter.
     */
    public $page_count;
    /**
     * @var public $page_redirectfile This is file name to be redirected on after the operation.
     */
    public $page_redirectfile;
    /**
     * @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 page_count to zero
     *
     */
    function PageMgmt()
    {
        $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 $page_id This is the database banner id.
     *
     * @return array
     */
    function getRecordById($page_id)
    {
        $qry = "select * from cms_pagemaster where page_active='1' ";

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

        $out = dbQuery($qry);

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

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

            $this->page_id = ($page_id);
            $this->menu_id = ($menu_id);
            $this->page_link = ($page_link);
            $this->page_name = ($page_name);
            $this->page_order = ($page_order);
            $this->page_active = ($page_active);

            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_pagemaster where page_active='1' order by page_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;
        }
        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 page_id,menu_id, page_name, page_link,page_order, page_active FROM cms_pagemaster 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);
            dbResult($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 page_id,menu_id, page_name, page_link,page_order, page_active,access FROM cms_pagemaster 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 page_id FROM cms_pagemaster where 1='1' ";

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

        $result = dbQuery($qry);

        if (dbRows($result) > 0) {

            $strIds = "";

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

                $strIds .= $row["page_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("cms_pagemaster", "page_name", $this->page_name, "page_id", $this->page_id)) {
            $this->page_msg = "<strong>" . $this->bullet . " Duplicate Memu found.</strong><br>Memu [" . decode($this->page_name) . "] already exists.";
            $this->page_redirectfile = "addeditmenu.php";
        } else {
            $Qry1 = "";

            $Qry1 .= "menu_id='" . Escape($this->menu_id) . "',";
            $Qry1 .= "page_name='" . Escape($this->page_name) . "',";
            $Qry1 .= "page_link='" . Escape($this->page_link) . "',";
            $Qry1 .= "page_order='" . Escape($this->page_order) . "',";
            $Qry1 .= "blnBackup = '0',";

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

            if ($Qry1 != "") {
                $Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
                $qry = "insert into cms_pagemaster set " . $Qry1 . " ";
                dbQuery($qry);
                $this->page_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("cms_pagemaster", "page_name", $this->page_name, "page_id", $this->page_id)) {
            $this->page_msg = "<strong>" . $this->bullet . " Duplicate Memu found.</strong><br>Memu [" . decode($this->page_name) . "] already exists.";
            $this->page_redirectfile = "addeditmenu.php";
        } else {
            $Qry1 = "";

            $Qry1 .= "menu_id='" . Escape($this->menu_id) . "',";
            $Qry1 .= "page_name='" . Escape($this->page_name) . "',";
            $Qry1 .= "page_link='" . Escape($this->page_link) . "',";
            $Qry1 .= "page_order='" . Escape($this->page_order) . "',";
            $Qry1 .= "blnBackup = '0',";

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

            if ($Qry1 != "") {
                $Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
                $qry = "update cms_pagemaster set " . $Qry1 . " where page_id=" . $this->page_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 cms_pagemaster set page_active = '0',blnBackup = '0' where 1=1 and page_id in ($strinactiveIds)";
        dbQuery($qry);

        $qry = "update cms_pagemaster set page_active = '1',blnBackup = '0' where 1=1 and page_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";
    }

    //get-user-access-tree.php
    function getpage($condition)
    {
   

        $qry ="SELECT * FROM cms_pagemaster where 1=1  AND page_active = 1 ";

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

        $out = dbQuery($qry);

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


    function getpageaccess($condition)
    {
   

        $qry ="SELECT access FROM cms_pagemaster where 1=1 ";
        if ($condition != "") {
            $qry .= $condition;
        }

        $out = dbQuery($qry);

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