/**
  * แสดงผล
  */
 public function render()
 {
     // แอดมิน
     if ($login = Login::isAdmin()) {
         // โหลด config
         $config = Config::load(ROOT_PATH . 'settings/config.php');
         // path ของ skin
         $dir = ROOT_PATH . 'skin';
         // action
         $action = self::$request->get('action')->toString();
         if (!empty($action)) {
             if ($login['email'] == 'demo' || !empty($login['fb'])) {
                 $message = '<aside class=error>{LNG_Unable to complete the transaction}</aside>';
             } else {
                 $theme = preg_replace('/[\\/\\\\]/ui', '', self::$request->get('theme')->text());
                 if (is_dir($dir . "/{$theme}")) {
                     if ($action == 'use') {
                         // skin ที่กำหนด
                         $config->skin = $theme;
                         unset($_SESSION['skin']);
                         // บันทึก config.php
                         if (Config::save($config, ROOT_PATH . 'settings/config.php')) {
                             self::$request->setSession('my_skin', $config->skin);
                             $message = '<aside class=message>{LNG_Select a new template successfully}</aside>';
                         } else {
                             $message = '<aside class=error>' . sprintf(Language::get('File %s cannot be created or is read-only.'), 'settings/config.php') . '</aside>';
                         }
                     } elseif ($action == 'delete') {
                         // ลบ skin
                         File::removeDirectory($dir . '/' . $theme . '/');
                         $message = '<aside class=message>{LNG_Successfully remove template files}</aside>';
                     }
                 }
             }
         }
         // แสดงผล
         $section = Html::create('section');
         // breadcrumbs
         $breadcrumbs = $section->add('div', array('class' => 'breadcrumbs'));
         $ul = $breadcrumbs->add('ul');
         $ul->appendChild('<li><span class="icon-settings">{LNG_Site settings}</span></li>');
         $ul->appendChild('<li><span>{LNG_Template}</span></li>');
         $section->add('header', array('innerHTML' => '<h1 class="icon-template">' . $this->title() . '</h1>'));
         if (!empty($message)) {
             $section->appendChild($message);
         }
         // อ่าน theme ทั้งหมด
         $themes = array();
         $f = opendir($dir);
         while (false !== ($text = readdir($f))) {
             if ($text !== $config->skin && $text !== "." && $text !== "..") {
                 if (is_dir($dir . "/{$text}") && is_file($dir . "/{$text}/style.css")) {
                     $themes[] = $text;
                 }
             }
         }
         closedir($f);
         // แสดงฟอร์ม
         $section->appendChild(createClass('Index\\Template\\View')->render($dir, $config, $themes));
         return $section->render();
     }
     // 404.html
     return \Index\Error\Controller::page404();
 }
 /**
  * Initial Counter & Useronline
  */
 public static function init()
 {
     if (defined('MAIN_INIT')) {
         // วันนี้
         $y = (int) date('Y');
         $m = (int) date('m');
         $d = (int) date('d');
         // ตรวจสอบ ว่าเคยเยี่ยมชมหรือไม่
         if (self::$request->cookie('counter_date')->toInt() != $d) {
             // เข้ามาครั้งแรกในวันนี้, บันทึก counter 1 วัน
             setCookie('counter_date', $d, time() + 3600 * 24, '/');
             $new_visitor = true;
         } else {
             $new_visitor = false;
         }
         // โฟลเดอร์ของ counter
         $counter_dir = ROOT_PATH . DATA_FOLDER . 'counter';
         // ตรวจสอบโฟลเดอร์
         File::makeDirectory($counter_dir);
         // ตรวจสอบวันใหม่
         $c = (int) @file_get_contents($counter_dir . '/index.php');
         if ($d != $c) {
             $f = @fopen($counter_dir . '/index.php', 'wb');
             if ($f) {
                 fwrite($f, $d);
                 fclose($f);
             }
             $f = @opendir($counter_dir);
             if ($f) {
                 while (false !== ($text = readdir($f))) {
                     if ($text != '.' && $text != '..') {
                         if ($text != $y) {
                             File::removeDirectory($counter_dir . "/{$text}");
                         }
                     }
                 }
                 closedir($f);
             }
         }
         // ตรวจสอบ + สร้าง โฟลเดอร์
         File::makeDirectory("{$counter_dir}/{$y}");
         File::makeDirectory("{$counter_dir}/{$y}/{$m}");
         // ip ปัจจุบัน
         $counter_ip = self::$request->getClientIp();
         // session ปัจจุบัน
         $counter_ssid = session_id();
         // วันนี้
         $counter_day = date('Y-m-d');
         // Model
         $model = new static();
         $db = $model->db();
         // อ่าน counter รายการล่าสุด
         $my_counter = $db->createQuery()->from('counter')->order('id DESC')->toArray()->first();
         if (!$my_counter) {
             $my_counter = array('date' => '', 'counter' => 0);
         }
         if ($my_counter['date'] != $counter_day) {
             // วันใหม่
             $my_counter['visited'] = 0;
             $my_counter['pages_view'] = 0;
             $my_counter['date'] = $counter_day;
             $new_day = true;
             // clear useronline
             $db->emptyTable($model->getFullTableName('useronline'));
             // clear visited_today
             $db->updateAll($model->getFullTableName('index'), array('visited_today' => 0));
         } else {
             $new_day = false;
         }
         // บันทึกลง log
         $counter_log = "{$counter_dir}/{$y}/{$m}/{$d}.dat";
         if (is_file($counter_log)) {
             // เปิดไฟล์เพื่อเขียนต่อ
             $f = @fopen($counter_log, 'ab');
         } else {
             // สร้างไฟล์ log ใหม่
             $f = @fopen($counter_log, 'wb');
         }
         if ($f) {
             $data = $counter_ssid . chr(1) . $counter_ip . chr(1) . self::$request->server('HTTP_REFERER', '') . chr(1) . self::$request->server('HTTP_USER_AGENT', '') . chr(1) . date('H:i:s') . "\n";
             fwrite($f, $data);
             fclose($f);
         }
         if ($new_visitor) {
             // ยังไม่เคยเยี่ยมชมในวันนี้
             $my_counter['visited']++;
             $my_counter['counter']++;
         }
         $my_counter['pages_view']++;
         $my_counter['time'] = time();
         if ($new_day) {
             unset($my_counter['id']);
             $db->insert($model->getFullTableName('counter'), $my_counter);
         } else {
             $db->update($model->getFullTableName('counter'), $my_counter['id'], $my_counter);
         }
         return $new_day;
     } else {
         // เรียก method โดยตรง
         new \Kotchasan\Http\NotFound('Do not call method directly');
     }
 }