<?php

require_once("imp/_imp.php");

class Controllers {

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

// 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 = 'programesetting';
        $this->shcontrollname = 'Programesetting';
        $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] = 'programesetting'; //controller name or tab name
        $active[1] = 'programesetting'; //controller name

        $this->active = $active;



        #include upload function
        require_once("../../_library/plugins/classupload/uploadfunctions.php");

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

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

    public function index() {

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

        //select table programesetting data to order by id
        $programesetting = R::getAll('select * from programesetting order by id desc');

        //send info to html
        $this->smarty->assign('programesetting', $programesetting[0]);

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

    public function addit() {

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


        //get all vars
        $color = filter_input(INPUT_POST, 'color');

        $left = filter_input(INPUT_POST, 'left');
        $right = filter_input(INPUT_POST, 'right');
        $center = filter_input(INPUT_POST, 'center');

        $centername = filter_input(INPUT_POST, 'centername');
        $doctorname = filter_input(INPUT_POST, 'doctorname');
        $address = filter_input(INPUT_POST, 'address');
        $phone = filter_input(INPUT_POST, 'phone');
        $spacial = filter_input(INPUT_POST, 'spacial');
        $gynalink = filter_input(INPUT_POST, 'gynalink');
        $antenatal = filter_input(INPUT_POST, 'antenatal');
        $fontsizeen = filter_input(INPUT_POST, 'fontsizeen');
        $fontsizear = filter_input(INPUT_POST, 'fontsizear');
        $alignttt = filter_input(INPUT_POST, 'alignttt');
        $ultrasound = filter_input(INPUT_POST, 'ultrasound');
        $patientnoaut = filter_input(INPUT_POST, 'patientnoaut');
        $addshours = filter_input(INPUT_POST, 'addshours');
        $above = filter_input(INPUT_POST, 'above');
        $buttom = filter_input(INPUT_POST, 'buttom');
        $ishide = filter_input(INPUT_POST, 'ishide');
        $simpleView = filter_input(INPUT_POST, 'simpleview');
        $displayDoctorName = filter_input(INPUT_POST, 'displaydoctorname');
        $entryorder = filter_input(INPUT_POST, 'entryorder');
        $investshow = filter_input(INPUT_POST, 'investshow');
        $prescriptioncashing = filter_input(INPUT_POST, 'prescriptioncashing');
        $visitsform = filter_input(INPUT_POST, 'visitsform');
        $diagnosshow = filter_input(INPUT_POST, 'diagnosshow');
        $backupdriver = filter_input(INPUT_POST, 'backupdriver');
        $phppath = filter_input(INPUT_POST, 'phppath');
        $hideId = filter_input(INPUT_POST, 'hideId');
        $email = filter_input(INPUT_POST, 'email');
        $password = filter_input(INPUT_POST, 'password');
        $balance = filter_input(INPUT_POST, 'balance');

        $id = filter_input(INPUT_POST, 'id');




        $head = filter_input(INPUT_POST, 'head');
        $chest = filter_input(INPUT_POST, 'chest');
        $abdomen = filter_input(INPUT_POST, 'abdomen');
        $pelvis = filter_input(INPUT_POST, 'pelvis');
        $extremitis = filter_input(INPUT_POST, 'extremitis');
        $btype = filter_input(INPUT_POST, 'btype');
        $hbtype = filter_input(INPUT_POST, 'hbtype');
        $risk = filter_input(INPUT_POST, 'risk');
        $notes = filter_input(INPUT_POST, 'notes');
        $menstrual = filter_input(INPUT_POST, 'menstrual');
        $contraception = filter_input(INPUT_POST, 'contraception');
        $obstetric = filter_input(INPUT_POST, 'obstetric');
        $past = filter_input(INPUT_POST, 'past');
        $family = filter_input(INPUT_POST, 'family');
        $medical = filter_input(INPUT_POST, 'medical');
        $surgical = filter_input(INPUT_POST, 'surgical');
        $gynecological = filter_input(INPUT_POST, 'gynecological');
        $art = filter_input(INPUT_POST, 'art');

        $art_external = filter_input(INPUT_POST, 'art_external');
        $art_internal = filter_input(INPUT_POST, 'art_internal');

        $entrydetails = filter_input(INPUT_POST, 'entrydetails');
        $gynalmp = filter_input(INPUT_POST, 'gynalmp');
        $namefontsize = filter_input(INPUT_POST, 'namefontsize');

        $programview = filter_input(INPUT_POST, 'programview');
        $patient_doctor = filter_input(INPUT_POST, 'patient_doctor');


        $infertility = filter_input(INPUT_POST, 'infertility');
        $visit_period = filter_input(INPUT_POST, 'visit_period');
        $print_diag = filter_input(INPUT_POST, 'print_diag');
        $previous_marriage = filter_input(INPUT_POST, 'previous_marriage');
        $branches = filter_input(INPUT_POST, 'branches');
        $printserial = filter_input(INPUT_POST, 'printserial');
          $visit_sms = filter_input(INPUT_POST, 'visit_sms');


        //select table siteinfo data to order by id
        $programesetting = R::findAll('programesetting', 'order by id desc');


        if (count($programesetting) > 0) {
            //update current
            $id = filter_input(INPUT_POST, 'id');
            $programesetting = R::load('programesetting', $id);
            $backgroundImageValue = filter_input(INPUT_POST, 'backgroundImageValue');

            if ($backgroundImageValue == 1) {

                if ($_FILES['bgfile']['size'] > 0 && $_FILES['bgfile']['error'] == 0) {
                    // background is not  empty and not an error 
                    $background = uploadupdate('bgfile', 'bimageurl', False, 0, 0, 'background');
                    $programesetting->background = $background;
                }
            } else {
                $background = '';
                $programesetting->background = $background;
            }
            #####################################

            if ($_FILES['gtimage']['size'] > 0 && $_FILES['gtimage']['error'] == 0) {
                // gtimage is not  empty and not an error 
                $gtimage = uploadupdate('gtimage', 'gtimageold', true, 1164, 476, 'gtimage');
                $programesetting->gtimage = $gtimage;
            }
            #####################################
        } else {
            //insert new
            $background = uploadnew('bgfile', False, 0, 0, 'background');
            $programesetting = R::dispense('programesetting');
            $programesetting->background = $background;
            $gtimage = uploadnew('gtimage', true, 1164, 476, 'gtimage');
            $programesetting->gtimage = $gtimage;
            #####################################
        }


        $programesetting->phppath = $phppath;
        $programesetting->backupdriver = $backupdriver;
        $programesetting->color = $color;
        $programesetting->above = $above;
        $programesetting->buttom = $buttom;

        $programesetting->pleft = $left;
        $programesetting->pright = $right;
        $programesetting->center = $center;
        $programesetting->centername = $centername;
        $programesetting->doctorname = $doctorname;
        $programesetting->address = $address;
        $programesetting->phone = $phone;
        $programesetting->spacial = $spacial;

        $programesetting->gynalink = $gynalink;
        $programesetting->antenatal = $antenatal;
        $programesetting->fontsizeen = $fontsizeen;
        $programesetting->fontsizear = $fontsizear;

        $programesetting->alignttt = $alignttt;
        $programesetting->ultrasound = $ultrasound;
        $programesetting->addshours = $addshours;
        $programesetting->patientnoaut = $patientnoaut;

        $programesetting->ishide = $ishide;
        $programesetting->simpleview = $simpleView;
        $programesetting->displaydoctorname = $displayDoctorName;

        $programesetting->entryorder = $entryorder;
        $programesetting->investshow = $investshow;
        $programesetting->diagnosshow = $diagnosshow;
        $programesetting->prescriptioncashing = $prescriptioncashing;
        $programesetting->visitsform = $visitsform;
        $programesetting->hideid = $hideId;
        $programesetting->email = $email;
        $programesetting->password = $password;
        $programesetting->balance = $balance;
        $programesetting->head = $head;
        $programesetting->chest = $chest;
        $programesetting->abdomen = $abdomen;
        $programesetting->pelvis = $pelvis;
        $programesetting->extremitis = $extremitis;
        $programesetting->btype = $btype;
        $programesetting->hbtype = $hbtype;
        $programesetting->risk = $risk;
        $programesetting->notes = $notes;
        $programesetting->menstrual = $menstrual;
        $programesetting->contraception = $contraception;
        $programesetting->obstetric = $obstetric;
        $programesetting->past = $past;
        $programesetting->family = $family;
        $programesetting->medical = $medical;
        $programesetting->surgical = $surgical;
        $programesetting->gynecological = $gynecological;
        $programesetting->art = $art;

        $programesetting->art_internal = $art_internal;
        $programesetting->art_external = $art_external;

        $programesetting->entrydetails = $entrydetails;
        $programesetting->gynalmp = $gynalmp;
        $programesetting->namefontsize = $namefontsize;

        $programesetting->programview = $programview;
        $programesetting->patient_doctor = $patient_doctor;

        $programesetting->infertility = $infertility;
        $programesetting->visit_period = $visit_period;
        $programesetting->print_diag = $print_diag;
        $programesetting->previous_marriage = $previous_marriage;
        $programesetting->branches = $branches;
        $programesetting->printserial = $printserial;
        $programesetting->visit_sms = $visit_sms;

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

    // at the end of class call
    public function __destruct() {
        //close connection
        R::close();
    }

}
