Location: PHPKode > projects > CSTracker > cstracker2-beta1/controllers/SupportController.php
<?php
/*
 * Created on Jul 23, 2009
 *
 * Author:: Muhammed Nilufer Nilar
 *
 */

require_once('../classes/SupportRequest.php');
require_once('../templates/supportRequestForm.php');
require_once('../classes/RequestComment.php');

ob_start();

session_start();

//print_r($_GET);exit;

if( !isset($_GET['code']) ){
    echo "Incorret requestttt";
    exit;
}

$mode = $_GET['code'];

switch($mode){
    case 'new' :
        flushRequestTemplate();
        break;

    case 'add' :
        //echo "<pre>"; print_r($_POST);exit;
        if( $_POST['id'] == '' ){
            $id = SupportRequest::getLastId() + 1 ;
            $saveMode = 'insert';
            $successMessage = "<p class='greenhighlight'>Support Request Saved Successfully!</p>";
            $failureMessage = "Request Saving Failed!<br>Entered data loaded below.";
            $rqstObj = new SupportRequest($id, $_POST['summary'], $_POST['cmbcustomer'], $_POST['date'], $_POST['status'], $_POST['description'], date('Y-m-d  H:i:s') , $_SESSION['full_name']);
        }
        else{
            $id = $_POST['id'];
            $saveMode = 'update';
            $successMessage = "<p class='greenhighlight'>Support Request Updated Successfully!</p>";
            $failureMessage = "Support Request Update Failed!<br>Entered data loaded below.";
            $rqstObj = new SupportRequest($id, $_POST['summary'], $_POST['cmbcustomer'], $_POST['date'], $_POST['status'], null, date('Y-m-d  H:i:s') , $_SESSION['full_name']);
        }

        //echo $newId;exit;
        
//        echo "<pre>";print_r($rqstObj);exit;
        $error = '';
        $rqstObj->prepareToSave();
        $saveStatus = $rqstObj->Save($saveMode, $error);
        if($saveStatus){
            $msg = $successMessage;
            $rqstObj->getRequestById();
            $rqstObj->prepareToShow();
        }
        else{
            $msg = "<p class='redhighlight'>" . $failureMessage . "<br>" . $error . "</p>";
            if($saveMode == 'insert'){
                $cstmrObj->id = null;
            }
        }

        flushRequestTemplate($rqstObj, $msg);
        break;

    case 'all' :
        $recsPerPage = 10;
        if( !isset($_GET['pgno']) ){
            $pgNo = 1;
        }
        else{
            $pgNo = $_GET['pgno'];
        }
        $count = SupportRequest::countAllRequests();
        $head = (int)($count / $recsPerPage);
        $tail = $count % $recsPerPage;
        //echo $head . " " . $tail;exit;
        if ($tail == 0){
            $numOfPages = $head;
        }
        else{
            $numOfPages = ++$head;
        }
        //echo $count;exit;
        $requests = SupportRequest::fetchAllRequests($pgNo, $recsPerPage);
        if ( is_array($requests) ){
            flushListTemplate($requests, $numOfPages, $pgNo);
        }
        else{
            echo $requests;
        }
        break;

    case 'filter' :
        $recsPerPage = 5;
        if( !isset($_GET['pgno']) ){
            $pgNo = 1;
        }
        else{
            $pgNo = $_GET['pgno'];
        }

        $by = $_GET['by'];
        switch($by){
            case 'plan' :
                $filterBy = 'plan';
                $plan = $_GET['value'];
                $count = SupportRequest::countRequests(null, $plan);
                $head = (int)($count / $recsPerPage);
                $tail = $count % $recsPerPage;
                //echo $head . " " . $tail;exit;
                if ($tail == 0){
                    $numOfPages = $head;
                }
                else{
                    $numOfPages = ++$head;
                }
                $requests = SupportRequest::fetchRequestsFilterByPlan($plan, $pgNo, $recsPerPage);
                flushListTemplate($requests, $numOfPages, $pgNo, $filterBy, $plan);
                break;

            case 'status' :
                $filterBy = 'status';
                $status = $_GET['value'];
                $count = SupportRequest::countRequests($status, null);
                $head = (int)($count / $recsPerPage);
                $tail = $count % $recsPerPage;
                //echo $head . " " . $tail;exit;
                if ($tail == 0){
                    $numOfPages = $head;
                }
                else{
                    $numOfPages = ++$head;
                }
                $requests = SupportRequest::fetchRequestsFilterByStatus($status, $pgNo, $recsPerPage);
                flushListTemplate($requests, $numOfPages, $pgNo, $filterBy, $status);
                break;
        }
        break;

    case 'single' :
        $id = $_GET['id'];
        $rqstObj = new SupportRequest($id);
        $msg = $rqstObj->getRequestById();
        $rqstObj->prepareToShow();
        flushRequestTemplate($rqstObj, $msg);
        break;

    case 'delete' :
        $id = $_GET['id'];
        $rqstObj = new SupportRequest($id);
        $error = '';
        $delStatus = $rqstObj->Delete($error);
        if($delStatus){
            $msg = "<p class='greenhighlight'>Request deleted successfully!</p>";
        }
        else{
            $msg = "<p class='redhighlight'>Request delete failed!" . "<br>" . $error . "</p>";
        }
        echo $msg;
        break;

    case 'comment' :
        //echo "<pre>"; print_r($_POST);exit;
        $id = $_POST['requestID'];
        $rqstObj = new SupportRequest($id);
        $comment = new RequestComment(null, $_POST['requestID'], date('Y-m-d  H:i:s'), $_POST['txtcomment'], $_SESSION['full_name']);
        //echo "<pre>";print_r($comment);exit;
        $comment->prepareToSave();
        $saveStatus = $comment->saveComment();
        if(is_bool($saveStatus) && $saveStatus == true){
            $msg = "<p class='greenhighlight'>Saved the comment successfully!</p>";
            $msg .= $rqstObj->getRequestById();
            $rqstObj->prepareToShow();
            flushRequestTemplate($rqstObj, $msg);
            break;
        }
        else{
            echo $saveStatus;
            break;
        }
        break;

    case 'search' :
        
//        if ( !isset($_GET['by']) ){
//        }
        $recsPerPage = 20;
        if( !isset($_GET['pgno']) ){
            $pgNo = 1;
        }
        else{
            $pgNo = $_GET['pgno'];
        }

//        print_r($_GET);

        if( isset($_GET['by']) && $_GET['by'] = 'plan' ){
            $status = $_GET["status"];//'Resolved';
            $plan = $_GET["plan"];
            $period = $_GET['when'];
            $date = $_GET['date'];
//print_r($_GET);
//            echo "<br>status = " . $status . " plan = " . $plan . " period= " . $period . " date = " . $date . " searchby = " . $searchBy;
            if ($period == 'before'){
                $comp = '<';
            }
            elseif($period == 'after'){
                $comp = '>';
            }

            if( $plan == 0 && $status == 'All') {
                $searchBy = "nothing";
            }
            elseif($plan == 0 && $status != 'All'){
                $searchBy = "status";
            }
            elseif($plan != 0 && $status == 'All'){
                $searchBy = "plan";
            }
            elseif($plan != 0 && $status != 'All'){
                $searchBy = "both";
            }
            else{
                echo "impossible";exit;
            }
//           echo "<br>status = " . $status . " plan = " . $plan . " period= " . $period . " date = " . $date . " searchby = " . $searchBy;

            $paramString = "code=search&by=plan&plan=$plan&status=$status&when=$period&date=$date";

            switch($searchBy){
                case 'nothing' :
                    if( !isset($_GET['pgno']) ){
                        $pgNo = 1;
                    }
                    else{
                        $pgNo = $_GET['pgno'];
                    }
                    $count = SupportRequest::countAllRequests();
                    $head = (int)($count / $recsPerPage);
                    $tail = $count % $recsPerPage;
                    //echo $head . " " . $tail;exit;
                    if ($tail == 0){
                        $numOfPages = $head;
                    }
                    else{
                        $numOfPages = ++$head;
                    }
                    //echo $count;exit;
                    $requests = SupportRequest::fetchAllRequests($pgNo, $recsPerPage);
                    if ( is_array($requests) ){
                        showSearchForm($requests, $numOfPages, $pgNo, $paramString, $count);
                    }
                    else{
                        echo $requests;
                    }
                    break;
            
                case 'plan' :
                    $count = SupportRequest::countRequests(null, $plan);
                    $head = (int)($count / $recsPerPage);
                    $tail = $count % $recsPerPage;
                    //echo $head . " " . $tail;exit;
                    if ($tail == 0){
                        $numOfPages = $head;
                    }
                    else{
                        $numOfPages = ++$head;
                    }
                    $requests = SupportRequest::fetchRequestsFilterByPlan($plan, $pgNo, $recsPerPage);
                    showSearchForm($requests, $numOfPages, $pgNo, $paramString, $count);
                    break;
                    
                case 'status' :
                    $count = SupportRequest::countRequests($status, null);
                    $head = (int)($count / $recsPerPage);
                    $tail = $count % $recsPerPage;
                    //echo $head . " " . $tail;exit;
                    if ($tail == 0){
                        $numOfPages = $head;
                    }
                    else{
                        $numOfPages = ++$head;
                    }
                    $requests = SupportRequest::fetchRequestsFilterByStatus($status, $pgNo, $recsPerPage);
                    showSearchForm($requests, $numOfPages, $pgNo, $paramString, $count);
                    break;

                case 'both' :
                    $count = SupportRequest::countRequests($status, $plan);
                    $head = (int)($count / $recsPerPage);
                    $tail = $count % $recsPerPage;
                    //echo $head . " " . $tail;exit;
                    if ($tail == 0){
                        $numOfPages = $head;
                    }
                    else{
                        $numOfPages = ++$head;
                    }
                    $requests = SupportRequest::fetchRequestsFilterByPlanStatus($plan, $status, $comp, $date, 1, 10);
                    showSearchForm($requests, $numOfPages, $pgNo, $paramString, $count);
                    break;

                case 'default' :
                    echo "Wrong search criteria!!!";
                    break;

            }



            
        }
        break;

    case 'default' :
        echo "Wrong url";
        break;
}

?>
Return current item: CSTracker