<?php
    class DATA_USER extends DATA
    {
        public function data_user_javainit()
        {

        }

        public function fn_user_init()
        {
            if(isset($_REQUEST['task'])){$task=trim(strtolower($_REQUEST['task']));}
            if(isset($_POST['task'])){$task=trim(strtolower($_POST['task']));}
            if($_REQUEST||$_POST){if($_REQUEST['sec']||$_POST['sec']){$task=$this->dec($task);}}

            switch($task)
            {
                case "default":
                    $this->fn_default_user();
                break;

                case $task:
                    $task="fn_".$task;
                    if(method_exists($this,$task)){$this->$task();}
                break;

                default:
                    $this->fn_default_user();
                break;
            }
        }
        public function fn_default_user()
        {
            echo $this->FN_CREATEHEADER();

            echo "<style>
                      .besidebtnsave {margin-left:104px;margin-top:-6px;position:absolute;}
                      .besidebtnupdate {margin-left:208px;margin-top:-6px;position:absolute;}
                      .besidebtnsubmit {margin-left:292px;margin-top:-6px;position:absolute;}
                      .besidebtndelete {margin-left:308px;margin-top:-6px;position:absolute;}
                  </style>";
            echo "<script>
                       var myForm,myFormsearch,dguser;
                       function initObject()
                       {";
                            $formdata=array(
                                              array("type"=>"settings","position"=>"label-left","labelWidth"=>"180","inputWidth"=>"120"),
                                              array("type"=>"block","width"=>"500",
                                                     "list"=>array(
                                                                      array("type"=>"hidden","name"=>"txt_id"),
                                                                      array("type"=>"input","name"=>"txt_nik","label"=>"Nik","required"=>"true","info"=>"true","inputWidth"=>"110","value"=>"",
                                                                            "tooltip"=>"Masukkan Nik first. Obrigado","validate"=>"NotEmpty","maxLength"=>"20","className"=>"txtcenter"),
                                                                      array("type"=>"input","name"=>"txt_employeename","label"=>"Employee Name","required"=>"true","info"=>"true","inputWidth"=>"280","value"=>"",
                                                                            "tooltip"=>"Masukkan Employee Name first. Obrigado","validate"=>"NotEmpty","maxLength"=>"80","className"=>"txtleft"),
                                                                      array("type"=>"input","name"=>"txt_employeetitle","label"=>"Title","required"=>"true","info"=>"true","inputWidth"=>"280","value"=>"",
                                                                            "tooltip"=>"Masukkan Title first. Obrigado","validate"=>"NotEmpty","maxLength"=>"100","className"=>"txtleft"),
                                                                      array("type"=>"combo","name"=>"cbo_idsection","label"=>"Section","required"=>"true","info"=>"true","inputWidth"=>"110","value"=>"0",
                                                                            "tooltip"=>"Pilih Section first. Obrigado","validate"=>"NotEmpty","maxLength"=>"6","className"=>"txtleft"),                                                                      
                                                                      array("type"=>"input","name"=>"txt_msisdn","label"=>"MSISDN","required"=>"true","info"=>"true","inputWidth"=>"160","value"=>"",
                                                                            "tooltip"=>"Masukkan MSISDN first. Obrigado","validate"=>"NotEmpty,ValidNumeric","maxLength"=>"8","className"=>"txtcenter"),
                                                                      array("type"=>"input","name"=>"txt_email","label"=>"Email","required"=>"true","info"=>"true","inputWidth"=>"160","value"=>"",
                                                                            "tooltip"=>"Masukkan Email first. Obrigado","validate"=>"NotEmpty","maxLength"=>"80","className"=>"txtleft"),
                                                                      array("type"=>"button","name"=>"btnsave","value"=>"Save","className"=>"button_update","offsetLeft"=>"20","offsetTop"=>"15"),
                                                                      array("type"=>"button","name"=>"btnupdate","value"=>"Update","className"=>"button_saveas besidebtnsave","offsetLeft"=>"10","disabled"=>"1"),
                                                                      //array("type"=>"button","name"=>"btnsubmit","value"=>"Submit","className"=>"button_submit besidebtnupdate","offsetLeft"=>"0","disabled"=>"1"),
                                                                      array("type"=>"button","name"=>"btndelete","value"=>"Delete","className"=>"button_delete besidebtnupdate","offsetLeft"=>"10","disabled"=>"1"),
                                                                      array("type"=>"button","name"=>"btncancel","value"=>"Cancel","className"=>"button_cancel besidebtndelete","offsetLeft"=>"10","disabled"=>"0")
                                                     )
                                              )
                                       );

                            $this->FN_DHTMLXFORM("myForm","divusernew",$formdata);

                            $urlcbo="fn_dhtmlxloadobject('".$this->enc("obj_controller")."','obj=".$this->enc("-")."&task=".$this->enc("dhtmlxcbofilter")."&tableobj=tbl_employee_section|id,sectionname')";
                            $this->FN_COMBOVALIDATIONVALUENEW("myForm","cbo_idsection","tbl_employee_section","$urlcbo","");

                            $arrerror=array("txt_nik"=>"Error Found : Please Insert <b>Nik</b> first.Obrigado",
                                            "txt_employeename"=>"Error Found : Please Insert <b>Employee Name</b> first.Obrigado",
                                            "txt_employeetitle"=>"Error Found : Please Insert <b>Title</b> first.Obrigado",
                                            "cbo_idsection"=>"Error Found : Please Insert <b>Section</b> first.Obrigado",
                                            "txt_msisdn"=>"Error Found : Please Insert <b>MSISDN</b> first with number.Obrigado",
                                            "txt_email"=>"Error Found : Please Insert <b>Email</b> first.Obrigado"
                                      );
                            $this->FN_DHTMLXFORMVALIDATIONERROR("myForm",$arrerror);

                            $arrayControls=array("txt_id"=>"",
                                                 "txt_nik"=>"",
                                                 "txt_employeename"=>"",
                                                 "txt_employeetitle"=>"",
                                                 "cbo_idsection"=>"",
                                                 "txt_msisdn"=>"",
                                                 "txt_email"=>""
                                          );

                            // === Form Event
                            $onobjkeydown="if(ev.keyCode==13){ev.stopPropagation();ev.preventDefault();if(myForm.isItemEnabled('btnupdate')){myForm.setItemFocus('btnupdate');dhtmlXclickButton('btnupdate');}else{myForm.setItemFocus('btnsave');}return false;}";

                            $onbuttonclickbtnsave="if(myForm.validate())
                                                   {
                                                        dhtmlx.confirm({
                                                              title:'Confirmation',text:'Are you will be saved and data is correctly?',type:'confirm',
                                                              callback:function(result){
                                                                  if(result)
                                                                  {";
                                                                      $params="'obj=".$this->enc("data_user")."&task=".$this->enc("saverecord")."'";
                                                                      $onformsenddata="url=fn_dhtmlxsenddata('".$this->enc("obj_data")."',$params);
                                                                      myForm.send(url,\"get\",function(loader, response){";
                                                                          $onformsenddata.=$this->FN_DHTMLXWINDOWSHIDEPROGRESS();
                                                                          $onformsenddata.="var sp=trim(response).split('|');
                                                                          //alert(response);
                                                                          if(sp[0]=='ok')
                                                                          {
                                                                              fn_dhtmlxmessage('','Data <b>'+myForm.getItemValue('txt_nik')+'</b> has been saved');";
                                                                              $onformsenddata.=$this->FN_DHTMLXFORMCLEAR2("myForm",$arrayControls,"txt_nik");
                                                                              $onformsenddata.="fn_grid_loaddata();
                                                                          }
                                                                          else if(sp[0]=='failed')
                                                                          {fn_dhtmlxmessage('error','Error Found : '+sp[1]);}
                                                                          else
                                                                          {fn_dhtmlxmessage('error','Error Found : '+response);}
                                                                      });";
                                                                      $onbuttonclickbtnsave.=$this->FN_DHTMLXWINDOWSHOWPROGESS($onformsenddata);
                            $onbuttonclickbtnsave.="              }
                                                              }
                                                        });
                                                    }";

                            $onbuttonclickbtnupdt="if(myForm.validate())
                                                   {
                                                        dhtmlx.confirm({
                                                             title:'Confirmation',text:'Are you will be updated and data is correctly?',type:'confirm',
                                                             callback:function(result){
                                                                if(result)
                                                                {";
                                                                   $params="'obj=".$this->enc("data_user")."&task=".$this->enc("updrecord")."'";
                                                                   $onformsenddata="url=fn_dhtmlxsenddata('".$this->enc("obj_data")."',$params);
                                                                   myForm.send(url,\"get\",function(loader, response){";
                                                                      $onformsenddata.=$this->FN_DHTMLXWINDOWSHIDEPROGRESS();
                                                                      $onformsenddata.="var sp=trim(response).split('|');
                                                                      //alert(response);
                                                                      if(sp[0]=='ok')
                                                                      {
                                                                           fn_dhtmlxmessage('','Data <b>'+myForm.getItemValue('txt_nik')+'</b> has been update');";
                                                                           $onformsenddata.=$this->FN_DHTMLXFORMCLEAR2("myForm",$arrayControls,"txt_nik");
                                                                           $onformsenddata.="myForm.enableItem('btnsave');";
                                                                           $onformsenddata.="myForm.disableItem('btnupdate');";
                                                                           $onformsenddata.="myForm.disableItem('btndelete');";
                                                                           $onformsenddata.="fn_grid_loaddata();
                                                                      }
                                                                      else if(sp[0]=='failed')
                                                                      {fn_dhtmlxmessage('error','Error Found : '+sp[1]);}
                                                                      else
                                                                      {fn_dhtmlxmessage('error','Error Found : '+response);}
                                                                   });";
                                                                   $onbuttonclickbtnupdt.=$this->FN_DHTMLXWINDOWSHOWPROGESS($onformsenddata);
                            $onbuttonclickbtnupdt.="            }
                                                             }
                                                        });
                                                   }";

                            $eventafterdeleted=$this->FN_DHTMLXFORMCLEAR2("myForm",$arrayControls,"txt_nik");
                            $eventafterdeleted.="myForm.enableItem('btnsave');";
                            $eventafterdeleted.="myForm.disableItem('btnupdate');";
                            $eventafterdeleted.="myForm.disableItem('btndelete');";
                            $eventafterdeleted.="fn_grid_loaddata();";
                            $onbuttonclickbtndeleted=$this->FN_DHTMLXREMARKFORDELETED2("obj_data","data_user","myForm.getItemValue('txt_id')","myForm.getItemValue('txt_nik')",$eventafterdeleted);

                            $onbuttonclickbtncancel=$this->FN_DHTMLXFORMCLEAR2("myForm",$arrayControls);
                            $onbuttonclickbtncancel.="myForm.enableItem('btnsave');";
                            $onbuttonclickbtncancel.="myForm.disableItem('btnupdate');";
                            $onbuttonclickbtncancel.="myForm.disableItem('btndelete');";
                            $onbuttonclickbtncancel.="myForm.setItemFocus('txt_nik');";
                            $onbuttonclickbtncancel.="fn_grid_loaddata();";

                            $arrevent=array(
                                                array("eventname"=>"onbuttonclick","eventparam"=>"id,val",
                                                          "switch"=>array(
                                                                                "btnsave"=>"$onbuttonclickbtnsave",
                                                                                "btnupdate"=>"$onbuttonclickbtnupdt",
                                                                                "btndelete"=>"$onbuttonclickbtndeleted",
                                                                                "btncancel"=>"$onbuttonclickbtncancel"
                                                          )
                                                ),
                                                array("eventname"=>"onkeydown","eventparam"=>"inp,ev,id,val",
                                                          "switch"=>array(
                                                                                "txt_email"=>"$onobjkeydown"
                                                          )
                                                )
                            );


                            $this->FN_DHTMLXFORMEVENT("myForm",$arrevent);

                            // === FORM SEARCH AND GRID
                            $formdata=array(
                                              array("type"=>"settings","position"=>"label-left","labelWidth"=>"180","inputWidth"=>"120"),
                                              array("type"=>"fieldset","width"=>"580","label"=>"Search",
                                                     "list"=>array(
                                                                      array("type"=>"input","name"=>"txtsearch","label"=>"Insert Your Keyword","info"=>"true","inputWidth"=>"240",
                                                                            "tooltip"=>"Insert Your Keyword","maxLength"=>"","className"=>"txtleft","labelWidth"=>"210"),
                                                                      array("type"=>"newcolumn"),
                                                                      array("type"=>"button","name"=>"btnfind","value"=>"","className"=>"button_find","offsetLeft"=>"10","offsetTop"=>"0")
                                                     )
                                              )
                            );

                            $this->FN_DHTMLXFORM("myFormsearch","divusersearch",$formdata,true);
                            $ontxtsearchkeydown="if(ev.keyCode==13){ev.stopPropagation();ev.preventDefault();myFormsearch.setItemFocus('btnfind');}";
                            $arrevent=array(
                                                array("eventname"=>"onbuttonclick","eventparam"=>"id,val",
                                                          "switch"=>array(
                                                                                "btnfind"=>"fn_grid_loaddata()"
                                                          )
                                                ),
                                                array("eventname"=>"onkeydown","eventparam"=>"inp,ev,id,val",
                                                          "switch"=>array(
                                                                                "txtsearch"=>"$ontxtsearchkeydown"
                                                          )
                                                )
                            );
                            $this->FN_DHTMLXFORMEVENT("myFormsearch",$arrevent);

                            $grdinit=array("gridname"=>"dguser","divtarget"=>"div_dguser","divpaging"=>"div_dguserpaging","paginglimit"=>PAGE_LIMIT,
                                           "showloader"=>"true");
                            $gridproperties=array("id"=>"right,ro,96,right,int,false,true",
                                                  "No.&nbsp;"=>"right,ro,35,center,int,false",
                                                  "Nik"=>"left,ro,94,left,str,false",
                                                  "Employee Name"=>"left,ro,160,left,str,false",
                                                  "Title"=>"left,ro,190,left,str,false",
                                                  "Section"=>"right,ro,136,right,int,false",
                                                  "MSISDN"=>"left,ro,128,left,str,false",
                                                  "Email"=>"left,ro,120,left,str,false");
                            $this->FN_DHTMLXGRIDVIEW($grdinit,$gridproperties,"bricks","","fn_grid_loaddata()");
                            $arrfields=array("txt_id"=>"","txt_nik"=>"","txt_employeename"=>"","txt_employeetitle"=>"","txt_msisdn"=>"","txt_email"=>"","cbo_idsection"=>"idsection|sectionname");
                            $otherevent="";
                            $this->FN_DHTMLXGRIDVIEWTOFORMNEW("dguser","myForm","tbl_employee",0,$arrfields,"");
            echo "    }

                      function fn_grid_loaddata()
                      {
                          var keyword=myFormsearch.getItemValue('txtsearch');
                          url=fn_dhtmlxloadobject('".$this->enc("obj_data")."','obj=".$this->enc("data_user")."&task=".$this->enc("grdrecord")."&keyword='+keyword);
                          //alert(url);
                          dguser.clearAndLoad(url);";
            echo "    }
                   </script>";
            echo $this->FN_CLOSEHEADER();
            echo $this->FN_CREATEBODY("e8f1ff");

            echo "<table width=\"98%\" border=\"0\">
                      <tr>
                          <td>";
                              $qry="select filepdf from ".DB_APORTIL.".tbl_manualbook where isdeleted=0 and filepdf='employee_information.pdf'";
                              $filepdf=$this->fn_get_fieldvalue("filepdf",$qry);  
                              $urlmanualbook="<a href='".UPLOAD_DIR."/manualbook/$filepdf"."' target='_blank'><img src='".PATH_THEME."/images/pdf.png' />&nbsp;<b>MANUAL BOOK</b></a>";
                              $url="location.reload();";
                              $myuri=array("<b>EMPLOYEE INFORMATION</b>"=>"$url|id='a_newticket'","$urlmanualbook"=>"|id='a_manualbook'");
                              echo $this->MULTIPLE_LINK($myuri);
            echo "       </td>
                      </tr>
                      <tr>
                          <td>
                              <div id=\"divusercontent\" style=\"margin-left:0px;\">
                                  <table width=\"98%\" border=\"0\">
                                      <tr>
                                          <td width=\"45%\" valign=\"top\">
                                              <div id=\"divusernew\" style=\"\"></div>
                                          </td>
                                          <td valign=\"top\">
                                              <table width=\"98%\" cellpadding=\"0\" cellspacing=\"0\">
                                                  <tr>
                                                      <td valign=\"top\">
                                                          <div id=\"divusersearch\" style=\"width:560px;margin-top:-6px;\"></div>
                                                      </td>
                                                  </tr>
                                                  <tr>
                                                      <td>
                                                          <div id=\"div_dguser\" style=\"height:380px;width:580px;overflow:hidden\"></div>
                                                      </td>
                                                  </tr>
                                                  <tr>
                                                      <td>
                                                          <div id=\"div_dguserpaging\" style=\"width:598px;margin-top:1px\"></div>
                                                      </td>
                                                  </tr>
                                                  <tr><td valign=\"top\"><div id=\"loadergrid\" style=\"z-index:100;position:absolute;margin-top:-220px;margin-left:287px;display:visibility;\">".$this->CREATE_IMAGE("loadergrid.gif")."</div></td></tr>
                                                  <tr>
                                                      <td valign=\"top\">
                                                          <table>
                                                              <tr>
                                                                  <td style='color:red;font-weight:bold;'>Notes</td>
                                                                  <td>:</td>
                                                                  <td>- Click Row For Edit</td>
                                                              </tr>
                                                          </table>
                                                      </td>
                                                  </tr>
                                              </table>
                                          </td>
                                      </tr>
                                  </table>
                              </div>
                          </td>
                      </tr>
                  </table>";
            echo $this->FN_CREATEFOOTER();

        }

        public function fn_grdrecord()
        {
            $keyword=$_REQUEST["keyword"];
            header("Content-type: text/xml");
            echo('<?xml version="1.0" encoding="utf-8"?>');
            echo "<rows id='0'>";

            $qry="select id,nik,employeename,employeetitle,msisdn,email,sectionname
                  from ".DB_APORTIL.".tbl_employee
                  where (nik like '%$keyword%' or employeename like '%$keyword%') and isdeleted=0
                  order by id asc limit ".TBL_PAGE_LIMIT;

            $rs=$this->selectquery($qry);
            if($rs)
            {
               $i=1;
               while($row=$this->data_row_array($rs))
               {
                  echo ("<row id='".$row["id"]."'>");
                        print("<cell><![CDATA[".$row["id"]."]]></cell>");
                        print("<cell><![CDATA[".$i."]]></cell>");
                        print("<cell><![CDATA[".$row["nik"]."]]></cell>");
                        print("<cell><![CDATA[".$row["employeename"]."]]></cell>");
                        print("<cell><![CDATA[".$row["employeetitle"]."]]></cell>");
                        print("<cell><![CDATA[".$row["sectionname"]."]]></cell>");
                        print("<cell><![CDATA[".$row["msisdn"]."]]></cell>");
                        print("<cell><![CDATA[".$row["email"]."]]></cell>");
                        
                  echo ("</row>");
                  $i+=1;
               }
            }
            else
            {
                  echo mysql_errno().": ".mysql_error()." at ".__LINE__." line in ".__FILE__." file<br>";
            }
            echo "</rows>";

        }

        public function fn_saverecord($isupdate=null)
        {
            if($isupdate){$id=$_REQUEST['txt_id'];}
            $nik=$_REQUEST['txt_nik'];
            $employeename=$_REQUEST['txt_employeename'];
            $employeetitle=$_REQUEST['txt_employeetitle'];
            $msisdn=$_REQUEST['txt_msisdn'];
            $email=$_REQUEST['txt_email'];
            $idsection=$_REQUEST['cbo_idsection'];
            $spsec=$this->fn_get_fieldsvalues("sectionname,iddept,departmentname","select id,sectionname,iddept,departmentname from ".DB_APORTIL.".tbl_employee_section where id='".$idsection."'");
            $sectionname=$spsec[0];$iddept=$spsec[1];$departmentname=$spsec[2];

            $qry="insert into ".DB_APORTIL.".tbl_employee 
                  set nik='$nik',employeename='$employeename',employeetitle='$employeetitle',msisdn='$msisdn',email='$email',idsection='$idsection',
                      sectionname='$sectionname',iddept='$iddept',deptname='$departmentname',iby='".$_SESSION['nik']."',idt=now()";
            if($isupdate)
            {
                $qry="update ".DB_APORTIL.".tbl_employee
                      set nik='$nik',employeename='$employeename',employeetitle='$employeetitle',msisdn='$msisdn',email='$email',idsection='$idsection',
                          sectionname='$sectionname',iddept='$iddept',deptname='$departmentname',uby='".$_SESSION['nik']."',udt=now()
                      where id='$id'";
            }

            if($this->executequery($qry)){echo "ok|";return true;}
        }
        public function fn_updrecord(){$this->fn_saverecord(true);}

        public function fn_delrecord()
        {
            $id=$_REQUEST['txt_id'];
            $remark=$_REQUEST['txtremark'];
            $qry="update ".DB_APORTIL.".tbl_employee
                  set isdeleted=1,remark='$remark',dby='".$_SESSION['nik']."',ddt=now()
                  where id='$id'";
            if($this->executequery($qry)){echo "ok|";return true;}
            else
            {
              echo "failed|".$this->getErrorQuery();
            }
        }
    }
?>