<?php

/*
 * 메인페이지꾸미기 플러그인
 */
if (!defined('_GNUBOARD_')) {
    exit;
}

function plugin_indexgroup($area_tag) {
    global $g5, $is_admin, $config;
    $cfg = get_plugin_config('indexgroup'); //플러그인 환경설정
    //플러그인 미설정 중지
    if (!$cfg) {
        return false;
    }
    //플러그인 미사용 중지
    if ($cfg['pc_use'] == 0) {
        return false;
    }
    //배열일경우 두번째 값은 옵션.
    if (is_array($area_tag)) {
        $tag = $area_tag['0'];
        $opt = $area_tag['1'];
        $mobileskin = $area_tag['2'];
    } else {
        $tag = $area_tag;
    }
    if ($tag) {
        //메인페이지
        $sql = "select * from ask_plugin_indexgroup where aig_status = '1' and aig_area = '{$tag}' order by aig_sort";
        $result = sql_query($sql);
        $str = "";
        for ($i = 0; $rows = sql_fetch_array($result); $i++) {
            if ($is_admin == 'super' && $opt == 'admin') {
                $group_modify = "<div class='plugin-modify-group'><a href='./indexgroup_add.php?w=u&idx={$rows['aig_idx']}&popup=1' class='button mdl-shadow--5dp' target='_blank'>그룹편집</a></div>";
            }
            //최신글목록 
            $query = "select * from ask_plugin_indexgroup_child where aic_parent_idx = '{$rows['aig_idx']}' and aic_status = '1' order by aic_sort";
            $res = sql_query($query);
            $k = 1;
            for ($a = 0; $child = sql_fetch_array($res); $a++) {

                //HTML 컨텐츠용
                $str .= grid_html($rows, $child, $i, $k, $a, $group_modify, false);
                $str .= grid_1($rows, $child, $i, $k, $a, $group_modify, false);
                $str .= grid_2_1_1($rows, $child, $i, $k, $a, $group_modify, false);
                $str .= grid_2_2_1($rows, $child, $i, $k, $a, $group_modify, false);
                $str .= grid_2_1_2($rows, $child, $i, $k, $a, $group_modify, false);
                $str .= grid_3($rows, $child, $i, $k, $a, $group_modify, false);

                //출력 부분 및 관리 아이콘 출력
                if ($child['aic_bo_table'] && !$child['aic_tab']) {
                    //일반
                    if ($opt == 'admin') {
                        //관리자 화면에서 최신글 보여주기.
                        $str .= latest_admin($child['aic_type'], $child['aic_bo_table'], $child['aic_rows'], '', '', $mobileskin);

                        if ($is_admin == 'super') {
                            //관리자일경우 2단계 편집 버튼을 보여준다.
                            if ($child['aic_html']) {
                                $page_file = 'page_';
                            } elseif ($child['aic_tab'] == '1') {
                                $page_file = 'tab_';
                            } else {
                                $page_file = '';
                            }
                            $link = "<a href='./indexgroup_child_{$page_file}add.php?w=u&idx={$child['aic_idx']}&popup=1' target='_blank' class='button mdl-shadow--5dp'><i class='fa fa-pencil-square-o' aria-hidden='true'></i></a>";
                            $str .='<div class="plugin-modify">' . $link . '</div>';
                        }
                    } else {
                        $str .= latest($child['aic_type'], $child['aic_bo_table'], $child['aic_rows']);
                    }
                } else if ($child['aic_tab'] == '1') {
                    //탭
                    $str .= "<!-- 탭최신글 -->";
                    if ($opt == 'admin') {
                        $str .= make_tab_tables($child['aic_type'], $child['aic_bo_table'], $child['aic_rows'], $mobileskin);
                        if ($is_admin == 'super') {
                            //관리자일경우 2단계 편집 버튼을 보여준다.
                            if ($child['aic_html']) {
                                $page_file = 'page_';
                            } elseif ($child['aic_tab'] == '1') {
                                $page_file = 'tab_';
                            } else {
                                $page_file = '';
                            }
                            $link = "<a href='./indexgroup_child_{$page_file}add.php?w=u&idx={$child['aic_idx']}&popup=1' target='_blank' class='button mdl-shadow--5dp'><i class='fa fa-pencil-square-o' aria-hidden='true'></i></a>";
                            $str .='<div class="plugin-modify">' . $link . '</div>';
                        }
                    } else {
                        $str .= make_tab_tables($child['aic_type'], $child['aic_bo_table'], $child['aic_rows']);
                    }
                    $str .= "<!-- //탭최신글 -->";
                }

                if (!$child['aic_bo_table'] && $child['aic_html']) {
                    if ($is_admin == 'super' && $opt == 'admin') {
                        //관리자일경우 2단계 편집 버튼을 보여준다.

                        $link = "<a href='./indexgroup_child_page_add.php?w=u&idx={$child['aic_idx']}&popup=1' target='_blank' class='button mdl-shadow--5dp'><i class='fa fa-pencil-square-o' aria-hidden='true'></i></a>";
                        $str .='<div class="plugin-modify">' . $link . '</div>';
                    }
                    $str .= $child['aic_html'];
                }
                /*
                 * 닫기 부분 시작
                 */
                //HTML 컨텐츠용
                $str .= grid_html($rows, $child, $i, $k, $a, $group_modify, true);
                //grid 1 of 12
                $str .= grid_1($rows, $child, $i, $k, $a, $group_modify, true);

                $str .= grid_2_1_1($rows, $child, $i, $k, $a, $group_modify, true);

                $str .= grid_2_2_1($rows, $child, $i, $k, $a, $group_modify, true);

                $str .= grid_2_1_2($rows, $child, $i, $k, $a, $group_modify, true);

                $str .= grid_3($rows, $child, $i, $k, $a, $group_modify, true);

                if ($child['aic_bo_table']) {
                    $k++;
                }
            }//for $a
        }//for $i
    } else {
        return false;
    }
    echo $str;
}

