Source for file HtmlHead.php

Documentation is available at HtmlHead.php

  1. <?php
  2. /*
  3. * This file is part of Sylar.
  4. *
  5. * Sylar is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Sylar is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with Sylar. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * @copyright Copyright Sylar Development Team
  19. * @license http://www.gnu.org/licenses/ GNU Public License V2.0
  20. * @see https://launchpad.net/sylar/
  21. * @see http://www.giano-solutions.com
  22. */
  23.  
  24. import('sylar.common.system.ConfigBox');
  25. import('sylar.presentation.html.Html');
  26.  
  27.  
  28.  
  29. /**
  30. * Html Head
  31. *
  32. * It manage the <head> of html page
  33. *
  34. * @package Sylar
  35. * @version 1.0
  36. * @since 31/mar/08
  37. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  38. * @copyright Sylar Development Team
  39. */
  40. class Sylar_HtmlHead extends Sylar_Html{
  41. // Html Page <TITLE>
  42. private $sTitle;
  43. // All javascript file link
  44. private $aJsScriptLink;
  45. // All floating javascript scripts to insert in the head tag
  46. private $aFloatingJsScript;
  47. // All links to CSS file Style
  48. private $aStyleLink;
  49. // All floating Style declaration to insert in the head tag
  50. private $aFloatingStyle;
  51. // All html meta tag
  52. private $aMetaTag;
  53. // Url root for application css
  54. private $sAppCssUrlRoot;
  55. // Url root for application javascript
  56. private $sAppJsUrlRoot;
  57. function __construct($sTitle=null){
  58. if(!is_null($sTitle)){
  59. $this->setPageTitle($sTitle);
  60. }
  61. // Prepare array
  62. $this->aJsScriptLink = array();
  63. $this->aFloatingJsScript= array();
  64. $this->aStyleLink = array();
  65. $this->aFloatingStyle = array();
  66. $this->aMetaTag = array();
  67. }
  68. function __destruct(){
  69. // nothing to do at the moment
  70. }
  71.  
  72. // Setter and Getter
  73. //__________________________________________________________________________
  74. public function setAppCssUrlRoot($sAppCssUrlRoot){
  75. $this->sAppCssUrlRoot = $sAppCssUrlRoot;
  76. }
  77. public function getAppCssUrlRoot(){
  78. return $this->sAppCssUrlRoot;
  79. }
  80. public function setAppJsUrlRoot($sAppJsUrlRoot){
  81. $this->sAppJsUrlRoot = $sAppJsUrlRoot;
  82. }
  83. public function getAppJsUrlRoot(){
  84. return $this->sAppJsUrlRoot;
  85. }
  86.  
  87. // Public Methods
  88. //__________________________________________________________________________
  89.  
  90.  
  91. /**
  92. * Set the Title of HTML Page included html tag <TITLE>
  93. *
  94. * @return void
  95. * @param string $sTitle is the title without html tag
  96. */
  97. public function setPageTitle($sTitle){
  98. $this->sTitle = "<title>".$sTitle."</title>";
  99. }
  100. public function getPageTitle(){
  101. return $this->sTitle;
  102. }
  103. /**
  104. * Prepare <META> html Tag
  105. * it prepares and store in the object the tag HTML <META> with
  106. * attributes value passed as parameters. Results is something like:
  107. * <meta http-equiv="pragma" content="no-cache">
  108. * <meta name="Author" content="pippo">
  109. *
  110. * @return void
  111. * @param string $sContent
  112. * @param string $sName
  113. * @param string $sHttpEquiv
  114. */
  115. public function addMetaTag($sContent, $sName=null, $sHttpEquiv=null ){
  116. $aAttributes = array( "name"=>$sName,
  117. "http-equiv"=>$sHttpEquiv,
  118. "content"=>$sContent
  119. );
  120. $this->aMetaTag[] = parent::fillTagAttributes("meta", $aAttributes);
  121. }
  122. /**
  123. * Prepare <SCRIPT> Html tag from Sylar default repository
  124. * it prepare the statment that includes js in html <script SRC='...'>...
  125. * The statment will be saved in the object.
  126. * An example of statment is:
  127. * <script src="sylar/javascript/mainGiano.js" type="text/javascript" charset="UTF-8"></script>
  128. *
  129. * @see addApplicationJavascript
  130. *
  131. * @return void
  132. * @param string $sScriptFile
  133. * @param string $sCharset
  134. */
  135. public function addSylarJavascript($sScriptFile, $sCharset=null){
  136. if($sCharset == null){
  137. $sCharset = Sylar_ConfigBox::getDefaultCharset();
  138. }
  139. $aAttributes = array( "src"=>Sylar_ConfigBox::getSylarJsUrlPath().$sScriptFile,
  140. "type"=>"text/javascript",
  141. "charset"=>$sCharset
  142. );
  143. $this->aJsScriptLink[] = parent::fillTagAttributes("script", $aAttributes)."</script>";
  144. }
  145. /**
  146. * Prepare <SCRIPT> Html tag from Application default repository
  147. * it prepare the statment that includes js in html <script SRC='...'>...
  148. * The statment will be saved in the object.
  149. * An example of statment is:
  150. * <script src="app/javascript/mainGiano.js" type="text/javascript" charset="UTF-8"></script>
  151. *
  152. * @see addSylarJavascript
  153. *
  154. * @return void
  155. * @param string $sScriptFile
  156. * @param string $sCharset
  157. */
  158. public function addApplicationJavascript($sScriptFile, $sCharset=null){
  159. if($sCharset == null){
  160. $sCharset = Sylar_ConfigBox::getDefaultCharset();
  161. }
  162. $aAttributes = array( "src"=>$this->getAppJsUrlRoot().$sScriptFile,
  163. "type"=>"text/javascript",
  164. "charset"=>$sCharset
  165. );
  166. $this->aJsScriptLink[] = parent::fillTagAttributes("script", $aAttributes)."</script>";
  167. }
  168.  
  169. /**
  170. * it prepares floating JS script.
  171. * it prepares and stores in the object a js script to include between the tag <SCRIPT>. For example:
  172. * <script>
  173. * var i=100;
  174. * for (j=0; j<i; j++){
  175. * etc...
  176. * </script>
  177. *
  178. * @return void
  179. * @param string $sScriptFile
  180. * @param string $sCharset
  181. */
  182. public function addFloatingJsScript($sScript, $sCharset=null){
  183. if($sCharset == null){
  184. $sCharset = Sylar_ConfigBox::getDefaultCharset();
  185. }
  186. if($sScript){
  187. $this->aFloatingJsScript[] = "<script type=\"text/javascript\" charset=\"".$sCharset."\">".$sScript."</script>";
  188. }
  189. }
  190. /**
  191. * Prepare and store <Style> to include Sylar css
  192. * It prepares and stores in the object the html tag needed to include a CSS file.
  193. * For example:
  194. * <link rel="stylesheet" href="/sylar/layouts/default/css/mainGiano.css" type="text/css" charset="UTF-8">
  195. *
  196. * @see addApplicationStyle
  197. *
  198. * @return void
  199. * @param string $sStyleFile
  200. * @param string $sCharset
  201. */
  202. public function addSylarStyle($sStyleFile, $sCharset=null){
  203. if($sCharset == null){
  204. $sCharset = Sylar_ConfigBox::getDefaultCharset();
  205. }
  206. $aAttributes = array( "rel"=>"stylesheet",
  207. "href"=>Sylar_ConfigBox::getSylarCssUrlPath().$sStyleFile,
  208. "type"=>"text/css",
  209. "charset"=>$sCharset
  210. );
  211. $this->aStyleLink[] = parent::fillTagAttributes("link", $aAttributes);
  212. }
  213.  
  214.  
  215. /**
  216. * Prepare and store <Style> to include Application css
  217. * It prepares and stores in the object the html tag needed to include a CSS file.
  218. * For example:
  219. * <link rel="stylesheet" href="/app/layouts/default/css/mainGiano.css" type="text/css" charset="UTF-8">
  220. *
  221. * @see addSylarStyle
  222. *
  223. * @return void
  224. * @param string $sStyleFile
  225. * @param string $sCharset
  226. */
  227. public function addApplicationStyle($sStyleFile, $sCharset=null){
  228. if($sCharset == null){
  229. $sCharset = Sylar_ConfigBox::getDefaultCharset();
  230. }
  231. $aAttributes = array( "rel"=>"stylesheet",
  232. "href"=>$this->getAppCssUrlRoot().$sStyleFile,
  233. "type"=>"text/css",
  234. "charset"=>$sCharset
  235. );
  236. $this->aStyleLink[] = parent::fillTagAttributes("link", $aAttributes);
  237. }
  238. /**
  239. * Prepare and store floating <Style>
  240. * It's used to include a style directly in the html page, for example:
  241. * <style type="text/css" media="all">
  242. * #one { width: 981px; }
  243. * #two { width: 9px; }
  244. * </style>
  245. *
  246. * @return void
  247. * @param string $sStyle
  248. * @param string $sMedia
  249. */
  250. public function addFloatingStyle($sStyle, $sMedia=null){
  251. if($sStyle!=null){
  252. $this->aFloatingStyle[] = "<style type=\"text/css\" media=\"all\"> ".$sStyle."</style>";
  253. }
  254. }
  255. /**
  256. * return the Html source
  257. * it return html code of entire object
  258. *
  259. * @return string
  260. */
  261. public function getHtmlSource(){
  262. return $this->render();
  263. }
  264. /**
  265. * Display the page
  266. * it prints the object Html source on screen
  267. *
  268. * @return void
  269. */
  270. public function show(){
  271. echo $this->render();
  272. }
  273. // Protected Methods
  274. //__________________________________________________________________________
  275. /**
  276. * Render <HEAD> html Tag
  277. * using all information stored in the object, this method render and returns
  278. * the <HEAD> html tag in a string.
  279. *
  280. * @return string
  281. */
  282. protected function render(){
  283. $sTagHead = "\n<head>";
  284. // Extract every data and push in the <HEAD> tag
  285. // Page TITLE
  286. $sTagHead .= "\n\t".$this->getPageTitle();
  287. // <Meta name="" content=""
  288. foreach ($this->aMetaTag as $val) {
  289. $sTagHead .= "\n\t".$val;
  290. }
  291. // CSS Files <link etc...
  292. foreach ($this->aStyleLink as $val) {
  293. $sTagHead .= "\n\t".$val;
  294. }
  295. // JS Script files <script src="" etc....
  296. foreach ($this->aJsScriptLink as $val) {
  297. $sTagHead .= "\n\t".$val;
  298. }
  299. // Floating Style in the page <style> #classname: etc... </style>
  300. foreach ($this->aFloatingStyle as $val) {
  301. $sTagHead .= "\n\t".str_replace("\n", "\n\t",$val);
  302. }
  303. // FloatingJavascript in the page <script> var foo = ""; etc... </script>
  304. foreach ($this->aFloatingJsScript as $val) {
  305. $sTagHead .= "\n\t".str_replace("\n", "\n\t",$val);
  306. }
  307. $sTagHead .= "\n</head>";
  308. return $sTagHead;
  309. }
  310. }
  311. ?>

Documentation generated on Thu, 24 Apr 2008 16:14:15 +0200 by phpDocumentor 1.3.0RC3