PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/cubi/openbiz/bin/easy/ViewRenderer.php

http://openbiz-cubi.googlecode.com/
PHP | 243 lines | 138 code | 28 blank | 77 comment | 27 complexity | 22d305283c562504ab79d6a4f82672d3 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * PHPOpenBiz Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. *
  10. * @package openbiz.bin.easy
  11. * @copyright Copyright (c) 2005-2011, Rocky Swen
  12. * @license http://www.opensource.org/licenses/bsd-license.php
  13. * @link http://www.phpopenbiz.org/
  14. * @version $Id: ViewRenderer.php 2553 2010-11-21 08:36:48Z mr_a_ton $
  15. */
  16. /**
  17. * ViewRenderer class is view helper for rendering form
  18. *
  19. * @package openbiz.bin.easy
  20. * @author Rocky Swen
  21. * @copyright Copyright (c) 2005-2011
  22. * @access public
  23. */
  24. class ViewRenderer
  25. {
  26. /**
  27. * Render view object
  28. *
  29. * @param EasyView $viewObj
  30. * @return string result of rendering process
  31. */
  32. static public function render ($viewObj)
  33. {
  34. $tplEngine = $viewObj->m_TemplateEngine;
  35. $tplAttributes = ViewRenderer::buildTemplateAttributes($viewObj);
  36. if(defined("PAGE_MINIFY") && PAGE_MINIFY==1)
  37. {
  38. //if(!ob_start("ob_gzhandler")) ob_start();
  39. ob_start();
  40. }
  41. if ($tplEngine == "Smarty" || $tplEngine == null)
  42. $result = ViewRenderer::renderSmarty($viewObj, $tplAttributes);
  43. else
  44. $result = ViewRenderer::renderPHP($viewObj, $tplAttributes);
  45. if(defined("PAGE_MINIFY") && PAGE_MINIFY==1)
  46. {
  47. $html = ob_get_contents();
  48. ob_end_clean();
  49. $html = self::MinifyOutput($html);
  50. echo $html;
  51. }
  52. return $html;
  53. }
  54. /**
  55. *
  56. * Minify the HTML code and rewrite the code for
  57. * including CSS and JS files to make it redirect to /bin/min/index.php?f
  58. * @param string $html
  59. * @return string $html
  60. */
  61. static public function MinifyOutput($html)
  62. {
  63. $minifyURL = APP_URL."/bin/min/index.php";
  64. $headEnd="</head>";
  65. //fetch js requests
  66. preg_match_all("/\<script.*?src\s?\=\s?[\"|\'](.*?\.js)[\"|\']/i",$html, $matches);
  67. $jsListStr = implode(array_unique($matches[1]), ',');
  68. $jsURL = $minifyURL . '?f='.$jsListStr;
  69. $jsCode = "<script type=\"text/javascript\" src=\"$jsURL\"></script>";
  70. //remove old js include
  71. $html = preg_replace("/\<script.*?src\s?\=\s?[\"|\'].*?\.js[\"|\'].*?\<\/script\>/i","",$html);
  72. //add new js include
  73. $html = str_replace($headEnd,$jsCode."\n".$headEnd,$html);
  74. preg_match_all("/\<link.*?href\s?\=\s?\"(.*?\.css)\"/i",$html, $matches);
  75. $cssListStr = implode(array_unique($matches[1]), ',');
  76. $cssURL = $minifyURL . '?f='.$cssListStr;
  77. $cssCode = "<link rel=\"stylesheet\" href=\"$cssURL\" type=\"text/css\">";
  78. $html = preg_replace("/\<link.*?href\s?\=\s?\"(.*?\.css)\".*?\>/i","",$html);
  79. $html = str_replace($headEnd,$cssCode."\n".$headEnd,$html);
  80. require_once APP_HOME.'/bin/min/lib/Minify/HTML.php';
  81. $html = Minify_HTML::minify($html);
  82. return $html;
  83. }
  84. /**
  85. * Gather all template variables needed. Should play well with Smarty or Zend templates
  86. *
  87. * @param EasyView $viewObj
  88. * @return array associative array holding all needed VIEW based template variables
  89. */
  90. static public function buildTemplateAttributes ($viewObj)
  91. {
  92. // Assocative Array to hold all Template Values
  93. // Fill with default viewobj attributes
  94. //$tplAttributes = $viewObj->outputAttrs();
  95. //Not sure what this is doing...
  96. $newClntObjs = '';
  97. //Fill other direct view variables
  98. $tplAttributes["module"] = $viewObj->getModuleName($viewObj->m_Name);
  99. $tplAttributes["description"] = $viewObj->m_Description;
  100. $tplAttributes["keywords"] = $viewObj->m_Keywords;
  101. if ($viewObj->m_Tiles) {
  102. foreach ($viewObj->m_Tiles as $tname => $tile) {
  103. foreach ($tile as $formRef) {
  104. if ($formRef->m_Display == false)
  105. continue;
  106. $tiles[$tname][$formRef->m_Name] = BizSystem::getObject($formRef->m_Name)->render();
  107. $tiletabs[$tname][$formRef->m_Name] = $formRef->m_Description;
  108. }
  109. }
  110. } else {
  111. foreach ($viewObj->m_FormRefs as $formRef) {
  112. if ($formRef->m_Display == false)
  113. continue;
  114. $forms[$formRef->m_Name] = BizSystem::getObject($formRef->m_Name)->render();
  115. $formtabs[$formRef->m_Name] = $formRef->m_Description;
  116. }
  117. }
  118. if(count($viewObj->m_Widgets)){
  119. foreach ($viewObj->m_Widgets as $formRef) {
  120. if ($formRef->m_Display == false)
  121. continue;
  122. $widgets[$formRef->m_Name] = BizSystem::getObject($formRef->m_Name)->render();
  123. }
  124. }
  125. //Fill Loop related data
  126. $tplAttributes["forms"] = $forms;
  127. $tplAttributes["widgets"] = $widgets;
  128. $tplAttributes["formtabs"] = $formtabs;
  129. $tplAttributes["tiles"] = $tiles;
  130. $tplAttributes["tiletabs"] = $tiletabs;
  131. // add clientProxy scripts
  132. $includedScripts = BizSystem::clientProxy()->getAppendedScripts();
  133. $tplAttributes["style_sheets"] = BizSystem::clientProxy()->getAppendedStyles();
  134. if ($viewObj->m_IsPopup && $bReRender == false) {
  135. $moveToCenter = "moveToCenter(self, " . $viewObj->m_Width . ", " . $viewObj->m_Height . ");";
  136. $tplAttributes["scripts"] = $includedScripts . "\n<script>\n" . $newClntObjs . $moveToCenter . "</script>\n";
  137. } else
  138. $tplAttributes["scripts"] = $includedScripts . "\n<script>\n" . $newClntObjs . "</script>\n";
  139. if ($viewObj->m_Title)
  140. $tplAttributes["title"] = Expression::evaluateExpression($viewObj->m_Title, $viewObj);
  141. else
  142. $tplAttributes["title"] = $viewObj->m_Description;
  143. if(DEFAULT_SYSTEM_NAME){
  144. $tplAttributes["title"] = $tplAttributes["title"].' - '.DEFAULT_SYSTEM_NAME;
  145. }
  146. return $tplAttributes;
  147. }
  148. /**
  149. * Render smarty template for view object
  150. *
  151. * @param EasyView $viewObj
  152. * @param string $tplFile
  153. * @return string result of rendering process
  154. */
  155. static protected function renderSmarty ($viewObj, $tplAttributes = Array())
  156. {
  157. $smarty = BizSystem::getSmartyTemplate();
  158. $viewOutput = $viewObj->outputAttrs();
  159. foreach ($viewOutput as $k=>$v) {
  160. $smarty->assign($k, $v);
  161. }
  162. // render the formobj attributes
  163. $smarty->assign("view", $viewOutput);
  164. //Translate Array of template variables to Zend template object
  165. foreach ($tplAttributes as $key => $value) {
  166. $smarty->assign($key, $value);
  167. }
  168. if ($viewObj->m_ConsoleOutput)
  169. $smarty->display(BizSystem::getTplFileWithPath($viewObj->m_TemplateFile, $viewObj->m_Package));
  170. else
  171. return $smarty->fetch(BizSystem::getTplFileWithPath($viewObj->m_TemplateFile, $viewObj->m_Package));
  172. }
  173. /**
  174. * Render PHP template for view object
  175. *
  176. * @param EasyForm $formObj
  177. * @param string $tplFile
  178. * @return string result of rendering process
  179. */
  180. static protected function renderPHP ($viewObj, $tplAttributes = Array())
  181. {
  182. $view = BizSystem::getZendTemplate();
  183. $tplFile = BizSystem::getTplFileWithPath($viewObj->m_TemplateFile, $viewObj->m_Package);
  184. $view->addScriptPath(dirname($tplFile));
  185. //Translate Array of template variables to Zend template object
  186. foreach ($tplAttributes as $key => $value) {
  187. if ($value == NULL) {
  188. $view->$key = '';
  189. } else {
  190. $view->$key = $value;
  191. }
  192. }
  193. if ($viewObj->m_ConsoleOutput)
  194. echo $view->render($viewObj->m_TemplateFile);
  195. else
  196. return $view->render($viewObj->m_TemplateFile);
  197. }
  198. /**
  199. * Set headers of view
  200. *
  201. * @param EasyView $viewObj
  202. * @return void
  203. */
  204. static protected function setHeaders ($viewObj)
  205. {
  206. // get the cache attribute
  207. // if cache = browser, set the cache control in headers
  208. header('Pragma:', true);
  209. header('Cache-Control: max-age=3600', true);
  210. $offset = 60 * 60 * 24 * - 1;
  211. $ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
  212. header($ExpStr, true);
  213. }
  214. }
  215. ?>