//html 
function grid_html($rows, $child, $i, $k, $a, $group_modify, $tail) {
    global $config;
    $str = '';
    if (!$child['aic_bo_table']) {

        if ($tail == false) { // 시작 끝 구분
            //foundation grid
            if ($config['cf_theme'] == 'asktheme-fd') {
                //1열
                $str .= '<div class="row">' . PHP_EOL;
                $str .= '<div class="small-12 columns">' . PHP_EOL;
            } else {
                //asktheme grid
                //1열
                $str .= '<div class="grid grid-pad">' . PHP_EOL;
                if ($a == 0) {
                    $str .= $group_modify;
                }
                $str .= '<div class="col-1-1 grid-last">' . PHP_EOL;
            }
        }

        //그리드 닫기
        if ($tail) {
            $str = '';
            if ($config['cf_theme'] == 'asktheme-fd') {
                $str .= '</div><!-- //.small-12 -->' . PHP_EOL;
                $str .= '</div><!-- //.row -->' . PHP_EOL;
            } else {
                $str .= '</div>' . PHP_EOL;
                $str .= '</div><!--//grid -->' . PHP_EOL;
            }
        }
    }//if child
    return $str;
}

//grid 1/12
function grid_1($rows, $child, $i, $k, $a, $group_modify, $tail) {
    global $config;
    $str = '';
    if ($child['aic_bo_table'] && $rows['aig_column'] == '1') {
        if ($tail == false) { // 시작 끝 구분
            //foundation grid
            if ($config['cf_theme'] == 'asktheme-fd') {

                //1열
                $str .= '<div class="row">' . PHP_EOL;
                if ($a == 0) {
                    $str .= $group_modify;
                }
                $str .= '<div class="small-12 columns">' . PHP_EOL;
            } else {
                //asktheme grid
                //1열
                $str .= '<div class="grid grid-pad">' . PHP_EOL;
                if ($a == 0) {
                    $str .= $group_modify;
                }
                $str .= '<div class="col-1-1 grid-last">' . PHP_EOL;
            }
        }
        //그리드 닫기
        if ($tail) {
            $str = '';
            if ($config['cf_theme'] == 'asktheme-fd') {

                $str .= '</div>' . PHP_EOL;
                $str .= '</div><!-- // .row -->' . PHP_EOL;
            } else {

                $str .= '</div>' . PHP_EOL;
                $str .= '</div><!--//grid -->' . PHP_EOL;
            }
        }
    }
    return $str;
}

