<?php

require_once("imp/_imp.php");

class Controllers {

//public vars
    public $smarty;
    public $hosturl;
    public $controllname;
    public $shcontrollname;
    public $active;
    public $time;
    public $today;
    public $patientid;

// it will run in everytime with
// default requried
    public function __construct() {
        require_once("../public/aw_config.php");
        require_once("imp/_autho.php"); //general functions
        $this->smarty = $smarty;
        $this->hosturl = $hosturl;

//here name of controll to known
        $this->controllname = 'operation';
        $this->shcontrollname = 'operation';
        $this->smarty->assign('controllname', $this->controllname);
        $this->smarty->assign('shcontrollname', $this->shcontrollname);

//breadcrumbs
        require_once("../../_library/plugins/Breadcrumbs/Breadcrumbs.php");
        $acty = filter_input(INPUT_GET, 'ac');
        $bc = new breadcrumbs($hosturl, '', '', $acty);
        $bc->crumbs();
        $breadcrumbs = $bc->breadcrumbs;
        $smarty->assign('breadcrumbs', $breadcrumbs);

# here show the active page in menu
// array called active[] with level of active element
        $active[0] = 'operation'; //controller name or tab name
        $active[1] = 'operation'; //controller name
// do name  // switch($do)
        switch ($acty) {
            case '':$active[2] = 'operationadd';
                break;
            case 'show':$active[2] = 'operationshow';
                break;
            default :$active[2] = 'operationshow';
                break;
        }
        $this->active = $active;

//here will call all php pages
        require_once("_header.php");
        require_once("_patientdata.php");
        require_once("_sidebar.php");
        require_once("_leftsidebar.php");
        require_once("_footer.php");

        $this->patientid = patientdata::patientid();

        //find loading time
        $this->time = microtime(true);
        $this->today = date('Y-m-d');
    }

    public function show() {

        //check login
        autho::checkautho($this->hosturl);
        //check authoize
        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);

        $operations = R::findAll('operation', 'del is null  ');
        $this->smarty->assign('operations', $operations);

        //call other class with other content
        header::headershow($this->smarty, $this->hosturl);
        sidebar::sidebarshow($this->smarty, $this->hosturl, $this->active);
        $this->smarty->display('operations/show.html');
        leftsidebar::leftsidebarshow($this->smarty, $this->hosturl);
        footer::footershow($this->smarty, $this->hosturl);
    }

    public function index() {

        //check login
        autho::checkautho($this->hosturl);
        //check authoize
        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);

        //call other class with other content
        header::headershow($this->smarty, $this->hosturl);
        sidebar::sidebarshow($this->smarty, $this->hosturl, $this->active);
        $this->smarty->display('operations/add.html');
        leftsidebar::leftsidebarshow($this->smarty, $this->hosturl);
        footer::footershow($this->smarty, $this->hosturl);
    }

    public function add() {

        //check login
        autho::checkautho($this->hosturl);
        //check authoize
        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);

        //vars here
        $name = filter_input(INPUT_POST, 'name');
        $content = filter_input(INPUT_POST, 'content');

        $operation = R::dispense('operation');

        $operation->title = $name;
        $operation->content = $content;

        try {
            R::store($operation);
            header('Location:' . $this->hosturl . '/controllers/' . $this->controllname . '.php?ac=show');
        } catch (Exception $e) {
            header('Location:' . $this->hosturl . '/error/notadd/' . $this->controllname);
        }
    }

    public function edit() {

        //check login
        autho::checkautho($this->hosturl);
        //check authoize
        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);

        //get id for item
        $id = filter_input(INPUT_GET, 'id');

        //load all here
        $editElement = R::load('operation', $id);
        $this->smarty->assign('editElement', $editElement);

        //call other class with other content
        header::headershow($this->smarty, $this->hosturl);
        sidebar::sidebarshow($this->smarty, $this->hosturl, $this->active);
        $this->smarty->display('operations/edit.html');
        leftsidebar::leftsidebarshow($this->smarty, $this->hosturl);
        footer::footershow($this->smarty, $this->hosturl);
    }

