• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List

E:/E/GEAMP/www/openbiz/openbiz/bin/Resource.php

00001 <?PHP
00002 
00030 class Resource
00031 {
00032     private static $_imageUrl;
00033     private static $_cssUrl;
00034     private static $_jsUrl;
00035     private static $_currentTheme;
00036 
00037     const DEFAULT_THEME = THEME_NAME;
00044     public static function loadMessage($messageFile, $packageName="")
00045     {
00046         if (isset($messageFile) && $messageFile != "")
00047         {
00048 
00049             // message file location order 
00050             // 1. MESSAGE_PATH."/".$messageFile
00051             // 2. MODULE_PATH . "/$moduleName/message/" . $messageFile;
00052             // 3. CORE_MODULE_PATH . "/$moduleName/message/" . $messageFile;
00053             // APP_HOME / MESSAGE_PATH : APP_HOME / messages
00054             if (is_file(MESSAGE_PATH . "/" . $messageFile))
00055             {
00056                 return parse_ini_file(MESSAGE_PATH . "/" . $messageFile);
00057             } else
00058             {
00059                 if (isset($packageName) && $packageName != "")
00060                 {
00061                     $dirs = explode('.', $packageName);
00062                     $moduleName = $dirs[0];
00063                     $msgFile = MODULE_PATH . "/$moduleName/message/" . $messageFile;
00064                     if (is_file($msgFile))
00065                     {
00066                         return parse_ini_file($msgFile);
00067                     } else
00068                     {
00069                         $errmsg = self::getMessage("SYS_ERROR_INVALID_MSGFILE", array($msgFile));
00070                         trigger_error($errmsg, E_USER_ERROR);
00071                     }
00072                 } else
00073                 {
00074                     $errmsg = self::getMessage("SYS_ERROR_INVALID_MSGFILE", array(MESSAGE_PATH . "/" . $messageFile));
00075                     trigger_error($errmsg, E_USER_ERROR);
00076                 }
00077             }
00078         }
00079         return null;
00080     }
00081 
00088     public static function getMessage($msgId, $params=array())
00089     {
00090         $message = constant($msgId);
00091         if (isset($message))
00092         {
00093             $message = I18n::t($message, $msgId, 'system');
00094             $result = vsprintf($message, $params);
00095         }
00096         return $result;
00097     }
00098 
00103     public static function getImageUrl()
00104     {
00105         if (isset(self::$_imageUrl))
00106             return self::$_imageUrl;
00107         $useTheme = !defined('USE_THEME') ? 0 : USE_THEME;
00108         $themeUrl = !defined('THEME_URL') ? "../themes" : THEME_URL;
00109         $themeName = Resource::getCurrentTheme();
00110         if ($useTheme)
00111             self::$_imageUrl = "$themeUrl/$themeName/images";
00112         else
00113             self::$_imageUrl = "../images";
00114 
00115         return self::$_imageUrl;
00116     }
00117 
00122     public static function getCssUrl()
00123     {
00124         if (isset(self::$_cssUrl))
00125             return self::$_cssUrl;
00126         $useTheme = !defined('USE_THEME') ? 0 : USE_THEME;
00127         $themeUrl = !defined('THEME_URL') ? APP_URL . "/themes" : THEME_URL;
00128               $themeName = Resource::getCurrentTheme();
00129         if ($useTheme)
00130             self::$_cssUrl = "$themeUrl/$themeName/css";
00131         else
00132             self::$_cssUrl = APP_URL . "/css";
00133         return self::$_cssUrl;
00134     }
00135 
00140     public static function getJsUrl()
00141     {
00142         if (isset(self::$_jsUrl))
00143             return self::$_jsUrl;
00144         self::$_jsUrl = !defined('JS_URL') ? APP_URL . "/js" : JS_URL;
00145         return self::$_jsUrl;
00146     }
00147 
00152     public static function getSmartyTemplate()
00153     {
00154        if(extension_loaded('ionCube Loader')){
00155               include_once(SMARTY_DIR . "Smarty.class.php");
00156        }else{
00157               include_once(SMARTY_DIR . "Smarty.class.src.php");
00158        }
00159         $smarty = new Smarty;
00160 
00161         $useTheme = !defined('USE_THEME') ? 0 : USE_THEME;
00162         if ($useTheme)
00163         {
00164             $theme = Resource::getCurrentTheme();
00165             $themePath = $theme;    // BizSystem::configuration()->GetThemePath($theme);
00166             if (is_dir(THEME_PATH . "/" . $themePath . "/template"))
00167             {
00168                 $templateRoot = THEME_PATH . "/" . $themePath . "/template";
00169             } else
00170             {
00171                 $templateRoot = THEME_PATH . "/" . $themePath . "/templates";
00172             }
00173             $smarty->template_dir = $templateRoot;
00174             $smarty->compile_dir = defined('SMARTY_CPL_PATH') ? SMARTY_CPL_PATH."/".$themePath : $templateRoot . "/cpl";
00175             $smarty->config_dir = $templateRoot . "/cfg";
00176                      if (!file_exists($smarty->compile_dir)) {
00177                 @mkdir($smarty->compile_dir, 0777);
00178             }
00179             // load the config file which has the images and css url defined
00180             $smarty->config_load('tpl.conf');
00181         } else
00182         {
00183             if (defined('SMARTY_TPL_PATH'))
00184                 $smarty->template_dir = SMARTY_TPL_PATH;
00185             if (defined('SMARTY_CPL_PATH'))
00186                 $smarty->compile_dir = SMARTY_CPL_PATH."/".$themePath;
00187             if (defined('SMARTY_CFG_PATH'))
00188                 $smarty->config_dir = SMARTY_CFG_PATH;
00189         }
00190         if(!is_dir($smarty->compile_dir)){
00191               mkdir($smarty->compile_dir,0777);
00192         }
00193         // load the config file which has the images and css url defined
00194         $smarty->assign('app_url', APP_URL);
00195         $smarty->assign('app_index', APP_INDEX);
00196         $smarty->assign('js_url', JS_URL);
00197         $smarty->assign('css_url', THEME_URL . "/" . $theme . "/css");
00198         $smarty->assign('resource_url', RESOURCE_URL );
00199         $smarty->assign('resource_php', RESOURCE_PHP );
00200         $smarty->assign('theme_js_url', THEME_URL . "/" . $theme . "/js");
00201         $smarty->assign('theme_url', THEME_URL . "/" . $theme);
00202         $smarty->assign('image_url', THEME_URL . "/" . $theme . "/images");
00203         $smarty->assign('lang', strtolower(I18n::getCurrentLangCode()));
00204         $smarty->assign('lang_name', I18n::getCurrentLangCode());
00205 
00206         return $smarty;
00207     }
00208 
00213     public static function getZendTemplate()
00214     {
00215         // now assign the book data to a Zend_View instance
00216         //Zend_Loader::loadClass('Zend_View');
00217         require_once 'Zend/View.php';
00218         $view = new Zend_View();
00219         if (defined('SMARTY_TPL_PATH'))
00220             $view->setScriptPath(SMARTY_TPL_PATH);
00221 
00222         $theme = Resource::getCurrentTheme();            
00223             
00224         // load the config file which has the images and css url defined
00225         $view->app_url = APP_URL;
00226         $view->app_index = APP_INDEX;
00227         $view->js_url = JS_URL;
00228         $view->css_url = THEME_URL . "/" . $theme . "/css";
00229         $view->resource_url = RESOURCE_URL;    
00230         $view->theme_js_url = THEME_URL . "/" . $theme . "/js";
00231         $view->theme_url = THEME_URL . "/" . $theme;
00232         $view->image_url = THEME_URL . "/" . $theme . "/images";
00233         $view->lang = strtolower(I18n::getCurrentLangCode());            
00234             
00235         return $view;
00236     }
00237 
00248     public static function getXmlFileWithPath($xmlObj)
00249     {
00250         $xmlFile = $xmlObj;
00251         if (strpos($xmlObj, ".xml") > 0)  // remove .xml suffix if any
00252             $xmlFile = substr($xmlObj, 0, strlen($xmlObj) - 4);
00253 
00254         // replace "." with "/"
00255         $xmlFile = str_replace(".", "/", $xmlFile);
00256         // check the leading char '@'
00257         $checkExtModule = true;
00258         if (strpos($xmlFile, '@') === 0) {
00259             $xmlFile = substr($xmlFile, 1);
00260             $checkExtModule = false;
00261         }
00262         $xmlFile .= ".xml";
00263         $xmlFile = "/" . $xmlFile;
00264         
00265         // search in modules directory first
00266         $xmlFileList[] = MODULE_PATH . $xmlFile;
00267         $xmlFileList[] = APP_HOME . $xmlFile;
00268         $xmlFileList[] = OPENBIZ_META . $xmlFile;
00269         if ($checkExtModule && defined('MODULE_EX_PATH')) array_unshift($xmlFileList, MODULE_EX_PATH . $xmlFile);
00270 
00271         foreach ($xmlFileList as $xmlFileItem)
00272         {
00273             if (file_exists($xmlFileItem))
00274                 return $xmlFileItem;
00275         }        
00276         return null;
00277     }
00278 
00285     public static function getTplFileWithPath($templateFile, $packageName)
00286     {
00287         //for not changing a lot things, the best injection point is added theme support here.
00288               $theme = Resource::getCurrentTheme();
00289         $themePath = $theme;    // BizSystem::configuration()->GetThemePath($theme);
00290         if ($themePath)
00291             $templateRoot = THEME_PATH . "/" . $themePath . "/template";
00292         else
00293             $templateRoot = SMARTY_TPL_PATH;
00294 
00295         $names = explode(".", $packageName);
00296         if (count($names) > 0)
00297             $moduleName = $names[0];
00298         $packagePath = str_replace('.', '/', $packageName);
00299         // check the leading char '@'
00300         $checkExtModule = true;
00301         if (strpos($packagePath, '@') === 0) {
00302             $packagePath = substr($packagePath, 1);
00303             $checkExtModule = false;
00304         }
00305         
00306         $searchTpls = array(
00307             MODULE_PATH . "/$packagePath/template/$templateFile",
00308             dirname(MODULE_PATH . "/$packagePath") . "/template/$templateFile",
00309             MODULE_PATH . "/$moduleName/template/$templateFile",
00310             //MODULE_PATH."/common/template/$templateFile",
00311             $templateRoot . "/$templateFile"
00312         );
00313         if ($checkExtModule && defined('MODULE_EX_PATH')) array_unshift($searchTpls, MODULE_EX_PATH . "/$packagePath/template/$templateFile");
00314         foreach ($searchTpls as $tplFile)
00315         {
00316             if (file_exists($tplFile))
00317             {
00318                 return $tplFile;
00319             }
00320         }
00321         $errmsg = BizSystem::getMessage("UNABLE_TO_LOCATE_TEMPLATE_FILE", array($templateFile));
00322         trigger_error($errmsg, E_USER_ERROR);
00323         return null;
00324     }
00325 
00332     public static function getLibFileWithPath($className, $packageName="")
00333     {
00334         if (!$className)
00335             return;
00336         // search it in cache first
00337         $cacheKey = $className . "_path";
00338         if (extension_loaded('0') && ($filePath = apc_fetch($cacheKey)) != null)
00339             return $filePath;
00340 
00341         if (strpos($className, ".") > 0)
00342             $className = str_replace(".", "/", $className);
00343 
00344         $filePath = null;
00345         $classFile = $className . ".php";
00346         $classFile_0 = $className . ".php";
00347         // convert package name to path, add it to classfile
00348         $classFileIsFound = false;
00349         if ($packageName)
00350         {
00351             $path = str_replace(".", "/", $packageName);
00352             // check the leading char '@'
00353             $checkExtModule = true;
00354             if (strpos($path, '@') === 0) {
00355                 $path = substr($path, 1);
00356                 $checkExtModule = false;
00357             }
00358             
00359             // search in apphome/modules directory first, search in apphome/bin directory then
00360             $classFiles[0] = MODULE_PATH . "/" . $path . "/" . $classFile;
00361             $classFiles[1] = APP_HOME . "/bin/" . $path . "/" . $classFile;
00362             if ($checkExtModule && defined('MODULE_EX_PATH')) array_unshift($classFiles, MODULE_EX_PATH . "/" . $path . "/" . $classFile);
00363             foreach ($classFiles as $classFile)
00364             {
00365                 if (file_exists($classFile))
00366                 {
00367                     $filePath = $classFile;
00368                     $classFileIsFound = true;
00369                     break;
00370                 }
00371             }
00372         }
00373 
00374         if (!$classFileIsFound)
00375             $filePath = self::_getCoreLibFilePath($className);
00376         // cache it to save file search
00377         if ($filePath && extension_loaded('apc'))
00378             apc_store($cacheKey, $filePath);
00379         /*if (!file_exists($filePath)) {
00380             trigger_error("Cannot find the library file of $className", E_USER_ERROR);
00381         }*/
00382         return $filePath;
00383     }
00384 
00391     private static function _getCoreLibFilePath($className)
00392     {
00393         $classFile = $className . '.php';
00394 
00395         // TODO: search the file under bin/, bin/data, bin/ui. bin/service, bin/easy, bin/easy/element.
00396         $corePaths = array('', 'data/', 'easy/', 'easy/element/', 'ui/', 'service/');
00397         foreach ($corePaths as $path)
00398         {
00399             $_classFile = OPENBIZ_BIN . $path . $classFile;
00400             if (file_exists($_classFile))
00401                 return $_classFile;
00402         }
00403         return null;
00404     }
00405 
00416     public static function &getXmlArray($xmlFile)
00417     {
00418         $objXmlFileName = $xmlFile;
00419         //$objCmpFileName = dirname($objXmlFileName) . "/__cmp/" . basename($objXmlFileName, "xml") . ".cmp";
00420         $_crc32 = sprintf('%08X', crc32(dirname($objXmlFileName)));
00421         $objCmpFileName = CACHE_METADATA_PATH . '/' . $_crc32 . '_'
00422                 . basename($objXmlFileName, "xml") . "cmp";
00423 
00424         $xmlArr = null;
00425         //$cacheKey = substr($objXmlFileName, strlen(META_PATH)+1);
00426         $cacheKey = $objXmlFileName;
00427         $findInCache = false;
00428         if (file_exists($objCmpFileName)
00429                 && (filemtime($objCmpFileName) > filemtime($objXmlFileName)))
00430         {
00431             // search in cache first
00432             if (!$xmlArr && extension_loaded('apc'))
00433             {
00434                 if (($xmlArr = apc_fetch($cacheKey)) != null)
00435                 {
00436                     $findInCache = true;
00437                 }
00438             }
00439             if (!$xmlArr)
00440             {
00441                 $content_array = file($objCmpFileName);
00442                 $xmlArr = unserialize(implode("", $content_array));
00443             }
00444         } else
00445         {
00446               if(extension_loaded('ionCube Loader')){
00447               include_once(OPENBIZ_BIN . "util/xmltoarray.php");
00448               }else{
00449                      include_once(OPENBIZ_BIN . "util/xmltoarray.src.php");
00450               }
00451             $parser = new XMLParser($objXmlFileName, 'file', 1);
00452             $xmlArr = $parser->getTree();
00453             // simple validate the xml array
00454             $root_keys = array_keys($xmlArr);
00455             $root_key = $root_keys[0];
00456             if (!$root_key || $root_key == "")
00457             {
00458                 trigger_error("Metadata file parsing error for file $objXmlFileName. Please double check your metadata xml file again.", E_USER_ERROR);
00459             }
00460             $xmlArrStr = serialize($xmlArr);
00461             if (!file_exists(dirname($objCmpFileName)))
00462                 mkdir(dirname($objCmpFileName));
00463             $cmp_file = fopen($objCmpFileName, 'w') or die("can't open cmp file to write");
00464             fwrite($cmp_file, $xmlArrStr) or die("can't write to the cmp file");
00465             fclose($cmp_file);
00466         }
00467         // save to cache to avoid file processing overhead
00468         if (!$findInCache && extension_loaded('apc'))
00469         {
00470             apc_store($cacheKey, $xmlArr);
00471         }
00472         return $xmlArr;
00473     }
00474        public static function getCurrentTheme ()
00475     {
00476        if (Resource::$_currentTheme != null)
00477             return Resource::$_currentTheme;
00478             
00479         $currentTheme = BizSystem::sessionContext()->getVar("THEME");        
00480         // default language
00481         if ($currentTheme == ""){
00482               $currentTheme = BizSystem::getUserPreference("theme");
00483               
00484         }
00485         if($currentTheme == ""){
00486             $currentTheme = Resource::DEFAULT_THEME;
00487         }
00488         // language from url
00489         if (isset($_GET['theme'])){
00490             $currentTheme = $_GET['theme'];
00491             BizSystem::sessionContext()->setVar("THEME",$currentTheme );
00492         }
00493 
00494         // TODO: user pereference has language setting
00495         
00496         BizSystem::sessionContext()->setVar("THEME", $currentTheme);
00497         Resource::$_currentTheme = $currentTheme;
00498         
00499         return $currentTheme;
00500     }
00501 }

Generated on Thu Apr 19 2012 17:09:13 for openbiz by  doxygen 1.7.2