Layout.class.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <?php
  2. /**
  3. *
  4. *
  5. * @author Ironpilot
  6. * @copyright Copywrite (c) 2011, STAPLE CODE
  7. *
  8. * This file is part of the STAPLE Framework.
  9. *
  10. * The STAPLE Framework is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by the
  12. * Free Software Foundation, either version 3 of the License, or (at your option)
  13. * any later version.
  14. *
  15. * The STAPLE Framework is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  18. * more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. class Staple_Layout
  24. {
  25. const DOC_HTML4_TRANS = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">';
  26. const DOC_HTML4_STRICT = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';
  27. const DOC_HTML5 = '<!DOCTYPE HTML>';
  28. const DOC_XHTML_TRANS = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
  29. const DOC_XHTML_STRICT = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
  30. /**
  31. * The name/filename of the layout.
  32. * @var string
  33. */
  34. protected $name;
  35. /**
  36. * Array of Script includes to add to the page source
  37. * @var array
  38. */
  39. protected $scripts = array();
  40. /**
  41. * Array of Script Blocks to write to the page source
  42. * @var array
  43. */
  44. protected $scriptBlocks = array();
  45. /**
  46. * An array of Stylesheets to add to the page.
  47. * @var array
  48. */
  49. protected $styles = array();
  50. /**
  51. * An array of META information
  52. * @var array[string]
  53. */
  54. protected $metas = array();
  55. /**
  56. * The view object
  57. * @var Staple_View
  58. */
  59. protected $view;
  60. /**
  61. * The buffered script output from the controller.
  62. * @var string
  63. */
  64. protected $buffer;
  65. /**
  66. * The page title.
  67. * @var string
  68. */
  69. public $title;
  70. /**
  71. * Stores the DocType for the layout
  72. * @var unknown_type
  73. */
  74. public $doctype;
  75. /**
  76. * The dynamic datastore.
  77. * @var array
  78. */
  79. protected $_store = array();
  80. public function __construct($name = NULL, $doctype = "html5")
  81. {
  82. if(isset($name))
  83. {
  84. $this->setName($name);
  85. }
  86. switch($doctype)
  87. {
  88. case 'html4_trans':
  89. case 'html4':
  90. $this->doctype = self::DOC_HTML4_TRANS;
  91. break;
  92. case 'html4_strict':
  93. $this->doctype = self::DOC_HTML4_STRICT;
  94. break;
  95. case 'xhtml_trans':
  96. case 'xhtml':
  97. $this->doctype = self::DOC_XHTML_TRANS;
  98. break;
  99. case 'xhtml_strict':
  100. $this->doctype = self::DOC_XHTML_STRICT;
  101. break;
  102. default:
  103. $this->doctype = self::DOC_HTML5;
  104. }
  105. $settings = Staple_Config::get('layout');
  106. if(is_array($settings))
  107. {
  108. //Add the default title to the layout
  109. if(array_key_exists('title', $settings))
  110. {
  111. $this->setTitle($settings['title']);
  112. }
  113. //Add the default scripts to the layout
  114. if(array_key_exists('scripts', $settings))
  115. {
  116. if(is_array($settings['scripts']))
  117. {
  118. foreach($settings['scripts'] as $src)
  119. {
  120. $this->addScript($src);
  121. }
  122. }
  123. else
  124. {
  125. $this->addScript($settings['scripts']);
  126. }
  127. }
  128. //Add the default styles to the layout
  129. if(array_key_exists('styles', $settings))
  130. {
  131. if(is_array($settings['styles']))
  132. {
  133. foreach($settings['styles'] as $href)
  134. {
  135. $this->addStylesheet($href);
  136. }
  137. }
  138. else
  139. {
  140. $this->layout->addStylesheet($settings['styles']);
  141. }
  142. }
  143. //Add the default metas to the layout
  144. if(array_key_exists('meta_description', $settings))
  145. {
  146. $this->setMetas('description', $settings['meta_description']);
  147. }
  148. if(array_key_exists('meta_keywords', $settings))
  149. {
  150. $this->setMetas('keywords', $settings['meta_keywords']);
  151. }
  152. }
  153. }
  154. /**
  155. * Overloaded __set allows for dynamic addition of properties.
  156. * @param string | int $key
  157. * @param mixed $value
  158. */
  159. public function __set($key,$value)
  160. {
  161. $this->_store[$key] = $value;
  162. }
  163. /**
  164. * Retrieves a stored property.
  165. * @param string | int $key
  166. */
  167. public function __get($key)
  168. {
  169. if(array_key_exists($key,$this->_store))
  170. {
  171. return $this->_store[$key];
  172. }
  173. else
  174. {
  175. return NULL;
  176. }
  177. }
  178. public function __sleep()
  179. {
  180. return array('name','scripts','styles','metas','view','title','doctype');
  181. }
  182. public function addScript($script,$keepProtocol = false)
  183. {
  184. if($keepProtocol != true)
  185. {
  186. $script = str_replace(array('http://','https://'), 'proto://', $script);
  187. }
  188. if(in_array($script, $this->scripts) === false)
  189. {
  190. $this->scripts[] = $script;
  191. }
  192. return $this;
  193. }
  194. public function removeScript($script)
  195. {
  196. if(($key = array_search($script, $this->scripts)) !== false)
  197. {
  198. unset($this->scripts[$key]);
  199. }
  200. return $this;
  201. }
  202. public function addScriptBlock($script)
  203. {
  204. if(!in_array($script, $this->scriptBlocks))
  205. {
  206. $this->scriptBlocks[] = $script;
  207. }
  208. return $this;
  209. }
  210. public function addStylesheet($style, $media = 'all')
  211. {
  212. if(array_key_exists($media, $this->styles))
  213. {
  214. if(is_array($this->styles[$media]))
  215. {
  216. if(in_array($style, $this->styles[$media]) === false)
  217. {
  218. $this->styles[$media][] = $style;
  219. }
  220. }
  221. else
  222. {
  223. $this->styles[$media] = array($style);
  224. }
  225. }
  226. else
  227. {
  228. $this->styles[$media] = array($style);
  229. }
  230. return $this;
  231. }
  232. public function removeStylesheet($style, $media)
  233. {
  234. if(array_key_exists($media, $this->styles))
  235. {
  236. if(is_array($this->styles[$media]))
  237. {
  238. if(($key = array_search($style, $this->styles[$media])) !== false)
  239. {
  240. unset($this->styles[$media][$key]);
  241. }
  242. }
  243. }
  244. return $this;
  245. }
  246. public function addMeta($name, $content)
  247. {
  248. if(!(array_key_exists($name, $this->metas)))
  249. {
  250. $this->metas[$name] = $content;
  251. }
  252. return $this;
  253. }
  254. public function removeMeta($name)
  255. {
  256. if(array_key_exists($name, $this->metas))
  257. {
  258. unset($this->metas[$name]);
  259. }
  260. return $this;
  261. }
  262. /**
  263. * @return the $metas
  264. */
  265. public function getMetas($name)
  266. {
  267. if(array_key_exists($name, $this->metas))
  268. {
  269. return $this->metas[$name];
  270. }
  271. else
  272. {
  273. return NULL;
  274. }
  275. }
  276. /**
  277. * @param field_type $metas
  278. */
  279. public function setMetas($name,$content)
  280. {
  281. $this->metas[$name] = $content;
  282. return $this;
  283. }
  284. public function setView(Staple_View $view)
  285. {
  286. $this->view = $view;
  287. return $this;
  288. }
  289. /**
  290. * @return the $title
  291. */
  292. public function getTitle()
  293. {
  294. return $this->title;
  295. }
  296. /**
  297. * @param string $title
  298. */
  299. public function setTitle($title)
  300. {
  301. $this->title = $title;
  302. return $this;
  303. }
  304. /**
  305. * @return the $name
  306. */
  307. public function getName()
  308. {
  309. return $this->name;
  310. }
  311. /**
  312. * Set the Layout Name
  313. * @param string $name
  314. * @throws Exception
  315. */
  316. public function setName($name)
  317. {
  318. if(ctype_alnum(str_replace('_','',$name)))
  319. {
  320. $this->name = $name;
  321. }
  322. else
  323. {
  324. throw new Exception('Invalid Layout', Staple_Error::APPLICATION_ERROR);
  325. }
  326. return $this;
  327. }
  328. public static function layoutExists($layoutName)
  329. {
  330. if(ctype_alnum(str_replace('_','',$layoutName)))
  331. {
  332. if(file_exists(LAYOUT_ROOT.$layoutName.'.phtml'))
  333. {
  334. return true;
  335. }
  336. }
  337. return false;
  338. }
  339. /*---------------------------------------Helper Fuctions---------------------------------------*/
  340. /**
  341. *
  342. * If an array is supplied, a link is created to a controller/action. If a string is
  343. * supplied, a file link is specified.
  344. * @param string | array $link
  345. * @param array $get
  346. */
  347. public function link($link,array $get = array())
  348. {
  349. return Staple_Link::get($link,$get);
  350. }
  351. /**
  352. * @see Staple_View::escape()
  353. * @param string $estring
  354. * @param boolean $strip
  355. */
  356. public static function escape($estring, $strip = false)
  357. {
  358. return Staple_View::escape($estring,$strip);
  359. }
  360. /*---------------------------------------Builder Fuctions---------------------------------------*/
  361. public function doctype()
  362. {
  363. echo $this->doctype."\r\n";
  364. }
  365. /**
  366. * Prints the stylesheets to the document
  367. */
  368. public function styles()
  369. {
  370. foreach($this->styles as $media=>$styles)
  371. {
  372. foreach($styles as $href)
  373. {
  374. switch($this->doctype)
  375. {
  376. case self::DOC_XHTML_TRANS:
  377. case self::DOC_XHTML_STRICT:
  378. echo "<link href=\"".htmlentities($href)."\" rel=\"stylesheet\" type=\"text/css\" media=\"".htmlentities($media)."\" />\n";
  379. break;
  380. default:
  381. echo "<link href=\"".htmlentities($href)."\" rel=\"stylesheet\" type=\"text/css\" media=\"".htmlentities($media)."\">\n";
  382. }
  383. }
  384. }
  385. }
  386. public function scripts()
  387. {
  388. $secure = Staple_Request::isSecure();
  389. foreach($this->scripts as $src)
  390. {
  391. if($secure === true)
  392. {
  393. $src = str_replace('proto://','https://', $src);
  394. }
  395. else
  396. {
  397. $src = str_replace('proto://','http://', $src);
  398. }
  399. echo "<script src=\"".htmlentities($src)."\" type=\"text/javascript\"></script>\n";
  400. }
  401. foreach($this->scriptBlocks as $sBlock)
  402. {
  403. echo "<script type=\"text/javascript\">\n<!--\n";
  404. echo $sBlock;
  405. echo "\n-->\n</script>\n";
  406. }
  407. }
  408. public function metas()
  409. {
  410. foreach($this->metas as $name=>$content)
  411. {
  412. switch($this->doctype)
  413. {
  414. case self::DOC_XHTML_TRANS:
  415. case self::DOC_XHTML_STRICT:
  416. echo "<meta name=\"".htmlentities($name)."\" content=\"".htmlentities($content)."\" />\n";
  417. break;
  418. default:
  419. echo "<meta name=\"".htmlentities($name)."\" content=\"".htmlentities($content)."\">\n";
  420. }
  421. }
  422. }
  423. public function content()
  424. {
  425. if(isset($this->buffer))
  426. {
  427. echo $this->buffer;
  428. }
  429. if($this->view instanceof Staple_View)
  430. {
  431. $this->view->build();
  432. }
  433. }
  434. public function build($buffer = NULL)
  435. {
  436. if(isset($this->name))
  437. {
  438. if(isset($buffer))
  439. {
  440. $this->buffer = $buffer;
  441. }
  442. $layout = Staple_Main::get()->getLoader()->loadLayout($this->name);
  443. include $layout;
  444. }
  445. else
  446. {
  447. throw new Exception("Attempted to build unknown layout.", Staple_Error::APPLICATION_ERROR);
  448. }
  449. }
  450. }
  451. ?>