<?php
$_REQUEST['cate_area'] = $_REQUEST['cate_area']?:"service";
$cate_id = "mnu_auth_{$_REQUEST['cate_area']}"; //현재 메뉴 ID 필수!!!
include_once "{$_SERVER['DOCUMENT_ROOT']}/common.inc.php";
include_once _ADM_INC_PATH_ ."/auth_check.php";
include_once _HOME_INC_PATH_."/ajax_init.php";

$mnuObj = new Menu(TB_menu_list);
$mnuObj->cate_area = $_REQUEST['cate_area'];

$cmem_auth = $mnuObj->gsetAuthTarget();

if( !_notAuth("R") ){/************ 권한체크*/
  switch( $_REQUEST['mode'] ) {
    case "getManages":
      $where = new DB_where("and");
      $where->add("member_status = ?",1);
      if( count($cmem_auth) ){
        $subwhere = $where->sub_where("or");
        foreach( $cmem_auth as $key => $val ){
          $subwhere->add("member_auth = ?", $key);
        }
      }

      $menu_list = $mnuObj->getMenuList("");
      $member_list = $baseObj->getMemberList("member_id, member_name, member_auth",$where,"member_name asc");
      $auth_list = $mnuObj->AUTH->getAuthList($_REQUEST['cate_area']);

      //없어진 메뉴 정보가 DB에 존재할때 걸러냄
      foreach( $menu_list as $menu ){
        $menu_id_row[] = $menu['id'];
      }
      foreach( $auth_list as $menu_id => $auth_menu ){
        if( !in_array($menu_id, $menu_id_row) ){
          unset($auth_list[$menu_id]);
        }
      }
      //없어진 메뉴 정보가 DB에 존재할때 걸러냄//

      foreach( $menu_list as $menu_data ){
        $menu_id = $menu_data['id'];
        foreach ( $mnuObj->authDest as $key => $val ){
          foreach( Array('R','W','D') as $v ){
            $auth_list[$menu_id]['G'][$key][$v] = (isset($auth_list[$menu_id]['G'][$key][$v]) && $auth_list[$menu_id]['G'][$key][$v]) ? true : false;
          }
        }
        foreach ( $member_list as $val ){
          foreach( Array('R','W','D') as $v ){
            $auth_list[$menu_id]['M'][$val['member_id']][$v] = (isset($auth_list[$menu_id]['M'][$val['member_id']][$v]) && $auth_list[$menu_id]['M'][$val['member_id']][$v]) ? true : false;
          }
        }
      }

      $retval['code'] = 1;
      $retval['data'] = Array(
        "menu_max_level" => $mnuObj->max_level_cate(),
        "menu_list"      => $menu_list,
        "group_list"     => $mnuObj->authDest,
        "member_list"    => $member_list,
        "auth_list"      => $auth_list,
      );
      break;


    case "setAuth":

      if( _notAuth("D") ) break; /************ 권한체크*/

      $_REQUEST['auth_data'] = json_decode($_REQUEST['auth_data']);

      foreach( (array)$_REQUEST['auth_data'] as $cate_id => $group_data ){
        foreach( (array)$group_data as $auth_area => $target_data ){
          foreach( (array)$target_data as $auth_target => $val ){

            if( (bool)$val->C ){
              $mnuObj->AUTH->setAuth(
                $mnuObj->cate_area,
                (string)$cate_id,
                (string)$auth_area,
                (string)$auth_target,
                $mnuObj->AUTH->getAuthToNum( (bool)$val->R, (bool)$val->W, (bool)$val->D )
              );
            }

          }
        }
      }

      $retval['code'] = 1;
      break;
  }
}

if( !$_REQUEST['no_echo'] ){
  echo json_encode($retval);
}