<?php
 namespace TCMS;
 class Controller {
 public $view = null;
 public $__module = null;
 public $__layoutModule = null;
 public $isLayoutController = false;
 public $layout = true;
 public function initialize($isNewView) {
 if ($isNewView) {
 $this->view = new TCmsView();
 \GlobalVars::$mViewClass = $this->view;
 }
 }
 public function __setAppModule($module, $layoutModule) {
 $this->__module = $module;
 $this->__layoutModule = $layoutModule;
 $this->view->__setController($this);
 }
 public function lang($langKey, array $args = null) {
 $langKeyArray = explode(".", $langKey);
 if (count($langKeyArray) == 2) {
 if ($this->isLayoutController) {
 $langPath = $this->__layoutModule ? $this->__layoutModule->moduleAbsolutePath : false;
 }
 else {
 $langPath = $this->__module ? $this->__module->moduleAbsolutePath : false;
 }
 if ($langPath) {
 $langPath .= 'langs' . DIRECTORY_SEPARATOR . $langKeyArray[0] . '.' . \GlobalVars::$requestInfo['cur_lang'] . '.php';
 if (file_exists($langPath)) {
 $langData = include($langPath);
 if (isset($langData[$langKeyArray[1]])) {
 if ($args == null || count($args) === 0) {
 return $langData[$langKeyArray[1]];
 }
 $text = $langData[$langKeyArray[1]];
 foreach ($args as $k => $v) {
 $text = str_replace('{' . $k . '}', $v, $text);
 }
 return $text;
 }
 }
 }
 }
 return $langKey;
 }
 private function readerView($viewFile) {
 \GlobalVars::$isHasCallRenderViewFromCtr = true;
 if (file_exists($viewFile . TCMS_VIEW_ENGINE . '.php')) {
 $this->view->renderTCmsViewEngine($viewFile . TCMS_VIEW_ENGINE . '.php', true);
 }
 else {
 if (file_exists($viewFile . '.php')) $this->view->renderTCmsViewEngine($viewFile . '.php', false);
 else throw new TException("Không tìm thấy view file " . str_replace(MODULE_PATH, "", $viewFile), 501);
 }
 }
 public function view($arg = null, $arg2 = null) {
 $viewFile = $this->getViewFile($arg, $arg2);
 if ($viewFile == null) throw new TException("Không tìm thấy view file", 501);
 if ($this->isLayoutController) {
 $controller = 'layout';
 if (\GlobalVars::$mobileDevice) {
 $mviewFile = $this->__layoutModule->moduleAbsolutePath . 'views' . DIRECTORY_SEPARATOR . $controller . DIRECTORY_SEPARATOR . 'm.' . $viewFile;
 if (file_exists($mviewFile . TCMS_VIEW_ENGINE . '.php')) {
 $viewFile = 'm.' . $viewFile;
 }
 }
 $viewFile = $this->__layoutModule->moduleAbsolutePath . 'views' . DIRECTORY_SEPARATOR . $controller . DIRECTORY_SEPARATOR . $viewFile;
 $this->readerView($viewFile);
 }
 else {
 $controller = \GlobalVars::$requestInfo['controller'];
 $controller = substr($controller, 0, strlen($controller) - 10);
 $controller = strtolower(preg_replace('/\B([A-Z])/', '-$1', $controller));
 if (\GlobalVars::$mobileDevice) {
 $mviewFile = $this->__module->moduleAbsolutePath . 'views' . DIRECTORY_SEPARATOR . $controller . DIRECTORY_SEPARATOR . 'm.' . $viewFile;
 if (file_exists($mviewFile . TCMS_VIEW_ENGINE . '.php')) {
 $viewFile = 'm.' . $viewFile;
 }
 }
 $viewFile = $this->__module->moduleAbsolutePath . 'views' . DIRECTORY_SEPARATOR . $controller . DIRECTORY_SEPARATOR . $viewFile;
 $this->readerView($viewFile);
 }
 }
 private function getViewFile($arg, $arg2) {
 $viewFile = null;
 if (isset($arg)) {
 if (is_string($arg)) {
 $viewFile = $arg;
 if (isset($arg2)) {
 $this->view->setViewData($arg2);
 }
 }
 else if (is_array($arg)) {
 $this->view->setViewData($arg);
 }
 else if ($arg instanceof \stdClass) {
 $this->view->setViewData($arg);
 }
 else if ($arg instanceof \TCMS\Model) {
 $this->view->setViewData($arg);
 }
 }
 if ($viewFile == null && $this->view->__parentExtension == null) {
 if ($this->isLayoutController) {
 $viewFile = "index";
 }
 else {
 $viewFile = strtolower(\GlobalVars::$requestInfo['action']);
 if (strpos($viewFile, "post") === 0) {
 $viewFile = substr($viewFile, 4);
 }
 else if (strpos($viewFile, "get") === 0) {
 $viewFile = substr($viewFile, 3);
 }
 }
 }
 return $viewFile;
 }
 public function json($data) {
 $this->layout = false;
 header("Content-Type: application/json; charset=UTF-8");
 http_response_code(200);
 if ($data !== null) {
 if (is_string($data)) echo $data;
 else if(is_object($data)) {
 if($data instanceof \TCMS\Model) {
 $stdClass = new \stdClass();
 $fields = $data->getFields();
 foreach($fields as $k => $f) {
 $stdClass->$k = $f->value;
 }
 echo json_encode($stdClass);
 }
 else {
 echo json_encode($data);
 }
 }
 else if(is_array($data)) {
 echo json_encode($data);
 }
 else {
 echo $data . '';
 }
 }
 }
 }
 class ApiController {
 public $__module = null;
 public $isLayoutController = false;
 public $layout = false;
 public $isAuth = true;
 public function initialize() {
 }
 public function __setAppModule($module, $layoutModule) {
 $this->__module = $module;
 }
 protected function lang($langKey, array $args = null) {
 $langKeyArray = explode(".", $langKey);
 if (count($langKeyArray) == 2 && $this->__module) {
 $langPath = $this->__module->moduleAbsolutePath . 'langs' . DIRECTORY_SEPARATOR . $langKeyArray[0] . '.' . \GlobalVars::$requestInfo['cur_lang'] . '.php';
 if (file_exists($langPath)) {
 $langData = include($langPath);
 if (isset($langData[$langKeyArray[1]])) {
 if ($args == null || count($args) === 0) {
 return $langData[$langKeyArray[1]];
 }
 $text = $langData[$langKeyArray[1]];
 foreach ($args as $k => $v) {
 $text = str_replace('{' . $k . '}', $v, $text);
 }
 return $text;
 }
 }
 }
 return $langKey;
 }
 private function setHeader() {
 header("Access-Control-Allow-Origin: *");
 header("Content-Type: application/json; charset=UTF-8");
 header("Access-Control-Allow-Methods: POST,GET,PUT,DELETE,PATCH");
 header("Access-Control-Max-Age: 3600");
 header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
 }
 protected function json($data) {
 $this->setHeader();
 http_response_code(200);
 if ($data !== null) {
 $this->echoData($data);
 }
 }
 private function echoData($data) {
 if (is_string($data)) echo $data;
 else if(is_object($data)) {
 if($data instanceof \TCMS\Model) {
 $stdClass = new \stdClass();
 $fields = $data->getFields();
 foreach($fields as $k => $f) {
 $stdClass->$k = $f->value;
 }
 echo json_encode($stdClass);
 }
 else {
 echo json_encode($data);
 }
 }
 else if(is_array($data)) {
 echo json_encode($data);
 }
 else {
 echo $data . '';
 }
 }
 protected function ok($data = null) {
 $this->setHeader();
 http_response_code(200);
 if ($data !== null) {
 $this->echoData($data);
 }
 }
 protected function badRequest($data = null) {
 $this->setHeader();
 http_response_code(400);
 if ($data !== null) {
 $this->echoData($data);
 }
 }
 protected function forbidden($data = null) {
 $this->setHeader();
 http_response_code(403);
 if ($data !== null) {
 $this->echoData($data);
 }
 }
 protected function notFound($data = null) {
 $this->setHeader();
 http_response_code(404);
 if ($data !== null) {
 $this->echoData($data);
 }
 }
 protected function unauthorized($data = null) {
 $this->setHeader();
 http_response_code(401);
 if ($data !== null) {
 $this->echoData($data);
 }
 }
 protected function error($data = null) {
 $this->setHeader();
 http_response_code(500);
 if ($data !== null) {
 $this->echoData($data);
 }
 }
 }
 