<?php
    class CONTROLLER extends MY_DHTMLX
    {
        public function FN_GETVALIDCOMBO($DBNAME=null)
        {
            if(!$DBNAME){$DBNAME=DB_APORTIL;}
            $id=$_REQUEST['id'];$fieldcriteria="id";
            if($_REQUEST['fieldcriteria']){$fieldcriteria=$_REQUEST['fieldcriteria'];}
            
            $tablename=$_REQUEST['tablename'];
            $sptablename=explode(".",$tablename);
            $OBJTABLE="$DBNAME.".$tablename;
            if(count($sptablename)>0){$OBJTABLE=$tablename;}
            $validationvalue=$_REQUEST['validationvalue'];
            $qry="select id from  $OBJTABLE where $fieldcriteria='$id'";
            //echo $qry;return false;
            if(!$this->fn_row_isexists($qry))
            {
                echo "failed|Mohon Maaf Kode <b>".$validationvalue."</b> tidak ditemukan. TERIMA KASIH";return false;
                //echo "failed|$qry";return false;
            }            
            echo "ok|";
        }
        
        
        public function FN_VALIDINPUTVALUE($DBNAME=null)
        {
            if(!$DBNAME){$DBNAME=DB_APORTIL;}
            $tablename=$_REQUEST['tableobj'];
            $fieldname=$_REQUEST['fields'];
            $fieldvalue=$_REQUEST['fieldvalue'];
            $OBJTABLE="$DBNAME.".$tablename;
            //echo $OBJTABLE." ".$fields;return false;
            //if(count($sptablename)>0){$OBJTABLE=$tablename;}
            //$validationvalue=$_REQUEST['validationvalue'];
            $qry="select $fieldname from  $OBJTABLE where $fieldname='$fieldvalue' and isdeleted=0";
            //echo $qry;return false;
            if($this->fn_row_isexists($qry))
            {
                echo "failed|Mohon Maaf Kode <b>".$fieldvalue."</b> sudah pernah ditambahkan. TERIMA KASIH";return false;
            }            
            echo "ok|";    
        }
        
        public function FN_GETFIELDVALUE()
        {
            $getfield=$_REQUEST['getfield'];
            $tablename=$_REQUEST['tableobj'];
            $swhere=$_REQUEST['swhere'];
            $sp1=explode("|",$swhere);
            $fswhere=$sp1[0];$valswhere=$sp1[1];
            $qry="select $getfield from  ".DB_APORTIL.".$tablename where $fswhere='$valswhere' and isdeleted=0 order by id desc limit 1";
            //echo $qry;
            if(!$this->fn_row_isexists($qry))
            {
                echo "Mohon Maaf Kode <b>".$valswhere."</b> tidak ditemukan / Kode kosong. TERIMA KASIH";return false;
            }         
            $rtnval=$this->fn_get_fieldvalue("$getfield",$qry);   
            echo $rtnval;    
            
        }
        
        public function FN_GETFIELDSVALUE()
        {
            $getfield=$_REQUEST['getfield'];
            $tablename=$_REQUEST['tableobj'];
            $swhere=$_REQUEST['swhere'];
            $sp1=explode("|",$swhere);
            $fswhere=$sp1[0];$valswhere=$sp1[1];
            $qry="select $getfield from  ".DB_APORTIL.".$tablename where $fswhere='$valswhere' and isdeleted=0 order by id desc limit 1";
            //echo $qry;
            if(!$this->fn_row_isexists($qry))
            {
                echo "Mohon Maaf Kode <b>".$valswhere."</b> tidak ditemukan / Kode kosong. TERIMA KASIH";return false;
            }         
            $sp=$this->fn_get_fieldsvalues("$getfield",$qry);$rtnval="";
            for($i=0;$i<count($sp);$i++)
            {
                $rtnval.=$sp[$i]."|";
            }
            $rtnval=substr($rtnval,0,strlen($rtnval)-1);
            echo $rtnval;    
            
        }
        
        public function FN_GETINFOTABLE2()
        {
            $result="";
            $id=$_POST['id'];
            $vartablename=$_POST['tablename'];
            $tablename=$vartablename;
            $DBNAME=DB_APORTIL;
            $sptbl=explode(".",$vartablename);
            if(count($sptbl)>1){$DBNAME=$sptbl[0];$tablename=$sptbl[1];}
            
            $fields=$_POST['fields'];
            $spfields=explode(",","id,".$fields);
            $fields="";
            for($xf=0;$xf<count($spfields);$xf++)
            {
                if($xf>0) // === exception id column
                {
                    $s=explode("|",$spfields[$xf]);
                    if(count($s)>1)
                    {
                        $spfields[$xf]="concat(".$s[0].",',',".$s[1].") f".$xf;//$fields.="f".$xf.",";
                        // === check link to another table for feed
                        $ss=explode(":",$s[1]);
                        if(count($ss)>1)
                        {
                            $qry="select f.".$ss[1]." from $ss[0] f inner join ".$DBNAME.".$tablename p on f.id=p.$s[0] where p.id='$id'";
                            //echo "ok|".$qry;return false;
                            $textcombo=$this->fn_get_fieldvalue($ss[1],$qry);
                            $spfields[$xf]="concat(".$s[0].",',','".$textcombo."') f".$xf;
                        }                        
                        // ========================================
                        $fields.="f".$xf.",";
                    }else{$fields.=$spfields[$xf].",";}
                    $cfields.=$spfields[$xf].",";
                }
            }
            if(substr($cfields,strlen($cfields)-1,1)==','){$cfields=substr($cfields,0,strlen($cfields)-1);}
            if(substr($fields,strlen($fields)-1,1)==','){$fields=substr($fields,0,strlen($fields)-1);}
            $qry="select id,$cfields,iby from ".$DBNAME.".$tablename where id='$id'";
            //echo "ok|".$qry." ".$fields;return false;
            //echo "ok|".$qry;return false;
            // === Validation User Input
            //$iby=$this->fn_get_fieldvalue("iby",$qry);//echo "failed|$iby";return false;
//            if($_SESSION['nik']!==$iby&&$_SESSION['idlevel']<=1)
//            {
//                $username=$this->fn_get_fieldvalue("namaemployee","select namaemployee from ".DB_SIMAS.".tbl_employee where nik='$iby'");
//                $result="failed|<b>Mohon Maaf</b> Row Data ini hanya bisa diedit oleh user <b>$username</b> !.TERIMA KASIH";
//                echo $result;return false;
//            }
            //echo $qry;return false;
            $rs=$this->selectquery($qry);
            if($rs)
            {
                //echo "failed|".mysql_error();return false;
                $sp=$this->fn_get_fieldsvalues("id,".$fields,$qry);
                //echo "ok|".$fields;return false;
                //$sp=$this->fn_get_fieldsvalues($fields,$qry);
                for($i=0;$i<count($sp);$i++)
                {
                    //if(strtotime($sp[$i])&&$_REQUEST['fielddatetime'])
                    if($_REQUEST['fielddatetime'])
                    {                    
                        $formatdate="Y-m-d";
                        if(isset($_REQUEST['fielddatetime']))
                        {                     
                            $spfdate=explode("|",trim($_REQUEST['fielddatetime']));
                            //$r="f";
                            if(count($spfdate)>1)
                            {   
                                //$j=0;   
                                for($j=0;$j<count($spfdate);$j++)
                                {
                                    if(trim($spfields[$i])==trim($spfdate[$j])){$formatdate="Y-m-d H:i";}
                                }    
                            }else{if($spfields[$i]==$spfdate[0]){$formatdate="Y-m-d H:i";}}
                            //$dtInfo=date_parse($sp[$i]);
                            //if($dtInfo['warning_count']==0&&$dtInfo['error_count']==0){$sp[$i]=date("$formatdate",strtotime($sp[$i]));}
                            if(preg_match("/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/i",$sp[$i])){$sp[$i]=date("$formatdate",strtotime($sp[$i]));}
                        }
                        
                    }
                    $val=$sp[$i];
                    if($sp[$i]=='0000-00-00 00:00:00')
                    {
                        $val='';
                    }
                    //if(date("Y-m-d",strtotime($sp[$i]))=='1970-01-01'){$val='';}
                    //if(!$sp[$i]){$val='';}
                    //$result.=trim($sp[$i])."|";
                    $result.=trim($val)."|";
                }
                $result=substr($result,0,strlen($result)-1);
                echo $result;    
            }
            else
            {echo "failed|".mysql_error();return false;}
        }
        
        public function is_date( $str ) 
        {
            try {
                $dt = new DateTime( trim($str) );
            }
            catch( Exception $e ) {
                return false;
            }
            $month = $dt->format('m');
            $day = $dt->format('d');
            $year = $dt->format('Y');
            if( checkdate($month, $day, $year) ) {
                return true;
            }
            else {
                return false;
            }
        }
            
        public function FN_GETINFOTABLE2_old()
        {
            $result="";
            $id=$_POST['id'];
            $tablename=$_POST['tablename'];
            $fields=$_POST['fields'];
            $spfields=explode(",","id,".$fields);
            $fields="";
            for($xf=0;$xf<count($spfields);$xf++)
            {
                //if($xf==0){$fields=$spfields[$xf];}
                if($xf>0)
                {
                    $s=explode("|",$spfields[$xf]);
                    if(count($s)>1){$spfields[$xf]="concat(".$s[0].",',',".$s[1].") f".$xf;$fields.="f".$xf.",";}else{$fields.=$spfields[$xf].",";}
                    $cfields.=$spfields[$xf].",";
                }
            }
            if(substr($cfields,strlen($cfields)-1,1)==','){$cfields=substr($cfields,0,strlen($cfields)-1);}
            if(substr($fields,strlen($fields)-1,1)==','){$fields=substr($fields,0,strlen($fields)-1);}
            $qry="select id,$cfields,iby from ".DB_APORTIL.".$tablename where id='$id'";
            //echo "ok|".$qry." ".$fields;return false;
            // === Validation User Input
            $iby=$this->fn_get_fieldvalue("iby",$qry);//echo "failed|$iby";return false;
            //if($tablename!=='tbl_jenisbiayalainsmu')
//            {
//                if($_SESSION['nik']!==$iby&&$_SESSION['idlevel']<=1)
//                {
//                    $username=$this->fn_get_fieldvalue("namaemployee","select namaemployee from ".DB_APORTIL.".tbl_userlogin where nik='$iby'");
//                    $result="failed|<b>Mohon Maaf</b> Row Data ini hanya bisa diedit oleh user <b>$username</b> !.TERIMA KASIH";
//                    echo $result;return false;
//                }                
//            }
            //echo $qry;return false;
            $rs=$this->selectquery($qry);
            if($rs)
            {
                //echo "failed|".mysql_error();return false;
                $sp=$this->fn_get_fieldsvalues("id,".$fields,$qry);
                //$sp=$this->fn_get_fieldsvalues($fields,$qry);
                for($i=0;$i<count($sp);$i++)
                {
                    //if(strtotime($sp[$i])&&$_REQUEST['fielddatetime'])
                    if($_REQUEST['fielddatetime'])
                    {                    
                        $formatdate="Y-m-d";
                        if(isset($_REQUEST['fielddatetime']))
                        {                     
                            $spfdate=explode("|",trim($_REQUEST['fielddatetime']));
                            //$r="f";
                            if(count($spfdate)>1)
                            {   
                                //$j=0;   
                                for($j=0;$j<count($spfdate);$j++)
                                {
                                    if(trim($spfields[$i])==trim($spfdate[$j])){$formatdate="Y-m-d H:i";}
                                }    
                            }else{if($spfields[$i]==$spfdate[0]){$formatdate="Y-m-d H:i";}}
                            //$dtInfo=date_parse($sp[$i]);
                            //if($dtInfo['warning_count']==0&&$dtInfo['error_count']==0){$sp[$i]=date("$formatdate",strtotime($sp[$i]));}
                            if(preg_match("/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/i",$sp[$i])){$sp[$i]=date("$formatdate",strtotime($sp[$i]));}
                        }
                        
                    }
                    $result.=trim($sp[$i])."|";
                }
                $result=substr($result,0,strlen($result)-1);
                echo $result;    
            }else{echo "failed|".mysql_error();return false;}
        }
        
        public function FN_GETINFOTABLE()
        {
            $result="";
            $id=$_POST['id'];
            $tablename=$_POST['tablename'];
            $fields=$_POST['fields'];
            $spfields=explode(",","id,".$fields);
            $qry="select id,$fields,iby from ".DB_APORTIL.".$tablename where id='$id'";
            // === Validation User Input
            $iby=$this->fn_get_fieldvalue("iby",$qry);
            if($_SESSION['nik']!==$iby&&$_SESSION['idlevel']<=1)
            {
                $username=$this->fn_get_fieldvalue("namaemployee","select namaemployee from ".DB_APORTIL.".tbl_userlogin where nik='$iby'");
                $result="failed|<b>Mohon Maaf</b> Row Data ini hanya bisa diedit oleh user <b>$username</b> !.TERIMA KASIH";
                //echo $result;return false;
            }
            //echo $qry;return false;
            $rs=$this->selectquery($qry);
            if($rs)
            {
                //echo "failed|".mysql_error();return false;
                $sp=$this->fn_get_fieldsvalues("id,".$fields,$qry);
                for($i=0;$i<count($sp);$i++)
                {
                    //if(strtotime($sp[$i])&&$_REQUEST['fielddatetime'])
                    if($_REQUEST['fielddatetime'])
                    {                    
                        $formatdate="Y-m-d";
                        if(isset($_REQUEST['fielddatetime']))
                        {                     
                            $spfdate=explode("|",trim($_REQUEST['fielddatetime']));
                            //$r="f";
                            if(count($spfdate)>1)
                            {   
                                //$j=0;   
                                for($j=0;$j<count($spfdate);$j++)
                                {
                                    if(trim($spfields[$i])==trim($spfdate[$j])){$formatdate="Y-m-d H:i";}
                                }    
                            }else{if($spfields[$i]==$spfdate[0]){$formatdate="Y-m-d H:i";}}
                            //$dtInfo=date_parse($sp[$i]);
                            //if($dtInfo['warning_count']==0&&$dtInfo['error_count']==0){$sp[$i]=date("$formatdate",strtotime($sp[$i]));}
                            if(preg_match("/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/i",$sp[$i])){$sp[$i]=date("$formatdate",strtotime($sp[$i]));}
                        }
                        
                    }
                    $result.=trim($sp[$i])."|";
                }
                $result=substr($result,0,strlen($result)-1);
                echo $result;    
            }else{echo "failed|".mysql_error();return false;}
        }
    }
    
    $cl=new CONTROLLER();
    
    if(isset($_REQUEST['task'])){$case=$_REQUEST['task'];}
    if(isset($_POST['task'])){$case=$_POST['task'];}

    if($_REQUEST){if($_REQUEST['sec']){$case=$cl->dec($case);}}

    //echo $case;
    
    switch($case)
    {        
        case $case:
            $task=strtoupper("fn_".$case);
            if(method_exists($cl,$task)){$cl->$task();}
        break;
        //case "getvalidcombo":
//            $cl->FN_GETVALIDCOMBO();
//        break;
//        
//        case "dhtmlxaddcombo":
//            $cl->FN_DHTMLXADDCOMBO();
//        break;
//        
//        case "validinputvalue":
//            $cl->FN_VALIDINPUTVALUE();
//        break;

//        case "contextmenu2":
//            $cl->FN_DHTMLXGRIDCONTEXTMENU2();
//        break;
//        
//        case "contxtmenustruc":
//            $cl->FN_DHTMLXCONTEXTMENUSTRUCTURE();
//        break;
    }
?>
