/php/lib/Gloo/Core/Web.php
PHP | 310 lines | 185 code | 79 blank | 46 comment | 26 complexity | f885012a6a4a569b80fa29603e7f1363 MD5 | raw file
- <?php
- /**
- *
- * @author rajeevj
- *
- * Web is a class that lets you access request and context
- * and exposes other helper methods also. Only one instance is in effect
- * during processing of a request.
- *
- * This naming was influenced by web.py micro framework. This glue class lets
- * part of the system interact with each other much on the lines of web.py
- *
- *
- *
- */
- class Gloo_Core_Web {
- private $pageVO ;
- private $counter ;
- private $request ;
- private $sourceMap ;
- private $controller ;
- private $hasPageView ;
- static private $instance = NULL ;
-
- private function __construct () {
- $this->pageVO = NULL;
- $this->request = new Gloo_Core_Request();
- $this->sourceMap = array();
- $this->counter = 0 ;
- $this->controller = NULL ;
- $this->hasPageView = false ;
-
- }
-
- static function getInstance() {
- if(self::$instance == NULL) {
- self::$instance = new Gloo_Core_Web();
- if(Gloo_Config::getInstance()->is_debug()) {
- $message = sprintf(" \n web core :: hash [%s] :: request started \n\n",spl_object_hash(self::$instance));
- Gloo_Logger::getInstance()->debug($message);
- }
- }
- return self::$instance ;
- }
-
- function getCounter() {
- $this->counter++ ;
- return $this->counter ;
- }
- function getRequest() {
- return $this->request ;
- }
-
- //request helper methods
- function getRequestParam($param){
- return $this->request->getParam($param);
- }
- function setRequestParam($param,$value){
- return $this->request->setParam($param,$value);
- }
- function getRequestAttribute($key){
- return $this->request->getAttribute($key);
- }
- function setRequestAttribute($key,$value){
- return $this->request->setAttribute($key,$value);
- }
-
- function setController($controller){
- $this->controller = $controller ;
- }
- function getController() {
- return $this->controller ;
- }
-
- function close() {
- Gloo_DB::getInstance()->closeConnection();
- if(Gloo_Config::getInstance()->is_debug()) {
- $message = sprintf("web core :: hash [%s] :: request terminated \n\n",spl_object_hash(self::$instance));
- Gloo_Logger::getInstance()->debug($message);
-
- }
- }
-
- #region - Helper methods to utilize PHP session
- //instead of providing a session key from each web instance, we wrap few
- // often used cases inside these helper methods
-
- function setStickyMap($form,$hashMap) {
- $this->storeInSession('stickyFormData',$form,$hashMap);
- }
-
- function getStickyMap($form) {
- $sticky = new Gloo_Form_Sticky();
- $data = $this->findInSession('stickyFormData',$form);
- if(!is_null($data)) {
- $sticky->setData($data);
- }
- return $sticky ;
- }
- // web just provides a convinient way to retrieve context
- //context is actually stored in HTTP session
-
- function setContext($context) {
- //check context data type
- if(!($context instanceof Gloo_Core_Context)) {
- trigger_error('wrong web context data type ',E_USER_ERROR);
- }
- $this->storeInSession('pageData','context',$context);
-
- }
-
- function getContext() {
- $context = $this->findInSession('pageData','context',false);
- //initialize context if none found
- if(is_null($context)) {
- $context = new Gloo_Core_Context();
- }
- return $context ;
- }
-
- //Getter/setter for script messages, namespace is page section
- function setMessages($section,$messages) {
- //script_message is an array of lists. Each element of array is indexed by
- // section name and contains a list of messages.
- $this->storeInSession('scriptMessages',$section,$messages);
- }
- function getMessages($section) {
- $list = $this->findInSession('scriptMessages',$section);
- if(is_null($list)) {
- $list = array();
- }
- return $list ;
- }
- function setErrors($section,$errors) {
- //script_message is an array of lists. Each element of array is indexed by
- // section name and contains a list of messages.
- $this->storeInSession('scriptErrors',$section,$errors);
- }
- function getErrors($section) {
- $list = $this->findInSession('scriptErrors',$section);
- if(is_null($list)) {
- $list = array();
- }
- return $list ;
- }
- #endregion
-
- //method to interact with PHP session
- function storeInSession($sessionKey,$dataKey,$data) {
- if(isset($_SESSION[$sessionKey]) && !empty($_SESSION[$sessionKey])) {
- // This key is defined in session, fetch the data structure for
- // this session key
- $sessionData = $_SESSION[$sessionKey];
- //index $form is populated
- $sessionData[$dataKey] = $data ;
- //push back in
- $_SESSION[$sessionKey] = $sessionData ;
- } else {
- //No session data stored for sessionKey
- $sessionData = array();
- $sessionData[$dataKey] = $data ;
- //store in session
- $_SESSION[$sessionKey] = $sessionData ;
- }
- if(Gloo_Config::getInstance()->is_debug()) {
- $message = '[{store:sessionKey='.$sessionKey.'} {data key='.$dataKey.'} {data='.$data.' }]' ;
- Gloo_Logger::getInstance()->debug($message);
- }
- }
-
- function findInSession($sessionKey,$dataKey,$makeNull=true) {
- $data = NULL ;
- if(isset($_SESSION[$sessionKey])
- && !empty($_SESSION[$sessionKey])
- && array_key_exists($dataKey,$_SESSION[$sessionKey])) {
- $sessionData = $_SESSION[$sessionKey];
- if(!is_null($sessionData[$dataKey])) {
- $data = $sessionData[$dataKey];
- //remove from session
- if($makeNull) {
- $sessionData[$dataKey] = NULL ;
- $_SESSION[$sessionKey] = $sessionData ;
- }
- }
- }
- return $data ;
- }
- # region Helper methods to load css and javascript files
- //we can invoke multiple calls for same source on $web but
- // $web should only load them once
-
- function loadCss($path) {
- $scheme = parse_url($path, PHP_URL_SCHEME);
- if(empty($scheme)) {
- $path = url::base().$path ;
- }
-
- $srctag = '' ;
- if(!$this->isFileInMap($path)) {
- $srctag = '<link rel="stylesheet" href="'.$path.'" type="text/css">' ."\n" ;
- }
- return $srctag ;
- }
-
- function loadJS($path) {
- $scheme = parse_url($path, PHP_URL_SCHEME);
- if(empty($scheme)) {
- $path = url::base().$path ;
- }
- $path = trim($path);
- $srctag = '' ;
-
- if(!$this->isFileInMap($path)) {
- $srctag = '<script type="text/javascript" src="'.$path.'"></script>'. "\n" ;
- }
-
- return $srctag ;
- }
-
- function isFileInMap($link) {
- $hash = md5($link);
- //already there
- if(in_array($hash,$this->sourceMap)) {
- return true;
- } else {
- //store the hash in map
- array_push($this->sourceMap,$hash);
- return false;
- }
- }
- #endregion
- // load data for all the blocks in this page using
- // page view object (pageVO)
-
- function loadPageViewData($orgId,$pageIdentKey=NULL) {
- if(!$this->hasPageView){
- $blockDao = new Gloo_Dao_Block();
- $this->pageVO = $blockDao->getPageViewData($orgId,$pageIdentKey);
- $this->pageView = true ;
- }
- }
-
- function getPageViewData() {
- return $this->pageVO ;
- }
-
- //return block view data, BLOCK
- // if there is no BlockView then NULL is returned
-
- function getBlockViewData($blockNo) {
- $block = NULL ;
-
- if(!is_null($this->pageVO)) {
- $block = $this->pageVO->getBlock($blockNo);
- }
-
- return $block;
- }
-
- }
- ?>