//    public function updateit() {
//
//        //check login
//        autho::checkautho($this->hosturl);
//        //check authoize
//        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);
//
//        //vars here
//        $name = filter_input(INPUT_POST, 'name');
//        $content = filter_input(INPUT_POST, 'content');
//
//        $id = filter_input(INPUT_POST, 'id');
//        $patientid = filter_input(INPUT_GET, "patientid");
//        $this->smarty->assign('patientId', $patientid);
//
//        //here the update
//        $operation = R::load('operation', $id);
//
//        $operation->name = $name;
//        $operation->content = $content;
//        $operation->patientid = 1;
//
//        try {
//            R::store($operation);
//            if (!empty($patientid)) {
//                header('Location:' . $this->hosturl . '/controllers/' . $this->controllname . '.php?ac=show&patientid=' . $patientid);
//            } else {
//                header('Location:' . $this->hosturl . '/controllers/' . $this->controllname . '.php?ac=show');
//            }
//        } catch (Exception $e) {
//            header('Location:' . $this->hosturl . '/error/notupdate/' . $this->controllname);
//        }
//    }

    public function del() {

        //check login
        autho::checkautho($this->hosturl);
        //check authoize
        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);

        $id = filter_input(INPUT_POST, 'id');
        try {
            R::exec('UPDATE operation SET del = null WHERE id = ' . $id);
            echo 1;
        } catch (Exception $e) {
            
        }
    }

