<?php

use Illuminate\Support\Facades\DB;

function getPagesInMenu () {
  $get = DB::table('blg_content')->select('title', 'slug')->where('public_status', 0)->where('isPost', 0)->where('isMenu', 1)->orderBy('sortorder', 'asc')->get();

  $res = array();
  $key = 0;

  foreach ($get as $i) {
    array_push($res, ['key' => $key, 'title' => $i->title, 'slug' => $i->slug]);
    $key++;
  }

  return $res;
}

function getCss () {
  $engine = new \App\Http\Controllers\Engine;
  return $engine->css;
}

function getPerms () {
  $engine = new \App\Http\Controllers\Engine;
  return $engine->getPermissions((isset($_COOKIE['kero_token']) ? $_COOKIE['kero_token'] : ''));
}

function getUser () {
  $engine = new \App\Http\Controllers\Engine;
  return $engine->user;
}

function getGroupColours () {
  return DB::table('usr_perm_module')->select('id', 'colour_m', 'colour_f', 'colour_u')->get()->toArray();
}

function checkLegit ($t) {
  if (!isset($t) || empty($t) || is_null($t)) return 0;
  $check = DB::table('users')->select('id')->where('kero_token', $t)->first();
  return $check->id;
}

function userDetail ($id, $kero=null) {
  if ($kero || $id) {
    $log_username = null;
    if (!is_null($id)) $log_username = DB::table('users')->select('id', 'username')->where('id', $id)->first();
    else $log_username = DB::table('users')->select('id', 'username')->where('kero_token', $kero)->first();
    $perm_id = DB::table('usr_perm_id')->select('perm_id')->where('user_id', $log_username->id)->first()->perm_id;
    $p3 = DB::table('usr_profile')->select('gender', 'avatar', 'name_style', 'display_name')->where('user_id', $log_username->id)->first();
    $user_id = $log_username->id;
    $log_username = $log_username->username;
    $showname = '';
    $showcol = '';
    $ucol = DB::table('usr_perm_module')->select('id', 'colour_m', 'colour_f', 'colour_u')->get();

    if (!empty($p3->display_name)) $showname = $p3->display_name;
    else $showname = $log_username;

    if (!empty($p3->name_style)) $showcol = $p3->name_style;
    else {
      foreach ($ucol as $j3) {
        if ($j3->id == $perm_id) {
          if ($p3->gender == 1) $showcol = $j3->colour_m;
          else if ($p3->gender == 2) $showcol = $j3->colour_f;
          else $showcol = $j3->colour_u;
        }
      }
    }

    $p3->avatar = ($p3->avatar ? '/'.$p3->avatar : '/img/noicon.jpg');
    return array('user_id' => $user_id, 'showname' => $showname, 'showcol' => $showcol, 'avatar' => $p3->avatar);
  }

  return new \stdClass();
}

function getCountries () {
  $flags = DB::table('nhn_country')->orderBy('id', 'asc')->get();
  $res = array();

  foreach ($flags as $flag) {
    $protocol = isset($_SERVER["HTTPS"]) ? 'https' : 'http';

    $res[] = array(
      'value' => $flag->nameEng,
      'label' => $flag->name
    );
  }

  return $res;
}

function makeToken () {
  $c  = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$!%&()';
  $clen = strlen($c);
  $token = '';

  for ($i = 0; $i < 128; $i++) {
    $token .= $c[rand(0, $clen - 1)];
  }

  return $token;
}

?>
