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.

105 lines
4.4 KiB

7 years ago
5 years ago
7 years ago
  1. <?php
  2. defined('BASEPATH') OR exit();
  3. /**
  4. * A base controller for CodeIgniter with view autoloading, layout support,
  5. * model loading, helper loading, asides/partials and per-controller 404
  6. *
  7. * @link http://github.com/jamierumbelow/codeigniter-base-controller
  8. * @copyright Copyright (c) 2012, Jamie Rumbelow <http://jamierumbelow.net>
  9. */
  10. class WB_Controller extends CI_Controller
  11. {
  12. public $view = FALSE;
  13. public $data = array();
  14. protected $asides = array();
  15. public $theme = FALSE;
  16. public $theme_file = "theme";
  17. public $active = NULL;
  18. public $sub_active = NULL;
  19. public $skin = NULL;
  20. public $skin_type = NULL;
  21. public function __construct()
  22. {
  23. parent::__construct();
  24. }
  25. public function _remap($method)
  26. {
  27. if (method_exists($this, $method)) call_user_func_array(array($this, $method), array_slice($this->uri->rsegments, 2));
  28. else
  29. {
  30. if (method_exists($this, '_404')) call_user_func_array(array($this, '_404'), array($method));
  31. else show_404(strtolower(get_class($this)).'/'.$method);
  32. }
  33. $this->_load_view();
  34. }
  35. protected function _load_view()
  36. {
  37. if( empty($this->view) ) return;
  38. $this->data['skin_url'] = ( $this->skin && $this->skin_type ) ? base_url("views/".DIR_SKIN . '/' . $this->skin_type . '/' . $this->skin . '/') : NULL;
  39. $this->data['theme_url'] = (isset($this->theme) && $this->theme != FALSE) ? base_url("views/". DIR_THEME . "/" . $this->theme . "/") : NULL;
  40. if( $this->skin && $this->skin_type )
  41. {
  42. $view = DIR_SKIN . '/' . $this->skin_type . '/' . $this->skin . '/' . $this->view;
  43. }
  44. else if ( isset($this->theme) && $this->theme )
  45. {
  46. $view = DIR_THEME . '/' . $this->theme . '/' . $this->view;
  47. }
  48. else {
  49. $view = $this->view;
  50. }
  51. if (!empty($this->asides) && is_array($this->asides))
  52. {
  53. foreach ($this->asides as $name => $file)
  54. {
  55. $file_url = (isset($this->theme) && $this->theme) ? DIR_THEME . '/' . $this->theme .'/' . $file : $file;
  56. $this->data['asides_'.$name] = $this->load->view($file_url, $this->data, TRUE);
  57. }
  58. }
  59. $data['contents'] = $this->load->view($view, $this->data, TRUE);
  60. if( $this->skin && $this->skin_type )
  61. {
  62. $data['contents'] = "<div id=\"skin-{$this->skin_type}-{$this->skin}\">" . $data['contents'] . '</div>';
  63. }
  64. $data = array_merge($this->data, $data);
  65. $theme = (isset($this->theme) && $this->theme != FALSE) ? $this->theme : NULL;
  66. $output_data = ($theme) ? $this->load->view( DIR_THEME . '/' . $theme. '/' . $this->theme_file , $data, TRUE) : $data['contents'];
  67. $output_data = preg_replace_callback('!\[widget([^\]]*)\]!is', array($this, '_widget_replace'), $output_data);
  68. $this->output->set_output($output_data);
  69. }
  70. protected function _widget_replace( $matches )
  71. {
  72. $vars = trim($matches[1]);
  73. $vars = preg_replace('/\r\n|\r|\n|\t/',' ',$vars);
  74. $vars = str_replace( array('"',' '), array('',' '), $vars );
  75. $vars = trim(str_replace( " ", '&', $vars ));
  76. parse_str($vars, $vars_array);
  77. $vars_array = array_merge( $vars_array, $this->data );
  78. $vars_array['CI'] = get_instance();
  79. // Name이 정의되지 않았다면 리턴
  80. if( ! isset($vars_array['name']) ) return $this->load->view('tools/widget_error', array("message"=>"위젯 속성중 [name] 값이 정의되지 않았습니다."), TRUE);
  81. if( ! file_exists( VIEWPATH . DIR_WIDGET . '/' . $vars_array['name']."/widget.php") ) return $this->load->view('tools/widget_error', array("message"=>"{$vars_array['name']} 위젯파일이 존재하지 않습니다."), TRUE);
  82. // CSS와 JS파일이 있다면 로드
  83. if( file_exists( VIEWPATH . DIR_WIDGET . '/' . $vars_array['name']."/widget.css") ) $this->site->add_css( '/views/' . DIR_WIDGET . '/' . $vars_array['name'] . "/widget.css");
  84. if( file_exists( VIEWPATH . DIR_WIDGET . '/' . $vars_array['name']."/widget.js") ) $this->site->add_js( '/views/' . DIR_WIDGET . '/' . $vars_array['name'] . "/widget.js");
  85. return "<div class=\"widget-{$vars_array['name']}\">". $this->load->view( DIR_WIDGET . '/' . $vars_array['name'] . '/widget', $vars_array, TRUE ) . "</div>";
  86. }
  87. }