Lithron.LVirtualNodes
[ class tree: Lithron.LVirtualNodes ] [ index: Lithron.LVirtualNodes ] [ all elements ]

Source for file LPageService.php

Documentation is available at LPageService.php

  1. <?php
  2.  
  3.  
  4. /**
  5. * class file.
  6. *
  7. * @license http://opensource.org/licenses/mozilla1.1.php Mozilla Public License
  8. * @copyright 2005, diemeisterei GmbH. All rights reserved.
  9. * @author $Author: schmunk $
  10. * @version $Revision: 252 $ $Date: 2006-06-06 00:42:56 +0200 (Di, 06 Jun 2006) $
  11. * @package Lithron.LVirtualNodes
  12. * @subpackage Services
  13. */
  14.  
  15. /**
  16. * Extended page service.
  17. *
  18. * This service is able to use propel node tree objects from a database
  19. * (l_virtual_nodes). You can request a node with a predefined NODE_PREFIX the node
  20. * maps to a .page or and external link.
  21. *
  22. * @package System.Web.Services
  23. * @subpackage Services
  24. */
  25.  
  26. Prado :: setPathOfAlias("LPageService", dirname(__FILE__));
  27. if (is_dir(dirname(__FILE__) . "/db/propel"))
  28. {
  29. #Prado :: setPathOfAlias("LPageServiceDb", );
  30. Prado :: using("LPageService.db.propel.*");
  31. Prado :: using("LPageService.db.propel.l_virtual_nodes.*");
  32. }
  33.  
  34. class LPageService extends TPageService
  35. {
  36. const NODE_PREFIX= '*';
  37. const PATH_PREFIX= 'ยง';
  38.  
  39. private $_nodes;
  40. private $_rootNodeObj;
  41. private $_currentNode;
  42. private $_areNodesAvailable;
  43.  
  44. /**
  45. * Returns if node-based service is available
  46. */
  47. public function areNodesAvailable()
  48. {
  49. if ($this->Application->getGlobalState("LVirtualNodes:available") !== true)
  50. {
  51. $this->checkVirtualNodes();
  52. $this->Application->setGlobalState("LVirtualNodes:available", $this->_areNodesAvailable);
  53. }
  54. else
  55. {
  56. $this->_areNodesAvailable= $this->Application->getGlobalState("LVirtualNodes:available");
  57. }
  58. return $this->_areNodesAvailable;
  59. }
  60.  
  61. /**
  62. * Returns all nodes in tree structure starting from root
  63. */
  64. function getNodes()
  65. {
  66. if (!is_object($this->_nodes))
  67. {
  68. $this->prepareNodes();
  69. }
  70. return $this->_nodes;
  71. }
  72.  
  73. /**
  74. * Returns the root node object
  75. */
  76. function getRootNodeObj()
  77. {
  78. return $this->_rootNodeObj;
  79. }
  80.  
  81. /**
  82. * Returns the current node object
  83. */
  84. function getCurrentNode()
  85. {
  86. return $this->_currentNode;
  87. }
  88. /**
  89. * return current selected page for this service
  90. */
  91. public function getServicePage()
  92. {
  93. $p= $this->getRequest()->getServiceParameter();
  94. $return= (empty ($p)) ? $this->getDefaultPage() : $p;
  95. return $return;
  96. }
  97.  
  98. /**
  99. * Reads MasterConfig
  100. */
  101. public function init($config)
  102. {
  103. $_file= $config->getAttributes()->itemAt("LPropelConfigFile");
  104. if (is_file(dirname(__FILE__) . "/" . $_file))
  105. {
  106. $this->_lPropelConfig= require (dirname(__FILE__) . "/" . $_file);
  107. }
  108. parent :: init($config);
  109. }
  110.  
  111. public function reload()
  112. {
  113. $url= $this->constructUrl($this->getRequest()->getServiceParameter());
  114. $this->Response->redirect($url);
  115. }
  116.  
  117. /**
  118. * Checks availablity of node service
  119. */
  120. private function checkVirtualNodes()
  121. {
  122. try
  123. {
  124. $conn= $this->Application->getModule("database")->getConnection("l_virtual_nodes", "creole");
  125. $query= "SELECT id FROM l_virtual_nodes_site LIMIT 1";
  126. $result= LCreole :: doQuery($query, $conn); // Propel-like object (getter & setter)
  127. #var_dump($result[0]);
  128. $this->_areNodesAvailable= true;
  129. Prado :: trace("Virtual Nodes available", 'Lithron.LPageService');
  130. }
  131. catch (Exception $e)
  132. {
  133. Prado :: log($e->getMessage(), 8, 'Lithron.LPageService');
  134. $this->_areNodesAvailable= false;
  135. }
  136. }
  137. /**
  138. * Queries all nodes in database and serves them for later use
  139. */
  140. private function prepareNodes()
  141. {
  142. // TODO
  143. // retrieves data item from cache
  144. Prado :: trace("Looking for nodes in cache ...", 'Lithron.LPageService');
  145. if ($cache= $this->Application->Cache)
  146. {
  147. $c= $cache->get("LPageService:Nodes");
  148. if (is_string($c))
  149. {
  150. $nodes= Prado :: unserialize($c);
  151. #$this->_nodes= $nodes;
  152. }
  153. }
  154. // query nodes if not in cache
  155. if (!is_object($this->_nodes))
  156. {
  157. $conn= $this->Application->getModule("database")->getConnection("l_virtual_nodes", "propel");
  158. $this->_nodes= LVirtualNodesSiteNodePeer :: retrieveRootNode(true, $conn);
  159.  
  160. if ($cache)
  161. {
  162. try
  163. {
  164. $c= Prado :: serialize($this->_nodes);
  165. $cache->set("LPageService:Nodes", $c, 60);
  166. }
  167. catch (Exception $e)
  168. {
  169. Prado :: log("Node caching failed: " . $e->getMessage(), 8, 'Lithron.LPageService');
  170.  
  171. }
  172. }
  173. }
  174. else
  175. {
  176. Prado :: trace("Using cached root node", 'Lithron.LPageService');
  177. }
  178.  
  179. }
  180.  
  181.  
  182. public function getNodeById($id)
  183. {
  184. return $this->findNodeById($this->getNodes(), str_replace(self::NODE_PREFIX,"",$id));
  185. }
  186. /**
  187. * Finds a node in the tree by id
  188. */
  189. private function findNodeById($startNode, $searchId)
  190. {
  191. $child= $startNode->getFirstChildNode();
  192. while ($child)
  193. {
  194. $nodeId= $child->getId();
  195. if ($nodeId == $searchId)
  196. {
  197. return $child;
  198. }
  199. $return= $this->findNodeById($child, $searchId);
  200. if ($return)
  201. return $return;
  202. $child= $child->getSiblingNode();
  203. }
  204. }
  205.  
  206. /**
  207. * Determines the requested page path.
  208. * 1. nodeId (database needed)
  209. * 2. path (database needed)
  210. * 3. page
  211. * @return string page path requested
  212. */
  213. protected function determineRequestedPagePath()
  214. {
  215. $special= $this->getRequest()->getServiceParameter();
  216. $special_key= substr($this->getRequest()->getServiceParameter(), 0, 1);
  217. $special_string= substr($this->getRequest()->getServiceParameter(), 1);
  218.  
  219. // initialize virtual node system $nodeId = $this->getRequest()->itemAt("nodeId")
  220. if ($special_key == self :: NODE_PREFIX && is_numeric($special_string))
  221. {
  222. // find current node
  223. $this->_currentNode= & $this->findNodeById($this->getNodes(), $special_string);
  224. if (!is_object($this->_currentNode))
  225. {
  226. Prado :: log("Node (" . $special_string . ") NOT FOUND", 3, 'Lithron.LPageService');
  227. }
  228. else
  229. {
  230. // set pagePath with value from node object
  231. $pagePath= $this->_currentNode->getPage();
  232. Prado :: log("Current node found ($pagePath)", 2, 'Lithron.LPageService');
  233. }
  234. }
  235. elseif ($special_key == self :: PATH_PREFIX)
  236. {
  237. // TODO
  238. Prado :: trace("Function currently not implemented", 'Lithron.LPageService');
  239. }
  240. else
  241. {
  242. // use normal mode
  243. $pagePath= $this->getRequest()->getServiceParameter();
  244. Prado :: log("Using PRADO-style request ($pagePath)", 2, 'Lithron.LPageService');
  245. }
  246. if (empty ($pagePath))
  247. $pagePath= $this->getDefaultPage();
  248. return $pagePath;
  249. }
  250.  
  251. /**
  252. * wrapper function
  253. */
  254. public function constructUrl($pagePath, $getParams= null, $encodeAmpersand= false, $encodeGetItems= true)
  255. {
  256. return $this->getRequest()->constructUrl($this->getID(), $pagePath, $getParams, $encodeAmpersand, $encodeGetItems);
  257. }
  258.  
  259. }
  260. ?>

Documentation generated on Tue, 20 Jun 2006 05:15:21 +0200 by phpDocumentor 1.3.0RC4