function grid_2_1_1($rows, $child, $i, $k, $a, $group_modify, $tail) {
    global $config;
    $str = '';
    if ($child['aic_bo_table'] && $rows['aig_column'] == '2-1-1') {
        if ($tail == false) { // 시작 끝 구분
            //foundation grid
            if ($config['cf_theme'] == 'asktheme-fd') {
                //시작 그리드
                if ($k % 2 == 1) {
                    $str .= '<div class="row">' . PHP_EOL;
                    if ($a == 0) {
                        $str .= $group_modify;
                    }
                }
                if (($k % 2) == 1) {
                    //홀수
                    $str .= '<div class="small-12 medium-6 large-6 columns">' . PHP_EOL;
                } else {
                    //짝수
                    $str .= '<div class="small-12 medium-6 large-6 columns">' . PHP_EOL;
                }
            } else {
                //asktheme grid
                //시작 그리드
                if ($k % 2 == 1) {
                    $str .= '<div class="grid grid-pad">' . PHP_EOL;
                    if ($a == 0) {
                        $str .= $group_modify;
                    }
                }
                if (($k % 2) == 1) {
                    //홀수
                    $str .= '<div class="col-1-2">' . PHP_EOL;
                } else {
                    //짝수
                    $str .= '<div class="col-1-2 grid-last">' . PHP_EOL;
                }
            }//else
        }//if tail
        //그리드 닫기
        if ($tail) {
            $str = '';
            if ($config['cf_theme'] == 'asktheme-fd') {
                $str .= '</div>' . PHP_EOL;
                //그리드 닫기
                if ($k % 2 == 0) {
                    $str .= '</div><!-- //.row -->' . PHP_EOL;
                }
            } else {
                $str .= '</div>' . PHP_EOL;
                //그리드 닫기
                if ($k % 2 == 0) {
                    $str .= '</div><!--//grid -->' . PHP_EOL;
                }
            }//else
        }
    }
    return $str;
}

function grid_2_2_1($rows, $child, $i, $k, $a, $group_modify, $tail) {
    global $config;
    $str = '';
    if ($child['aic_bo_table'] && $rows['aig_column'] == '2-2-1') {
        if ($tail == false) { // 시작 끝 구분
            //foundation grid
            if ($config['cf_theme'] == 'asktheme-fd') {
                if ($k % 2 == 1) {
                    $str .= '<div class="row">' . PHP_EOL;
                    if ($a == 0) {
                        $str .= $group_modify;
                    }
                }
                if (($k % 2) == 1) {
                    //홀수
                    $str .= '<div class="small-12 medium-8 large-8 columns">' . PHP_EOL;
                } else {
                    //짝수
                    $str .= '<div class="small-12 medium-4 large-4 columns">' . PHP_EOL;
                }
            } else {
                //asktheme grid
                //시작 그리드
                if ($k % 2 == 1) {
                    $str .= '<div class="grid grid-pad">' . PHP_EOL;
                    if ($a == 0) {
                        $str .= $group_modify;
                    }
                }
                if (($k % 2) == 1) {
                    //홀수
                    $str .= '<div class="col-2-3">' . PHP_EOL;
                } else {
                    //짝수
                    $str .= '<div class="col-1-3 grid-last">' . PHP_EOL;
                }
            }
        }
        //그리드 닫기
        if ($tail) {
            $str = '';
            if ($config['cf_theme'] == 'asktheme-fd') {
                $str .= '</div>' . PHP_EOL;
                //그리드 닫기
                if ($k % 2 == 0) {
                    $str .= '</div><!-- //row -->' . PHP_EOL;
                }
            } else {
                $str .= '</div>' . PHP_EOL;
                //그리드 닫기
                if ($k % 2 == 0) {
                    $str .= '</div><!--//grid -->' . PHP_EOL;
                }
            }
        }
    }
    return $str;
}

function grid_2_1_2($rows, $child, $i, $k, $a, $group_modify, $tail) {
    global $config;
    $str = '';
    if ($child['aic_bo_table'] && $rows['aig_column'] == '2-1-2') {
        if ($tail == false) { // 시작 끝 구분
            //foundation grid
            if ($config['cf_theme'] == 'asktheme-fd') {
                if ($k % 2 == 1) {
                    $str .= '<div class="row">' . PHP_EOL;
                    if ($a == 0) {
                        $str .= $group_modify;
                    }
                }
                if (($k % 2) == 1) {
                    //홀수
                    $str .= '<div class="small-12 medium-4 large-4 columns">' . PHP_EOL;
                } else {
                    //짝수
                    $str .= '<div class="small-12 medium-8 large-8 columns">' . PHP_EOL;
                }
            } else {
                //asktheme grid
                //시작 그리드
                if ($k % 2 == 1) {
                    $str .= '<div class="grid grid-pad">' . PHP_EOL;
                    if ($a == 0) {
                        $str .= $group_modify;
                    }
                }
                if (($k % 2) == 1) {
                    //홀수
                    $str .= '<div class="col-1-3">' . PHP_EOL;
                } else {
                    //짝수
                    $str .= '<div class="col-2-3 grid-last">' . PHP_EOL;
                }
            }
        }
        //그리드 닫기
        if ($tail) {
            $str = '';
            if ($config['cf_theme'] == 'asktheme-fd') {
                $str .= '</div>' . PHP_EOL;
                //그리드 닫기
                if ($k % 2 == 0) {
                    $str .= '</div><!-- //.row -->' . PHP_EOL;
                }
            } else {
                $str .= '</div>' . PHP_EOL;
                //그리드 닫기
                if ($k % 2 == 0) {
                    $str .= '</div><!--//grid -->' . PHP_EOL;
                }
            }
        }
    }
    return $str;
}

