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.

309 lines
13 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
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. {
  48. $CI =& get_instance();
  49. $CI->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file', 'key_prefix' => PROJECT));
  50. $menu_key = $this->viewmode == DEVICE_MOBILE ? 'menu_mobile' : 'menu_desktop';
  51. $where_key = $this->viewmode == DEVICE_MOBILE ? 'mnu_mobile' : 'mnu_desktop';
  52. if( ! $menu_list = $CI->cache->get($menu_key) )
  53. {
  54. $CI->db->where($where_key, 'Y');
  55. $result = $CI->db->get("menu");
  56. $menu_list = $result->result_array();
  57. $menu_list = $this->menu_arrange($menu_list);
  58. $CI->cache->save($menu_key, $menu_list);
  59. }
  60. foreach($menu_list as &$menu1) {
  61. $menu1['active'] = $CI->active == $menu1['mnu_active_key'];
  62. foreach($menu1['children'] as &$mnu2)
  63. {
  64. $mnu2['active'] = $CI->active == $mnu2['mnu_active_key'];
  65. foreach($mnu2['children'] as &$mnu3)
  66. {
  67. $mnu3['active'] = $CI->active == $mnu3['mnu_active_key'];
  68. if( $mnu3['active'] ) {
  69. $mnu2['active'] = TRUE;
  70. $menu1['active'] = TRUE;
  71. break;
  72. }
  73. }
  74. if($CI->active == $mnu2['mnu_active_key'] )
  75. {
  76. $mnu2['active'] = TRUE;
  77. $menu1['active'] = TRUE;
  78. }
  79. }
  80. if( $CI->active == $menu1['mnu_active_key'] )
  81. {
  82. $menu1['active'] = TRUE;
  83. }
  84. }
  85. //print_r2($menu_list);
  86. return $menu_list;
  87. }
  88. private function menu_arrange($array, $parent_id=0)
  89. {
  90. $return = array();
  91. foreach($array as $arr) {
  92. $arr['active'] = FALSE;
  93. if( $arr['mnu_parent'] == $parent_id ) {
  94. $children = $this->menu_arrange( $array, $arr['mnu_idx'] );
  95. if( $children ) {
  96. $arr['children'] = $children;
  97. }
  98. $return[] = $arr;
  99. }
  100. }
  101. return $return;
  102. }
  103. /*********************************************************
  104. * 현재 접속 기기에 따라 필요한 레이아웃을 가져온다.
  105. *********************************************************/
  106. public function get_layout()
  107. {
  108. return ( $this->viewmode == DEVICE_MOBILE ) ? THEME_MOBILE : THEME_DESKTOP;
  109. }
  110. /*********************************************************
  111. * 사이트에 사용할 CSS를 추가합니다.
  112. * @param $url 추가할 CSS
  113. * @param bool $insert_last 마지막에 추가할지 처음에 추가할지
  114. ********************************************************/
  115. public function add_css( $url, $insert_first = FALSE) {
  116. if(!empty($url) && ! in_array($url, $this->css_after) && !in_array($url, $this->css_before)) {
  117. if( $insert_first ) {
  118. array_push($this->css_before, $url);
  119. }
  120. else {
  121. array_push($this->css_after, $url);
  122. }
  123. }
  124. }
  125. /*********************************************************
  126. * 사이트에 사용할 JS를 추가한다.
  127. * @param $url 추가할 JS
  128. * @param bool $insert_last 마지막에 추가할것인가?
  129. ********************************************************/
  130. public function add_js( $url, $insert_first = FALSE ) {
  131. if(!empty($url) && ! in_array($url, $this->js_before) && ! in_array($url, $this->js_after)) {
  132. if( $insert_first ) {
  133. array_push($this->js_before, $url);
  134. }
  135. else {
  136. array_push($this->js_after, $url);
  137. }
  138. }
  139. }
  140. /*********************************************************
  141. * 배열에 담긴 CSS를 메타태그와 함께 같이 출력한다.
  142. * @return string
  143. ********************************************************/
  144. public function display_css() {
  145. $CI =& get_instance();
  146. $return = '';
  147. // Layout 기본 CSS가 있다면 추가한다.
  148. if( $CI->skin_type && $CI->skin && file_exists(VIEWPATH.'/'.DIR_SKIN.'/'.$CI->skin_type.'/'.$CI->skin.'/skin.min.css')) {
  149. $this->add_css( base_url("views/".DIR_SKIN."/".$CI->skin_type.'/'.$CI->skin."/skin.min.css"), TRUE);
  150. }
  151. else if( $CI->skin_type && $CI->skin && file_exists(VIEWPATH.'/'.DIR_SKIN.'/'.$CI->skin_type.'/'.$CI->skin.'/skin.css')) {
  152. $this->add_css( base_url("views/".DIR_SKIN."/".$CI->skin_type.'/'.$CI->skin."/skin.css"), TRUE);
  153. }
  154. $css_array = array_merge($this->css_before, $this->css_after);
  155. $css_array = array_unique($css_array);
  156. foreach($css_array as $css) {
  157. if( is_my_domain( $css ) ) {
  158. $filepath = str_replace(base_url(), "/", $css);
  159. $css .= "?" . date('YmdHis', filemtime( FCPATH.ltrim($filepath,DIRECTORY_SEPARATOR) ));
  160. if( ! (strpos($css, base_url()) !== FALSE) ) {
  161. $css = base_url($css);
  162. }
  163. }
  164. $return .= '<link rel="stylesheet" href="'.$css.'" />'.PHP_EOL;
  165. }
  166. return $return;
  167. }
  168. /*********************************************************
  169. * 배열에 담긴 JS를 메타태그와 함께 같이 출력한다.
  170. * @return string
  171. ********************************************************/
  172. public function display_js() {
  173. $CI =& get_instance();
  174. $return = '';
  175. if( $CI->skin_type && $CI->skin && file_exists(VIEWPATH.'/'.DIR_SKIN.'/'.$CI->skin_type.'/'.$CI->skin.'/skin.min.js')) {
  176. $this->add_js(base_url("views/".DIR_SKIN."/".$CI->skin_type.'/'.$CI->skin."/skin.min.js"), TRUE);
  177. }
  178. else if ($CI->skin_type && $CI->skin && file_exists(VIEWPATH.'/'.DIR_SKIN.'/'.$CI->skin_type.'/'.$CI->skin.'/skin.js')) {
  179. $this->add_js(base_url("views/".DIR_SKIN."/".$CI->skin_type.'/'.$CI->skin."/skin.js"), TRUE);
  180. }
  181. $js_array = array_merge($this->js_before, $this->js_after);
  182. $js_array = array_unique($js_array);
  183. foreach($js_array as $js) {
  184. if( is_my_domain( $js ) ) {
  185. $filepath = str_replace(base_url(), "/", $js);
  186. $js .= "?" . date('YmdHis', filemtime( FCPATH.ltrim($filepath,DIRECTORY_SEPARATOR) ));
  187. if( ! (strpos($js, base_url()) !== FALSE) ) {
  188. $js = base_url($js);
  189. }
  190. }
  191. $return .= '<script src="'.$js.'"></script>'.PHP_EOL;
  192. }
  193. // 사이트를 위한 javascript
  194. $return .= '<script>';
  195. $return .= 'var base_url="'.base_url().'";';
  196. $return .= 'var current_url="'.current_url().'";';
  197. $return .= 'var cookie_domain="'.COOKIE_DOMAIN.'";';
  198. $return .= 'var is_admin='.( PAGE_ADMIN ? 'true' : 'false' ).';';
  199. $return .= '</script>';
  200. return $return;
  201. }
  202. /*********************************************************
  203. * 페이지의 타이틀을 가져온다.
  204. ********************************************************/
  205. public function page_title() {
  206. $this->meta_title = str_replace(' :: '.$this->config('site_title'), "", $this->meta_title);
  207. $this->meta_title = $this->meta_title ? $this->meta_title : $this->config('site_subtitle');
  208. if( ! empty($this->meta_title) ) $this->meta_title .= ' :: ';
  209. $this->meta_title .= $this->config('site_title');
  210. return $this->meta_title;
  211. }
  212. /*********************************************************
  213. * 메타태그를 자동으로 생성하여 표시한다.
  214. ********************************************************/
  215. public function display_meta(){
  216. // Default 값 설정
  217. $this->page_title();
  218. $this->meta_description = $this->meta_description ? $this->meta_description : $this->config('site_meta_description');
  219. $this->meta_keywords = $this->meta_keywords ? $this->meta_keywords : "";
  220. $this->meta_image = $this->meta_image ?
  221. $this->meta_image : str_replace(DIRECTORY_SEPARATOR, "/", $this->config('site_meta_image' )
  222. ? base_url(str_replace(DIRECTORY_SEPARATOR, "/", $this->config('site_meta_image' ))) : NULL);
  223. $default_keywords = explode(",", $this->config('site_meta_keywords'));
  224. $in_keywords = explode(",", $this->meta_keywords);
  225. foreach($in_keywords as $keyword) {
  226. $keyword = trim($keyword);
  227. if(! in_array($keyword, $default_keywords)) {
  228. array_push($default_keywords, $keyword);
  229. }
  230. }
  231. $default_keywords = array_unique($default_keywords);
  232. $this->meta_keywords = "";
  233. // 합친 키워드를 다시 직렬화
  234. foreach($default_keywords as $keyword) {
  235. $this->meta_keywords .= $keyword.",";
  236. }
  237. $this->meta_keywords = rtrim($this->meta_keywords,",");
  238. // 기본태그
  239. $return = "";
  240. $return .= '<meta charset="utf-8">'.PHP_EOL;
  241. // 기본 메타 태그
  242. $return .= '<title>' . $this->meta_title . '</title>'.PHP_EOL;
  243. $return .= '<meta name="description" content="'.$this->meta_description.'">'.PHP_EOL;
  244. $return .= '<meta name="keywords" content="'. $this->meta_keywords.'">'.PHP_EOL;
  245. $return .= ($this->meta_image ? '<link rel="image_src" href="'.$this->meta_image.'">': '') .PHP_EOL;
  246. // 페이스북 메타 태그
  247. $return .= '<meta property="og:title" content="'.$this->meta_title.'" />' .PHP_EOL;
  248. $return .= '<meta property="og:type" content="article" />' .PHP_EOL;
  249. $return .= '<meta property="og:url" content="'.current_url().'" />' .PHP_EOL;
  250. $return .= ($this->meta_image ? '<meta property="og:image" content="'.$this->meta_image.'" />': '') .PHP_EOL;
  251. $return .= '<meta property="og:description" content="'.$this->meta_description.'" />'.PHP_EOL;
  252. $return .= '<meta property="og:site_name" content="'.$this->config('site_title').'" />'.PHP_EOL;
  253. // 트위터 메타 태그
  254. $return .= '<meta name="twitter:card" content="summary"/>'.PHP_EOL;
  255. $return .= '<meta name="twitter:site" content="'.$this->config('site_title').'"/>'.PHP_EOL;
  256. $return .= '<meta name="twitter:title" content="'.$this->meta_title.'">'.PHP_EOL;
  257. $return .= '<meta name="twitter:description" content="'.$this->meta_description.'"/>'.PHP_EOL;
  258. $return .= '<meta name="twitter:creator" content="'.$this->config('site_title').'"/>'.PHP_EOL;
  259. $return .= ($this->meta_image ? '<meta name="twitter:image:src" content="'.$this->meta_image.'"/>' : '').PHP_EOL;
  260. $return .= '<meta name="twitter:domain" content="'.base_url().'"/>'.PHP_EOL;
  261. // 네이트온 메타 태그
  262. $return .= '<meta name="nate:title" content="'.$this->meta_title.'" />'.PHP_EOL;
  263. $return .= '<meta name="nate:description" content="'.$this->meta_description.'" />'.PHP_EOL;
  264. $return .= '<meta name="nate:site_name" content="'.$this->config('site_title').'" />'.PHP_EOL;
  265. $return .= '<meta name="nate:url" content="'.current_url().'" />'.PHP_EOL;
  266. $return .= ($this->meta_image ? '<meta name="nate:image" content="'.$this->meta_image.'" />' : '').PHP_EOL;
  267. $return .= '<link rel="canonical" href="'.current_full_url().'">'.PHP_EOL;
  268. $return .= $this->display_css().PHP_EOL;
  269. if(! empty($this->config('extra_tag_meta')) ) $return .= $this->config('extra_tag_meta') .PHP_EOL;
  270. return $return;
  271. }
  272. }