<?php
    class MENUCONFIG_LIST extends MENUCONFIG
    {     
        public function menuconfig_list_javainit()
        {
            echo "<script language=\"javascript\">
              
                  </script>";
        } 
        
        public function fn_list_init()
        {    
            if(isset($_REQUEST["task"])){$task=trim(strtolower($_REQUEST["task"]));}
            if(isset($_POST["task"])){$task=trim(strtolower($_POST["task"]));}
            switch($task)
            {                  
                case "searchmenu":
                    $this->fn_searchmenu();
                break;
                
                case "paging":
                    $this->fn_disp_page();
                break;

                case "editmenu":
                    $this->fn_editmenu();
                break;
                
                case "iconlists":
                    $this->fn_iconlists();
                break;
                
                case "cbomenugroupproject":
                    $this->fn_cbomenugroupproject();
                break;
                
                case "updatemenu":
                    $this->fn_updatemenu();
                break;
                
                default:
                    //if($_SESSION["nikx"]!=='9999'){echo $this->under_construction();}
                    $this->fn_default_menulist();                    
                break;
            }
        }
                           
        public function fn_default_menulist()
        {
            echo $this->FN_CREATEHEADER();
            //echo "<div><span id=\"pagingArea\"></span>&nbsp;<span id=\"infoArea\"></span></div>";
            echo "<script>  
                        var myForm,dgmenuconfig,dp;
                        function initObject()
                        {
                            formData=[
                                        {type: \"settings\", position: \"label-left\", labelWidth: \"auto\", inputWidth: \"auto\"},
                                        {type: \"fieldset\",label:\"Cari menu\", width: 900, list:[
                                                {type: \"input\", name:\"txtsearch\",label: \"Masukkan Nama Menu\", inputWidth:160,info:true,tooltip:\"Masukkan Kata yang ingin dicari\"},
                                                {type:\"newcolumn\"},
                                                {type:\"combo\",name:\"cbotipemenu\",required:true,info:true,inputWidth:120,readonly:0,options:";
                                                        $arrval=array(array(value=>"tree",text=>"TreeMenu"),array(value=>"",text=>"Menu"));
                                                        echo json_encode($arrval);
                                        echo "  },
                                                {type:\"newcolumn\"},
                                                {type:\"combo\",name:\"cbogroupproject\",required:true,info:true,inputWidth:220,readonly:0},
                                                {type:\"newcolumn\"},
                                                {type: \"button\", name:\"btnfindmenu\",value: \"Cari Menu\",note:\"My status\",offsetTop:0,offsetLeft:10}
                                        ]}                                        
                                     ];
                              // ==== Init Form  
                              myForm = new dhtmlXForm(\"divmyform\", formData);                              
                              myForm.enableLiveValidation(true);
                              myForm.setFocusOnFirstActive(true);";
                              $urlcbo="fn_dhtmlxloadobject('".$this->enc("obj_controller")."','obj=".$this->enc("-")."&task=".$this->enc("dhtmlxaddcombo")."&tableobj=tbl_menu_groupproject|id,groupname')";
                              $this->FN_COMBOVALIDATIONVALUE2("myForm","cbogroupproject","","$urlcbo","");
            echo "            // ==== Create context Menu
                              contextmenu=new dhtmlXMenuObject();
                              contextmenu.setIconsPath(\"".PATH_THEME."/icons/\");
                              contextmenu.renderAsContextMenu();
                              contextmenu.attachEvent(\"onClick\",function(id){
                                    if(id=='contextmenu_deleted')
                                    {
                                        var data=dgmenuconfig.contextID.split(\"_\");
                                        var getValMenuText=dgmenuconfig.cellById(data[0],4).getValue();
                                        
                                        dhtmlx.confirm({
                                            title:'Confirm',
                                            type:'confirm',
                                            text:'Data <b>'+getValMenuText+'</b> ingin <font color=\"red\">dihapus</font>?',
                                            callback:function(result){
                                                if(result){
                                                    dgmenuconfig.deleteRow(data[0]);
                                                }
                                            }
                                        });
                                        
                                        //fn_dhtmlxconfirm('Confirm','Data <b>'+getValMenuText+'</b> ingin <font color=\"red\">dihapus</font>?',dgmenuconfig.deleteRow(dgmenuconfig.getRowId(data[0]-1)));
                                    }
                                });
                              url=fn_dhtmlxloadobject('".$this->enc("obj_menuconfig")."','obj=".$this->enc("-")."&task=".$this->enc("contxmnu")."');
                              contextmenu.loadStruct(url);
            
                              // ==== attach dgmenuconfig
                              //dgmenuconfig=new dhtmlXGridObject(myForm.getContainer(\"dgmenuconfig\"));
                              dgmenuconfig=new dhtmlXGridObject(\"divdgmenuconfig\");
                              dgmenuconfig.setSkin(\"dhx_skyblue\");
                              dgmenuconfig.setImagePath(\"".PATH_THEME."/codebase/imgs/\");
                              dgmenuconfig.setHeader(\"Id,Menu Id,Menu Type,Menu Header,Menu Text,Menu Description,Menu Image,Obj Option,Obj Name,Obj Task,Obj Target,Menu Order,Menu Group,Group Project,idgroupproject,Activated\");
                              dgmenuconfig.setColTypes(\"ro,ed,ed,ed,ed,ed,combo,ed,ed,ed,ed,ed,ed,combo,ro,ch\"); // ro => Read Only, ed => Edit
                              dgmenuconfig.attachHeader(\"#rspan,#rspan,#combo_filter,#combo_filter,#rspan,#rspan,#rspan,#rspan,#rspan,#rspan,#rspan,#rspan,#combo_filter,#rspan,#rspan,#rspan\");
                              dgmenuconfig.setInitWidths(\"30,160,80,120,180,300,100,100,100,100,100,120,110,110,110,110\");
                              //dgmenuconfig.enableAutoWidth(true);
                              dgmenuconfig.setColAlign(\"left,left,left,right,left,left,left,left,left,left,left,left,left,left,left,center\");
                              dgmenuconfig.setColSorting(\"str,str,str,int,str,str,str,str,str,str,str,str,str,str,str,str\");
                              dgmenuconfig.setDateFormat(\"%Y-%m-%d %H:%i:%s\");
                              dgmenuconfig.enablePaging(true,".PAGE_LIMIT.",null,\"divpagingarea\",true,\"recInfoArea\");
                              //dgmenuconfig.setPagingSkin(\"toolbar\",\"dhx_skyblue\");
                              dgmenuconfig.setPagingSkin(\"bricks\");
                              dgmenuconfig.enableContextMenu(contextmenu);
                              dgmenuconfig.enableTooltips(\"false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false\");
                              dgmenuconfig.init();
                              dgmenuconfig.enableHeaderMenu();
                              
                              dgmenuconfig.attachEvent(\"onEditCell\",function(stage,rowid,colid){
                                  if(colid==13&&stage==2)
                                  {
                                      var idmenugroupproject=dgmenuconfig.cells(rowid,13).getValue();
                                      dgmenuconfig.cells(rowid,14).setValue(trim(idmenugroupproject));
                                  }
                                  return true;
                              });
                              
                              //dgmenuconfig.splitAt(2);
                              // ==== Set combo in Cell Icons
                              var cboiconlist=dgmenuconfig.getColumnCombo(6);
                                  cboiconlist.enableFilteringMode(false);
                                  cboiconlist.readonly(true);
                                  url=fn_dhtmlxloadobject('".$this->enc("obj_menuconfig")."','obj=".$this->enc("menuconfig_list")."&task=iconlists');
                              cboiconlist.load(url);

                              var cbomenugroupproject=dgmenuconfig.getColumnCombo(13);
                                  cbomenugroupproject.enableFilteringMode(true);
                                  //cbomenugroupproject.readonly(true);
                                  url=fn_dhtmlxloadobject('".$this->enc("obj_menuconfig")."','obj=".$this->enc("menuconfig_list")."&task=cbomenugroupproject');
                                  //alert(url);
                              cbomenugroupproject.load(url);

                              // ==== Attach Event Click
                              myForm.attachEvent(\"onButtonClick\",function(id){
                                   switch(id)
                                   {
                                        case 'btnfindmenu':
                                            url=fn_dhtmlxloadobject('".$this->enc("obj_menuconfig")."','obj=".$this->enc("menuconfig_list")."&task=searchmenu&tipemenu='+myForm.getCombo(\"cbotipemenu\").getSelectedValue()+'&idgroupproject='+myForm.getCombo(\"cbogroupproject\").getSelectedValue()+'&keyword='+myForm.getItemValue(\"txtsearch\"));
                                            //alert(url);
                                            //dgmenuconfig.enableSmartRendering(true,50);
                                            dgmenuconfig.clearAndLoad(url);                                            
                                                                                        
                                            url=fn_dhtmlxloadobject('".$this->enc("obj_menuconfig")."','obj=".$this->enc("menuconfig_list")."&task=updatemenu&tipemenu='+myForm.getCombo(\"cbotipemenu\").getSelectedValue()+'&keyword='+myForm.getItemValue(\"txtsearch\"));
                                            dp = new dataProcessor(url);
                                            dp.init(dgmenuconfig);
                                        break;
                                   } 
                              });
                        }
                  </script>";
            echo $this->FN_CLOSEHEADER();
            
            echo $this->FN_CREATEBODY("e8f1ff");
            echo "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">
                      <tr>
                          <td>
                              <div id=\"divmyform\" style=\"width:100%;\"></div>
                          </td>
                      </tr>
                      <tr>
                          <td>
                              <div id=\"divdgmenuconfig\" style=\"width:898px; height:400px; background-color:white;\"></div>
                          </td>
                      </tr>
                      <tr>
                          <td>
                              <div id=\"divpagingarea\" style=\"width:888px;margin-top:1px\"></div>
                        </td>
                      </tr>
                  </table>";
            echo $this->FN_CREATEFOOTER();
        }
        
        public function fn_searchmenu()
        {
            $tipemenu=$_REQUEST["tipemenu"];
            $idgrouproject=$_REQUEST["idgroupproject"];$criteriaidgroupproject="";
            if($idgrouproject>=1){$criteriaidgroupproject=" and m.id_menu_groupproject like '%$idgrouproject%'";}
            $keyword=$_REQUEST["keyword"];
            //include XML Header (as response will be in xml format)
            header("Content-type: text/xml");
            //encoding may be different in your case
            echo('<?xml version="1.0" encoding="utf-8"?>'); 

            //start output of data
            echo "<rows id=\"0\">";

            //output data from DB as XML
            $qry="select m.id,m.menuid,m.menutype,m.menuheader,m.menutext,m.menudesc,m.menuimg,m.objoption,m.id_menu_groupproject,
                         m.objname,m.objtask,m.objtarget,m.menuorder,m.menugroup,gp.groupname,m.menuactivated
                  from ".DB_APORTIL.".tbl_".$tipemenu."menu m left join ".DB_APORTIL.".tbl_menu_groupproject gp on m.id_menu_groupproject=gp.id
                  where (m.menutext like '%$keyword%' or m.menudesc like '%$keyword%') and m.isdeleted=0 $criteriaidgroupproject
                  order by m.id asc limit ".TBL_PAGE_LIMIT;
            $rs=$this->selectquery($qry);                    
            if($rs)
            {
                while($row=$this->data_row_array($rs))
                {
                    //create xml tag for grid's row
                    echo ("<row id='".$row["id"]."'>");
                            print("<cell><![CDATA[".$row["id"]."]]></cell>");
                            print("<cell><![CDATA[".$row["menuid"]."]]></cell>");
                            print("<cell><![CDATA[".$row["menutype"]."]]></cell>");
                            print("<cell><![CDATA[".$row["menuheader"]."]]></cell>");
                            print("<cell><![CDATA[".$row["menutext"]."]]></cell>");
                            print("<cell><![CDATA[".$row["menudesc"]."]]></cell>");
                            print("<cell><![CDATA[".$row["menuimg"]."]]></cell>");
                            print("<cell><![CDATA[".$row["objoption"]."]]></cell>");
                            print("<cell><![CDATA[".$row["objname"]."]]></cell>");
                            print("<cell><![CDATA[".$row["objtask"]."]]></cell>");
                            print("<cell><![CDATA[".$row["objtarget"]."]]></cell>");
                            print("<cell><![CDATA[".$row["menuorder"]."]]></cell>");
                            print("<cell><![CDATA[".$row["menugroup"]."]]></cell>");
                            print("<cell><![CDATA[".$row["groupname"]."]]></cell>");
                            print("<cell><![CDATA[".$row["id_menu_groupproject"]."]]></cell>");
                            print("<cell><![CDATA[".$row["menuactivated"]."]]></cell>");
                    print("</row>");
                }
            }
            else
            {
                //error occurs
                echo mysql_errno().": ".mysql_error()." at ".__LINE__." line in ".__FILE__." file<br>";
            }
            echo "</rows>";
    
        }
        
        public function fn_updatemenuTest()
        {
            //include XML Header (as response will be in xml format)
            header("Content-type: text/xml");
            //encoding may differ in your case
            echo('<?xml version="1.0" encoding="iso-8859-1"?>'); 


            $mode = $_GET["!nativeeditor_status"]; //get request mode
            $rowId = $_GET["gr_id"]; //id or row which was updated 
            $newId = $_GET["gr_id"]; //will be used for insert operation

            switch($mode)
            {
                case "inserted":
                    //row adding request
                    //$action = add_row();
                    $action = "insert";
                break;
                case "deleted":
                    //row deleting request
                    $action = "delete";//delete_row();
                break;
                default:
                    //row updating request
//                    $qry="update ".DB_APORTIL.".tbl_menu 
//                          set menuid='".$_GET["c1"]."',menutype='".$_GET["c2"]."',menuheader='".$_GET["c3"]."',menutext='".$_GET["c4"]."',menudesc='".$_GET["c5"]."', 
//                              menuimg='".$_GET["c6"]."',objoption='".$_GET["c7"]."',objname='".$_GET["c8"]."',objtask='".$_GET["c9"]."',objtarget='".$_GET["c10"]."',
//                              menuorder='".$_GET["c11"]."',menugroup='".$_GET["c12"]."',menuactivated='".$_GET["c13"]."',uby='".$_POST["kduser"]."',udt=now()
//                          where id='".$_GET["gr_id"]."'";
                    $qry="update ".DB_APORTIL.".tbl_menu 
                          set menuid='".$_GET["c1"]."',menutype='".$_GET["c2"]."',menuheader='".$_GET["c3"]."',menutext='".$_GET["c4"]."',menudesc='".$_GET["c5"]."', 
                              menuimg='".$_GET["c6"]."',objoption='".$_GET["c7"]."',objname='".$_GET["c8"]."',objtask='".$_GET["c9"]."',objtarget='".$_GET["c10"]."',
                              menuorder='".$_GET["c11"]."',menugroup='".$_GET["c12"]."',menuactivated='".$_GET["c13"]."',uby='".$_POST["kduser"]."',udt=now()
                          where id='".$_GET["gr_id"]."'";
                    $action = "update";
                break;
            }
            $this->executequery($qry);


            //output update results
            echo "<data>";
            echo "<action type='".$action."' sid='".$rowId."' tid='".$newId."'/>";
            echo "</data>";

        }
        
        public function fn_updatemenu()
        {
            $tipemenu=$_REQUEST["tipemenu"];
            $keyword=$_REQUEST["keyword"];
            $con=mysql_connect(DB_HOST_NAME,DB_USER_NAME,DB_USER_PASS);
                 mysql_select_db(DB_APORTIL);

            require(PATH_THEME."/connector/codebase/grid_connector.php");
            $grid = new GridConnector($con);
            
            //$config = new GridConfiguration();
            //$config->setHeader("id,menutext,menu");
            //$config->setColTypes("ro,ed,ed,ed,ed,ed,ed,ed");
            //$grid->set_config($config);
            
            //$grid->render_table("tbl_menu","id","id,menutext,menudesc,menuactivated");    
            $qry="select * from ".DB_APORTIL.".tbl_".$tipemenu."menu 
                  where (menutext like '%$keyword%' or menudesc like '%$keyword%') and isdeleted=0
                  order by id asc limit ".TBL_PAGE_LIMIT;
                  
            $qry="select m.id,m.menuid,m.menutype,m.menuheader,m.menutext,m.menudesc,m.menuimg,m.objoption,m.id_menu_groupproject,
                         m.objname,m.objtask,m.objtarget,m.menuorder,m.menugroup,gp.id idgroupproject,gp.groupname,m.menuactivated
                  from ".DB_APORTIL.".tbl_".$tipemenu."menu m inner join ".DB_APORTIL.".tbl_menu_groupproject gp on m.id_menu_groupproject=gp.id
                  where (m.menutext like '%$keyword%' or m.menudesc like '%$keyword%') and m.isdeleted=0
                  order by m.id asc limit ".TBL_PAGE_LIMIT;
            //$grid->event->attach("beforeDelete","Function");            
            //$grid->sql->attach("Delete","update ".DB_APORTIL.".tbl_menu set isdeleted=1,dby='".$_SESSION['nik']."',ddt=now() where id={id}");
            $qryupd="update ".DB_APORTIL.".tbl_".$tipemenu."menu 
                     set menuid='{menuid}',menutype='{menutype}',menuheader='{menuheader}',menutext='{menutext}',menudesc='{menudesc}', 
                         menuimg='{menuimg}',objoption='{objoption}',objname='{objname}',objtask='{objtask}',objtarget='{objtarget}',
                         menuorder='{menuorder}',menugroup='{menugroup}',id_menu_groupproject='{id_menu_groupproject}',menuactivated='{menuactivated}',uby='".$_SESSION['nik']."',udt=now()
                     where id={id}";
            $grid->sql->attach("Update",$qryupd);
            $grid->sql->attach("Delete","update ".DB_APORTIL.".tbl_".$tipemenu."menu set isdeleted=1,dby='".$_SESSION['nik']."',ddt=now() where id={id}");
                        
            $grid->render_sql("$qry","id","id,menuid,menutype,menuheader,menutext,menudesc,menuimg,objoption,objname,objtask,objtarget,menuorder,menugroup,groupname,id_menu_groupproject,menuactivated");    
                            
        }   
        
        public function fn_iconlists()
        {
            header("Content-type: text/xml");
            echo("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 
            echo("<complete>"); 
                     echo("<option value=\"\">-</option>"); 
                     $qry="select imgname img,imgname,imgname gif from ".DB_APORTIL.".tbl_menu_img where isdeleted=0 order by id asc";
                     $rsimg=$this->selectquery($qry);
                     if($rsimg)
                     {
                         while($rw=$this->data_row_object($rsimg))
                         {
                             //echo("<option value=\"".$rw->img."\" img_src=\"".$rw->imgname."\">".$rw->imgname."</option>"); 
                             echo("<option value=\"".$rw->img."\">".$rw->imgname."</option>"); 
                         }
                     }
                     $this->clearrecordset($rsimg);
            echo("</complete>"); 
            
        }
        
        public function fn_cbomenugroupproject()
        {
            header("Content-type: text/xml");
            echo("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 
            echo("<complete>"); 
                     echo("<option value=\"\">-</option>"); 
                     $qry="select id,groupname from ".DB_APORTIL.".tbl_menu_groupproject where isdeleted=0 order by id asc";
                     $rs=$this->selectquery($qry);
                     if($rs)
                     {
                         while($rw=$this->data_row_object($rs))
                         {
                             echo("<option value=\"".$rw->id."\">".$rw->groupname."</option>"); 
                         }
                     }
                     $this->clearrecordset($rsimg);
            echo("</complete>"); 
            
        }
                                      

        
    }
?>
