PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/system/application/libraries/SZ_Loader.php

https://bitbucket.org/seezoo/seezoo/
PHP | 434 lines | 297 code | 48 blank | 89 comment | 45 complexity | 73f720ad88784a4169e13210906a1b3c MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * ====================================================
  4. * Extended CodeIgniter builtin Loader Class
  5. *
  6. * @addtionalmethods:
  7. * area : Load area data
  8. * block : Load block data
  9. * block_view : Load block view data
  10. * template view : Load template view data
  11. * sz_include : include file in template handles.
  12. *
  13. * @package Seezoo Core
  14. * @author Yoshiaki Sugimoto <neo.yoshiaki.sugimoto@gmail.com>
  15. * ====================================================
  16. */
  17. class SZ_Loader extends CI_Loader
  18. {
  19. function __construct()
  20. {
  21. parent::CI_Loader();
  22. }
  23. /**
  24. * Load Area method
  25. * @param string$area
  26. * @return void
  27. */
  28. function area($area = FALSE, $editable = TRUE, $moveable = TRUE)
  29. {
  30. if (!$area)
  31. {
  32. show_error('undefined area name');
  33. }
  34. $CI =& get_instance();
  35. if ($CI->edit_mode === 'EDIT_SELF')
  36. {
  37. $sql = 'SELECT * FROM areas '
  38. . 'WHERE area_name = ? AND page_id = ? '
  39. . 'ORDER BY area_id DESC '
  40. . 'LIMIT 1';
  41. $query = $CI->db->query($sql, array($area, $CI->page_id, $CI->version_number));
  42. }
  43. else
  44. {
  45. $sql = 'SELECT * FROM areas '
  46. . 'WHERE area_name = ? AND page_id = ? '
  47. . 'ORDER BY area_id DESC '
  48. . 'LIMIT 1';
  49. $query = $CI->db->query($sql, array($area, $CI->page_id, $CI->version_number));
  50. }
  51. if ($query->num_rows() === 0)
  52. {
  53. // if record is NULL, create new record
  54. $area_property = array(
  55. 'area_name' => $area,
  56. 'page_id' => $CI->page_id,
  57. 'created_date' => db_datetime()
  58. );
  59. $CI->db->insert('areas', $area_property);
  60. $area_property['area_id'] = $CI->db->insert_id();
  61. }
  62. else
  63. {
  64. $area_property = $query->row_array();
  65. }
  66. new Area($area_property, $editable, $moveable);
  67. }
  68. /**
  69. * Load block Object
  70. * @param string $cname
  71. * @param int $bid
  72. * @param bool $returnable
  73. * @return mixed
  74. */
  75. function block($cname = FALSE, $bid = FALSE, $returnable = FALSE)
  76. {
  77. if (!$cname)
  78. {
  79. return;
  80. }
  81. // to correct basename
  82. $cname = kill_traversal($cname);
  83. $CI =& get_instance();
  84. if ( ! class_exists(ucfirst($cname . '_block')))
  85. {
  86. $block_path = 'blocks/' . $cname . '/' . $cname . '.php';
  87. if (file_exists(SZ_EXT_PATH . $block_path))
  88. {
  89. require_once(SZ_EXT_PATH . $block_path);
  90. }
  91. else
  92. {
  93. require_once(FCPATH . $block_path);
  94. }
  95. }
  96. $name = ucfirst($cname) . '_block';
  97. $b = new $name();
  98. $b->collection_name = $cname;
  99. $b->init($bid, $cname);
  100. if ($returnable === FALSE)
  101. {
  102. $this->block_view($cname . '/view', array('controller' => $b));
  103. }
  104. else
  105. {
  106. return $b;
  107. }
  108. }
  109. function block_view($view, $vars = array(), $return = FALSE)
  110. {
  111. // split path
  112. $dir = substr($view, 0, strrpos($view, '/')) . '/';
  113. $file = substr($view, strrpos($view, '/'));
  114. // carrier detection
  115. switch ( config_item('final_output_mode'))
  116. {
  117. case 'sp':
  118. $prefix = 'smartphone/';
  119. break;
  120. case 'mb':
  121. $prefix = 'mobile/';
  122. break;
  123. default:
  124. $prefix = '';
  125. }
  126. $ext_view = $dir . $prefix . $file;
  127. if (file_exists(SZ_EXT_PATH . 'blocks/' . $ext_view.EXT))
  128. {
  129. $this->_ci_view_path = SZ_EXT_PATH . 'blocks/';
  130. $view = $ext_view;
  131. }
  132. else if ( file_exists(FCPATH . 'blocks/' . $ext_view.EXT))
  133. {
  134. $this->_ci_view_path = FCPATH . 'blocks/';
  135. $view = $ext_view;
  136. }
  137. else if ( file_exists(SZ_EXT_PATH . 'blocks/' . $view.EXT))
  138. {
  139. $this->_ci_view_path = SZ_EXT_PATH . 'blocks/';
  140. }
  141. else
  142. {
  143. $this->_ci_view_path = FCPATH . 'blocks/';
  144. }
  145. return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
  146. }
  147. function template_view($view, $vars = array(), $return = FALSE)
  148. {
  149. $this->_ci_view_path = 'templates/';
  150. $vars = $this->_merge_page_data_vars($vars);
  151. return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
  152. }
  153. function _merge_page_data_vars($vars)
  154. {
  155. $CI =& get_instance();
  156. if ( ! isset($CI->page_data))
  157. {
  158. return $vars;
  159. }
  160. if (is_object($vars))
  161. {
  162. $vars = $this->_ci_object_to_array($vars);
  163. }
  164. if (! is_array($vars))
  165. {
  166. $vars = array($vars);
  167. }
  168. return array_merge($CI->page_data, $vars);
  169. }
  170. // overwrite core method
  171. function view($view, $vars = array(), $return = FALSE)
  172. {
  173. $ext_view = SZ_EXT_PATH. 'views/' . $view.EXT;
  174. if ( file_exists($ext_view))
  175. {
  176. $this->_ci_view_path = SZ_EXT_PATH . 'views/';
  177. }
  178. else
  179. {
  180. $this->_ci_view_path = APPPATH . 'views/';
  181. }
  182. $vars = $this->_merge_page_data_vars($vars);
  183. return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
  184. }
  185. // load template parts
  186. function sz_include($file)
  187. {
  188. $CI =& get_instance();
  189. // this method works cms mode only.
  190. if ( ! isset($CI->cms_mode) || $CI->cms_mode !== TRUE)
  191. {
  192. return '';
  193. }
  194. $ext = 'php';
  195. if (($pos = strrpos($file, '.')) === FALSE)
  196. {
  197. $ext = substr($file, $pos + 1);
  198. $filename = substr($file, 0, $pos);
  199. }
  200. $this->_ci_view_path = $CI->relative_template_path;
  201. if ($ext == 'php')
  202. {
  203. return $this->_ci_load(array('_ci_view' => $file, '_ci_vars' => array(), '_ci_return' => FALSE));
  204. }
  205. else
  206. {
  207. return $this->file($file, FALSE);
  208. }
  209. }
  210. /**
  211. * override helper method
  212. *
  213. * @param $helpers
  214. */
  215. function helper($helpers = array())
  216. {
  217. if ( ! is_array($helpers))
  218. {
  219. $helpers = array($helpers);
  220. }
  221. foreach ($helpers as $helper)
  222. {
  223. $helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper');
  224. if (isset($this->_ci_helpers[$helper]))
  225. {
  226. continue;
  227. }
  228. $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.EXT;
  229. // package
  230. $pkg_ext_helper = SZ_EXT_PATH.'helpers/'.config_item('subclass_prefix').$helper.EXT;
  231. // Is this a package extension request?
  232. if (file_exists($pkg_ext_helper))
  233. {
  234. $base_helper = BASEPATH.'helpers/'.$helper.EXT;
  235. if ( ! file_exists($base_helper))
  236. {
  237. show_error('Unable to load the requested file: helpers/'.$helper.EXT);
  238. }
  239. include_once($pkg_ext_helper);
  240. include_once($base_helper);
  241. }
  242. else if (file_exists(SZ_EXT_PATH.'helpers/'.$helper.EXT))
  243. {
  244. include_once(SZ_EXT_PATH.'helpers/'.$helper.EXT);
  245. }
  246. // Is this a helper extension request?
  247. else if (file_exists($ext_helper))
  248. {
  249. $base_helper = BASEPATH.'helpers/'.$helper.EXT;
  250. if ( ! file_exists($base_helper))
  251. {
  252. show_error('Unable to load the requested file: helpers/'.$helper.EXT);
  253. }
  254. include_once($ext_helper);
  255. include_once($base_helper);
  256. }
  257. elseif (file_exists(APPPATH.'helpers/'.$helper.EXT))
  258. {
  259. include_once(APPPATH.'helpers/'.$helper.EXT);
  260. }
  261. else
  262. {
  263. if (file_exists(BASEPATH.'helpers/'.$helper.EXT))
  264. {
  265. include_once(BASEPATH.'helpers/'.$helper.EXT);
  266. }
  267. else
  268. {
  269. show_error('Unable to load the requested file: helpers/'.$helper.EXT);
  270. }
  271. }
  272. $this->_ci_helpers[$helper] = TRUE;
  273. log_message('debug', 'Helper loaded: '.$helper);
  274. }
  275. // if ( ! is_array($helpers))
  276. // {
  277. // $helpers = array($helpers);
  278. // }
  279. //
  280. // foreach ($helpers as $helper)
  281. // {
  282. // $helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper');
  283. //
  284. // if (isset($this->_ci_helpers[$helper]))
  285. // {
  286. // continue;
  287. // }
  288. //
  289. // $ext_path = SZ_EXT_PATH . 'helpers/';
  290. //
  291. // if ( file_exists($ext_path . config_item('subclass_prefix') . $helper.EXT))
  292. // {
  293. // include_once($ext_path . config_item('subclass_prefix') . $helper.EXT);
  294. // parent::helper($helper);
  295. //// $this->_ci_helpers[$helper] = TRUE;
  296. //// continue;
  297. // }
  298. // else if ( file_exists($ext_path . $helper.EXT))
  299. // {
  300. // include_once($ext_path . $helper.EXT);
  301. //
  302. // if ( file_exists(BASEPATH . 'helpers/' . $helper.EXT)
  303. // || file_exists(APPPATH . 'helpers/' . $helper.EXT))
  304. // {
  305. // parent::helper($helper);
  306. // }
  307. // continue;
  308. // }
  309. // else
  310. // {
  311. // parent::helper($helper);
  312. // }
  313. // }
  314. }
  315. /**
  316. * override model method
  317. * try original plugins model file load.
  318. * if file not exists, call Core Loader::model method.
  319. * ### notice: plugin model file name should be differnt of Seezoo Core Model filenames.
  320. */
  321. function model($model, $name = '', $db_conn = FALSE)
  322. {
  323. if (is_array($model))
  324. {
  325. foreach($model as $babe)
  326. {
  327. $this->model($babe);
  328. }
  329. return;
  330. }
  331. if ($model == '')
  332. {
  333. return;
  334. }
  335. // stack to original path
  336. $orig_model = $model;
  337. $orig_name = $name;
  338. // Is the model in a sub-folder? If so, parse out the filename and path.
  339. if (strpos($model, '/') === FALSE)
  340. {
  341. $path = '';
  342. }
  343. else
  344. {
  345. $x = explode('/', $model);
  346. $model = end($x);
  347. unset($x[count($x)-1]);
  348. $path = implode('/', $x).'/';
  349. }
  350. if ($name == '')
  351. {
  352. $name = $model;
  353. $orig_name = $name;
  354. }
  355. if (in_array($name, $this->_ci_models, TRUE))
  356. {
  357. return;
  358. }
  359. $CI =& get_instance();
  360. if (isset($CI->$name))
  361. {
  362. show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
  363. }
  364. $model = strtolower($model);
  365. if ( ! file_exists(SZ_EXT_PATH.'models/'.$path.$model.EXT))
  366. {
  367. parent::model($orig_model, $orig_name, $db_conn);
  368. return;
  369. }
  370. if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
  371. {
  372. if ($db_conn === TRUE)
  373. $db_conn = '';
  374. $CI->load->database($db_conn, FALSE, TRUE);
  375. }
  376. if ( ! class_exists('Model'))
  377. {
  378. load_class('Model', FALSE);
  379. }
  380. require_once(SZ_EXT_PATH.'models/'.$path.$model.EXT);
  381. $model = ucfirst($model);
  382. $CI->$name = new $model();
  383. $CI->$name->_assign_libraries();
  384. $this->_ci_models[] = $name;
  385. }
  386. }