/src/system/application/libraries/SZ_Loader.php
PHP | 434 lines | 297 code | 48 blank | 89 comment | 45 complexity | 73f720ad88784a4169e13210906a1b3c MD5 | raw file
Possible License(s): GPL-2.0
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- /**
- * ====================================================
- * Extended CodeIgniter builtin Loader Class
- *
- * @addtionalmethods:
- * area : Load area data
- * block : Load block data
- * block_view : Load block view data
- * template view : Load template view data
- * sz_include : include file in template handles.
- *
- * @package Seezoo Core
- * @author Yoshiaki Sugimoto <neo.yoshiaki.sugimoto@gmail.com>
- * ====================================================
- */
- class SZ_Loader extends CI_Loader
- {
- function __construct()
- {
- parent::CI_Loader();
- }
- /**
- * Load Area method
- * @param string$area
- * @return void
- */
- function area($area = FALSE, $editable = TRUE, $moveable = TRUE)
- {
- if (!$area)
- {
- show_error('undefined area name');
- }
- $CI =& get_instance();
- if ($CI->edit_mode === 'EDIT_SELF')
- {
- $sql = 'SELECT * FROM areas '
- . 'WHERE area_name = ? AND page_id = ? '
- . 'ORDER BY area_id DESC '
- . 'LIMIT 1';
- $query = $CI->db->query($sql, array($area, $CI->page_id, $CI->version_number));
- }
- else
- {
- $sql = 'SELECT * FROM areas '
- . 'WHERE area_name = ? AND page_id = ? '
- . 'ORDER BY area_id DESC '
- . 'LIMIT 1';
- $query = $CI->db->query($sql, array($area, $CI->page_id, $CI->version_number));
- }
- if ($query->num_rows() === 0)
- {
- // if record is NULL, create new record
- $area_property = array(
- 'area_name' => $area,
- 'page_id' => $CI->page_id,
- 'created_date' => db_datetime()
- );
- $CI->db->insert('areas', $area_property);
- $area_property['area_id'] = $CI->db->insert_id();
- }
- else
- {
- $area_property = $query->row_array();
- }
- new Area($area_property, $editable, $moveable);
- }
- /**
- * Load block Object
- * @param string $cname
- * @param int $bid
- * @param bool $returnable
- * @return mixed
- */
- function block($cname = FALSE, $bid = FALSE, $returnable = FALSE)
- {
- if (!$cname)
- {
- return;
- }
- // to correct basename
- $cname = kill_traversal($cname);
- $CI =& get_instance();
- if ( ! class_exists(ucfirst($cname . '_block')))
- {
- $block_path = 'blocks/' . $cname . '/' . $cname . '.php';
-
- if (file_exists(SZ_EXT_PATH . $block_path))
- {
- require_once(SZ_EXT_PATH . $block_path);
- }
- else
- {
- require_once(FCPATH . $block_path);
- }
- }
- $name = ucfirst($cname) . '_block';
- $b = new $name();
- $b->collection_name = $cname;
- $b->init($bid, $cname);
- if ($returnable === FALSE)
- {
- $this->block_view($cname . '/view', array('controller' => $b));
- }
- else
- {
- return $b;
- }
- }
- function block_view($view, $vars = array(), $return = FALSE)
- {
- // split path
- $dir = substr($view, 0, strrpos($view, '/')) . '/';
- $file = substr($view, strrpos($view, '/'));
-
- // carrier detection
- switch ( config_item('final_output_mode'))
- {
- case 'sp':
- $prefix = 'smartphone/';
- break;
- case 'mb':
- $prefix = 'mobile/';
- break;
- default:
- $prefix = '';
- }
- $ext_view = $dir . $prefix . $file;
-
- if (file_exists(SZ_EXT_PATH . 'blocks/' . $ext_view.EXT))
- {
- $this->_ci_view_path = SZ_EXT_PATH . 'blocks/';
- $view = $ext_view;
- }
- else if ( file_exists(FCPATH . 'blocks/' . $ext_view.EXT))
- {
- $this->_ci_view_path = FCPATH . 'blocks/';
- $view = $ext_view;
- }
- else if ( file_exists(SZ_EXT_PATH . 'blocks/' . $view.EXT))
- {
- $this->_ci_view_path = SZ_EXT_PATH . 'blocks/';
- }
- else
- {
- $this->_ci_view_path = FCPATH . 'blocks/';
- }
-
-
- return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
- }
- function template_view($view, $vars = array(), $return = FALSE)
- {
- $this->_ci_view_path = 'templates/';
- $vars = $this->_merge_page_data_vars($vars);
- return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
- }
- function _merge_page_data_vars($vars)
- {
- $CI =& get_instance();
- if ( ! isset($CI->page_data))
- {
- return $vars;
- }
- if (is_object($vars))
- {
- $vars = $this->_ci_object_to_array($vars);
- }
- if (! is_array($vars))
- {
- $vars = array($vars);
- }
- return array_merge($CI->page_data, $vars);
- }
- // overwrite core method
- function view($view, $vars = array(), $return = FALSE)
- {
- $ext_view = SZ_EXT_PATH. 'views/' . $view.EXT;
- if ( file_exists($ext_view))
- {
- $this->_ci_view_path = SZ_EXT_PATH . 'views/';
- }
- else
- {
- $this->_ci_view_path = APPPATH . 'views/';
- }
- $vars = $this->_merge_page_data_vars($vars);
- return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
- }
- // load template parts
- function sz_include($file)
- {
- $CI =& get_instance();
- // this method works cms mode only.
- if ( ! isset($CI->cms_mode) || $CI->cms_mode !== TRUE)
- {
- return '';
- }
- $ext = 'php';
- if (($pos = strrpos($file, '.')) === FALSE)
- {
- $ext = substr($file, $pos + 1);
- $filename = substr($file, 0, $pos);
- }
- $this->_ci_view_path = $CI->relative_template_path;
- if ($ext == 'php')
- {
- return $this->_ci_load(array('_ci_view' => $file, '_ci_vars' => array(), '_ci_return' => FALSE));
- }
- else
- {
- return $this->file($file, FALSE);
- }
- }
- /**
- * override helper method
- *
- * @param $helpers
- */
- function helper($helpers = array())
- {
- if ( ! is_array($helpers))
- {
- $helpers = array($helpers);
- }
-
- foreach ($helpers as $helper)
- {
- $helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper');
- if (isset($this->_ci_helpers[$helper]))
- {
- continue;
- }
-
- $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.EXT;
-
- // package
- $pkg_ext_helper = SZ_EXT_PATH.'helpers/'.config_item('subclass_prefix').$helper.EXT;
- // Is this a package extension request?
- if (file_exists($pkg_ext_helper))
- {
- $base_helper = BASEPATH.'helpers/'.$helper.EXT;
-
- if ( ! file_exists($base_helper))
- {
- show_error('Unable to load the requested file: helpers/'.$helper.EXT);
- }
-
- include_once($pkg_ext_helper);
- include_once($base_helper);
- }
- else if (file_exists(SZ_EXT_PATH.'helpers/'.$helper.EXT))
- {
- include_once(SZ_EXT_PATH.'helpers/'.$helper.EXT);
- }
- // Is this a helper extension request?
- else if (file_exists($ext_helper))
- {
- $base_helper = BASEPATH.'helpers/'.$helper.EXT;
-
- if ( ! file_exists($base_helper))
- {
- show_error('Unable to load the requested file: helpers/'.$helper.EXT);
- }
-
- include_once($ext_helper);
- include_once($base_helper);
- }
- elseif (file_exists(APPPATH.'helpers/'.$helper.EXT))
- {
- include_once(APPPATH.'helpers/'.$helper.EXT);
- }
- else
- {
- if (file_exists(BASEPATH.'helpers/'.$helper.EXT))
- {
- include_once(BASEPATH.'helpers/'.$helper.EXT);
- }
- else
- {
- show_error('Unable to load the requested file: helpers/'.$helper.EXT);
- }
- }
- $this->_ci_helpers[$helper] = TRUE;
- log_message('debug', 'Helper loaded: '.$helper);
- }
- // if ( ! is_array($helpers))
- // {
- // $helpers = array($helpers);
- // }
- //
- // foreach ($helpers as $helper)
- // {
- // $helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper');
- //
- // if (isset($this->_ci_helpers[$helper]))
- // {
- // continue;
- // }
- //
- // $ext_path = SZ_EXT_PATH . 'helpers/';
- //
- // if ( file_exists($ext_path . config_item('subclass_prefix') . $helper.EXT))
- // {
- // include_once($ext_path . config_item('subclass_prefix') . $helper.EXT);
- // parent::helper($helper);
- //// $this->_ci_helpers[$helper] = TRUE;
- //// continue;
- // }
- // else if ( file_exists($ext_path . $helper.EXT))
- // {
- // include_once($ext_path . $helper.EXT);
- //
- // if ( file_exists(BASEPATH . 'helpers/' . $helper.EXT)
- // || file_exists(APPPATH . 'helpers/' . $helper.EXT))
- // {
- // parent::helper($helper);
- // }
- // continue;
- // }
- // else
- // {
- // parent::helper($helper);
- // }
- // }
- }
- /**
- * override model method
- * try original plugins model file load.
- * if file not exists, call Core Loader::model method.
- * ### notice: plugin model file name should be differnt of Seezoo Core Model filenames.
- */
- function model($model, $name = '', $db_conn = FALSE)
- {
- if (is_array($model))
- {
- foreach($model as $babe)
- {
- $this->model($babe);
- }
- return;
- }
- if ($model == '')
- {
- return;
- }
- // stack to original path
- $orig_model = $model;
- $orig_name = $name;
- // Is the model in a sub-folder? If so, parse out the filename and path.
- if (strpos($model, '/') === FALSE)
- {
- $path = '';
- }
- else
- {
- $x = explode('/', $model);
- $model = end($x);
- unset($x[count($x)-1]);
- $path = implode('/', $x).'/';
- }
- if ($name == '')
- {
- $name = $model;
- $orig_name = $name;
- }
- if (in_array($name, $this->_ci_models, TRUE))
- {
- return;
- }
- $CI =& get_instance();
- if (isset($CI->$name))
- {
- show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
- }
- $model = strtolower($model);
- if ( ! file_exists(SZ_EXT_PATH.'models/'.$path.$model.EXT))
- {
- parent::model($orig_model, $orig_name, $db_conn);
- return;
- }
- if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
- {
- if ($db_conn === TRUE)
- $db_conn = '';
- $CI->load->database($db_conn, FALSE, TRUE);
- }
- if ( ! class_exists('Model'))
- {
- load_class('Model', FALSE);
- }
- require_once(SZ_EXT_PATH.'models/'.$path.$model.EXT);
- $model = ucfirst($model);
- $CI->$name = new $model();
- $CI->$name->_assign_libraries();
- $this->_ci_models[] = $name;
- }
- }