PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/doc/fx_layout.php

http://comet.googlecode.com/
PHP | 313 lines | 277 code | 21 blank | 15 comment | 51 complexity | 12529a3b468b31d088e85d09eaaad0f0 MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php
  2. class Fx_Layout extends Fx_Block {
  3. protected $_id = '';
  4. protected $_baseDir = 'layouts/';
  5. protected $_module = '';
  6. protected $_file = 'layout.xml';
  7. protected $_pages = array();
  8. protected $_current_page = '';
  9. public function __construct() {
  10. parent::__construct();
  11. $this->_id = 'layout_id_' . md5(time());
  12. }
  13. public function getId() {
  14. return (string)$this->_id;
  15. }
  16. public function addPage(Fx_Page $new_page) {
  17. if (!empty($new_page)) {
  18. $new_page->setLayout($this);
  19. $this->_pages[$new_page->getUrl()] = $new_page;
  20. }
  21. return $this;
  22. }
  23. public function removePage(Fx_Page $new_page) {
  24. if (isset($this->_pages[$url])) {
  25. unset($this->_pages[$url]);
  26. }
  27. return $this;
  28. }
  29. public function getPage($url, $useCache = false) {
  30. // Cacher
  31. if (isset($this->_pages[$url])) {
  32. if (!$useCache) {
  33. return $this->_pages[$url];
  34. }
  35. $page = $this->_pages[$url];
  36. $reg = st_registry::getInstance();
  37. if ($reg->registry('page-'.$url) === null) {
  38. $reg->register('page-'.$url, $page, true);
  39. return $page;
  40. } else {
  41. return $reg->registry('page-'.$url);
  42. }
  43. }
  44. return $this;
  45. }
  46. // Load Relative Path: [./application/themes/default/layouts/] => Do not need
  47. // Load: welcome/layout.xml
  48. public function loadLayout($_layout = '') {
  49. $_layout = sprintf(APP_LAYOUT_PATH, $this->_theme) . $_layout;
  50. $this->setLayout($_layout);
  51. $this->_parseLayout();
  52. return $this;
  53. }
  54. public function _parseLayout() {
  55. $_layout = $this->getLayout();
  56. if (empty($_layout)) {
  57. return $this;
  58. }
  59. $_layouts = simplexml_load_file($_layout, 'SimpleXMLElement', LIBXML_NOBLANKS);
  60. // Check Loaded XML Layout Succsessfully
  61. if (!$_layouts instanceof SimpleXMLElement || count($_layouts->children()) == 0) {
  62. return $this;
  63. }
  64. // Get One By One URL Then Create Fx_Page For It
  65. foreach ($_layouts->children() as $_url => $_doc) {
  66. if (count($_doc->children()) == 0) {
  67. continue;
  68. }
  69. $this->addPage(new Fx_Page($_url, NULL, NULL, NULL));
  70. foreach ($_doc->children() as $_tagName => $_xmlBlock) {
  71. $_block = new Fx_Block();
  72. switch ($_tagName) {
  73. case 'action':
  74. if (isset($_xmlBlock['method'])) {
  75. $method = (string)$_xmlBlock['method'];
  76. $param = preg_replace_callback("/{{(.*)}}/",
  77. create_function(
  78. '$matches',
  79. 'return constant($matches[1]);'
  80. ), (string)$_xmlBlock);
  81. $this->_pages[$_url]->$method($param);
  82. }
  83. break;
  84. case 'block':
  85. $_attributes = $_xmlBlock->attributes();
  86. if (count($_attributes) > 0) {
  87. foreach ($_attributes as $key => $value) {
  88. $value = preg_replace_callback("/{{(.*)}}/",
  89. create_function(
  90. '$matches',
  91. 'return constant($matches[1]);'
  92. ), $value);
  93. $method = 'set' . ucfirst($key);
  94. switch ($key) {
  95. case 'type':
  96. $class_name = substr($value, strrpos($value, '/') + 1);
  97. if (strstr($class_name, '_')) {
  98. $class_name = implode('_', array_map("ucfirst", explode('_', $class_name)));
  99. }
  100. $value = sprintf("%s%s%s", APP_MODULE_PATH, $value, EXT);
  101. require_once $value;
  102. if (!class_exists($class_name)) {
  103. $error_msg = "$classname not found in path: $value";
  104. throw new Exception($error_msg);
  105. }
  106. $_block = new $class_name();
  107. break;
  108. /**id, alias, name, template*/
  109. default:
  110. $_block->$method($value);
  111. break;
  112. }
  113. }
  114. }
  115. break;
  116. /**Parse Params In Page*/
  117. case 'params':
  118. $_params = $_xmlBlock->param;
  119. if (count($_params) > 0) {
  120. foreach ($_params as $_param) {
  121. // Param has not name then ignore it
  122. if (!isset($_param['name'])) {
  123. continue;
  124. }
  125. $this->_pages[$_url]->addVariable($_param['name'], (string)$_param);
  126. }
  127. }
  128. break;
  129. /**Parse Constants In Page*/
  130. case 'constants':
  131. $_constants = $_xmlBlock->constant;
  132. if (count($_constants) > 0) {
  133. foreach ($_constants as $_constant) {
  134. // Param has not name then ignore it
  135. if (!isset($_constant['name'])) {
  136. continue;
  137. }
  138. if (!defined($_constant['name'])) {
  139. define($_constant['name'], (string)$_constant);
  140. }
  141. }
  142. }
  143. break;
  144. default:
  145. break;
  146. }
  147. if ($_block->getId()) {
  148. if (!$_block->hasTemplate()) {
  149. $_block->setTemplate(APP_TEMPLATE_DEFAULT);
  150. }
  151. $this->_pages[$_url]->appendChild($_block);
  152. }
  153. $_children = $_xmlBlock->children();
  154. if (count($_children) > 0) {
  155. foreach ($_children as $_xmlChildBlock) {
  156. $this->_recursiveXMLBlocks($_xmlChildBlock, $_block);
  157. }
  158. }
  159. }
  160. }
  161. return $this;
  162. }
  163. public function _recursiveXMLBlocks(SimpleXMLElement & $_xmlBlock = NULL, & $_parentBlock = NULL) {
  164. if (empty($_xmlBlock) || empty($_parentBlock) || !$_parentBlock instanceof Fx_Block) {
  165. return $this;
  166. }
  167. $_xmlChildren = $_xmlBlock->children();
  168. $_child_block = $this->addXMLBlocks($_xmlBlock, $_parentBlock);
  169. if (count($_xmlChildren) > 0) {
  170. foreach ($_xmlChildren as $_xmlChildBlock) {
  171. $this->_recursiveXMLBlocks($_xmlChildBlock, $_child_block);
  172. }
  173. }
  174. return $this;
  175. }
  176. public function addXMLBlocks(SimpleXMLElement $_xmlBlock = NULL, & $_parentBlock = NULL) {
  177. if (empty($_xmlBlock) || empty($_parentBlock) || !$_parentBlock instanceof Fx_Block) {
  178. return NULL;
  179. }
  180. $_child_block = new Fx_Block();
  181. $_tagName = $_xmlBlock->getName();
  182. switch ($_tagName) {
  183. case 'action':
  184. if (isset($_xmlBlock['method'])) {
  185. $method = (string)$_xmlBlock['method'];
  186. $param = preg_replace_callback("/{{(.*)}}/",
  187. create_function(
  188. '$matches',
  189. 'return constant($matches[1]);'
  190. ), (string)$_xmlBlock);
  191. $_parentBlock->$method($param);
  192. }
  193. break;
  194. case 'block':
  195. $_attributes = $_xmlBlock->attributes();
  196. if (count($_attributes) > 0) {
  197. foreach ($_attributes as $key => $value) {
  198. $value = preg_replace_callback("/{{(.*)}}/",
  199. create_function(
  200. '$matches',
  201. 'return constant($matches[1]);'
  202. ), $value);
  203. $method = 'set' . ucfirst($key);
  204. switch ($key) {
  205. case 'type':
  206. $class_name = substr($value, strrpos($value, '/') + 1);
  207. if (strstr($class_name, '_')) {
  208. $class_name = implode('_', array_map("ucfirst", explode('_', $class_name)));
  209. }
  210. $value = sprintf("%s%s%s", APP_MODULE_PATH, $value, EXT);
  211. require_once $value;
  212. if (!class_exists($class_name)) {
  213. $error_msg = "$classname not found in path: $value";
  214. throw new Exception($error_msg);
  215. }
  216. $_child_block = new $class_name();
  217. break;
  218. /**id, alias, name, template*/
  219. default:
  220. $_child_block->$method($value);
  221. break;
  222. }
  223. }
  224. }
  225. break;
  226. /**Parse Params In Block*/
  227. case 'params':
  228. $_params = $_xmlBlock->param;
  229. if (count($_params) > 0) {
  230. foreach ($_params as $_param) {
  231. // Param has not name then ignore it
  232. if (!isset($_param['name'])) {
  233. continue;
  234. }
  235. $_parentBlock->addVariable($_param['name'], (string)$_param);
  236. }
  237. }
  238. break;
  239. /**Parse Constants In Block*/
  240. case 'constants':
  241. $_constants = $_xmlBlock->constant;
  242. if (count($_constants) > 0) {
  243. foreach ($_constants as $_constant) {
  244. // Param has not name then ignore it
  245. if (!isset($_constant['name'])) {
  246. continue;
  247. }
  248. if (!defined($_constant['name'])) {
  249. define($_constant['name'], (string)$_constant);
  250. }
  251. }
  252. }
  253. break;
  254. default:
  255. break;
  256. }
  257. if ($_child_block->getId()) {
  258. if (!$_child_block->hasTemplate()) {
  259. $_child_block->setTemplate(APP_TEMPLATE_DEFAULT);
  260. }
  261. $_parentBlock->appendChild($_child_block);
  262. return $_child_block;
  263. }
  264. return NULL;
  265. }
  266. public function getPageHtml($_url) {
  267. if (isset($this->_pages[$_url])) {
  268. return $this->pages[$_url]->getOutputHtml();
  269. }
  270. return NULL;
  271. }
  272. public function getLayout() {
  273. return (string)$this->_layout;
  274. }
  275. public function setLayout($_layout) {
  276. if (!isset($_layout) || !is_string($_layout)) {
  277. return $this;
  278. }
  279. if (!file_exists($_layout)) {
  280. throw new Exception("Layout File: $_layout Not Found");
  281. }
  282. $this->_layout = $_layout;
  283. return $this;
  284. }
  285. public function hasPage($_pageIndex) {
  286. if (isset($this->_pages[$_pageIndex])) {
  287. return true;
  288. }
  289. return false;
  290. }
  291. }
  292. ?>