Beispiel #1
0
 /**
  * @author  goFrendiAsgard
  *
  * @param   string view_url
  * @param   string data
  * @param   string navigation_name
  * @param   array config
  * @param   bool return_as_string
  *
  * @return string or null
  * @desc    replace $this->load->view. This method will also load header, menu etc except there is _only_content parameter via GET or POST
  */
 protected function view($view_url, $data = null, $navigation_name = null, $config = null, $return_as_string = false)
 {
     $this->load->library('template');
     $result = null;
     $view_url = $this->cms_parse_keyword($view_url);
     /*
      * PREPARE PARAMETERS *********************************************************************************************
      */
     // get dynamic widget status
     // (this is necessary since sometime the function called directly without run the constructor, i.e: when using Modules::run)
     if (isset($_REQUEST['__cms_dynamic_widget'])) {
         $this->__cms_dynamic_widget = true;
     }
     /*
      * PREPARE PARAMETERS *********************************************************************************************
      */
     // this method can be called as $this->view('view_path', $data, true);
     // or $this->view('view_path', $data, $navigation_name, true);
     if (is_bool($navigation_name) && count($config) == 0) {
         $return_as_string = $navigation_name;
         $navigation_name = null;
         $config = null;
     } else {
         if (is_bool($config)) {
             $return_as_string = $config;
             $config = null;
         }
     }
     if (!isset($return_as_string)) {
         $return_as_string = false;
     }
     if (!isset($config)) {
         $config = array();
     }
     $privilege_required = isset($config['privileges']) ? $config['privileges'] : array();
     $custom_theme = isset($config['theme']) ? $config['theme'] : null;
     $custom_layout = isset($config['layout']) ? $config['layout'] : null;
     $custom_title = isset($config['title']) ? $config['title'] : null;
     $custom_metadata = isset($config['metadata']) ? $config['metadata'] : array();
     $custom_partial = isset($config['partials']) ? $config['partials'] : null;
     $custom_keyword = isset($config['keyword']) ? $config['keyword'] : null;
     $custom_description = isset($config['description']) ? $config['description'] : null;
     $custom_author = isset($config['author']) ? $config['author'] : null;
     $only_content = isset($config['only_content']) ? $config['only_content'] : false;
     $always_allow = isset($config['always_allow']) ? $config['always_allow'] : false;
     $layout_suffix = isset($config['layout_suffix']) ? $config['layout_suffix'] : '';
     $custom_css = isset($config['css']) ? $config['css'] : '';
     $custom_js = isset($config['js']) ? $config['js'] : '';
     /*
      * GUESS $navigation_name THROUGH ITS URL  ***********************************************************************
      */
     $navigation_name_provided = true;
     if (!isset($navigation_name) && !$this->__cms_dynamic_widget) {
         $navigation_name = $this->cms_navigation_name();
         if (!$navigation_name) {
             $navigation_name_provided = false;
         }
     }
     /*
      * CHECK IF THE CURRENT NAVIGATION IS ACCESSIBLE  *****************************************************************
      */
     if (!$always_allow) {
         $this->cms_guard_page($navigation_name, $privilege_required);
     }
     // privilege is absolute
     $this->cms_guard_page(null, $privilege_required);
     /*
      * CHECK IF THE PAGE IS STATIC  **********************************************************************************
      */
     $data = (array) $data;
     $row_navigation = null;
     if ($navigation_name != null) {
         $query = $this->db->select('navigation_id, title, page_title, page_keyword, description, default_theme, default_layout, only_content, is_static, static_content')->from(cms_table_name('main_navigation'))->where(array('navigation_name' => $navigation_name))->get();
         if ($query->num_rows() > 0) {
             $row_navigation = $query->row();
         }
     }
     if ($navigation_name_provided && !isset($data['_content']) && $row_navigation != null) {
         if ($row_navigation->is_static == 1) {
             $static_content = $row_navigation->static_content;
             // static_content should contains string
             if (!$static_content) {
                 $static_content = '';
             }
             if ($this->cms_editing_mode() && $this->cms_allow_navigate('main_navigation_management')) {
                 $static_content = '<div class="row" style="padding-top:10px; padding-bottom:10px;"><a class="btn btn-primary pull-right" href="{{ SITE_URL }}main/navigation/edit/' . $row_navigation->navigation_id . '">' . '<i class="glyphicon glyphicon-pencil"></i> Edit Page' . '</a></div>' . $static_content;
             }
             $data['cms_content'] = $static_content;
             $view_url = 'CMS_View';
         }
     }
     /*
      * SHOW THE PAGE IF IT IS ACCESSIBLE  *****************************************************************************
      */
     // GET THE THEME, TITLE & ONLY_CONTENT FROM DATABASE
     $theme = '';
     $title = '';
     $keyword = '';
     $default_theme = null;
     $default_layout = null;
     $page_title = null;
     $page_keyword = null;
     $page_description = null;
     $page_author = null;
     if ($navigation_name_provided && $row_navigation != null) {
         $default_theme = $row_navigation->default_theme;
         $default_layout = $row_navigation->default_layout;
         // title
         if (isset($row_navigation->page_title) && $row_navigation->page_title !== null && $row_navigation->page_title != '') {
             $page_title = $row_navigation->page_title;
         } elseif (isset($row_navigation->title) && $row_navigation->title !== null && $row_navigation->title != '') {
             $page_title = $row_navigation->title;
         }
         $page_title = isset($page_title) && $page_title !== null ? $page_title : '';
         // keyword
         $page_keyword = isset($row_navigation->page_keyword) && $row_navigation->page_keyword !== null ? $row_navigation->page_keyword : '';
         // keyword
         $page_description = isset($row_navigation->description) && $row_navigation->description !== null ? $row_navigation->description : '';
         // only content
         if (!isset($only_content)) {
             $only_content = $row_navigation->only_content == 1;
         }
     }
     // ASSIGN THEME
     if (isset($custom_theme) && $custom_theme !== null && $custom_theme != '') {
         $theme = $custom_theme;
     } elseif (isset($default_theme) && $default_theme != null && $default_theme != '') {
         $themes = $this->cms_get_theme_list();
         $theme_path = array();
         foreach ($themes as $theme) {
             $theme_path[] = $theme['path'];
         }
         if (in_array($default_theme, $theme_path)) {
             $theme = $default_theme;
         }
     } else {
         $theme = $this->cms_get_user_theme();
         if ($theme == NULL || $theme == '') {
             $theme = $this->cms_get_config('site_theme');
         }
     }
     // ASSIGN TITLE
     $title = '';
     if (isset($custom_title) && $custom_title !== null && $custom_title != '') {
         $title = $this->cms_get_config('site_name') . ' - ' . $custom_title;
     } elseif (isset($page_title) && $page_title !== null && $page_title != '') {
         $title = $this->cms_get_config('site_name') . ' - ' . $page_title;
     } else {
         $title = $this->cms_get_config('site_name');
     }
     // ASSIGN KEYWORD
     if (isset($custom_keyword) && $custom_keyword != null && $custom_keyword != '') {
         $keyword = $custom_keyword;
     } elseif (isset($page_keyword) && $page_keyword !== null && $page_keyword != '') {
         $keyword = $page_keyword;
         if ($custom_keyword != '') {
             $keyword .= ', ' . $custom_keyword;
         }
     } else {
         $keyword = '';
     }
     // ASSIGN DESCRIPTION
     if (isset($custom_description) && $custom_description != null && $custom_description != '') {
         $description = $custom_description;
     } elseif (isset($page_description) && $page_description !== null && $page_description != '') {
         $description = $page_description;
         if ($custom_description != '') {
             $description .= ', ' . $custom_description;
         }
     } else {
         $description = '';
     }
     // ASSIGN AUTHOR
     if (isset($custom_author) && $custom_author != null && $custom_author != '') {
         $author = $custom_author;
     } else {
         $super_admin = $this->{$this->__cms_base_model_name}->cms_get_super_admin();
         $author = $super_admin['real_name'];
     }
     // GET THE LAYOUT
     if (isset($custom_layout)) {
         $layout = $custom_layout;
     } elseif (isset($default_layout) && $default_layout != '') {
         $layout = $default_layout;
     } else {
         $this->load->library('user_agent');
         $layout = $this->agent->is_mobile() ? 'mobile' : $this->cms_get_config('site_layout');
     }
     // ADJUST THEME AND LAYOUT
     if (!$this->cms_layout_exists($theme, $layout)) {
         // ASSIGN LAYOUT
         if (!file_exists(FCPATH . 'themes/' . $theme) || !is_dir(FCPATH . 'themes/' . $theme)) {
             $theme = 'neutral';
         }
         if (!file_exists(FCPATH . 'themes/' . $theme . '/views/layouts/' . $layout . '.php')) {
             $layout = 'default';
             if (!file_exists(FCPATH . 'themes/' . $theme . '/views/layouts/default.php')) {
                 $theme = 'neutral';
             }
         }
     }
     // save used_theme
     $this->session->set_userdata('__cms_used_theme', $theme);
     // ADD AUTHENTICATED SUFFIX (in case of user has logged in)
     $cms_user_id = $this->cms_user_id();
     if ($layout_suffix == '' && isset($cms_user_id) && $cms_user_id) {
         $layout_suffix = 'authenticated';
     }
     if ($this->cms_layout_exists($theme, $layout . '_' . $layout_suffix)) {
         $layout = $layout . '_' . $layout_suffix;
     }
     $data['__is_bootstrap_cdn_connected'] = false;
     // IT'S SHOW TIME
     if ($only_content || $this->__cms_dynamic_widget || isset($_REQUEST['_only_content']) || $this->input->is_ajax_request()) {
         $result = $this->load->view($view_url, $data, true);
         $result = $custom_css . $custom_js . $result;
     } else {
         // save navigation name
         $this->cms_ci_session('__cms_navigation_name', $navigation_name);
         // set theme, layout and title
         $this->template->title($title);
         $this->template->set_theme($theme);
         $this->template->set_layout($layout);
         // set keyword metadata
         if ($keyword != '') {
             $keyword_metadata = '<meta name="keyword" content="' . $keyword . '">';
             $this->template->append_metadata($keyword_metadata);
         }
         // set description metadata
         if ($description != '') {
             $description_metadata = '<meta name="description" content="' . $description . '">';
             $this->template->append_metadata($description_metadata);
         }
         // set author metadata
         if ($author != '') {
             $author_metadata = '<meta name="author" content="' . $author . '">';
             $this->template->append_metadata($author_metadata);
         }
         // add IE compatibility
         $this->template->append_metadata('<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">');
         // add width
         $this->template->append_metadata('<meta name="viewport" content="width=device-width, initial-scale=1.0">');
         $asset = new Cms_asset();
         $asset->add_js($this->JQUERY_PATH);
         // ckeditor adjustment thing
         $asset->add_internal_js($this->cms_ck_adjust_script());
         // add javascript base_url for ckeditor
         $asset->add_internal_js('var __cms_base_url = "' . base_url() . '";');
         // inject css for background
         $injected_css = 'body{';
         $css_configuration = array('site_background_image' => 'background-image', 'site_background_color' => 'background-color', 'site_text_color' => 'color', 'site_background_position' => 'background-position', 'site_background_size' => 'background-size', 'site_background_repeat' => 'background-repeat', 'site_background_origin' => 'background-origin', 'site_background_clip' => 'background-clip', 'site_background_attachment' => 'background-attachment');
         foreach ($css_configuration as $config => $css_key) {
             if (trim($this->cms_get_config($config)) != '') {
                 // get value from config
                 $value = $this->cms_get_config($config);
                 // if key is site_background_image, add "url" part
                 if ($config == 'site_background_image') {
                     $value = 'url(\'' . addslashes($value) . '\')';
                 }
                 $injected_css .= $css_key . ':' . $value . '!important;';
             }
         }
         $injected_css .= '}';
         $asset->add_internal_css($injected_css);
         // check login status
         //$login_code = '<script type="text/javascript">';
         $login_code = '';
         if ($this->cms_user_id() > 0) {
             $login_code .= 'var __cms_is_login = true;';
         } else {
             $login_code .= 'var __cms_is_login = false;';
         }
         $login_code .= 'setInterval(function(){
             $.ajax({
                 url : "{{ site_url }}main/json_login_info",
                 dataType: "json",
                 success: function(response){
                     if(response.is_login != __cms_is_login){
                         window.location = $(location).attr("href");
                     }
                 }
             });
         },300000);';
         $asset->add_internal_js($login_code);
         // google analytic
         $analytic_property_id = $this->cms_get_config('cms_google_analytic_property_id');
         if (trim($analytic_property_id) != '') {
             if ($this->cms_is_connect('google-analytics.com')) {
                 // create analytic code
                 $analytic_code = '';
                 $analytic_code .= 'var _gaq = _gaq || []; ';
                 $analytic_code .= '_gaq.push([\'_setAccount\', \'' . $analytic_property_id . '\']); ';
                 $analytic_code .= '_gaq.push([\'_trackPageview\']); ';
                 $analytic_code .= '(function() { ';
                 $analytic_code .= 'var ga = document.createElement(\'script\'); ga.type = \'text/javascript\'; ga.async = true; ';
                 $analytic_code .= 'ga.src = (\'https:\' == document.location.protocol ? \'https://ssl\' : \'http://www\') + \'.google-analytics.com/ga.js\'; ';
                 $analytic_code .= 'var s = document.getElementsByTagName(\'script\')[0]; s.parentNode.insertBefore(ga, s); ';
                 $analytic_code .= '})(); ';
                 $asset->add_internal_js($analytic_code);
             }
         }
         // add hack if exists
         if (!isset($_SESSION)) {
             session_start();
         }
         if (isset($_SESSION['__cms_flash_metadata'])) {
             $this->template->append_metadata($_SESSION['__cms_flash_metadata']);
             unset($_SESSION['__cms_flash_metadata']);
         }
         // config metadata
         foreach ($custom_metadata as $metadata) {
             $this->template->append_metadata($metadata);
         }
         // append custom css & js
         $this->template->append_js($asset->compile_js());
         $this->template->append_css($asset->compile_css());
         $this->template->append_js($custom_js);
         $this->template->append_css($custom_css);
         $this->load->helper('directory');
         $partial_path = BASEPATH . '../themes/' . $theme . '/views/partials/' . $layout . '/';
         if (is_dir($partial_path)) {
             $partials = directory_map($partial_path, 1);
             foreach ($partials as $partial) {
                 // if is directory or is not php, then ignore it
                 if (is_dir($partial)) {
                     continue;
                 }
                 $partial_extension = pathinfo($partial_path . $partial, PATHINFO_EXTENSION);
                 if (strtoupper($partial_extension) != 'PHP') {
                     continue;
                 }
                 // add partial to template
                 $partial_name = pathinfo($partial_path . $partial, PATHINFO_FILENAME);
                 if (isset($custom_partial[$partial_name])) {
                     $this->template->inject_partial($partial_name, $custom_partial[$partial_name]);
                 } else {
                     $this->template->set_partial($partial_name, 'partials/' . $layout . '/' . $partial, $data);
                 }
             }
         }
         $result = $this->template->build($view_url, $data, true);
     }
     // parse keyword
     $result = $this->cms_parse_keyword($result);
     // parse widgets used_theme & navigation_path
     $result = $this->__cms_parse_widget_theme_path($result, $theme, $layout, $navigation_name);
     $this->load->library('cms_asset');
     $asset = new Cms_asset();
     $result = $asset->minify($result, 'html');
     if ($return_as_string) {
         return $result;
     } else {
         $this->cms_show_html($result);
     }
 }