<?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');
        $this->smarty->assign('programesetting', $programesetting[0]);

        $programesetting2 = R::findAll('programesetting2');
        $this->smarty->assign('programesetting2', $programesetting2);

        $programesettingex = R::findOne('programesettingex');
        if (count($programesettingex) > 0) {
            
        } else {
            $programesettingex = R::dispense('programesettingex');
            $programesettingex->revisioned = 0;
            $programesettingex->id = R::store($programesettingex);
        }
        $this->smarty->assign('programesettingex', $programesettingex);

        $labSetting = R::findOne('programesettinglab');
        if (!(count($labSetting) > 0 )) {
            $labSetting = R::dispense('programesettinglab');
            $labSetting->lab_style = 1;
            R::store($labSetting);
        }
        $this->smarty->assign('labSetting', $labSetting);

        //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');
        $hideId = filter_input(INPUT_POST, 'hideId');
        $loginbar = filter_input(INPUT_POST, 'loginbar');
        $email = filter_input(INPUT_POST, 'email');
        $password = filter_input(INPUT_POST, 'password');
        $above2 = filter_input(INPUT_POST, 'above2');
        $buttom2 = filter_input(INPUT_POST, 'buttom2');
        $fontsizeen2 = filter_input(INPUT_POST, 'fontsizeen2');
        $fontsizear2 = filter_input(INPUT_POST, 'fontsizear2');
        $visittype = filter_input(INPUT_POST, 'visittype');
        $presquantity = filter_input(INPUT_POST, 'presquantity');
        $system_type = filter_input(INPUT_POST, 'system_type');
        $system_lang = filter_input(INPUT_POST, 'system_lang');
        $start_hour = filter_input(INPUT_POST, 'start_hour');
        $end_hour = filter_input(INPUT_POST, 'end_hour');
        $rooms = filter_input(INPUT_POST, 'rooms');
        $surgeon = filter_input(INPUT_POST, 'surgeon');
        $triggering = filter_input(INPUT_POST, 'triggering');
        $ovum_tab = filter_input(INPUT_POST, 'ovum_tab');
        $embro_tab = filter_input(INPUT_POST, 'embro_tab');
        $others_tab = filter_input(INPUT_POST, 'others_tab');
        $notes = filter_input(INPUT_POST, 'notes');
        $organizations = filter_input(INPUT_POST, 'organizations');
        $refer_doctor = filter_input(INPUT_POST, 'refer_doctor_sett');
        $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');
        $sexual = filter_input(INPUT_POST, 'sexual');
        $personal = filter_input(INPUT_POST, 'personal');
        $past_visits = filter_input(INPUT_POST, 'past_visits');
        $barcode_print = filter_input(INPUT_POST, 'barcode_print');
        $temp_patient = filter_input(INPUT_POST, 'temp_patient');
        $fin_doc_dept = filter_input(INPUT_POST, 'fin_doc_dept');
        $infertility = filter_input(INPUT_POST, 'infertility');
        $gynalmp = filter_input(INPUT_POST, 'gynalmp');
        $entrydetails = filter_input(INPUT_POST, 'entrydetails');
        $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');
        $case_summary = filter_input(INPUT_POST, 'case_summary');
        $national_id_req = filter_input(INPUT_POST, 'national_id_req');
        $confirm_attend = filter_input(INPUT_POST, 'confirm_attend');
        $birth_notify = filter_input(INPUT_POST, 'birth_notify');
        $visit_phones = filter_input(INPUT_POST, 'visit_phones');
        $voice_record = filter_input(INPUT_POST, 'voice_record');
        $whatsapp_sms = filter_input(INPUT_POST, 'whatsapp_sms');
        $rfc = filter_input(INPUT_POST, 'rfc');
        $whatsapp_txt = filter_input(INPUT_POST, 'whatsapp_txt');
        $past_search = filter_input(INPUT_POST, 'past_search');
        $diagnos_ant = filter_input(INPUT_POST, 'diagnos_ant');
        $diagnos_gyna = filter_input(INPUT_POST, 'diagnos_gyna');
        $attend_today = filter_input(INPUT_POST, 'attend_today');
        $search_result_no = (filter_input(INPUT_POST, 'search_result_no') > 0) ? filter_input(INPUT_POST, 'search_result_no') : 25;
        $search_letters_no = (filter_input(INPUT_POST, 'search_letters_no') > 0) ? filter_input(INPUT_POST, 'search_letters_no') : 1;
        $printserial = filter_input(INPUT_POST, 'printserial');
        $branches = filter_input(INPUT_POST, 'branches');
        $tanks_view = filter_input(INPUT_POST, 'tanks_view');
        $regions = filter_input(INPUT_POST, 'regions');
        $advance_payment = filter_input(INPUT_POST, 'advance_payment');
        $visit_period = filter_input(INPUT_POST, 'visit_period');

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

        //select table siteinfo data to order by id
        $programesetting = R::findAll('programesetting', 'order by id desc');
        if (count($programesetting) > 0) {
            //update current
            //get id vars in update statution
            $id = filter_input(INPUT_POST, 'id');
            $backgroundImageValue = filter_input(INPUT_POST, 'backgroundImageValue');
            if ($backgroundImageValue == 1) {
                $background = uploadupdate('bgfile', 'bimageurl', False, 0, 0, 'background');
            } else {
                $background = '';
            }

            $pres_headerImageValue = filter_input(INPUT_POST, 'pres_headerImageValue');
            if ($pres_headerImageValue == 1) {
                $pres_header = uploadupdate('pres_header', 'pres_headerurl', False, 0, 0, 'background');
            } else {
                $pres_header = '';
            }

            $pres_footerImageValue = filter_input(INPUT_POST, 'pres_footerImageValue');
            if ($pres_footerImageValue == 1) {
                $pres_footer = uploadupdate('pres_footer', 'pres_footerurl', False, 0, 0, 'background');
            } else {
                $pres_footer = '';
            }

            $lab_headerImageValue = filter_input(INPUT_POST, 'lab_headerImageValue');
            if ($lab_headerImageValue == 1) {
                $lab_header = uploadupdate('lab_header', 'lab_headerurl', False, 0, 0, 'background');
            } else {
                $lab_header = '';
            }

            $lab_footerImageValue = filter_input(INPUT_POST, 'lab_footerImageValue');
            if ($lab_footerImageValue == 1) {
                $lab_footer = uploadupdate('lab_footer', 'lab_footerurl', False, 0, 0, 'background');
            } else {
                $lab_footer = '';
            }

            $programesetting = R::load('programesetting', $id);
        } else {
            //insert new
            $background = uploadnew('bgfile', False, 0, 0, 'background');
            $pres_header = uploadnew('pres_header', False, 0, 0, 'background');
            $pres_footer = uploadnew('pres_footer', False, 0, 0, 'background');
            $lab_header = uploadnew('lab_header', False, 0, 0, 'background');
            $lab_footer = uploadnew('lab_footer', False, 0, 0, 'background');
            $programesetting = R::dispense('programesetting');
        }

        $programesetting->tanks_view = $tanks_view;
        $programesetting->attend_today = $attend_today;
        $programesetting->search_result_no = $search_result_no;
        $programesetting->search_letters_no = $search_letters_no;
        $programesetting->past_search = $past_search;
        $programesetting->diagnos_ant = $diagnos_ant;
        $programesetting->diagnos_gyna = $diagnos_gyna;
        $programesetting->case_summary = $case_summary;
        $programesetting->national_id_req = $national_id_req;
        $programesetting->confirm_attend = $confirm_attend;
        $programesetting->birth_notify = $birth_notify;
        $programesetting->whatsapp_txt = $whatsapp_txt;
        $programesetting->organizations = $organizations;
        $programesetting->surgeon = $surgeon;
        $programesetting->triggering = $triggering;
        $programesetting->ovum_tab = $ovum_tab;
        $programesetting->embro_tab = $embro_tab;
        $programesetting->others_tab = $others_tab;
        $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->sexual = $sexual;
        $programesetting->personal = $personal;
        $programesetting->rooms = $rooms;
        $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->loginbar = $loginbar;
        $programesetting->visittype = $visittype;
        $programesetting->presquantity = $presquantity;
        $programesetting->above2 = $above2;
        $programesetting->buttom2 = $buttom2;
        $programesetting->fontsizeen2 = $fontsizeen2;
        $programesetting->fontsizear2 = $fontsizear2;
        $programesetting->background = $background;
        $programesetting->temp_patient = $temp_patient;
        $programesetting->system_lang = $system_lang;
        $programesetting->system_type = $system_type;
        $programesetting->start_hour = $start_hour;
        $programesetting->end_hour = $end_hour;
        $programesetting->barcode_print = $barcode_print;
        $programesetting->head = $head;
        $programesetting->chest = $chest;
        $programesetting->abdomen = $abdomen;
        $programesetting->pelvis = $pelvis;
        $programesetting->extremitis = $extremitis;
        $programesetting->infertility = $infertility;
        $programesetting->gynalmp = $gynalmp;
        $programesetting->entrydetails = $entrydetails;
        $programesetting->visit_phones = $visit_phones;
        $programesetting->voice_record = $voice_record;
        $programesetting->whatsapp_sms = $whatsapp_sms;
        $programesetting->rfc = $rfc;
        $programesetting->refer_doctor = $refer_doctor;
        $programesetting->fin_doc_dept = $fin_doc_dept;
        $programesetting->printserial = $printserial;
        $programesetting->branches = $branches;
        $programesetting->regions = $regions;
        $programesetting->advance_payment = $advance_payment;
        $programesetting->visit_period = $visit_period;

        $programesetting->pres_header = $pres_header;
        $programesetting->pres_footer = $pres_footer;
        $programesetting->lab_header = $lab_header;
        $programesetting->lab_footer = $lab_footer;

        try {
            R::store($programesetting);
            $programesettingex = R::findOne('programesettingex');

            $programesetting2 = R::findAll('programesetting2');
            foreach ($programesetting2 as $setting) {
                $columName = $setting->colum_name;
                //   $value = filter_input(INPUT_POST, '' . $columName);
                $value = (filter_input(INPUT_POST, '' . $columName) > 0) ? filter_input(INPUT_POST, '' . $columName) : 0;
                $programesettingex->$columName = $value;
            }
            R::store($programesettingex);
            header('Location:' . $this->hosturl . '/controllers/' . $this->controllname . '.php');
        } catch (Exception $e) {
            //print_r($e);
            header('Location:' . $this->hosturl . '/error/notadd/' . $this->controllname);
        }
    }

    public function lab() {

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

        $labSetting = R::findOne('programesettinglab');
        if (!(count($labSetting) > 0 )) {
            $labSetting = R::dispense('programesettinglab');
            $labSetting->lab_style = 1;
            R::store($labSetting);
        }
        $this->smarty->assign('labSetting', $labSetting);

        $setting = R::findOne('programesetting');
        if (!(count($setting) > 0 )) {
            $setting = R::dispense('programesetting');
            $setting->centername = '';
            R::store($setting);
        }
        $this->smarty->assign('setting', $setting);

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

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