Are you a regular stikked user? Signup so you can keep track of your pastes!

Layout Lib 17/12/08

By Phil Sturgeon, 1 Year ago, written in PHP.
URL http://stikked.com/view/89135719
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author Rick Ellis
  9. * @copyright Copyright (c) 2006, EllisLab, Inc.
  10. * @license http://www.codeignitor.com/user_guide/license.html
  11. * @link http://www.codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15.  
  16. // ------------------------------------------------------------------------
  17.  
  18. /**
  19. * CodeIgniter Layout Class
  20. *
  21. * Permits admin pages to be constructed easier.
  22. *
  23. * @package CodeIgniter
  24. * @subpackage Libraries
  25. * @category Libraries
  26. * @author Philip Sturgeon
  27. * @link
  28. */
  29. class Layout {
  30.  
  31. var $page_body = '';
  32. var $_directory = '';
  33. var $_module = '';
  34. var $_controller = '';
  35. var $_method = '';
  36.  
  37. var $_page_title = '';
  38. var $_extra_head_content = '';
  39. var $_breadcrumbs = array();
  40. var $_navigation = array();
  41.  
  42. // Default wrapper files
  43. var $layout_file = 'layout.php';
  44.  
  45. var $folder_mode = 'matchbox'; // 'subdir', 'matchbox'
  46. var $wrap_mode = true;
  47. var $html_mode = false;
  48.  
  49. // Seconds that cache will be alive for
  50. var $cache_lifetime = 0;//7200;
  51.  
  52. var $CI;
  53. var $data;
  54.  
  55. /**
  56. * Constructor - Calls the CI instance and sets a debug message
  57. *
  58. * The constructor can be passed an array of config values
  59. */
  60. function __construct()
  61. {
  62. $this->CI =& get_instance();
  63. log_message('debug', "Template Class Initialized");
  64.  
  65. if($this->folder_mode == 'subdir'):
  66.  
  67. if($this->_module == '' && $this->CI->uri->router->fetch_directory() != '/'):
  68. $this->_module = str_replace('/', '', $this->CI->uri->router->fetch_directory());
  69. endif;
  70.  
  71. elseif($this->folder_mode == 'matchbox'):
  72.  
  73. if($this->_module == '' && $this->CI->matchbox->fetch_module() != ''):
  74. $this->_module = str_replace(array('modules/', '/'), '', $this->CI->matchbox->fetch_module());
  75. endif;
  76. endif;
  77. $this->_controller = strtolower(get_class($this->CI));
  78. $s = $this->CI->uri->rsegment_array();
  79. $n = array_search($this->_controller, $s);
  80. $this->_method = $this->CI->uri->rsegment($n+1);
  81. }
  82.  
  83. // --------------------------------------------------------------------
  84.  
  85. /**
  86. * Set the mode of the creation
  87. *
  88. * @access public
  89. * @param string
  90. * @return void
  91. */
  92. function create($page_body = '', $data = NULL, $return = false, $module = '')
  93. {
  94. if($page_body != '') $this->page_body = $page_body;
  95. if($module != '') $this->_module = $module;
  96.  
  97. // Merge all the data together
  98. $this->CI->load->helper('array');
  99. array_object_merge($this->data, $data);
  100.  
  101. if(empty($this->_page_title)) $this->_guess_title();
  102.  
  103. // Set the basic defaults
  104. $this->data->page_title = $this->_page_title;
  105. //$this->data->navigation = $this->_create_navigation();
  106. $this->data->breadcrumbs = $this->_create_breadcrumbs();
  107. $this->data->extra_head_content = $this->_extra_head_content;
  108.  
  109. // Disable sodding IE7's constant cacheing!!
  110. $this->CI->output->set_header("HTTP/1.0 200 OK");
  111. $this->CI->output->set_header("HTTP/1.1 200 OK");
  112. $this->CI->output->set_header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
  113. $this->CI->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
  114. $this->CI->output->set_header("Cache-Control: post-check=0, pre-check=0, max-age=0");
  115. $this->CI->output->set_header('Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
  116. $this->CI->output->set_header("Pragma: no-cache");
  117.  
  118. // Let CI do the caching instead of bloody IE7
  119. $this->CI->output->cache($this->cache_lifetime);
  120.  
  121. // Time to make the body, load view or parse HTML?
  122. if($this->html_mode):
  123. $output = $this->page_body;
  124.  
  125. else:
  126. // If directory or module is set, use it
  127. $view_file = (!empty($this->_directory)) ? $this->_directory.'/'.$this->page_body : $this->page_body;
  128. $output = $this->CI->load->view($view_file, $this->data, true, $this->_module);
  129. endif;
  130. // Want this file wrapped with the layout file?
  131. if($this->wrap_mode):
  132. // Send what we have so far to the layout view
  133. $this->data->page_output = $output;
  134. // If directory is set, use it
  135. $layout_file = (!empty($this->_directory)) ? $this->_directory.'/'.$this->layout_file : $this->layout_file;
  136. $output = $this->CI->load->view($layout_file, $this->data, true);
  137. else:
  138. $this->data->page_output = $output;
  139. endif;
  140.  
  141. // Want it returned or output to browser?
  142. if($return):
  143. return $output;
  144. else:
  145. // Send it to output
  146. $this->CI->output->set_output($output);
  147. endif;
  148.  
  149. }
  150.  
  151. /**
  152. * Set the title of the page
  153. *
  154. * @access public
  155. * @param string
  156. * @return void
  157. */
  158.  
  159. function title($title = '')
  160. {
  161. if($title != '') $this->_page_title = $title;
  162. }
  163.  
  164. /**
  165. * Put extra javascipt, css, meta tags, etc
  166. *
  167. * @access public
  168. * @param string
  169. * @return void
  170. */
  171.  
  172. function extra_head($str = '')
  173. {
  174. $this->_extra_head_content .= $str."\n";
  175. }
  176.  
  177. function module($module = '')
  178. {
  179. $this->_module = $module;
  180. }
  181.  
  182. /**
  183. * Should we include headers and footers?
  184. *
  185. * @access public
  186. * @param string
  187. * @return void
  188. */
  189.  
  190. function html_mode($html = true)
  191. {
  192. $this->html_mode = $html;
  193. }
  194.  
  195. /**
  196. * Should we include headers and footers?
  197. *
  198. * @access public
  199. * @param string
  200. * @return void
  201. */
  202.  
  203. function wrap_mode($wrap = true)
  204. {
  205. $this->wrap_mode = $wrap;
  206. }
  207.  
  208.  
  209. /**
  210. * Create navigations for a specific page
  211. *
  212. * @access public
  213. * @param string
  214. * @param string
  215. * @return void
  216. */
  217.  
  218. function navigation($links = array())
  219. {
  220. $this->_navigation = $this->_navigation + $links;
  221. }
  222.  
  223.  
  224. /**
  225. * Helps build custom breadcrumb trails
  226. *
  227. * @access public
  228. * @param string
  229. * @param string
  230. * @return void
  231. */
  232.  
  233. function add_breadcrumb($name, $url_ref = '')
  234. {
  235. $this->_breadcrumbs[] = array('name'=>$name, 'url_ref'=>$segment);
  236. }
  237.  
  238.  
  239. function _guess_title()
  240. {
  241. $this->CI->load->helper('inflector');
  242.  
  243. // Obviously no title, lets get making one
  244. $title_parts = array();
  245.  
  246. // Is there a module?
  247. if(!empty($this->_module)) $title_parts[] = strtolower($this->_module);
  248. else $title_parts[] = strtolower($this->_controller);
  249.  
  250. // If the method is something other than index, use that
  251. if($this->_method != 'index' && $title_parts[0] != $this->_method) $title_parts[] = $this->_method;
  252.  
  253. $this->_page_title = humanize(implode(' | ', $title_parts));
  254.  
  255. return $this->_page_title;
  256. }
  257.  
  258.  
  259. // Build the array into a string with anchors and ->'s
  260. function _create_navigation()
  261. {/*
  262. $nav_parts = array();
  263. foreach($this->_navigation as $text => $link):
  264. // Support javascript links
  265. if(strpos($link, 'javascript:') === 0):
  266. $nav_parts[] = '<a href="'.$link.'" title="'.$text.'">'.$text.'</a>';
  267. else:
  268. $nav_parts[] = anchor($link, $text);
  269. endif;
  270. endforeach;
  271. return $nav_parts;
  272. */}
  273.  
  274.  
  275. // Build the array into a string with anchors and ->'s
  276. function _create_breadcrumbs()
  277. {
  278. $this->CI->load->helper('inflector');
  279.  
  280. // No crumbs (other than possibly the section crumb we just added)?
  281. if(count($this->_breadcrumbs) <= 1):
  282.  
  283. $url_parts = array();
  284. $segment_array = $this->CI->uri->segment_array();
  285. $last_segment = array_pop($segment_array);
  286. foreach($segment_array as $url_ref):
  287. // Skip if we already have this breadcrumb and its not admin
  288. if(in_array($url_ref, $url_parts) or $url_ref == 'admin') continue;
  289.  
  290. $url_parts[] = $url_ref;
  291. $this->_breadcrumbs[] = array('name'=>humanize(str_replace('-', ' ', $url_ref)), 'url_ref'=>implode('/', $url_parts), 'current_page'=>false);
  292. endforeach;
  293. $url_parts[] = $last_segment;
  294. $this->_breadcrumbs[] = array('name'=>humanize(str_replace('-', ' ', $last_segment)), 'url_ref'=>implode('/', $url_parts), 'current_page'=>true);
  295.  
  296. endif;
  297.  
  298. return $this->_breadcrumbs;
  299. }
  300.  
  301. }
  302. // END Layout class
  303. ?>

Reply to "Layout Lib 17/12/08"

Here you can reply to the paste above

Create a snipurl

Make Private

Feeling clever? Set some advanced options.