You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

315 lines
15 KiB

7 years ago
  1. <?php
  2. /**
  3. * Class Site
  4. * ------------------------------------------------------------------
  5. * 사이트 전역설정및 레이아웃과 관련된 라이브러리
  6. */
  7. class Site {
  8. public $viewmode;
  9. public $device;
  10. public $lang;
  11. protected $config;
  12. protected $css_before = array();
  13. protected $css_after = array();
  14. protected $js_before = array();
  15. protected $js_after = array();
  16. public $meta_title = "";
  17. public $meta_description = "";
  18. public $meta_keywords = "";
  19. public $meta_image = "";
  20. /**********************************************************
  21. * 사이트 전역설정중 특정 컬럼의 값을 반환한다.
  22. * @param $column 반활할 컬럼 이름
  23. * @return var 컬럼의
  24. *********************************************************/
  25. public function config($column) {
  26. // 컬럼값이 없으면 리턴한다.
  27. if( empty($column) ) return NULL;
  28. // 캐시 드라이버 로드
  29. $CI =& get_instance();
  30. $CI->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file', 'key_prefix' => PROJECT));
  31. if( ! $config = $CI->cache->get('site_config') )
  32. {
  33. $result = $CI->db->get("config");
  34. $config_list = $result->result_array();
  35. $config = array();
  36. foreach( $config_list as $row ) {
  37. $config[$row['cfg_key']] = $row['cfg_value'];
  38. }
  39. $CI->cache->save('site_config', $config);
  40. }
  41. return element($column, $config, NULL);
  42. }
  43. /**
  44. * 사이트 메뉴를 가져온다
  45. */
  46. public function menu() {
  47. $CI =& get_instance();
  48. $CI->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file', 'key_prefix' => PROJECT));
  49. if( ! $menu = $CI->cache->get('site_menu_'. $this->viewmode ) )
  50. {
  51. $menu = $CI->db->where('mnu_'.$this->viewmode, 'Y')->where('mnu_parent','0')->order_by('mnu_order ASC')->get('menu')->result_array();
  52. // 2차메뉴 가져오기
  53. foreach($menu as &$row)
  54. {
  55. $row['children']= $CI->db->where('mnu_'.$this->viewmode, 'Y')->where('mnu_parent',$row['mnu_idx'])->order_by('mnu_order ASC')->get('menu')->result_array();
  56. foreach( $row['children'] as &$rw )
  57. {
  58. $rw['children']= $CI->db->where('mnu_'.$this->viewmode, 'Y')->where('mnu_parent',$rw['mnu_idx'])->order_by('mnu_order ASC')->get('menu')->result_array();
  59. }
  60. }
  61. $CI->cache->save('site_menu_'. $this->viewmode, $menu);
  62. }
  63. // active
  64. foreach($menu as &$mnu)
  65. {
  66. $mnu['active'] = (! empty($mnu['mnu_active_key']) && $CI->active == $mnu['mnu_active_key']);
  67. foreach($mnu['children'] as &$mnu2)
  68. {
  69. foreach($mnu['children'] as &$mnu3)
  70. {
  71. $mnu3['active'] = (! empty($mnu3['mnu_active_key']) && $CI->active == $mnu3['mnu_active_key']);
  72. if( $mnu3['active'] ) {
  73. $mnu2['active'] = TRUE;
  74. $mnu['active'] = TRUE;
  75. break;
  76. }
  77. }
  78. if(! empty($mnu2['mnu_active_key']) && $CI->active == $mnu2['mnu_active_key'] )
  79. {
  80. $mnu2['active'] = TRUE;
  81. $mnu['active'] = TRUE;
  82. }
  83. }
  84. if(! empty($mnu['mnu_active_key']) && $CI->active == $mnu['mnu_active_key'] )
  85. {
  86. $mnu['active'] = TRUE;
  87. }
  88. }
  89. return $menu;
  90. }
  91. /*********************************************************
  92. * 현재 접속 기기에 따라 필요한 레이아웃을 가져온다.
  93. *********************************************************/
  94. public function get_layout()
  95. {
  96. return ( $this->viewmode == DEVICE_MOBILE ) ? THEME_MOBILE : THEME_DESKTOP;
  97. }
  98. /*********************************************************
  99. * 사이트에 사용할 CSS를 추가합니다.
  100. * @param $url 추가할 CSS
  101. * @param bool $insert_last 마지막에 추가할지 처음에 추가할지
  102. ********************************************************/
  103. public function add_css( $url, $insert_first = FALSE) {
  104. if(!empty($url) && ! in_array($url, $this->css_after) && !in_array($url, $this->css_before)) {
  105. if( $insert_first ) {
  106. array_push($this->css_before, $url);
  107. }
  108. else {
  109. array_push($this->css_after, $url);
  110. }
  111. }
  112. }
  113. /*********************************************************
  114. * 사이트에 사용할 JS를 추가한다.
  115. * @param $url 추가할 JS
  116. * @param bool $insert_last 마지막에 추가할것인가?
  117. ********************************************************/
  118. public function add_js( $url, $insert_first = FALSE ) {
  119. if(!empty($url) && ! in_array($url, $this->js_before) && ! in_array($url, $this->js_after)) {
  120. if( $insert_first ) {
  121. array_push($this->js_before, $url);
  122. }
  123. else {
  124. array_push($this->js_after, $url);
  125. }
  126. }
  127. }
  128. /*********************************************************
  129. * 배열에 담긴 CSS를 메타태그와 함께 같이 출력한다.
  130. * @return string
  131. ********************************************************/
  132. public function display_css() {
  133. $CI =& get_instance();
  134. $return = '';
  135. // Layout 기본 CSS가 있다면 추가한다.
  136. if( $CI->skin_type && $CI->skin && file_exists(VIEWPATH.'/'.DIR_SKIN.'/'.$CI->skin_type.'/'.$CI->skin.'/skin.min.css')) {
  137. $this->add_css( base_url("views/".DIR_SKIN."/".$CI->skin_type.'/'.$CI->skin."/skin.min.css"), TRUE);
  138. }
  139. else if( $CI->skin_type && $CI->skin && file_exists(VIEWPATH.'/'.DIR_SKIN.'/'.$CI->skin_type.'/'.$CI->skin.'/skin.css')) {
  140. $this->add_css( base_url("views/".DIR_SKIN."/".$CI->skin_type.'/'.$CI->skin."/skin.css"), TRUE);
  141. }
  142. $css_array = array_merge($this->css_before, $this->css_after);
  143. $css_array = array_unique($css_array);
  144. foreach($css_array as $css) {
  145. if( is_my_domain( $css ) ) {
  146. $filepath = str_replace(base_url(), "/", $css);
  147. $css .= "?" . date('YmdHis', filemtime( FCPATH.ltrim($filepath,DIRECTORY_SEPARATOR) ));
  148. if( ! (strpos($css, base_url()) !== FALSE) ) {
  149. $css = base_url($css);
  150. }
  151. }
  152. $return .= '<link rel="stylesheet" href="'.$css.'" />'.PHP_EOL;
  153. }
  154. return $return;
  155. }
  156. /*********************************************************
  157. * 배열에 담긴 JS를 메타태그와 함께 같이 출력한다.
  158. * @return string
  159. ********************************************************/
  160. public function display_js() {
  161. $CI =& get_instance();
  162. $return = '';
  163. if( $CI->skin_type && $CI->skin && file_exists(VIEWPATH.'/'.DIR_SKIN.'/'.$CI->skin_type.'/'.$CI->skin.'/skin.min.js')) {
  164. $this->add_js(base_url("views/".DIR_SKIN."/".$CI->skin_type.'/'.$CI->skin."/skin.min.js"), TRUE);
  165. }
  166. else if ($CI->skin_type && $CI->skin && file_exists(VIEWPATH.'/'.DIR_SKIN.'/'.$CI->skin_type.'/'.$CI->skin.'/skin.js')) {
  167. $this->add_js(base_url("views/".DIR_SKIN."/".$CI->skin_type.'/'.$CI->skin."/skin.js"), TRUE);
  168. }
  169. $js_array = array_merge($this->js_before, $this->js_after);
  170. $js_array = array_unique($js_array);
  171. foreach($js_array as $js) {
  172. if( is_my_domain( $js ) ) {
  173. $filepath = str_replace(base_url(), "/", $js);
  174. $js .= "?" . date('YmdHis', filemtime( FCPATH.ltrim($filepath,DIRECTORY_SEPARATOR) ));
  175. if( ! (strpos($js, base_url()) !== FALSE) ) {
  176. $js = base_url($js);
  177. }
  178. }
  179. $return .= '<script src="'.$js.'"></script>'.PHP_EOL;
  180. }
  181. // 사이트를 위한 javascript
  182. $return .= '<script>';
  183. $return .= 'var base_url="'.base_url().'";';
  184. $return .= 'var current_url="'.current_url().'";';
  185. $return .= 'var cookie_domain="'.COOKIE_DOMAIN.'";';
  186. $return .= 'var is_admin='.( PAGE_ADMIN ? 'true' : 'false' ).';';
  187. $return .= '</script>';
  188. return $return;
  189. }
  190. /*********************************************************
  191. * 페이지의 타이틀을 가져온다.
  192. ********************************************************/
  193. public function page_title() {
  194. $this->meta_title = str_replace(' :: '.$this->config('site_title'), "", $this->meta_title);
  195. $this->meta_title = $this->meta_title ? $this->meta_title : $this->config('site_subtitle');
  196. if( ! empty($this->meta_title) ) $this->meta_title .= ' :: ';
  197. $this->meta_title .= $this->config('site_title');
  198. return $this->meta_title;
  199. }
  200. /*********************************************************
  201. * 메타태그를 자동으로 생성하여 표시한다.
  202. ********************************************************/
  203. public function display_meta(){
  204. // Default 값 설정
  205. $this->page_title();
  206. $this->meta_description = $this->meta_description ? $this->meta_description : $this->config('site_meta_description');
  207. $this->meta_keywords = $this->meta_keywords ? $this->meta_keywords : "";
  208. $this->meta_image = $this->meta_image ?
  209. $this->meta_image : str_replace(DIRECTORY_SEPARATOR, "/", $this->config('site_meta_image' )
  210. ? base_url(str_replace(DIRECTORY_SEPARATOR, "/", $this->config('site_meta_image' ))) : NULL);
  211. $default_keywords = explode(",", $this->config('site_meta_keywords'));
  212. $in_keywords = explode(",", $this->meta_keywords);
  213. foreach($in_keywords as $keyword) {
  214. $keyword = trim($keyword);
  215. if(! in_array($keyword, $default_keywords)) {
  216. array_push($default_keywords, $keyword);
  217. }
  218. }
  219. $default_keywords = array_unique($default_keywords);
  220. $this->meta_keywords = "";
  221. // 합친 키워드를 다시 직렬화
  222. foreach($default_keywords as $keyword) {
  223. $this->meta_keywords .= $keyword.",";
  224. }
  225. $this->meta_keywords = rtrim($this->meta_keywords,",");
  226. // 기본태그
  227. $return = "";
  228. $return .= '<meta charset="utf-8">'.PHP_EOL;
  229. $return .= (($this->viewmode == DEVICE_DESKTOP) ? '<meta name="viewport" content="width=device-width,initial-scale=1">' : '<meta name="viewport" content="width=device-width,initial-scale=1">') .PHP_EOL;
  230. $return .= '<meta http-equiv="X-UA-Compatible" content="IE=edge">'.PHP_EOL;
  231. // 기본 메타 태그
  232. $return .= '<title>' . $this->meta_title . '</title>'.PHP_EOL;
  233. $return .= '<meta name="description" content="'.$this->meta_description.'">'.PHP_EOL;
  234. $return .= '<meta name="keywords" content="'. $this->meta_keywords.'">'.PHP_EOL;
  235. $return .= ($this->meta_image ? '<link rel="image_src" href="'.$this->meta_image.'">': '') .PHP_EOL;
  236. // 페이스북 메타 태그
  237. $return .= '<meta property="og:title" content="'.$this->meta_title.'" />' .PHP_EOL;
  238. $return .= '<meta property="og:type" content="article" />' .PHP_EOL;
  239. $return .= '<meta property="og:url" content="'.current_url().'" />' .PHP_EOL;
  240. $return .= ($this->meta_image ? '<meta property="og:image" content="'.$this->meta_image.'" />': '') .PHP_EOL;
  241. $return .= '<meta property="og:description" content="'.$this->meta_description.'" />'.PHP_EOL;
  242. $return .= '<meta property="og:site_name" content="'.$this->config('site_title').'" />'.PHP_EOL;
  243. // 트위터 메타 태그
  244. $return .= '<meta name="twitter:card" content="summary"/>'.PHP_EOL;
  245. $return .= '<meta name="twitter:site" content="'.$this->config('site_title').'"/>'.PHP_EOL;
  246. $return .= '<meta name="twitter:title" content="'.$this->meta_title.'">'.PHP_EOL;
  247. $return .= '<meta name="twitter:description" content="'.$this->meta_description.'"/>'.PHP_EOL;
  248. $return .= '<meta name="twitter:creator" content="'.$this->config('site_title').'"/>'.PHP_EOL;
  249. $return .= ($this->meta_image ? '<meta name="twitter:image:src" content="'.$this->meta_image.'"/>' : '').PHP_EOL;
  250. $return .= '<meta name="twitter:domain" content="'.base_url().'"/>'.PHP_EOL;
  251. // 네이트온 메타 태그
  252. $return .= '<meta name="nate:title" content="'.$this->meta_title.'" />'.PHP_EOL;
  253. $return .= '<meta name="nate:description" content="'.$this->meta_description.'" />'.PHP_EOL;
  254. $return .= '<meta name="nate:site_name" content="'.$this->config('site_title').'" />'.PHP_EOL;
  255. $return .= '<meta name="nate:url" content="'.current_url().'" />'.PHP_EOL;
  256. $return .= ($this->meta_image ? '<meta name="nate:image" content="'.$this->meta_image.'" />' : '').PHP_EOL;
  257. // 파비콘
  258. $return .= '<link rel="apple-touch-icon" sizes="57x57" href="/apple-icon-57x57.png">'.PHP_EOL;
  259. $return .= '<link rel="apple-touch-icon" sizes="60x60" href="/apple-icon-60x60.png">'.PHP_EOL;
  260. $return .= '<link rel="apple-touch-icon" sizes="72x72" href="/apple-icon-72x72.png">'.PHP_EOL;
  261. $return .= '<link rel="apple-touch-icon" sizes="76x76" href="/apple-icon-76x76.png">'.PHP_EOL;
  262. $return .= '<link rel="apple-touch-icon" sizes="114x114" href="/apple-icon-114x114.png">'.PHP_EOL;
  263. $return .= '<link rel="apple-touch-icon" sizes="120x120" href="/apple-icon-120x120.png">'.PHP_EOL;
  264. $return .= '<link rel="apple-touch-icon" sizes="144x144" href="/apple-icon-144x144.png">'.PHP_EOL;
  265. $return .= '<link rel="apple-touch-icon" sizes="152x152" href="/apple-icon-152x152.png">'.PHP_EOL;
  266. $return .= '<link rel="apple-touch-icon" sizes="180x180" href="/apple-icon-180x180.png">'.PHP_EOL;
  267. $return .= '<link rel="icon" type="image/png" sizes="192x192" href="/android-icon-192x192.png">'.PHP_EOL;
  268. $return .= '<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">'.PHP_EOL;
  269. $return .= '<link rel="icon" type="image/png" sizes="96x96" href="/favicon-96x96.png">'.PHP_EOL;
  270. $return .= '<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">'.PHP_EOL;
  271. $return .= '<link rel="manifest" href="/manifest.json">'.PHP_EOL;
  272. $return .= '<meta name="msapplication-TileColor" content="#ffffff">'.PHP_EOL;
  273. $return .= '<meta name="msapplication-TileImage" content="/ms-icon-144x144.png">'.PHP_EOL;
  274. $return .= '<meta name="theme-color" content="#ffffff">'.PHP_EOL;
  275. $return .= '<link rel="canonical" href="'.current_full_url().'">'.PHP_EOL;
  276. // Verification 이 있다면 메타태그 추가
  277. if(! empty($this->config('verification_google')) ) $return .= $this->config('verification_google') .PHP_EOL;
  278. if(! empty($this->config('verification_naver')) ) $return .= $this->config('verification_naver').PHP_EOL;
  279. $CI =& get_instance();
  280. return $return;
  281. }
  282. }