//    public function getprint() {
//
//	//check login
//	autho::checkautho($this->hosturl);
//	//check authoize
//	autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);
//
//	$operationArray = json_decode(stripslashes(filter_input(INPUT_POST, 'jsonString')));
//
//
//	$operationArrayNew = array();
//
//	$patientId = filter_input(INPUT_POST, "patientId");
//
//	foreach ($operationArray as $inst) {
//	    $operation = R::load('operation', $inst);
//	    array_push($operationArrayNew, $operation);
//	}
//	if (!empty($patientId)) {
//	    $patient = R::load('patients', $patientId);
//
//	    $wifetypes = R::load('wifetypes', $patient->wiftypeid);
//	    $this->smarty->assign('name', $patient->wifename);
//	    $this->smarty->assign('title', $wifetypes->title);
//	} else {
//	    $patientId = 0;
//	}
//
//	$this->smarty->assign('date', $this->today);
//
//	$this->smarty->assign('patientId', $patientId);
//
//	$ProgramesettingData = R::findone('programesetting');
//	$this->smarty->assign('ProgramesettingData', $ProgramesettingData);
//
//	$this->smarty->assign('operations', $operationArrayNew);
//	$this->smarty->assign('show', 0);
//	$this->smarty->display('operations/print.html');
//    }
//
//    public function getinstdetail() {
//
//	//check login
////        autho::checkautho($this->hosturl);
////        //check authoize
////        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);
//
//	$operationId = filter_input(INPUT_POST, "operationId");
//	$operation = R::load('operation', $operationId);
//	$patientId = filter_input(INPUT_POST, "patientId");
//
//	if (!empty($patientId)) {
//	    $patient = R::load('patients', $patientId);
//
//	    $wifetypes = R::load('wifetypes', $patient->wiftypeid);
//	    $this->smarty->assign('name', $patient->wifename);
//	    $this->smarty->assign('title', $wifetypes->title);
//	} else {
//	    $patientId = 0;
//	}
//
//	$this->smarty->assign('date', $this->today);
//
//	$this->smarty->assign('patientId', $patientId);
//
//	$ProgramesettingData = R::findone('programesetting');
//	$this->smarty->assign('ProgramesettingData', $ProgramesettingData);
//
//	$this->smarty->assign('operations', $operation);
//	$this->smarty->assign('show', 1);
//	$this->smarty->display('operations/print.html');
//    }
//
//    public function showtransfer() {
//	//check login
//	autho::checkautho($this->hosturl);
//	//check authoize
//	autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);
//	$patientid = filter_input(INPUT_GET, "patientid");
//	if (empty($patientid)) {
//	    header('Location:' . $this->hosturl . '/controllers/index.php?empty=1');
//	    exit();
//	}
//
//
//	$myallobsteric = R::getAll('select id  from phobstetric where del = ?  and patientid = ? ', [0, $patientid]);
//	$getAllPatientData = R::getRow("SELECT * FROM patients WHERE id = $patientid");
//	$p = $getAllPatientData['pno'];
//	$ab = $getAllPatientData['ab'];
//	$Ectopic = $getAllPatientData['ectopic'];
//	$vmodel = $getAllPatientData['vmodel'];
//
//	$obstriccountnew = count($myallobsteric) + $p + $ab + $Ectopic + $vmodel;
//	$mainObj = R::findOne('mainantenental', 'done = 0 AND patientid = ?', [ $patientid]); ##  done = 0 current pregnancy
//	$mainObj->g = $obstriccountnew + 1;
//
//	$svddata = R::getAll('select id from phobstetric where obstermination =? and del = 0  and patientid = ?  ', [1, $patientid]);
//	$csdata = R::getAll('select id from phobstetric where obstermination =? and del = 0  and patientid = ?  ', [2, $patientid]);
//	$pcounter = count($svddata) + count($csdata) + $p;
//
//	if (!empty($mainObj->eed) && $mainObj->eed != "0000-00-00") {
//	    $mainObj->eed = date("d-m-Y", strtotime($mainObj->eed));
//	} elseif ($mainObj->eed == "0000-00-00") {
//	    $mainObj->eed = '';
//	}
//
//	$blodd = R::load('bloodtypes', $getAllPatientData['wifebl']);
//
//	$this->smarty->assign('patientData', $getAllPatientData);
//	$this->smarty->assign('mainObj', $mainObj);
//	$this->smarty->assign('p', $pcounter);
//	$this->smarty->assign('blodd', $blodd->title);
//
//
//	$date1 = new DateTime(date('Y-m-d'));
//	$date2 = new DateTime($mainObj->lmp);
//	$interval = $date1->diff($date2);
//
//	$this->smarty->assign('weeks', round($interval->days / 7));
//
//	$antUS = R::getRow('select placenta from mainantenentalus where deleted = ? and patientid = ? and mainantenatalid = ?  ORDER BY date DESC limit 1 ', [0, $patientid, $mainObj->id]);
//	$this->smarty->assign('antUS', $antUS);
//
//######################################################################################################################################################
//	## medical Diseases
//	$mypastmedical = R::findall('phpastmedical', 'del = ? and patientid = ? ', [0, $patientid]);
//	$allD = '';
//	foreach ($mypastmedical as $myallpastmedical) {
//	    $disease = R::getRow('select name from phpastmedicaldisease where id = ?', [$myallpastmedical->medicaldisease]);
//	    if ($disease['name'] && $disease['name'] != '') {
//		if ($allD != '') {
//		    $allD .= ' , ';
//		    $allD .= $disease['name'];
//		} else {
//		    $allD .= $disease['name'];
//		}
//	    }
//	}
//	$this->smarty->assign('mypastmedical', $allD);
//
//#####################################################################################################################################################
//	## hospitals
//	$hospitalnames = R::findall('hospitalnames');
//	$this->smarty->assign('hospitalnames', $hospitalnames);
//
//#############################################################################################################################################
//	## call other class with other content
//	header::headershow($this->smarty, $this->hosturl);
//	sidebar::sidebarshow($this->smarty, $this->hosturl, $this->active);
//	if ($patientid) {
//	    patientdata::patientdatashow($this->smarty, $this->hosturl);
//	}
//
//#############################################################################################################################################
//	$operations = R::findall('operationoperations', 'deleted = 0');
//	$this->smarty->assign('operations', $operations);
//
//
//	$this->smarty->display('operations/transfer.html');
//	leftsidebar::leftsidebarshow($this->smarty, $this->hosturl);
//	footer::footershow($this->smarty, $this->hosturl);
//    }
//
//    public function printtransfer() {
//
//	//check login
//	autho::checkautho($this->hosturl);
//	//check authoize
//	autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);
//
//	$hospitalId = filter_input(INPUT_POST, "hospitalName");
//	$opInst = filter_input(INPUT_POST, "value");
//        $instNotes = filter_input(INPUT_POST, "instNotes");
//        $patientId = filter_input(INPUT_POST, "patientId");
//
//
//	$myallobsteric = R::getAll('select id  from phobstetric where del = ?  and patientid = ? ', [0, $patientId]);
//	$getAllPatientData = R::getRow("SELECT * FROM patients WHERE id = $patientId");
//	$p = $getAllPatientData['pno'];
//	$ab = $getAllPatientData['ab'];
//	$Ectopic = $getAllPatientData['ectopic'];
//	$vmodel = $getAllPatientData['vmodel'];
//
//	$obstriccountnew = count($myallobsteric) + $p + $ab + $Ectopic + $vmodel;
//	$mainObj = R::findOne('mainantenental', 'done = 0 AND patientid = ?', [ $patientId]); ##  done = 0 current pregnancy
//	$mainObj->g = $obstriccountnew + 1;
//
//	$svddata = R::getAll('select id from phobstetric where obstermination =? and del = 0  and patientid = ?  ', [1, $patientId]);
//	$csdata = R::getAll('select id from phobstetric where obstermination =? and del = 0  and patientid = ?  ', [2, $patientId]);
//	$pcounter = count($svddata) + count($csdata) + $p;
//
//	if (!empty($mainObj->eed) && $mainObj->eed != "0000-00-00") {
//	    $mainObj->eed = date("Y/m/d", strtotime($mainObj->eed));
//	} elseif ($mainObj->eed == "0000-00-00") {
//	    $mainObj->eed = '';
//	}
//
//	$blodd = R::load('bloodtypes', $getAllPatientData['wifebl']);
//
//	$this->smarty->assign('patientData', $getAllPatientData);
//	$this->smarty->assign('mainObj', $mainObj);
//	$this->smarty->assign('p', $pcounter);
//	$this->smarty->assign('blodd', $blodd->title);
//
//
//	$date1 = new DateTime(date('Y-m-d'));
//	$date2 = new DateTime($mainObj->lmp);
//	$interval = $date1->diff($date2);
//
//
//	$getHospital = R::getRow("SELECT name FROM hospitalnames WHERE id = ?", [$hospitalId]);
//	$hospitalName = $getHospital['name'];
//	$allOp = json_decode($opInst);
//
//	$allOpInst = Array();
//	foreach ($allOp as $op) {
//	    $opTxt = R::load('operationoperations', $op);
//	    array_push($allOpInst, $opTxt);
//	}
//	$wifetypes = R::load('wifetypes', $getAllPatientData['wiftypeid']);
//	$this->smarty->assign('name', $getAllPatientData['wifename']);
//	$this->smarty->assign('title', $wifetypes->title);
//	$this->smarty->assign('statusNo', $getAllPatientData['statusno']);
//
//	$this->smarty->assign('patientData', $getAllPatientData);
//	$this->smarty->assign('mainObj', $mainObj);
//	$this->smarty->assign('p', $pcounter);
//	$this->smarty->assign('blodd', $blodd->title);
//	$this->smarty->assign('weeks', round($interval->days / 7));
//	$this->smarty->assign('hospitalName', $hospitalName);
//
//	$this->smarty->assign('opInst', $allOpInst);
//        $this->smarty->assign('instNotes', $instNotes);
//        $this->smarty->assign('patientId', $patientId);
//
//	$date = date("Y/m/d");
//	$this->smarty->assign('date', $date);
//
//	$ProgramesettingData = R::findone('programesetting');
//	$this->smarty->assign('ProgramesettingData', $ProgramesettingData);
//
//	$antUS = R::getRow('select placenta from mainantenentalus where deleted = ? and patientid = ? and mainantenatalid = ?  ORDER BY date DESC limit 1 ', [0, $patientId, $mainObj->id]);
//	$this->smarty->assign('antUS', $antUS);
//
//
//
//######################################################################################################################################################
//	## medical Diseases
//	$mypastmedical = R::findall('phpastmedical', 'del = ? and patientid = ? ', [0, $patientId]);
//	$allD = '';
//	foreach ($mypastmedical as $myallpastmedical) {
//	    $disease = R::getRow('select name from phpastmedicaldisease where id = ?', [$myallpastmedical->medicaldisease]);
//	    if ($disease['name'] && $disease['name'] != '') {
//		if ($allD != '') {
//		    $allD .= ' , ';
//		    $allD .= $disease['name'];
//		} else {
//		    $allD .= $disease['name'];
//		}
//	    }
//	}
//	$this->smarty->assign('mypastmedical', $allD);
//
//	######################################################################################################################################################
//	## medical Diseases
//	$mypastsurgical = R::findall('phpastsurgical', 'del = ? and patientid = ? ', [0, $patientId]);
//	$allS = '';
//	foreach ($mypastsurgical as $pastSurgical) {
//	    $operation = R::getRow('select name from phpastsurgicaloperation where id = ?', [$pastSurgical->surgicaloperation]);
//	    if ($operation['name'] && $operation['name'] != '') {
//		if ($allS != '') {
//		    $allS .= ' , ';
//		    $allS .= $operation['name'];
//		} else {
//		    $allS .= $operation['name'];
//		}
//	    }
//	}
//	$this->smarty->assign('mypastsurgical', $allS);
//
//	######################################################################################################################################################
//	## medical Diseases
//	$mypastgyna = R::findall('phpastgynecological', 'del = ? and patientid = ? ', [0, $patientId]);
//	$allG = '';
//	foreach ($mypastgyna as $gynacological) {
//	    $opG = R::getRow('select name from phpastgynecologicaloperation where id = ?', [$gynacological->gynecologicaloperation]);
//	    if ($opG['name'] && $opG['name'] != '') {
//		if ($allG != '') {
//		    $allG .= ' , ';
//		    $allG .= $opG['name'];
//		} else {
//		    $allG .= $opG['name'];
//		}
//	    }
//	}
//	$this->smarty->assign('mypastgyna', $allG);
//
//#############################################################################################################################################
//
//
//
//	$this->smarty->display('operations/printtransfer.html');
//    }
//
//    public function addnewhospital() {
//
//	//check login
//	autho::checkautho($this->hosturl);
//	//check authoize
//	autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);
//
//
//	//vars here
//	$name = filter_input(INPUT_POST, 'newitem');
//
//	### insert into table
//	$newtable = R::dispense('hospitalnames');
//	$newtable->name = $name;
//
//	$insertid = R::store($newtable);
//
//	echo $insertid;
//    }
//
//    public function editselect() {
//
//	//check login
//	autho::checkautho($this->hosturl);
//	//check authoize
//	autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);
//
//	## hospitals
//	$hospitalnames = R::findall('hospitalnames');
//	$this->smarty->assign('hospitalnames', $hospitalnames);
//	$this->smarty->display('operations/editmodel.html');
//    }
//
//    public function delhospital() {
//
//	//check login
//	autho::checkautho($this->hosturl);
//	//check authoize
//	autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);
//
//	//vars here
//	$id = filter_input(INPUT_POST, 'id');
//
//	### insert into table
//	$hospital = R::load('hospitalnames', $id);
//	R::trash($hospital);
//	echo 1;
//    }
//
//    public function allinst() {
//	$allInstructions = R::findall('operationoperations', 'deleted = 0 ');
//	$this->smarty->assign('alldata', $allInstructions);
//	$this->smarty->display('operations/editmodelinst.html');
//    }
//
//    public function addopinst() {
//
////        //check login
////        autho::checkautho($this->hosturl);
////        //check authoize
////        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);
//	//vars here
//	$name = filter_input(INPUT_POST, 'newitem');
//
//	//here the add
//	$opInst = R::dispense('operationoperations');
//
//	$opInst->insttext = $name;
//
//
//	try {
//	    $id = R::store($opInst);
//	    echo $id;
//	} catch (Exception $e) {
//	    header('Location:' . $this->hosturl . '/error/notadd/' . $this->controllname);
//	}
//    }
//
//    public function editopinst() {
//
////        //check login
////        autho::checkautho($this->hosturl);
////        //check authoize
////        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);
//	//vars here
//	$name = filter_input(INPUT_POST, 'newitem');
//
//	//here the add
//	$alldata = R::findAll('operationoperations', 'deleted = 0');
//
//	// print_r($mydata);
//
//	$this->smarty->assign('alldata', $alldata);
//
//	$this->smarty->display('operations/editmodelinst.html');
//    }
//
//    public function update() {
//
//	//check login
//	//autho::checkautho($this->hosturl);
//	//check authoize
//	//autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);
//	//vars here
//	$id = filter_input(INPUT_POST, "id");
//	$value = filter_input(INPUT_POST, "value");
//	$tableName = filter_input(INPUT_POST, "tableName");
//	$colName = filter_input(INPUT_POST, "colName");
//	$patientId = filter_input(INPUT_POST, "patientId");
//
//	//here the update
//	$mydata = R::load($tableName, $id);
//
//	$mydata->$colName = $value;
//	R::store($mydata);
//    }
//
//    public function delinst() {
//
//	$id = filter_input(INPUT_POST, "id");
//	$tableName = filter_input(INPUT_POST, "tableName");
//
//	$myData = R::load($tableName, $id);
//	$myData->deleted = 1;
//	$myid = R::store($myData);
//
//	echo 1;
//	//  header('Location:' . $this->hosturl . '/controllers/' . $this->controllname . '.php');
//    }
    // at the end of class call
    public function __destruct() {
        //colse connection
        R::close();
    }
}
