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.

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