PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/cubi/openbiz/bin/easy/HTMLTabs.php

http://openbiz-cubi.googlecode.com/
PHP | 377 lines | 221 code | 35 blank | 121 comment | 27 complexity | bd83f65b9a74cccc17593e330b63b547 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: HTMLTabs.php 2553 2010-11-21 08:36:48Z mr_a_ton $
  15. */
  16. /**
  17. * HTMLTabs class is the base class of HTML tabs
  18. *
  19. * @package openbiz.bin.easy
  20. * @author Rocky Swen
  21. * @copyright Copyright (c) 2005-2009
  22. * @since 1.2
  23. * @access public
  24. */
  25. class HTMLTabs extends MetaObject implements iUIControl
  26. {
  27. public $m_TemplateFile;
  28. public $m_TabViews = null;
  29. protected $m_CurrentTab = null;
  30. protected $m_ActiveCssClassName = null;
  31. protected $m_InactiveCssClassName = null;
  32. /**
  33. * Initialize HTMLTabs with xml array
  34. *
  35. * @param array $xmlArr
  36. * @return void
  37. */
  38. function __construct(&$xmlArr)
  39. {
  40. $this->readMetadata($xmlArr);
  41. }
  42. /**
  43. * Read Metadata from xml array
  44. *
  45. * @param array $xmlArr
  46. */
  47. protected function readMetadata(&$xmlArr)
  48. {
  49. $this->m_Name = $xmlArr["TABS"]["ATTRIBUTES"]["NAME"];
  50. $this->m_Package = $xmlArr["TABS"]["ATTRIBUTES"]["PACKAGE"];
  51. $this->m_Class = $xmlArr["TABS"]["ATTRIBUTES"]["CLASS"];
  52. $this->m_TemplateFile = $xmlArr["TABS"]["ATTRIBUTES"]["TEMPLATEFILE"];
  53. $this->m_TabViews = new MetaIterator($xmlArr["TABS"]["TABVIEWS"]["VIEW"],"TabView");
  54. $this->m_ActiveCssClassName = "'{$xmlArr["TABS"]["ATTRIBUTES"]["ACTIVECSSCLASSNAME"]}'";
  55. $this->m_InactiveCssClassName = "'{$xmlArr["TABS"]["ATTRIBUTES"]["INACTIVECSSCLASSNAME"]}'";
  56. }
  57. /**
  58. * Render JS Code to create multidimensional array of forms for a given HTMLTab
  59. *
  60. * @param array $forms
  61. * @return array $js_array
  62. **/
  63. private function _renderJSCodeForForms($forms)
  64. {
  65. $js_array="new Array(";
  66. if($forms)
  67. {
  68. foreach($forms as $form)
  69. {
  70. if(!is_null($form))
  71. {
  72. $js_array.="new Array('{$form['NAME']}','{$form['VISIBLE']}'),";
  73. }
  74. else
  75. {
  76. // No array entry will be created
  77. }
  78. }
  79. $js_array = rtrim($js_array,',').")";
  80. }
  81. else
  82. {
  83. $js_array = 'null';
  84. }
  85. return $js_array;
  86. }
  87. /**
  88. * Render a URL for hide or show forms or in another case, go to URL specified in xml
  89. *
  90. * @param TabView $tabView
  91. * @return string javascript string to either show a EasyForm or load a different URL
  92. **/
  93. private function _renderURL($tabView)
  94. {
  95. if($tabView->hasForms())
  96. {
  97. $url = "javascript:ChangeTab(this, {$tabView->m_Name}_config)";
  98. }
  99. else if($tabView->m_URL)
  100. {
  101. $url = $tabView->m_URL;
  102. }
  103. else
  104. {
  105. $url = "javascript:Openbiz.Net.loadView('{$tabView->m_View}')";
  106. }
  107. return $url;
  108. }
  109. /**
  110. * Set current tab with view name
  111. *
  112. * @param string $viewName name of a view
  113. * @return void
  114. */
  115. public function setCurrentTab($viewName)
  116. {
  117. $this->m_CurrentTab = $viewName;
  118. }
  119. /**
  120. * Ask if the $this tab object is the current tab
  121. *
  122. * @param TabView $tabView
  123. * @param EasyView $curViewObj current View Object
  124. * @param string $curViewName name of the current view
  125. * @return boolean TRUE if on the current tab, otherwise FALSE
  126. */
  127. public function isCurrentTab($tabView, $curViewObj, $curViewName)
  128. { //--jmmz
  129. $currentTab = false; //this variable save 'true' if is the current tab and 'false' in otherwise --jmmz
  130. if ($this->m_CurrentTab)
  131. {
  132. $currentTab = ($this->m_CurrentTab == $tabView->m_Name || $this->m_CurrentTab == $tabView->m_Tab)
  133. ? TRUE
  134. : FALSE;
  135. }
  136. elseif ($tabView->m_ViewSet)
  137. {
  138. if ($curViewObj)
  139. // check if current view's viewset == tview->m_ViewSet
  140. $currentTab = ($curViewObj->getViewSet() == $tabView->m_ViewSet) ? true : false;
  141. }
  142. else
  143. {
  144. $currentTab = ($curViewName == $tabView->m_View || $curViewObj->m_Tab == $tabView->m_Name) ? true : false;
  145. }
  146. return $currentTab;
  147. }
  148. /**
  149. * Save the current tab in the session object
  150. *
  151. * @param TabView $tview
  152. * @param EasyView $curViewObj current View Object
  153. * @param string $curViewName name of the current view
  154. * @return void
  155. */
  156. public function setCurrentTabInSession($tview, $curViewObj, $curViewName)
  157. {
  158. $sessionContext = BizSystem::sessionContext();
  159. if (!$sessionContext->varExists('CURRENT_TAB_'.$this->m_Name))
  160. {
  161. if ($this->isCurrentTab($tview,$curViewObj, $curViewName))
  162. {
  163. $sessionContext->setVar('CURRENT_TAB_'.$this->m_Name,$tview->m_Name);
  164. }
  165. else
  166. {
  167. //Don't set var if isn't the current var
  168. }
  169. }
  170. else
  171. {
  172. $this->setCurrentTab($sessionContext->getVar('CURRENT_TAB_'.$this->m_Name));
  173. }
  174. }
  175. /**
  176. * Render the html tabs
  177. *
  178. * @global BizSystem $g_BizSystem
  179. * @return string html content of the tabs
  180. */
  181. public function render()
  182. {
  183. global $g_BizSystem;
  184. $curView = $g_BizSystem->getCurrentViewName();
  185. $curViewobj = ($curView) ? BizSystem::getObject($curView) : null;
  186. $profile = $g_BizSystem->getUserProfile();
  187. $svcobj = BizSystem::getService(ACCESS_SERVICE);
  188. $role = isset($profile["ROLE"]) ? $profile["ROLE"] : null;
  189. // list all views and highlight the current view
  190. // pass $tabs(caption, url, target, icon, current) to template
  191. $smarty = BizSystem::getSmartyTemplate();
  192. $tabs = array();
  193. $i = 0;
  194. $hasForms = false;
  195. foreach ($this->m_TabViews as $tview)
  196. {
  197. // tab is renderd if no definition is found in accessservice.xml (default)
  198. if ($svcobj->allowViewAccess($tview->m_View, $role))
  199. {
  200. $tabs[$i]['name']=$tview->m_Name; //Name of each tab--jmmz
  201. $tabs[$i]['forms']=$this->_renderJSCodeForForms($tview->m_Forms);//Configuration of the forms to hide or show--jmmz
  202. $tabs[$i]['caption'] = $tview->m_Caption;
  203. $tabs[$i]['url'] = $this->_renderURL($tview); //Call the method to render the url--jmmz
  204. //If I have forms to hide or show I add the event because I don't need an URL, I need an event
  205. if( (bool) $tview->hasForms() )
  206. {
  207. $tabs[$i]['event']=$tabs[$i]['url']; //Assign The url rendered to the event on click
  208. $tabs[$i]['url']='javascript:void(0)'; //If I put url in '' then the href want send me to another direction
  209. $this->setCurrentTabInSession($tview, $curViewobj, $curView); //I set the current tab wrote in session
  210. $hasForms = TRUE;
  211. }
  212. $tabs[$i]['target'] = $tview->m_Target;
  213. $tabs[$i]['icon'] = $tview->m_Icon;
  214. $tabs[$i]['current'] = $this->isCurrentTab($tview,$curViewobj, $curView); //I get the current tab.
  215. $i++;
  216. }
  217. }
  218. $this->setClientScripts($tabs, $hasForms);
  219. $smarty->assign("tabs", $tabs);
  220. $smarty->assign("tabs_Name",$this->m_Name);
  221. return $smarty->fetch(BizSystem::getTplFileWithPath($this->m_TemplateFile, $this->m_Package));
  222. }
  223. /**
  224. * Rerender the tabs
  225. *
  226. * @return string html content of the menu
  227. */
  228. public function rerender()
  229. {
  230. return $this->render();
  231. }
  232. /**
  233. * Include client javascripts or CSS in the html content
  234. *
  235. * @param array $tabs
  236. * @param boolean $hasForms
  237. * @return void
  238. */
  239. protected function setClientScripts($tabs, $hasForms)
  240. {
  241. global $g_BizSystem;
  242. BizSystem::clientProxy()->appendScripts("tabs", "tabs.js");
  243. BizSystem::clientProxy()->appendStyles("tabs", "tabs.css");
  244. if ($hasForms)
  245. {
  246. $tab_script = '<script type = "text/javascript">'.PHP_EOL;
  247. foreach ($tabs as $tab)
  248. {
  249. $tab_script .= 'var '.$tab['name'].'_config = '.$tab['forms'].';'.PHP_EOL;
  250. }
  251. $tab_script .= 'var '.$this->m_Name.'_active = '.$this->m_ActiveCssClassName.';'.PHP_EOL;
  252. $tab_script .= 'var '.$this->m_Name.'_inactive = '.$this->m_InactiveCssClassName.';'.PHP_EOL;
  253. $tab_script .= '</script>';
  254. BizSystem::clientProxy()->appendScripts("tab_forms_$this->m_Name", $tab_script, FALSE);
  255. }
  256. }
  257. }
  258. /**
  259. * TabView class is internal class mapping to the metadata of View element in HTMLTabs
  260. *
  261. * @package openbiz.bin.easy
  262. * @author Rocky Swen
  263. * @copyright Copyright (c) 2005-2009
  264. * @since 1.2
  265. * @access public
  266. */
  267. class TabView
  268. {
  269. public $m_Name;
  270. public $m_View;
  271. public $m_ViewSet;
  272. public $m_Caption;
  273. public $m_URL;
  274. public $m_Target;
  275. public $m_Icon;
  276. public $m_Forms; //Forms for hide or show in a BizView
  277. /**
  278. * Get forms or the form to hide or show.
  279. * When It has one form It hasn't the ATTRIBUTES property
  280. *
  281. * @param array $forms
  282. * @return array
  283. **/
  284. private function _getForms($forms)
  285. {
  286. $recArr=array();
  287. if (count($forms) == 0 )
  288. return $recArr;
  289. foreach($forms as $form)
  290. {
  291. if(!is_null($form["ATTRIBUTES"]))
  292. $recArr[]=$form["ATTRIBUTES"];
  293. else
  294. $recArr[]=$form;
  295. }
  296. return $recArr;
  297. }
  298. /**
  299. * Initialize TabView with xml array
  300. *
  301. * @param array $xmlArr
  302. * @return void
  303. */
  304. function __construct(&$xmlArr)
  305. {
  306. $this->m_Name = $xmlArr["ATTRIBUTES"]["NAME"];
  307. $this->m_View = $xmlArr["ATTRIBUTES"]["VIEW"];
  308. if(array_key_exists("VIEWSET", $xmlArr["ATTRIBUTES"]))
  309. $this->m_ViewSet = $xmlArr["ATTRIBUTES"]["VIEWSET"];
  310. $this->m_Caption = $this->translate($xmlArr["ATTRIBUTES"]["CAPTION"]);
  311. if(array_key_exists("URL", $xmlArr["ATTRIBUTES"]))
  312. $this->m_URL = $xmlArr["ATTRIBUTES"]["URL"];
  313. if(array_key_exists("TARGET", $xmlArr["ATTRIBUTES"]))
  314. $this->m_Target = $xmlArr["ATTRIBUTES"]["TARGET"];
  315. if(array_key_exists("ICON", $xmlArr["ATTRIBUTES"]))
  316. $this->m_Icon = $xmlArr["ATTRIBUTES"]["ICON"];
  317. $this->m_Forms = NULL;
  318. if(array_key_exists("FORM", $xmlArr))
  319. $this->m_Forms = $this->_getForms($xmlArr["FORM"]); //Get form or forms to hide or show
  320. //$this->m_Forms = (!is_null($xmlArr["FORM"]))?$this->getForms($xmlArr["FORM"]):null;
  321. }
  322. /**
  323. * Return TRUE if the current tabView has forms related to it
  324. *
  325. * @return bool
  326. */
  327. function hasForms()
  328. {
  329. return (bool) $this->m_Forms;
  330. }
  331. protected function translate($caption)
  332. {
  333. $module = $this->getModuleName($this->m_Name);
  334. return I18n::t($caption, $this->getTransKey(caption), $module);
  335. }
  336. protected function getTransKey($name)
  337. {
  338. $shortFormName = substr($this->m_Name,intval(strrpos($this->m_Name,'.'))+1);
  339. return strtoupper($shortFormName.'_'.$name);
  340. }
  341. }
  342. ?>