function grid_3($rows, $child, $i, $k, $a, $group_modify, $tail) {
    global $config;
    $str = '';
    if ($child['aic_bo_table'] && $rows['aig_column'] == '3') {
        if ($tail == false) { // 시작 끝 구분
            //foundation grid
            if ($config['cf_theme'] == 'asktheme-fd') {
                //시작 그리드
                if ($k % 3 == 1) {
                    $str .= '<div class="row">' . PHP_EOL;
                    if ($a == 0) {
                        $str .= $group_modify;
                    }
                }
                if ($k % 3 == 1) {
                    $str .= '<div class="small-12 medium-4 large-4 columns">' . PHP_EOL;
                } elseif ($k % 3 == 2) {
                    $str .= '<div class="small-12 medium-4 large-4 columns">' . PHP_EOL;
                } else {
                    $str .= '<div class="small-12 medium-4 large-4 columns">' . PHP_EOL;
                }
            } else {
                //asktheme grid
                //시작 그리드
                if ($k % 3 == 1) {
                    $str .= '<div class="grid grid-pad">' . PHP_EOL;
                    if ($a == 0) {
                        $str .= $group_modify;
                    }
                }
                if ($k % 3 == 1) {
                    $str .= '<div class="col-1-3">' . PHP_EOL;
                } elseif ($k % 3 == 2) {
                    $str .= '<div class="col-1-3">' . PHP_EOL;
                } else {
                    $str .= '<div class="col-1-3 grid-last">' . PHP_EOL;
                }
            }
        }
        //그리드 닫기
        if ($tail) {
            $str = '';
            if ($config['cf_theme'] == 'asktheme-fd') {
                $str .= '</div>' . PHP_EOL;
                if ($k % 3 == 0) {
                    $str .= '</div><!-- //.row -->' . PHP_EOL;
                }
            } else {
                $str .= '</div>' . PHP_EOL;
                if ($k % 3 == 0) {
                    $str .= '</div>' . PHP_EOL;
                }
            }
        }
    }
    return $str;
}

/*
 * function grid_1($rows, $child, $i, $k, $a, $group_modify, $tail) {
  global $config;
  $str = '';
  if ($tail == false) { // 시작 끝 구분
  //foundation grid
  if ($config['cf_theme'] == 'asktheme-fd') {

  } else {
  //asktheme grid

  }
  }
  //그리드 닫기
  if ($tail) {
  $str = '';
  if ($config['cf_theme'] == 'asktheme-fd') {

  } else {

  }
  }
  return $str;
  }
 */

function make_tab_tables($type, $bo_tables, $rows, $option = false) {
    $id = 'tab_' . cut_str(md5($type . $bo_tables . $rows), 10, '');
    $array = explode(',', trim($bo_tables));
    $tables = array_filter($array);
    $header_tab = '';
    $tab_content = '';
    $i = 1;
    foreach ($tables as $val) {
        $tabs = preg_replace("/\(.*?\)/", "", $val);
        $bo_subject = ask_get_board($tabs);
        if ($i == 1) {
            $active = "active";
        } else {
            $active = "";
        }
        $header_tab .= "<li class='{$active}'><a href='#{$id}{$i}' data-target='{$id}{$i}'>{$bo_subject['bo_subject']}</a></li>";
        $tab_content .="<div class='tab-item {$id}{$i}'>";
        if ($option == 'mobile-demo') {
            //관리자 화면에서 최신글 보여주기.
            $tab_content.= latest_admin($type, $tabs, $rows, '', '', $option);
        } else {
            $tab_content.= latest($type, $tabs, $rows);
        }
        $tab_content.= "</div>";
        $i++;
    }
    $str = "<!--tab latest--><div class='tab-wrap'><ul class='tabs tab-header {$id}'>";
    $str .= $header_tab;
    $str .= "</ul>";
    $str .= "<div class='tab-contents'>";
    $str .= $tab_content;
    $str .="</div>";
    $str .="</div><!--//tab latest-->";
    $str .="<script type='text/javascript'>
    $(function () {
        $('.{$id} li a').click(function () {
            var targetItem = $(this).data('target');
            $(this).parent('li').siblings('li').removeClass('active');
            $(this).parent('li').addClass('active');
            $('.' + targetItem).siblings('.tab-item').hide();
            $('.' + targetItem).show();
        });
    });
</script>";
    return $str;
}
