PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/php/lib/Gloo/Core/Web.php

http://webgloo.googlecode.com/
PHP | 310 lines | 185 code | 79 blank | 46 comment | 26 complexity | f885012a6a4a569b80fa29603e7f1363 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @author rajeevj
  5. *
  6. * Web is a class that lets you access request and context
  7. * and exposes other helper methods also. Only one instance is in effect
  8. * during processing of a request.
  9. *
  10. * This naming was influenced by web.py micro framework. This glue class lets
  11. * part of the system interact with each other much on the lines of web.py
  12. *
  13. *
  14. *
  15. */
  16. class Gloo_Core_Web {
  17. private $pageVO ;
  18. private $counter ;
  19. private $request ;
  20. private $sourceMap ;
  21. private $controller ;
  22. private $hasPageView ;
  23. static private $instance = NULL ;
  24. private function __construct () {
  25. $this->pageVO = NULL;
  26. $this->request = new Gloo_Core_Request();
  27. $this->sourceMap = array();
  28. $this->counter = 0 ;
  29. $this->controller = NULL ;
  30. $this->hasPageView = false ;
  31. }
  32. static function getInstance() {
  33. if(self::$instance == NULL) {
  34. self::$instance = new Gloo_Core_Web();
  35. if(Gloo_Config::getInstance()->is_debug()) {
  36. $message = sprintf(" \n web core :: hash [%s] :: request started \n\n",spl_object_hash(self::$instance));
  37. Gloo_Logger::getInstance()->debug($message);
  38. }
  39. }
  40. return self::$instance ;
  41. }
  42. function getCounter() {
  43. $this->counter++ ;
  44. return $this->counter ;
  45. }
  46. function getRequest() {
  47. return $this->request ;
  48. }
  49. //request helper methods
  50. function getRequestParam($param){
  51. return $this->request->getParam($param);
  52. }
  53. function setRequestParam($param,$value){
  54. return $this->request->setParam($param,$value);
  55. }
  56. function getRequestAttribute($key){
  57. return $this->request->getAttribute($key);
  58. }
  59. function setRequestAttribute($key,$value){
  60. return $this->request->setAttribute($key,$value);
  61. }
  62. function setController($controller){
  63. $this->controller = $controller ;
  64. }
  65. function getController() {
  66. return $this->controller ;
  67. }
  68. function close() {
  69. Gloo_DB::getInstance()->closeConnection();
  70. if(Gloo_Config::getInstance()->is_debug()) {
  71. $message = sprintf("web core :: hash [%s] :: request terminated \n\n",spl_object_hash(self::$instance));
  72. Gloo_Logger::getInstance()->debug($message);
  73. }
  74. }
  75. #region - Helper methods to utilize PHP session
  76. //instead of providing a session key from each web instance, we wrap few
  77. // often used cases inside these helper methods
  78. function setStickyMap($form,$hashMap) {
  79. $this->storeInSession('stickyFormData',$form,$hashMap);
  80. }
  81. function getStickyMap($form) {
  82. $sticky = new Gloo_Form_Sticky();
  83. $data = $this->findInSession('stickyFormData',$form);
  84. if(!is_null($data)) {
  85. $sticky->setData($data);
  86. }
  87. return $sticky ;
  88. }
  89. // web just provides a convinient way to retrieve context
  90. //context is actually stored in HTTP session
  91. function setContext($context) {
  92. //check context data type
  93. if(!($context instanceof Gloo_Core_Context)) {
  94. trigger_error('wrong web context data type ',E_USER_ERROR);
  95. }
  96. $this->storeInSession('pageData','context',$context);
  97. }
  98. function getContext() {
  99. $context = $this->findInSession('pageData','context',false);
  100. //initialize context if none found
  101. if(is_null($context)) {
  102. $context = new Gloo_Core_Context();
  103. }
  104. return $context ;
  105. }
  106. //Getter/setter for script messages, namespace is page section
  107. function setMessages($section,$messages) {
  108. //script_message is an array of lists. Each element of array is indexed by
  109. // section name and contains a list of messages.
  110. $this->storeInSession('scriptMessages',$section,$messages);
  111. }
  112. function getMessages($section) {
  113. $list = $this->findInSession('scriptMessages',$section);
  114. if(is_null($list)) {
  115. $list = array();
  116. }
  117. return $list ;
  118. }
  119. function setErrors($section,$errors) {
  120. //script_message is an array of lists. Each element of array is indexed by
  121. // section name and contains a list of messages.
  122. $this->storeInSession('scriptErrors',$section,$errors);
  123. }
  124. function getErrors($section) {
  125. $list = $this->findInSession('scriptErrors',$section);
  126. if(is_null($list)) {
  127. $list = array();
  128. }
  129. return $list ;
  130. }
  131. #endregion
  132. //method to interact with PHP session
  133. function storeInSession($sessionKey,$dataKey,$data) {
  134. if(isset($_SESSION[$sessionKey]) && !empty($_SESSION[$sessionKey])) {
  135. // This key is defined in session, fetch the data structure for
  136. // this session key
  137. $sessionData = $_SESSION[$sessionKey];
  138. //index $form is populated
  139. $sessionData[$dataKey] = $data ;
  140. //push back in
  141. $_SESSION[$sessionKey] = $sessionData ;
  142. } else {
  143. //No session data stored for sessionKey
  144. $sessionData = array();
  145. $sessionData[$dataKey] = $data ;
  146. //store in session
  147. $_SESSION[$sessionKey] = $sessionData ;
  148. }
  149. if(Gloo_Config::getInstance()->is_debug()) {
  150. $message = '[{store:sessionKey='.$sessionKey.'} {data key='.$dataKey.'} {data='.$data.' }]' ;
  151. Gloo_Logger::getInstance()->debug($message);
  152. }
  153. }
  154. function findInSession($sessionKey,$dataKey,$makeNull=true) {
  155. $data = NULL ;
  156. if(isset($_SESSION[$sessionKey])
  157. && !empty($_SESSION[$sessionKey])
  158. && array_key_exists($dataKey,$_SESSION[$sessionKey])) {
  159. $sessionData = $_SESSION[$sessionKey];
  160. if(!is_null($sessionData[$dataKey])) {
  161. $data = $sessionData[$dataKey];
  162. //remove from session
  163. if($makeNull) {
  164. $sessionData[$dataKey] = NULL ;
  165. $_SESSION[$sessionKey] = $sessionData ;
  166. }
  167. }
  168. }
  169. return $data ;
  170. }
  171. # region Helper methods to load css and javascript files
  172. //we can invoke multiple calls for same source on $web but
  173. // $web should only load them once
  174. function loadCss($path) {
  175. $scheme = parse_url($path, PHP_URL_SCHEME);
  176. if(empty($scheme)) {
  177. $path = url::base().$path ;
  178. }
  179. $srctag = '' ;
  180. if(!$this->isFileInMap($path)) {
  181. $srctag = '<link rel="stylesheet" href="'.$path.'" type="text/css">' ."\n" ;
  182. }
  183. return $srctag ;
  184. }
  185. function loadJS($path) {
  186. $scheme = parse_url($path, PHP_URL_SCHEME);
  187. if(empty($scheme)) {
  188. $path = url::base().$path ;
  189. }
  190. $path = trim($path);
  191. $srctag = '' ;
  192. if(!$this->isFileInMap($path)) {
  193. $srctag = '<script type="text/javascript" src="'.$path.'"></script>'. "\n" ;
  194. }
  195. return $srctag ;
  196. }
  197. function isFileInMap($link) {
  198. $hash = md5($link);
  199. //already there
  200. if(in_array($hash,$this->sourceMap)) {
  201. return true;
  202. } else {
  203. //store the hash in map
  204. array_push($this->sourceMap,$hash);
  205. return false;
  206. }
  207. }
  208. #endregion
  209. // load data for all the blocks in this page using
  210. // page view object (pageVO)
  211. function loadPageViewData($orgId,$pageIdentKey=NULL) {
  212. if(!$this->hasPageView){
  213. $blockDao = new Gloo_Dao_Block();
  214. $this->pageVO = $blockDao->getPageViewData($orgId,$pageIdentKey);
  215. $this->pageView = true ;
  216. }
  217. }
  218. function getPageViewData() {
  219. return $this->pageVO ;
  220. }
  221. //return block view data, BLOCK
  222. // if there is no BlockView then NULL is returned
  223. function getBlockViewData($blockNo) {
  224. $block = NULL ;
  225. if(!is_null($this->pageVO)) {
  226. $block = $this->pageVO->getBlock($blockNo);
  227. }
  228. return $block;
  229. }
  230. }
  231. ?>